|
7 | 7 | # Modifications Copyright OpenSearch Contributors. See
|
8 | 8 | # GitHub history for details.
|
9 | 9 |
|
| 10 | +import abc |
10 | 11 | import logging
|
11 | 12 | import os
|
12 | 13 |
|
| 14 | +from git.git_repository import GitRepository |
13 | 15 | from paths.script_finder import ScriptFinder
|
14 | 16 | from system.execute import execute
|
15 |
| -from test_workflow.test_component import TestComponent |
| 17 | +from test_workflow.test_recorder.test_result_data import TestResultData |
| 18 | +from test_workflow.test_result.test_component_results import TestComponentResults |
| 19 | +from test_workflow.test_result.test_result import TestResult |
16 | 20 |
|
17 | 21 |
|
18 |
| -class BwcTestSuite: |
19 |
| - manifest: str |
20 |
| - work_dir: str |
21 |
| - component: str |
22 |
| - keep: bool |
| 22 | +class BwcTestSuite(abc.ABC): |
23 | 23 |
|
24 |
| - def __init__(self, manifest, work_dir, component=None, keep=False): |
25 |
| - self.manifest = manifest |
| 24 | + def __init__( |
| 25 | + self, |
| 26 | + work_dir, |
| 27 | + component, |
| 28 | + test_config, |
| 29 | + test_recorder, |
| 30 | + manifest |
| 31 | + ): |
26 | 32 | self.work_dir = work_dir
|
27 | 33 | self.component = component
|
28 |
| - self.keep = keep |
29 |
| - |
30 |
| - def run_tests(self, work_dir, component_name): |
31 |
| - script = ScriptFinder.find_bwc_test_script(component_name, work_dir) |
32 |
| - (status, stdout, stderr) = execute(script, work_dir, True, False) |
33 |
| - return (status, stdout, stderr) |
34 |
| - |
35 |
| - def component_bwc_tests(self, component): |
36 |
| - test_component = TestComponent(component.repository, component.commit_id) |
37 |
| - test_component.checkout(os.path.join(self.work_dir, component.name)) |
38 |
| - try: |
39 |
| - console_output = self.run_tests(os.path.join(self.work_dir, component.name), component.name) |
40 |
| - return console_output |
41 |
| - except: |
42 |
| - # TODO: Store and report test failures for {component} |
43 |
| - logging.info(f"Exception while running BWC tests for {component.name}") |
44 |
| - |
45 |
| - def execute(self): |
46 |
| - # For each component, check out the git repo and run `bwctest.sh` |
47 |
| - for component in self.manifest.components.select(focus=self.component): |
48 |
| - # TODO: Store and report test results, send notification via {console_output} |
49 |
| - self.component_bwc_tests(component) |
| 34 | + self.test_config = test_config |
| 35 | + self.test_recorder = test_recorder |
| 36 | + self.manifest = manifest |
| 37 | + |
| 38 | + self.repo = GitRepository( |
| 39 | + self.component.repository, |
| 40 | + self.component.commit_id, |
| 41 | + os.path.join(self.work_dir, self.component.name), |
| 42 | + test_config.working_directory |
| 43 | + ) |
| 44 | + |
| 45 | + self.save_logs = test_recorder.test_results_logs |
| 46 | + |
| 47 | + def execute_tests(self): |
| 48 | + test_results = TestComponentResults() |
| 49 | + |
| 50 | + for config in self.test_config.bwc_test["test-configs"]: |
| 51 | + status = self.execute_bwctest_sh(config) |
| 52 | + |
| 53 | + test_results.append(TestResult(self.component.name, config, status)) |
| 54 | + return test_results |
| 55 | + |
| 56 | + def execute_bwctest_sh(self, config): |
| 57 | + security = self.is_security_enabled(config) |
| 58 | + script = ScriptFinder.find_bwc_test_script(self.component.name, self.repo.working_directory) |
| 59 | + if os.path.exists(script): |
| 60 | + cmd = self.get_cmd(script, security, self.manifest.build.location) |
| 61 | + self.repo_work_dir = os.path.join( |
| 62 | + self.repo.dir, self.test_config.working_directory) if self.test_config.working_directory is not None else self.repo.dir |
| 63 | + (status, stdout, stderr) = execute(cmd, self.repo_work_dir, True, False) |
| 64 | + |
| 65 | + test_result_data = TestResultData( |
| 66 | + self.component.name, |
| 67 | + self.test_config, |
| 68 | + status, |
| 69 | + stdout, |
| 70 | + stderr, |
| 71 | + self.test_artifact_files |
| 72 | + ) |
| 73 | + self.save_logs.save_test_result_data(test_result_data) |
| 74 | + if stderr: |
| 75 | + logging.info("BWC test run failed for component " + self.component.name) |
| 76 | + logging.info(stderr) |
| 77 | + return status |
| 78 | + else: |
| 79 | + logging.info(f"{script} does not exist. Skipping integ tests for {self.component.name}") |
| 80 | + |
| 81 | + def is_security_enabled(self, config): |
| 82 | + if config in ["with-security", "without-security"]: |
| 83 | + return True if config == "with-security" else False |
| 84 | + else: |
| 85 | + raise InvalidTestConfigError("Unsupported test config: " + config) |
| 86 | + |
| 87 | + def pretty_print_message(self, message): |
| 88 | + logging.info("===============================================") |
| 89 | + logging.info(message) |
| 90 | + logging.info("===============================================") |
| 91 | + |
| 92 | + @abc.abstractmethod |
| 93 | + def get_cmd(self): |
| 94 | + pass |
| 95 | + |
| 96 | + @property |
| 97 | + @abc.abstractmethod |
| 98 | + def test_artifact_files(self): |
| 99 | + pass |
| 100 | + |
| 101 | + |
| 102 | +class InvalidTestConfigError(Exception): |
| 103 | + pass |
0 commit comments