Skip to content

Commit

Permalink
Extend coverage of prepare/finish ansible plugin with more images (te…
Browse files Browse the repository at this point in the history
…emtee#3110)

The coverage now matches what `prepare/install` is tested with. This
change required refactoring of some bits of `prepare/install` test:

* moving generic helpers into `tests/install.sh`,
* changing tests for `ansible` plugin to use `tests/images.sh` instead
of its original one-shot use of images.

This gives us:

* the same coverage for both plugins,
* one place where new environment needs to be added,
* less code duplication, more re-use of "image testing" helpers,
* and, the best, it actually discovers a genuine bug: `ansible` plugins
  did not report Python interpreter as their essential requirement,
  therefore playbooks were impossible to run in some environments.
  • Loading branch information
happz authored and The-Mule committed Oct 14, 2024
1 parent c8485e6 commit 6568e92
Show file tree
Hide file tree
Showing 14 changed files with 292 additions and 169 deletions.
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ TMT_TEST_CONTAINER_IMAGE_NAME_PREFIX = tmt/tests/container

TMT_TEST_CONTAINER_IMAGES := $(TMT_TEST_CONTAINER_IMAGE_NAME_PREFIX)/alpine:latest \
$(TMT_TEST_CONTAINER_IMAGE_NAME_PREFIX)/alpine/upstream:latest \
$(TMT_TEST_CONTAINER_IMAGE_NAME_PREFIX)/centos/7:latest \
$(TMT_TEST_CONTAINER_IMAGE_NAME_PREFIX)/centos/7/upstream:latest \
$(TMT_TEST_CONTAINER_IMAGE_NAME_PREFIX)/centos/stream9:latest \
$(TMT_TEST_CONTAINER_IMAGE_NAME_PREFIX)/centos/stream9/upstream:latest \
$(TMT_TEST_CONTAINER_IMAGE_NAME_PREFIX)/fedora/coreos:stable \
$(TMT_TEST_CONTAINER_IMAGE_NAME_PREFIX)/fedora/coreos/ostree:stable \
Expand Down Expand Up @@ -128,9 +130,15 @@ $(TMT_TEST_IMAGE_TARGET_PREFIX)/$(TMT_TEST_CONTAINER_IMAGE_NAME_PREFIX)/alpine\:
$(TMT_TEST_IMAGE_TARGET_PREFIX)/$(TMT_TEST_CONTAINER_IMAGE_NAME_PREFIX)/alpine/upstream\:latest:
$(call build-test-container-image,$@,alpine/Containerfile.upstream)

$(TMT_TEST_IMAGE_TARGET_PREFIX)/$(TMT_TEST_CONTAINER_IMAGE_NAME_PREFIX)/centos/7\:latest:
$(call build-test-container-image,$@,centos/7/Containerfile)

$(TMT_TEST_IMAGE_TARGET_PREFIX)/$(TMT_TEST_CONTAINER_IMAGE_NAME_PREFIX)/centos/7/upstream\:latest:
$(call build-test-container-image,$@,centos/7/Containerfile.upstream)

$(TMT_TEST_IMAGE_TARGET_PREFIX)/$(TMT_TEST_CONTAINER_IMAGE_NAME_PREFIX)/centos/stream9\:latest:
$(call build-test-container-image,$@,centos/stream9/Containerfile)

$(TMT_TEST_IMAGE_TARGET_PREFIX)/$(TMT_TEST_CONTAINER_IMAGE_NAME_PREFIX)/centos/stream9/upstream\:latest:
$(call build-test-container-image,$@,centos/stream9/Containerfile.upstream)

Expand Down
15 changes: 15 additions & 0 deletions containers/centos/7/Containerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#
# A CentOS 7 image tailored for tmt test suite
#
# tmt/tests/centos/7/upstream:latest
#

FROM quay.io/centos/centos:7

# Use latest vault repos, mirrors are gone after centos EOL
RUN cd /etc/yum.repos.d/ \
&& sed '/mirrorlist/d' -i *repo \
&& sed 's|#baseurl=http://mirror.centos.org/centos/$releasever|baseurl=https://vault.centos.org/7.9.2009|' -i *repo

# Populate yum cache
RUN yum makecache
10 changes: 10 additions & 0 deletions containers/centos/stream9/Containerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#
# A CentOS Stream 9 image tailored for tmt test suite
#
# tmt/tests/centos/stream9/upstream:latest
#

FROM quay.io/centos/centos:stream9

# Populate dnf cache
RUN dnf makecache
23 changes: 15 additions & 8 deletions tests/finish/ansible/data/plan.fmf
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
provision:
how: container
execute:
how: tmt
script: echo fake

environment:
SIMPLE: word
SPACES: several words with spaces
provision:
how: container

finish:
- name: State before is valid (no file)
order: 20
order: 80
how: shell
script: bash -xc "! [ -f /tmp/finished ]"

- name: Ansible we want to test
order: 85
how: ansible
playbook: playbook.yml
extra-args: '-e ansible_remote_tmp=/tmp'

- name: State after is as expected (file created)
order: 70
order: 90
how: shell
script: bash -xc "[ -f /tmp/finished ]"

- name: Create a file that is pulled during the finish stage
order: 80
order: 95
how: shell
script: touch $TMT_PLAN_DATA/my_file.txt
execute:
how: tmt
script: echo fake
1 change: 1 addition & 0 deletions tests/finish/ansible/main.fmf
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ tag+:
- provision-container
- provision-local
- provision-virtual
duration: 30m
57 changes: 38 additions & 19 deletions tests/finish/ansible/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,52 @@ rlJournalStart
rlPhaseStartSetup
rlRun "PROVISION_HOW=${PROVISION_HOW:-local}"

build_container_image "ubi/8/upstream\:latest"
build_container_image "centos/7/upstream\:latest"
if [ "$PROVISION_HOW" = "container" ]; then
rlRun "IMAGES='$TEST_CONTAINER_IMAGES'"

build_container_images

elif [ "$PROVISION_HOW" = "virtual" ]; then
rlRun "IMAGES='$TEST_VIRTUAL_IMAGES'"

else
rlRun "IMAGES="
fi

rlRun "pushd data"
rlRun "run=\$(mktemp -d)" 0 "Create run directory"
rlPhaseEnd

rlPhaseStartTest "Test ($PROVISION_HOW)"
# Prepare common options, run given method
tmt="tmt run -i $run --scratch"
rlRun "$tmt -av provision -h $PROVISION_HOW"
while IFS= read -r image; do
phase_prefix="$(test_phase_prefix $image)"

# Check that created file is synced back
rlRun "ls -l $run/plan/data"
rlAssertExists "$run/plan/data/my_file.txt"
rlPhaseStartTest "$phase_prefix Test Ansible playbook"
if is_fedora_coreos "$image"; then
rlLogInfo "Skipping because of https:/teemtee/tmt/issues/2884: tmt cannot run tests on Fedora CoreOS containers"
rlPhaseEnd

# For container provision try centos images as well
if [[ $PROVISION_HOW == container ]]; then
rlRun "$tmt -av finish provision -h $PROVISION_HOW -i $TEST_IMAGE_PREFIX/centos/7/upstream:latest"
rlRun "$tmt -av finish provision -h $PROVISION_HOW -i $TEST_IMAGE_PREFIX/ubi/8/upstream:latest"
fi
continue
fi

# After the local provision remove the test file
if [[ $PROVISION_HOW == local ]]; then
rlRun "sudo rm -f /tmp/finished"
fi
rlPhaseEnd
[ "$PROVISION_HOW" = "container" ] && rlRun "podman images $image"

# Run given method
if [ "$PROVISION_HOW" = "local" ]; then
rlRun "tmt run -i $run --scratch -av provision -h $PROVISION_HOW"
else
rlRun "tmt run -i $run --scratch -av provision -h $PROVISION_HOW -i $image"
fi

# Check that created file is synced back
rlRun "ls -l $run/plan/data"
rlAssertExists "$run/plan/data/my_file.txt"

# After the local provision remove the test file
if [[ $PROVISION_HOW == local ]]; then
rlRun "sudo rm -f /tmp/finished"
fi
rlPhaseEnd
done <<< "$IMAGES"

rlPhaseStartCleanup
rlRun "rm -r $run" 0 "Removing run directory"
Expand Down
114 changes: 114 additions & 0 deletions tests/images.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,121 @@ TEST_IMAGE_PREFIX="localhost/tmt/tests/container"
# "Source" of this script -> its absolute path -> dirname -> one level above `tests/`
_MAKEFILE_DIR="$(dirname $(readlink -f ${BASH_SOURCE[0]}))/.."

# Basic set of container images to test on
#
# It does not contain "unprivileged" images.
TEST_CONTAINER_IMAGES="${TEST_CONTAINER_IMAGES:-$TEST_IMAGE_PREFIX/alpine:latest
$TEST_IMAGE_PREFIX/centos/7/upstream:latest
$TEST_IMAGE_PREFIX/centos/stream9/upstream:latest
$TEST_IMAGE_PREFIX/fedora/39/upstream:latest
$TEST_IMAGE_PREFIX/fedora/40/upstream:latest
$TEST_IMAGE_PREFIX/fedora/rawhide/upstream:latest
$TEST_IMAGE_PREFIX/fedora/coreos:stable
$TEST_IMAGE_PREFIX/fedora/coreos/ostree:stable
$TEST_IMAGE_PREFIX/ubi/8/upstream:latest
$TEST_IMAGE_PREFIX/ubuntu/22.04/upstream:latest}"

# Basic set of virtual images to test on.
#
# Enable Alpine
# TODO: enable Ubuntu
# TODO: enable centos-7 again with modified repo files
TEST_VIRTUAL_IMAGES="${TEST_VIRTUAL_IMAGES:-centos-stream-9
fedora-39
fedora-40
fedora-rawhide
fedora-coreos}"

# A couple of "is image this?" helpers, to simplify conditions.
function is_fedora_rawhide () {
[[ "$1" =~ ^.*fedora/rawhide[:/].* ]] && return 0
[[ "$1" = "fedora-rawhide" ]] && return 0

return 1
}

function is_fedora_40 () {
[[ "$1" =~ ^.*fedora/40[:/].* ]] && return 0
[[ "$1" = "fedora-40" ]] && return 0

return 1
}

function is_fedora_39 () {
[[ "$1" =~ ^.*fedora/39[:/].* ]] && return 0
[[ "$1" = "fedora-39" ]] && return 0

return 1
}

function is_centos_stream_9 () {
[[ "$1" =~ ^.*centos/stream9[:/].* ]] && return 0
[[ "$1" = "centos-stream-9" ]] && return 0

return 1
}

function is_centos_7 () {
[[ "$1" =~ ^.*centos/7[:/].* ]] && return 0
[[ "$1" = "centos-7" ]] && return 0

return 1
}

function is_ubuntu () {
[[ "$1" =~ ^.*ubuntu/.* ]] && return 0
[[ "$1" = "ubuntu" ]] && return 0

return 1
}

function is_ostree () {
[[ "$1" =~ ^.*fedora/coreos/ostree:stable ]] && return 0
[[ "$1" = "fedora-coreos" && "$PROVISION_HOW" = "virtual" ]] && return 0

return 1
}

function is_fedora_coreos () {
[[ "$1" =~ ^.*fedora/coreos(/ostree)?:stable ]] && return 0
[[ "$1" = "fedora-coreos" ]] && return 0

return 1
}

function is_fedora () {
[[ "$1" =~ ^.*fedora.* ]] && return 0 || return 1
}

function is_centos () {
[[ "$1" =~ ^.*centos.* ]] && return 0 || return 1
}

function is_rhel () {
is_ubi "$1" && return 0 || return 1
}

function is_alpine () {
[[ "$1" =~ ^.*alpine.* ]] && return 0 || return 1
}

function is_ubi () {
[[ "$1" =~ ^.*ubi.* ]] && return 0 || return 1
}

function is_ubi_8 () {
[[ "$1" =~ ^.*ubi/8.* ]] && return 0 || return 1
}

function test_phase_prefix () {
if [ "$PROVISION_HOW" = "local" ]; then
echo "[$PROVISION_HOW]"
else
echo "[$PROVISION_HOW / $(echo $1 | sed "s|$TEST_IMAGE_PREFIX/||")]"
fi
}

# Building of container images
function build_container_image () {
if [ "$PROVISION_HOW" != "container" ]; then
rlLogInfo "Skipping the build of $1 container image because of PROVISION_HOW=$PROVISION_HOW"
Expand Down
33 changes: 24 additions & 9 deletions tests/prepare/ansible/data/plan.fmf
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
discover:
how: fmf
environment:
SIMPLE: word
SPACES: several words with spaces
provision:
how: container
execute:
how: tmt
script: echo fake

environment:
SIMPLE: word
SPACES: several words with spaces

prepare:
- name: State before is valid (no file)
order: 80
how: shell
script: bash -xc "! [ -f /tmp/prepared ]"

- name: State after is as expected (file created)
order: 90
how: shell
script: bash -xc "[ -f /tmp/prepared ]"

/local:
prepare:
prepare+:
- name: Ansible we want to test
order: 85
how: ansible
playbook: playbook.yml
extra-args: '-vvv'
extra-args: '-vvv -e ansible_remote_tmp=/tmp'

/remote:
prepare:
prepare+:
- name: Ansible we want to test
order: 85
how: ansible
# This is the very same playbook as the local one, just living in tmt's repository.
playbook: https://raw.githubusercontent.com/teemtee/tmt/main/tests/prepare/ansible/data/playbook.yml
extra-args: '-vvv'
extra-args: '-vvv -e ansible_remote_tmp=/tmp'
2 changes: 0 additions & 2 deletions tests/prepare/ansible/data/test.fmf

This file was deleted.

1 change: 1 addition & 0 deletions tests/prepare/ansible/main.fmf
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ tag+:
- provision-container
- provision-local
- provision-virtual
duration: 30m
Loading

0 comments on commit 6568e92

Please sign in to comment.