|
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 | 5 | import logging |
8 | | -from os import path |
9 | 6 |
|
10 | 7 |
|
11 | 8 | TEST_WORKING_DIR= os.getcwd() |
|
15 | 12 | WORK_DIR=OUTPUT_DIRECTORY+"/output/" |
16 | 13 | PLAYBOOKS_DIR="/project/operator-test-playbooks/" |
17 | 14 | ERROR_MESSAGE_PATH=TEST_WORKING_DIR+"/.errormessage" |
18 | | -logging.basicConfig(filename=ERROR_MESSAGE_PATH) |
| 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)) |
| 22 | + |
19 | 23 |
|
20 | 24 | class RunMidstreamCVPTests(): |
21 | 25 |
|
22 | | - def setUp(self): |
| 26 | + def __init__(self): |
23 | 27 | # IMAGE_TO_TESTis an environment variable that takes |
24 | 28 | # operator-bundle-image |
25 | 29 | self.image_to_test = os.getenv('IMAGE_TO_TEST') |
26 | 30 | # VERBOSITY is verbosity of output log range varying from 1 to 4 |
27 | 31 | self.verbosity = int(os.getenv('VERBOSITY', '1'))*"v" |
28 | 32 |
|
29 | | - def run_subprocess_command(self, exec_cmd): |
30 | | - print("[INFO] Running the subprocess ansible command ") |
31 | | - print(exec_cmd) |
| 33 | + @staticmethod |
| 34 | + def run_subprocess_command(exec_cmd): |
| 35 | + logging.info("Running the subprocess ansible command ") |
| 36 | + logging.info(exec_cmd) |
32 | 37 | env = os.environ.copy() |
33 | | - result = {} |
34 | 38 | process = subprocess.run(exec_cmd, |
35 | | - stdout=subprocess.PIPE, |
36 | | - stderr=subprocess.PIPE, |
37 | | - shell=True, |
38 | | - universal_newlines=True, |
39 | | - env=env) |
40 | | - result["stdout"] = process.stdout |
41 | | - result["stderr"] = process.stderr |
42 | | - result["return_code"] = process.returncode |
43 | | - return result |
| 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 | + } |
44 | 49 |
|
45 | 50 | def run_extract_operator_bundle(self): |
46 | 51 | exec_cmd = "/usr/bin/ansible-playbook -i localhost, -c local -{verbosity} {playbook_dir}/extract-operator-bundle.yml \ |
47 | 52 | -e 'bundle_image={bundle_image}' \ |
48 | 53 | -e 'operator_work_dir={operator_work_dir}' \ |
49 | | - -e 'work_dir={work_dir}'".format( |
50 | | - operator_dir=OPERATOR_DIR, |
51 | | - playbook_dir=PLAYBOOKS_DIR, |
52 | | - operator_work_dir=OPERATOR_WORK_DIR, |
53 | | - bundle_image=self.image_to_test, |
54 | | - verbosity=self.verbosity, |
55 | | - work_dir=WORK_DIR) |
56 | | - result = self.run_subprocess_command(exec_cmd) |
57 | | - 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) |
58 | 60 |
|
59 | 61 | def run_validate_operator_bundle(self): |
60 | | - exec_cmd = "/usr/bin/ansible-playbook -{verbosity} -i localhost, --connection local \ |
61 | | - {playbook_dir}/validate-operator-bundle.yml \ |
62 | | - -e 'operator_dir={operator_dir}' \ |
63 | | - -e 'operator_work_dir={operator_work_dir}' \ |
64 | | - -e 'work_dir={work_dir}'".format(operator_dir=OPERATOR_DIR, |
65 | | - operator_work_dir=OPERATOR_WORK_DIR, |
66 | | - work_dir=WORK_DIR, |
67 | | - playbook_dir=PLAYBOOKS_DIR, |
68 | | - verbosity=self.verbosity) |
69 | | - result = self.run_subprocess_command(exec_cmd) |
70 | | - 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) |
71 | 72 |
|
72 | 73 | def test_for_extract_and_validate_bundle_image(self): |
73 | 74 |
|
74 | | - self.setUp() |
75 | | - global exit_code |
76 | | - exit_code = 0 |
77 | 75 | # check if IMAGE_TO_TEST is defined, return exit_code 102 in case it's not |
78 | | - if (self.image_to_test is None): |
79 | | - logging.error("Environment variable IMAGE_TO_TEST not set! Stopping the tests.") |
80 | | - logging.error("Result code: 102 Error message: Environment variable IMAGE_TO_TEST not set!") |
81 | | - exit_code = 102 |
82 | | - 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 | + |
83 | 81 | result = self.run_extract_operator_bundle() |
84 | | - if (result["return_code"] != 0): |
85 | | - logging.error("Ansible playbook extract-operator-bundle.yml failed with result code: %s , see the file .errormessage for more info." % result["return_code"]) |
86 | | - logging.error("Result code: " + str(result["return_code"])) |
87 | | - logging.error("Error message: " + str(result["stderr"])) |
88 | | - logging.error("Result stdout: " + str(result["stdout"])) |
89 | | - exit_code = 50 |
90 | | - 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 |
91 | 88 | result = self.run_validate_operator_bundle() |
92 | | - with open(WORK_DIR+"/validation-rc.txt", 'r') as reader: |
93 | | - rc = reader.read() |
94 | | - if (int(rc) != 0): |
95 | | - logging.error("Image bundle validation failed with result code: %s , see /project/output/validation-output.txt file for more info." % int(rc)) |
96 | | - exit_code = 70 |
97 | | - return exit_code |
98 | | - print("[INFO] {image_to_test} has passed operator bundle image validation test".format(image_to_test=self.image_to_test)) |
| 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) |
99 | 98 | return 0 |
100 | 99 |
|
101 | 100 | if __name__ == '__main__': |
| 101 | + subprocess.run(['fix_etc_passwd.sh'], check=True) |
102 | 102 | resultcodes = [0, 50, 70, 102] |
103 | | - runTest = RunMidstreamCVPTests() |
104 | | - if (runTest.test_for_extract_and_validate_bundle_image() not in resultcodes): |
105 | | - exit_code = 1 |
106 | | - print("Error occured during unit tests, please see .errormessage file for more info.") |
107 | | - sys.exit(exit_code) |
| 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