diff --git a/avocado/core/job.py b/avocado/core/job.py index 6d17f5fb177d280d86aac6448fad15d7562f6489..030dde8ab984d26ff88835b31968a105e8a5bff6 100644 --- a/avocado/core/job.py +++ b/avocado/core/job.py @@ -375,7 +375,8 @@ class Job: @classmethod def from_config(cls, job_config, suites_configs=None): suites_configs = suites_configs or [deepcopy(job_config)] - suites = [TestSuite.from_config(config) for config in suites_configs] + suites = [TestSuite.from_config(config, job_config=job_config) + for config in suites_configs] return cls(job_config, suites) @property diff --git a/avocado/core/suite.py b/avocado/core/suite.py index 26199a91586830433fdb37179f7f85c2e1a3b66b..1e2e170ff385243a757aa6c67215548bc3055bae 100644 --- a/avocado/core/suite.py +++ b/avocado/core/suite.py @@ -24,13 +24,15 @@ class TestSuiteStatus(Enum): class TestSuite: - def __init__(self, name, config, tests=None): + def __init__(self, name, config, tests=None, job_config=None): self.name = name self.tests = tests # Create a complete config dict with all registered options + custom # config self.config = settings.as_dict() + if job_config: + self.config.update(job_config) if config: self.config.update(config) @@ -142,7 +144,9 @@ class TestSuite: return self.runner.run_suite(job, self) @classmethod - def from_config(cls, config, name=None): + def from_config(cls, config, name=None, job_config=None): + if job_config: + config.update(job_config) runner = config.get('run.test_runner') or 'runner' if runner == 'nrunner': suite = cls._from_config_with_resolver(config, name) diff --git a/selftests/unit/test_job.py b/selftests/unit/test_job.py index 4b56baac86e57a14de50878d9b6a5e9fb825c4fa..875ac7b04f224691e41c5f088604f81d156ffb12 100644 --- a/selftests/unit/test_job.py +++ b/selftests/unit/test_job.py @@ -289,6 +289,31 @@ class JobTest(unittest.TestCase): self.assertEqual(os.path.dirname(self.job.logdir), self.tmpdir.name) self.assertTrue(os.path.isfile(os.path.join(self.job.logdir, 'id'))) + def test_job_suite_parent_config(self): + """This will test if test suites are inheriting configs from job.""" + config = {'core.show': ['none'], + 'run.results_dir': self.tmpdir.name} + + suite_config = {'run.references': ['/bin/true']} + + # Manual/Custom method + suite = TestSuite('foo-test', config=suite_config, job_config=config) + self.job = job.Job(config, [suite]) + self.assertEqual(self.job.test_suites[0].config.get('run.results_dir'), + self.tmpdir.name) + + # Automatic method passing suites + self.job = job.Job.from_config(job_config=config, + suites_configs=[suite_config]) + self.assertEqual(self.job.test_suites[0].config.get('run.results_dir'), + self.tmpdir.name) + + # Automatic method passing only one config + config.update({'run.references': ['/bin/true']}) + self.job = job.Job.from_config(job_config=config) + self.assertEqual(self.job.test_suites[0].config.get('run.results_dir'), + self.tmpdir.name) + def test_job_dryrun_no_base_logdir(self): config = {'core.show': ['none'], 'run.store_logging_stream': [], diff --git a/selftests/unit/test_suite.py b/selftests/unit/test_suite.py new file mode 100644 index 0000000000000000000000000000000000000000..4f909cb635cdac6551bcf0ae338dad2c0fe110c3 --- /dev/null +++ b/selftests/unit/test_suite.py @@ -0,0 +1,88 @@ +import tempfile +import unittest.mock + +from avocado.core import data_dir +from avocado.core.suite import TestSuite +from avocado.utils import path as utils_path + +from .. import setup_avocado_loggers, temp_dir_prefix + +setup_avocado_loggers() + + +class TestSuiteTest(unittest.TestCase): + + def setUp(self): + self.suite = None + data_dir._tmp_tracker.unittest_refresh_dir_tracker() + prefix = temp_dir_prefix(__name__, self, 'setUp') + self.tmpdir = tempfile.TemporaryDirectory(prefix=prefix) + + @staticmethod + def _find_simple_test_candidates(candidates=None): + if candidates is None: + candidates = ['true'] + found = [] + for candidate in candidates: + try: + found.append(utils_path.find_command(candidate)) + except utils_path.CmdNotFoundError: + pass + return found + + def test_custom_suite(self): + """Custom suites should assume custom tests. + + When using custom suites (from constructor) we are assuming no + magic, no tests should be created from run.references. + """ + tests = self._find_simple_test_candidates() + config = {'run.results_dir': self.tmpdir.name, + 'core.show': ['none'], + 'run.references': tests} + + self.suite = TestSuite(name='foo', config=config) + self.assertEqual(0, self.suite.size) + + def test_automatic_suite(self): + """Automatic suites should create tests. + + When using automatic suites we are assuming magic, + and, tests should be created from run.references. + """ + tests = self._find_simple_test_candidates() + config = {'run.results_dir': self.tmpdir.name, + 'core.show': ['none'], + 'run.references': tests} + + self.suite = TestSuite.from_config(config=config) + self.assertEqual(1, self.suite.size) + + def test_config_extend_manual(self): + """Test extends config from job when using manual method.""" + tests = self._find_simple_test_candidates() + job_config = {'run.results_dir': self.tmpdir.name, + 'core.show': ['none']} + suite_config = {'run.references': tests} + self.suite = TestSuite(name='foo', + config=suite_config, + job_config=job_config) + self.assertEqual(self.suite.config.get('core.show'), ['none']) + + def test_config_extend_automatic(self): + """Test extends config from job when using automatic method.""" + tests = self._find_simple_test_candidates() + job_config = {'run.results_dir': self.tmpdir.name, + 'core.show': ['none']} + suite_config = {'run.references': tests} + self.suite = TestSuite.from_config(config=suite_config, + job_config=job_config) + self.assertEqual(self.suite.config.get('core.show'), ['none']) + + def tearDown(self): + data_dir._tmp_tracker.unittest_refresh_dir_tracker() + self.tmpdir.cleanup() + + +if __name__ == '__main__': + unittest.main()