|
| 1 | +# grown-up modules |
| 2 | +import logging |
| 3 | + |
| 4 | +# local modules |
| 5 | +from . import context |
| 6 | +from . import execute |
| 7 | + |
| 8 | +class test_runner: |
| 9 | + """A class that manages a list of tests and can execute them on a managed container.""" |
| 10 | + |
| 11 | + def __init__(self, executing_container, tests=None): |
| 12 | + """Constructor for `test_runner`. |
| 13 | +
|
| 14 | + The list of tests is not required to be initialized here. `add_test` will append a |
| 15 | + test to the end of the test list. |
| 16 | +
|
| 17 | + Arguments: |
| 18 | + executing_container -- the container on which tests will be executed |
| 19 | + tests -- optional list of tests to use with this runner |
| 20 | + """ |
| 21 | + # TODO: each test runner is tied to a single container... might need to abstract this out later |
| 22 | + self.executor = executing_container |
| 23 | + self.tests = tests or list() |
| 24 | + self.passed = list() |
| 25 | + self.failed = list() |
| 26 | + self.rc = 0 |
| 27 | + |
| 28 | + |
| 29 | + def __str__(self): |
| 30 | + """Return a string representation of a map representing the data members.""" |
| 31 | + return str({ |
| 32 | + 'executing_container': self.name(), |
| 33 | + 'return_code': self.rc, |
| 34 | + 'test_list': self.tests, |
| 35 | + 'passed_tests': self.passed_tests(), |
| 36 | + 'failed_tests': self.failed_tests() |
| 37 | + }) |
| 38 | + |
| 39 | + |
| 40 | + def add_test(self, test): |
| 41 | + """Append `test` to the test list and return self.""" |
| 42 | + logging.info('before [{}]'.format(self)) |
| 43 | + self.tests.append(test) |
| 44 | + logging.info('after [{}]'.format(self)) |
| 45 | + return self |
| 46 | + |
| 47 | + |
| 48 | + def name(self): |
| 49 | + """Return the name of the executing container.""" |
| 50 | + return self.executor.name |
| 51 | + |
| 52 | + |
| 53 | + def test_list(self): |
| 54 | + """Return the list of tests for which this runner is responsible.""" |
| 55 | + return self.tests |
| 56 | + |
| 57 | + |
| 58 | + def passed_tests(self): |
| 59 | + """Return the list of tests which have been executed and passed.""" |
| 60 | + return self.passed |
| 61 | + |
| 62 | + |
| 63 | + def failed_tests(self): |
| 64 | + """Return the list of tests which have been executed and failed.""" |
| 65 | + return self.failed |
| 66 | + |
| 67 | + |
| 68 | + def skipped_tests(self): |
| 69 | + """Return the list of tests which have not been executed.""" |
| 70 | + executed_tests = self.passed_tests() + self.failed_tests() |
| 71 | + return list(filter(lambda t: t not in executed_tests, self.test_list())) |
| 72 | + |
| 73 | + |
| 74 | + def result_string(self): |
| 75 | + """Return a string representing the results of running the test list.""" |
| 76 | + r = '-----\nresults for [{}]\n'.format(self.name()) |
| 77 | + |
| 78 | + r = r + '\tpassed tests:\n' |
| 79 | + for t in self.passed_tests(): |
| 80 | + r = r + '\t\t[{}]\n'.format(t) |
| 81 | + |
| 82 | + r = r + '\tskipped tests:\n' |
| 83 | + for t in self.skipped_tests(): |
| 84 | + r = r + '\t\t[{}]\n'.format(t) |
| 85 | + |
| 86 | + r = r + '\tfailed tests:\n' |
| 87 | + for t in self.failed_tests(): |
| 88 | + r = r + '\t\t[{}]\n'.format(t) |
| 89 | + |
| 90 | + r = r + '\treturn code:[{}]\n-----\n'.format(self.rc) |
| 91 | + |
| 92 | + return r |
| 93 | + |
| 94 | + |
| 95 | + def run(self, fail_fast=True, *args): |
| 96 | + """Execute test list sequentially on executing container. |
| 97 | +
|
| 98 | + Arguments: |
| 99 | + fail_fast -- if True, the first test to fail ends the run |
| 100 | + *args -- any additional arguments that the test execution can take |
| 101 | + """ |
| 102 | + for t in self.tests: |
| 103 | + cmd, ec = self.execute_test(t, *args) |
| 104 | + |
| 105 | + if ec is 0: |
| 106 | + self.passed_tests().append(t) |
| 107 | + logging.info('[{}]: cmd succeeded [{}]'.format(self.name(), cmd)) |
| 108 | + |
| 109 | + else: |
| 110 | + self.rc = ec |
| 111 | + self.failed_tests().append(t) |
| 112 | + logging.warning('[{}]: cmd failed [{}] [{}]'.format(self.name(), ec, cmd)) |
| 113 | + |
| 114 | + if fail_fast: |
| 115 | + raise RuntimeError('[{}]: command failed [{}]'.format(self.name(), cmd)) |
| 116 | + |
| 117 | + if self.rc is not 0: |
| 118 | + logging.error('[{}]: tests that failed [{}]'.format(self.name(), self.failed_tests())) |
| 119 | + |
| 120 | + |
| 121 | + def execute_test(self, test, *args): |
| 122 | + """Execute `test` with return the command run and the return code.""" |
| 123 | + raise NotImplementedError('test_runner is a base class and should not be used directly') |
| 124 | + |
| 125 | + |
| 126 | +class test_runner_irods_python_suite(test_runner): |
| 127 | + def __init__(self, executing_container, tests=None): |
| 128 | + super(test_runner_irods_python_suite, self).__init__(executing_container, tests) |
| 129 | + |
| 130 | + |
| 131 | + @staticmethod |
| 132 | + def run_tests_command(): |
| 133 | + """Return a list of strings used as a space-delimited invocation of the test runner.""" |
| 134 | + return [context.python(), context.run_tests_script()] |
| 135 | + |
| 136 | + |
| 137 | + def execute_test(self, test, options=None): |
| 138 | + """Execute `test` with `options` and return the command run and the return code.""" |
| 139 | + cmd = self.run_tests_command() + ['--run_specific_test', test] |
| 140 | + if options: cmd.append(options) |
| 141 | + return cmd, execute.execute_command(self.executor, |
| 142 | + ' '.join(cmd), |
| 143 | + user='irods', |
| 144 | + workdir=context.irods_home(), |
| 145 | + stream_output=True) |
0 commit comments