|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 | import os |
3 | 3 | import sys |
4 | | -import json |
5 | | -import unittest |
6 | 4 | 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)) |
8 | 22 |
|
9 | 23 |
|
10 | 24 | class RunMidstreamCVPTests(): |
11 | 25 |
|
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 |
21 | 29 | 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 | + } |
24 | 49 |
|
25 | 50 | 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 \ |
27 | 52 | -e 'bundle_image={bundle_image}' \ |
28 | 53 | -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) |
36 | 60 |
|
37 | 61 | 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) |
47 | 72 |
|
48 | 73 | def test_for_extract_and_validate_bundle_image(self): |
49 | 74 |
|
50 | | - self.setUp() |
51 | | - global exit_code |
52 | | - exit_code = 0 |
53 | 75 | # 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 | + |
70 | 81 | 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 |
77 | 88 | 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) |
87 | 98 | return 0 |
88 | 99 |
|
89 | 100 | 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