Skip to content

Commit 40ab6a9

Browse files
Merge pull request #160 from samvarankashyap/add_playbooks
[CVP-2109] Add playbooks and update calls to multiple processes
2 parents ce1a3ff + 3088aeb commit 40ab6a9

File tree

9 files changed

+192
-147
lines changed

9 files changed

+192
-147
lines changed

.github/workflows/build_and_test_midstream_image.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,9 @@ jobs:
2929
- name: "Build the midstream image inside Dockerfiles"
3030
run: |
3131
podman --version
32-
cd Dockerfiles/midstream/
33-
podman build -t midstream_image:latest -f Dockerfile --build-arg OPERATOR_SDK_VERSION=v1.4.0
32+
podman build -t midstream_image:latest -f Dockerfiles/midstream/Dockerfile .
3433
3534
- name: "Run unit tests"
3635
continue-on-error: false
3736
run: |
38-
podman run -it -v $PWD:/project/operator-test-playbooks midstream_image:latest python3 /unit_tests.py
39-
37+
podman run --rm -ti --user=3149:0 midstream_image:latest unit_tests.py -vvv

Dockerfiles/midstream/Dockerfile

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
FROM registry.fedoraproject.org/fedora:33
2-
WORKDIR /project
3-
ARG OPERATOR_SDK_VERSION=v1.4.0
4-
ARG OPERATOR_TEST_PLAYBOOKS_TAG=v1.0.11
2+
ARG OPERATOR_SDK_VERSION=v1.9.0
53
ARG UMOCI_VERSION=v0.4.5
6-
ADD ./fix_etc_passwd.sh /usr/bin/fix_etc_passwd.sh
4+
ENV ANSIBLE_CONFIG=/project/operator-test-playbooks/Dockerfiles/midstream/
5+
ENV ANSIBLE_LOCAL_TEMP=/tmp/
6+
ADD extract-operator-bundle.yml validate-operator-bundle.yml /project/operator-test-playbooks/
77
RUN export ARCH=$(case $(arch) in x86_64) echo -n amd64 ;; aarch64) echo -n arm64 ;; *) echo -n $(arch) ;; esac);\
88
export OS=$(uname | awk '{print tolower($0)}');\
99
export OPERATOR_SDK_DL_URL=https://github.com/operator-framework/operator-sdk/releases/download/$OPERATOR_SDK_VERSION/;\
1010
curl -L -o /usr/local/bin/operator-sdk ${OPERATOR_SDK_DL_URL}/operator-sdk_${OS}_${ARCH} && \
1111
chmod a+x /usr/local/bin/operator-sdk && \
1212
curl -fL -o /usr/local/bin/umoci https://github.com/opencontainers/umoci/releases/download/${UMOCI_VERSION}/umoci.amd64 && \
1313
chmod a+x /usr/local/bin/umoci && \
14-
mkdir /project/output && \
1514
chmod g+w /etc/passwd && \
1615
dnf install --setopt=install_weak_deps=False -y git-core ansible skopeo && \
1716
dnf clean all
18-
ADD ./run_tests.py /run_tests.py
19-
ADD ./unit_tests.py /unit_tests.py
20-
CMD ["/run_tests.py"]
17+
ADD ./Dockerfiles/midstream/run_tests.py ./Dockerfiles/midstream/unit_tests.py ./Dockerfiles/midstream/fix_etc_passwd.sh /usr/bin/
18+
ADD roles /project/operator-test-playbooks/roles/
19+
ADD ./Dockerfiles/midstream/ansible.cfg /project/operator-test-playbooks/
20+
WORKDIR /var/tmp

Dockerfiles/midstream/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ First thing you reckon is that you are running this command directly from upstre
1515

1616
```
1717
git clone https://github.com/redhat-operator-ecosystem/operator-test-playbooks.git
18-
cd Dockerfiles
19-
### podman build -t <image_name>:<tagname> -f <filepath> --build-arg OPERATOR_SDK_VERSION=<operator_sdk_version> --build-arg OPERATOR_TEST_PLAYBOOKS_TAG=<operator_test_playbooks_tag>
18+
cd operator-test-playbooks
19+
### podman build -t <image_name>:<tagname> -f <filepath>
2020
### example:
21-
podman build -t midstream_image:latest -f Dockerfile --build-arg OPERATOR_SDK_VERSION=v1.4.0 --build-arg OPERATOR_TEST_PLAYBOOKS_TAG=v1.0.11
21+
podman build -t midstream_image:latest -f Dockerfiles/midstream/Dockerfile .
2222
```
2323

2424
## Running image with operator bundle
2525

2626
```
27-
podman run -it -v <operator_bundle_dir>:/project/operator-bundle -v <output_log>:/project/output --security-opt label=disable <imagename>:<tagname> -e IMAGE_TO_TEST=<image_url_to_test>
27+
podman run -it -v <output_log>:/project/output --security-opt label=disable <imagename>:<tagname> -e IMAGE_TO_TEST=<image_url_to_test> -e VERBOSITY=<number in range of 1 to 4>
2828
mkdir output_logs
29-
podman run -it -v ./example-bundle:/project/operator-bundle -v ./output_logs:/project/output --security-opt label=disable midstream_image:latest -e IMAGE_TO_TEST='quay.io://image_url_to_test'
29+
podman run -it -v ./output_logs:/project/output --security-opt label=disable midstream_image:latest -e IMAGE_TO_TEST='quay.io://image_url_to_test' VERBOSITY=3
3030
```
3131

3232
### Note:

Dockerfiles/midstream/ansible.cfg

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[defaults]
2+
# the following configuration is added to created
3+
# to generate ansible tempprary files in /tmp/
4+
# Refer: https://docs.ansible.com/ansible/latest/collections/ansible/builtin/sh_shell.html#parameter-remote_tmp
5+
remote_tmp = /tmp/
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,38 @@
1-
#!/bin/bash -e
1+
#!/usr/bin/bash -e
2+
23
main() {
34
local username="${SSH_AUTH_USER:-default}"
45
local home
56
home=$(mk_home "$username")
67
setup_passwd "$username" "$home"
78
}
9+
810
mk_home() {
911
local username="${1:?}"
1012
mkdir -p "/tmp/home/$username" && \
1113
echo "/tmp/home/$username" && \
1214
return 0
1315
mktemp --tmpdir -d "${username}.XXXXXX"
1416
}
17+
1518
setup_passwd() {
1619
local username="${1:?}"
1720
local home="${2:?}"
1821
local uid="$(id -u)"
1922
local gid="${3:-0}"
2023
local passwd=/etc/passwd
21-
22-
! getent passwd $uid 2>/dev/null || return 0
24+
25+
# do not change anything if user exists
26+
getent passwd $uid 2>/dev/null >/dev/null && return 0
2327
if ! [[ -w "$passwd" ]]; then
2428
echo "There is no permission to add user $username to $passwd"
25-
return 0
29+
return 1
2630
fi
27-
[[ "$uid" ]] || uid="$(id -u)"
2831
echo \
2932
"${username}:x:${uid}:${gid}:Default Application User:${home}:/sbin/nologin" \
3033
>> /etc/passwd
3134
}
35+
3236
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
3337
main "$@"
3438
fi

Dockerfiles/midstream/run_tests.py

Lines changed: 84 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,108 @@
11
#!/usr/bin/env python3
22
import os
33
import sys
4-
import json
5-
import unittest
64
import subprocess
7-
from os import path
5+
import logging
6+
7+
8+
TEST_WORKING_DIR= os.getcwd()
9+
OUTPUT_DIRECTORY = "/tmp/"# global variables set
10+
OPERATOR_DIR=OUTPUT_DIRECTORY+"/operator_dir/"
11+
OPERATOR_WORK_DIR=OUTPUT_DIRECTORY+"/test_operator_work_dir/"
12+
WORK_DIR=OUTPUT_DIRECTORY+"/output/"
13+
PLAYBOOKS_DIR="/project/operator-test-playbooks/"
14+
ERROR_MESSAGE_PATH=TEST_WORKING_DIR+"/.errormessage"
15+
16+
logging.basicConfig(handlers=[logging.StreamHandler()],
17+
level=logging.INFO)
18+
VALIDATION_LOGGER = logging.getLogger('.errormessage')
19+
# by default will be logged logs to stderr but also add the same messages
20+
# to the file which midstream expects
21+
VALIDATION_LOGGER.addHandler(logging.FileHandler(ERROR_MESSAGE_PATH))
822

923

1024
class RunMidstreamCVPTests():
1125

12-
def setUp(self):
13-
self.operator_dir = os.getenv('OPERATOR_DIR',
14-
"/project/operator_dir/")
15-
self.operator_work_dir = os.getenv('OPERATOR_WORK_DIR',
16-
"/project/test_operator_work_dir/")
17-
self.work_dir = os.getenv('WORK_DIR',
18-
"/project/output/")
19-
self.playbooks_dir = os.getenv('PLAYBOOKS_DIR',
20-
"/project/operator-test-playbooks/")
26+
def __init__(self):
27+
# IMAGE_TO_TESTis an environment variable that takes
28+
# operator-bundle-image
2129
self.image_to_test = os.getenv('IMAGE_TO_TEST')
22-
self.umoci_bin_path = os.getenv('UMOCI_BIN_PATH',
23-
'/usr/local/bin/umoci')
30+
# VERBOSITY is verbosity of output log range varying from 1 to 4
31+
self.verbosity = int(os.getenv('VERBOSITY', '1'))*"v"
32+
33+
@staticmethod
34+
def run_subprocess_command(exec_cmd):
35+
logging.info("Running the subprocess ansible command ")
36+
logging.info(exec_cmd)
37+
env = os.environ.copy()
38+
process = subprocess.run(exec_cmd,
39+
stdout=subprocess.PIPE,
40+
stderr=subprocess.PIPE,
41+
shell=True,
42+
universal_newlines=True,
43+
env=env)
44+
return {
45+
"stdout": process.stdout,
46+
"stderr": process.stderr,
47+
"return_code": process.returncode,
48+
}
2449

2550
def run_extract_operator_bundle(self):
26-
exec_cmd = "ansible-playbook -i localhost, -c local -v operator-test-playbooks/extract-operator-bundle.yml \
51+
exec_cmd = "/usr/bin/ansible-playbook -i localhost, -c local -{verbosity} {playbook_dir}/extract-operator-bundle.yml \
2752
-e 'bundle_image={bundle_image}' \
2853
-e 'operator_work_dir={operator_work_dir}' \
29-
-e 'umoci_bin_path={umoci_bin_path}'".format(
30-
operator_dir=self.operator_dir,
31-
operator_work_dir=self.operator_work_dir,
32-
bundle_image=self.image_to_test,
33-
umoci_bin_path=self.umoci_bin_path)
34-
result = subprocess.run(exec_cmd, shell=True, capture_output=True)
35-
return result
54+
-e 'work_dir={work_dir}'".format(verbosity=self.verbosity,
55+
playbook_dir=PLAYBOOKS_DIR,
56+
bundle_image=self.image_to_test,
57+
operator_work_dir=OPERATOR_WORK_DIR,
58+
work_dir=WORK_DIR)
59+
return RunMidstreamCVPTests.run_subprocess_command(exec_cmd)
3660

3761
def run_validate_operator_bundle(self):
38-
exec_cmd = "ansible-playbook -vvv -i localhost, --connection local \
39-
operator-test-playbooks/validate-operator-bundle.yml \
40-
-e 'operator_dir={operator_dir}' \
41-
-e 'operator_work_dir={operator_work_dir}' \
42-
-e 'work_dir={work_dir}'".format(operator_dir=self.operator_dir,
43-
operator_work_dir=self.operator_work_dir,
44-
work_dir=self.work_dir)
45-
result = subprocess.run(exec_cmd, shell=True, capture_output=True)
46-
return result
62+
exec_cmd = "/usr/bin/ansible-playbook -i localhost, -c local -{verbosity} \
63+
{playbook_dir}/validate-operator-bundle.yml \
64+
-e 'operator_dir={operator_dir}' \
65+
-e 'operator_work_dir={operator_work_dir}' \
66+
-e 'work_dir={work_dir}'".format(verbosity=self.verbosity,
67+
playbook_dir=PLAYBOOKS_DIR,
68+
operator_work_dir=OPERATOR_WORK_DIR,
69+
operator_dir=OPERATOR_DIR,
70+
work_dir=WORK_DIR)
71+
return RunMidstreamCVPTests.run_subprocess_command(exec_cmd)
4772

4873
def test_for_extract_and_validate_bundle_image(self):
4974

50-
self.setUp()
51-
global exit_code
52-
exit_code = 0
5375
# check if IMAGE_TO_TEST is defined, return exit_code 102 in case it's not
54-
if (self.image_to_test is None):
55-
print("Environment variable IMAGE_TO_TEST not set! Stopping the tests.")
56-
with open(".errormessage", 'w') as error_msg:
57-
print("Result code: 102 Error message: Environment variable IMAGE_TO_TEST not set!", file=error_msg)
58-
exit_code = 102
59-
return exit_code
60-
if (self.operator_dir != '/project/operator_dir/' or
61-
self.operator_work_dir != '/project/test_operator_work_dir/' or
62-
self.playbooks_dir != '/project/operator-test-playbooks/' or
63-
self.umoci_bin_path != '/usr/local/bin/umoci' or
64-
self.work_dir != '/project/output/'):
65-
print("Environment variable misconfigured!")
66-
with open(".errormessage", 'w') as error_msg:
67-
print("Result code: 103 Error message: Environment variable was not set correctly!", file=error_msg)
68-
exit_code = 103
69-
return exit_code
76+
if not self.image_to_test:
77+
VALIDATION_LOGGER.error("Environment variable IMAGE_TO_TEST not set! Stopping the tests.")
78+
VALIDATION_LOGGER.error("Result code: 102 Error message: Environment variable IMAGE_TO_TEST not set!")
79+
return 102
80+
7081
result = self.run_extract_operator_bundle()
71-
if (result.returncode != 0):
72-
print("Ansible playbook extract-operator-bundle.yml failed with result code: %s , see the file .errormessage for more info." % result.returncode)
73-
with open(".errormessage", "w") as error_msg:
74-
print("Result code: " + str(result.returncode), "Error message: " + str(result.stderr), file=error_msg)
75-
exit_code = 50
76-
return exit_code
82+
if result["return_code"] != 0:
83+
VALIDATION_LOGGER.error("Ansible playbook extract-operator-bundle.yml failed with result code: %s , see the file .errormessage for more info.", result["return_code"])
84+
VALIDATION_LOGGER.error("Result code: %d", result["return_code"])
85+
VALIDATION_LOGGER.error("Error message: %s", result["stderr"])
86+
VALIDATION_LOGGER.error("Result stdout: %s", result["stdout"])
87+
return 50
7788
result = self.run_validate_operator_bundle()
78-
assert (path.exists("/project/output/validation-rc.txt"))
79-
assert (path.exists("/project/output/validation-output.txt"))
80-
assert (path.exists("/project/output/validation-version.txt"))
81-
with open('/project/output/validation-rc.txt', 'r') as reader:
82-
rc = reader.read()
83-
if (int(rc) != 0):
84-
print("Image bundle validation failed with result code: %s , see /project/output/validation-output.txt file for more info." % int(rc))
85-
exit_code = 70
86-
return exit_code
89+
with open(WORK_DIR + "/validation-rc.txt", 'r') as reader:
90+
validation_rc = reader.read()
91+
if int(validation_rc) != 0:
92+
with open(WORK_DIR + "/validation-output.txt", 'r') as validate_reader:
93+
validation_output = validate_reader.read()
94+
VALIDATION_LOGGER.error("Image bundle validation failed with result code: %s and error:\n%s", validation_rc, validation_output)
95+
return 70
96+
97+
logging.info("Image: %s has passed operator bundle image validation test", self.image_to_test)
8798
return 0
8899

89100
if __name__ == '__main__':
90-
resultcodes = [0, 50, 70, 102, 103]
91-
# run fix_etc_passwd.sh to setup random user generated in openshift
92-
subprocess.call(['sh', "/usr/bin/fix_etc_passwd.sh"])
93-
runTest = RunMidstreamCVPTests()
94-
with open("tests_result.log", 'w') as f:
95-
if (runTest.test_for_extract_and_validate_bundle_image() not in resultcodes):
96-
exit_code = 1
97-
print("Error occured during unit tests, please see .errormessage file for more info.")
98-
os.rename(r'tests_result.log', r'.errormessage')
99-
sys.exit(exit_code)
101+
subprocess.run(['fix_etc_passwd.sh'], check=True)
102+
resultcodes = [0, 50, 70, 102]
103+
test_runner = RunMidstreamCVPTests()
104+
return_code = test_runner.test_for_extract_and_validate_bundle_image()
105+
if return_code not in resultcodes:
106+
VALIDATION_LOGGER.error("Unexpected error (%d) occured during unit tests, please see .errormessage file for more info.", return_code)
107+
return_code = 1
108+
sys.exit(return_code)

0 commit comments

Comments
 (0)