From ba1b4565d3d99e0a713664ce892aef91ef78283d Mon Sep 17 00:00:00 2001 From: Matt Clay Date: Tue, 13 Aug 2019 16:28:04 -0700 Subject: [PATCH] Clean up ansible-test results path handling. This brings us closer to being able to use `tests/` instead of `test/` in collections. --- .../ansible_test/_internal/ansible_util.py | 2 +- .../ansible_test/_internal/cloud/__init__.py | 3 ++- test/lib/ansible_test/_internal/cover.py | 19 +++++++++++-------- test/lib/ansible_test/_internal/data.py | 1 + test/lib/ansible_test/_internal/env.py | 6 +++++- test/lib/ansible_test/_internal/executor.py | 11 ++++++----- .../_internal/sanity/integration_aliases.py | 6 +++++- test/lib/ansible_test/_internal/test.py | 6 +++++- .../ansible_test/_internal/units/__init__.py | 3 +-- .../lib/ansible_test/_internal/util_common.py | 6 +++++- 10 files changed, 42 insertions(+), 21 deletions(-) diff --git a/test/lib/ansible_test/_internal/ansible_util.py b/test/lib/ansible_test/_internal/ansible_util.py index 18c4c13b2df..7b4c1e5470e 100644 --- a/test/lib/ansible_test/_internal/ansible_util.py +++ b/test/lib/ansible_test/_internal/ansible_util.py @@ -81,7 +81,7 @@ def ansible_environment(args, color=True, ansible_config=None): if args.debug: env.update(dict( ANSIBLE_DEBUG='true', - ANSIBLE_LOG_PATH=os.path.abspath('test/results/logs/debug.log'), + ANSIBLE_LOG_PATH=os.path.join(data_context().results, 'logs', 'debug.log'), )) if data_context().content.collection: diff --git a/test/lib/ansible_test/_internal/cloud/__init__.py b/test/lib/ansible_test/_internal/cloud/__init__.py index dea1439ab82..1e5e304e8f4 100644 --- a/test/lib/ansible_test/_internal/cloud/__init__.py +++ b/test/lib/ansible_test/_internal/cloud/__init__.py @@ -158,7 +158,8 @@ def cloud_init(args, targets): ) if not args.explain and results: - results_path = 'test/results/data/%s-%s.json' % (args.command, re.sub(r'[^0-9]', '-', str(datetime.datetime.utcnow().replace(microsecond=0)))) + results_path = os.path.join(data_context().results, 'data', '%s-%s.json' % ( + args.command, re.sub(r'[^0-9]', '-', str(datetime.datetime.utcnow().replace(microsecond=0))))) data = dict( clouds=results, diff --git a/test/lib/ansible_test/_internal/cover.py b/test/lib/ansible_test/_internal/cover.py index 968f8ebf8aa..4b38a26e491 100644 --- a/test/lib/ansible_test/_internal/cover.py +++ b/test/lib/ansible_test/_internal/cover.py @@ -35,8 +35,6 @@ from .data import ( data_context, ) -COVERAGE_DIR = 'test/results/coverage' -COVERAGE_FILE = os.path.join(COVERAGE_DIR, 'coverage') COVERAGE_GROUPS = ('command', 'target', 'environment', 'version') COVERAGE_CONFIG_PATH = os.path.join(ANSIBLE_TEST_DATA_ROOT, 'coveragerc') @@ -50,7 +48,8 @@ def command_coverage_combine(args): modules = dict((t.module, t.path) for t in list(walk_module_targets()) if t.path.endswith('.py')) - coverage_files = [os.path.join(COVERAGE_DIR, f) for f in os.listdir(COVERAGE_DIR) if '=coverage.' in f] + coverage_dir = os.path.join(data_context().results, 'coverage') + coverage_files = [os.path.join(coverage_dir, f) for f in os.listdir(coverage_dir) if '=coverage.' in f] ansible_path = os.path.abspath('lib/ansible/') + '/' root_path = data_context().content.root + '/' @@ -183,6 +182,8 @@ def command_coverage_combine(args): invalid_path_count = 0 invalid_path_chars = 0 + coverage_file = os.path.join(data_context().results, 'coverage', 'coverage') + for group in sorted(groups): arc_data = groups[group] @@ -208,7 +209,7 @@ def command_coverage_combine(args): updated.add_arcs(dict((source, []) for source in sources)) if not args.explain: - output_file = COVERAGE_FILE + group + output_file = coverage_file + group updated.write_file(output_file) output_files.append(output_file) @@ -251,7 +252,7 @@ def command_coverage_html(args): output_files = command_coverage_combine(args) for output_file in output_files: - dir_name = 'test/results/reports/%s' % os.path.basename(output_file) + dir_name = os.path.join(data_context().results, 'reports', os.path.basename(output_file)) env = common_environment() env.update(dict(COVERAGE_FILE=output_file)) run_command(args, env=env, cmd=['coverage', 'html', '--rcfile', COVERAGE_CONFIG_PATH, '-i', '-d', dir_name]) @@ -264,7 +265,7 @@ def command_coverage_xml(args): output_files = command_coverage_combine(args) for output_file in output_files: - xml_name = 'test/results/reports/%s.xml' % os.path.basename(output_file) + xml_name = os.path.join(data_context().results, 'reports', '%s.xml' % os.path.basename(output_file)) env = common_environment() env.update(dict(COVERAGE_FILE=output_file)) run_command(args, env=env, cmd=['coverage', 'xml', '--rcfile', COVERAGE_CONFIG_PATH, '-i', '-o', xml_name]) @@ -276,11 +277,13 @@ def command_coverage_erase(args): """ initialize_coverage(args) - for name in os.listdir(COVERAGE_DIR): + coverage_dir = os.path.join(data_context().results, 'coverage') + + for name in os.listdir(coverage_dir): if not name.startswith('coverage') and '=coverage.' not in name: continue - path = os.path.join(COVERAGE_DIR, name) + path = os.path.join(coverage_dir, name) if not args.explain: os.remove(path) diff --git a/test/lib/ansible_test/_internal/data.py b/test/lib/ansible_test/_internal/data.py index b28b5b86669..126e028f648 100644 --- a/test/lib/ansible_test/_internal/data.py +++ b/test/lib/ansible_test/_internal/data.py @@ -70,6 +70,7 @@ class DataContext: content = self.__create_content_layout(layout_providers, source_providers, current_path, True) self.content = content # type: ContentLayout + self.results = os.path.join(self.content.root, 'test', 'results') @staticmethod def __create_content_layout(layout_providers, # type: t.List[t.Type[LayoutProvider]] diff --git a/test/lib/ansible_test/_internal/env.py b/test/lib/ansible_test/_internal/env.py index ce7a65876aa..f5603e5f692 100644 --- a/test/lib/ansible_test/_internal/env.py +++ b/test/lib/ansible_test/_internal/env.py @@ -47,6 +47,10 @@ from .test import ( TestTimeout, ) +from .data import ( + data_context, +) + class EnvConfig(CommonConfig): """Configuration for the tools command.""" @@ -105,7 +109,7 @@ def show_dump_env(args): show_dict(data, verbose) if args.dump and not args.explain: - with open('test/results/bot/data-environment.json', 'w') as results_fd: + with open(os.path.join(data_context().results, 'bot', 'data-environment.json'), 'w') as results_fd: results_fd.write(json.dumps(data, sort_keys=True)) diff --git a/test/lib/ansible_test/_internal/executor.py b/test/lib/ansible_test/_internal/executor.py index c9240ebed8d..15256b3e8d2 100644 --- a/test/lib/ansible_test/_internal/executor.py +++ b/test/lib/ansible_test/_internal/executor.py @@ -194,8 +194,8 @@ def install_command_requirements(args, python_version=None): :type python_version: str | None """ if not args.explain: - make_dirs('test/results/coverage') - make_dirs('test/results/data') + make_dirs(os.path.join(data_context().results, 'coverage')) + make_dirs(os.path.join(data_context().results, 'data')) if isinstance(args, ShellConfig): if args.raw: @@ -1001,14 +1001,15 @@ def command_integration_filtered(args, targets, all_targets, inventory_path, pre if not args.explain: if args.coverage: coverage_temp_path = os.path.join(common_temp_path, COVERAGE_OUTPUT_NAME) - coverage_save_path = 'test/results/coverage' + coverage_save_path = os.path.join(data_context().results, 'coverage') for filename in os.listdir(coverage_temp_path): shutil.copy(os.path.join(coverage_temp_path, filename), os.path.join(coverage_save_path, filename)) remove_tree(common_temp_path) - results_path = 'test/results/data/%s-%s.json' % (args.command, re.sub(r'[^0-9]', '-', str(datetime.datetime.utcnow().replace(microsecond=0)))) + results_path = os.path.join(data_context().results, 'data', '%s-%s.json' % ( + args.command, re.sub(r'[^0-9]', '-', str(datetime.datetime.utcnow().replace(microsecond=0))))) data = dict( targets=results, @@ -1199,7 +1200,7 @@ def integration_environment(args, target, test_dir, inventory_path, ansible_conf callback_plugins = ['junit'] + (env_config.callback_plugins or [] if env_config else []) integration = dict( - JUNIT_OUTPUT_DIR=os.path.abspath('test/results/junit'), + JUNIT_OUTPUT_DIR=os.path.join(data_context().results, 'junit'), ANSIBLE_CALLBACK_WHITELIST=','.join(sorted(set(callback_plugins))), ANSIBLE_TEST_CI=args.metadata.ci_provider, ANSIBLE_TEST_COVERAGE='check' if args.coverage_check else ('yes' if args.coverage else ''), diff --git a/test/lib/ansible_test/_internal/sanity/integration_aliases.py b/test/lib/ansible_test/_internal/sanity/integration_aliases.py index e47b8bc5056..5c5a475d549 100644 --- a/test/lib/ansible_test/_internal/sanity/integration_aliases.py +++ b/test/lib/ansible_test/_internal/sanity/integration_aliases.py @@ -37,6 +37,10 @@ from ..util import ( display, ) +from ..data import ( + data_context, +) + class IntegrationAliasesTest(SanityVersionNeutral): """Sanity test to evaluate integration test aliases.""" @@ -176,7 +180,7 @@ class IntegrationAliasesTest(SanityVersionNeutral): self.check_changes(args, results) - with open('test/results/bot/data-sanity-ci.json', 'w') as results_fd: + with open(os.path.join(data_context().results, 'bot', 'data-sanity-ci.json'), 'w') as results_fd: json.dump(results, results_fd, sort_keys=True, indent=4) messages = [] diff --git a/test/lib/ansible_test/_internal/test.py b/test/lib/ansible_test/_internal/test.py index c33212fe14d..294ba35388b 100644 --- a/test/lib/ansible_test/_internal/test.py +++ b/test/lib/ansible_test/_internal/test.py @@ -18,6 +18,10 @@ from .config import ( TestConfig, ) +from .data import ( + data_context, +) + def calculate_best_confidence(choices, metadata): """ @@ -120,7 +124,7 @@ class TestResult: :type extension: str :rtype: str """ - path = 'test/results/%s/ansible-test-%s' % (directory, self.command) + path = os.path.join(data_context().results, directory, 'ansible-test-%s' % self.command) if self.test: path += '-%s' % self.test diff --git a/test/lib/ansible_test/_internal/units/__init__.py b/test/lib/ansible_test/_internal/units/__init__.py index 3801aa24a02..f125abc9a25 100644 --- a/test/lib/ansible_test/_internal/units/__init__.py +++ b/test/lib/ansible_test/_internal/units/__init__.py @@ -98,8 +98,7 @@ def command_units(args): 'yes' if args.color else 'no', '-p', 'no:cacheprovider', '-c', os.path.join(ANSIBLE_TEST_DATA_ROOT, 'pytest.ini'), - '--junit-xml', - 'test/results/junit/python%s-units.xml' % version, + '--junit-xml', os.path.join(data_context().results, 'junit', 'python%s-units.xml' % version), ] if version != '2.6': diff --git a/test/lib/ansible_test/_internal/util_common.py b/test/lib/ansible_test/_internal/util_common.py index 5649234d6b3..b9f613b7c9c 100644 --- a/test/lib/ansible_test/_internal/util_common.py +++ b/test/lib/ansible_test/_internal/util_common.py @@ -24,6 +24,10 @@ from .util import ( ANSIBLE_TEST_DATA_ROOT, ) +from .data import ( + data_context, +) + class CommonConfig: """Configuration common to all commands.""" @@ -159,7 +163,7 @@ def get_coverage_environment(args, target_name, version, temp_path, module_cover # config is in a temporary directory # results are in the source tree coverage_config_base_path = args.coverage_config_base_path - coverage_output_base_path = os.path.abspath(os.path.join('test/results')) + coverage_output_base_path = data_context().results else: raise Exception('No temp path and no coverage config base path. Check for missing coverage_context usage.')