From 8a7b89c66f6a607b2e6032b98455ce7137996dce Mon Sep 17 00:00:00 2001 From: Matt Clay Date: Tue, 27 Aug 2019 00:59:55 -0700 Subject: [PATCH] Support delegation testing collections with deps. All collections in the same collection root as the collection being tested will be sent to the remote system/container. --- test/lib/ansible_test/_internal/data.py | 40 +++++++++++++++++++ test/lib/ansible_test/_internal/payload.py | 9 +++-- .../_internal/provider/layout/__init__.py | 4 +- .../_internal/provider/layout/collection.py | 2 - 4 files changed, 47 insertions(+), 8 deletions(-) diff --git a/test/lib/ansible_test/_internal/data.py b/test/lib/ansible_test/_internal/data.py index 126e028f648..ccdfb4eb61c 100644 --- a/test/lib/ansible_test/_internal/data.py +++ b/test/lib/ansible_test/_internal/data.py @@ -13,6 +13,7 @@ from .util import ( ANSIBLE_LIB_ROOT, ANSIBLE_TEST_ROOT, ANSIBLE_SOURCE_ROOT, + display, ) from .provider import ( @@ -57,6 +58,7 @@ class DataContext: layout_providers = get_path_provider_classes(LayoutProvider) source_providers = get_path_provider_classes(SourceProvider) + self.__layout_providers = layout_providers self.__source_providers = source_providers self.__ansible_source = None # type: t.Optional[t.Tuple[t.Tuple[str, str], ...]] @@ -72,6 +74,44 @@ class DataContext: self.content = content # type: ContentLayout self.results = os.path.join(self.content.root, 'test', 'results') + def create_collection_layouts(self): # type: () -> t.List[ContentLayout] + """ + Return a list of collection layouts, one for each collection in the same collection root as the current collection layout. + An empty list is returned if the current content layout is not a collection layout. + """ + layout = self.content + collection = layout.collection + + if not collection: + return [] + + root_path = os.path.join(collection.root, 'ansible_collections') + display.info('Scanning collection root: %s' % root_path, verbosity=1) + namespace_names = sorted(name for name in os.listdir(root_path) if os.path.isdir(os.path.join(root_path, name))) + collections = [] + + for namespace_name in namespace_names: + namespace_path = os.path.join(root_path, namespace_name) + collection_names = sorted(name for name in os.listdir(namespace_path) if os.path.isdir(os.path.join(namespace_path, name))) + + for collection_name in collection_names: + collection_path = os.path.join(namespace_path, collection_name) + + if collection_path == os.path.join(collection.root, collection.directory): + collection_layout = layout + else: + collection_layout = self.__create_content_layout(self.__layout_providers, self.__source_providers, collection_path, False) + + file_count = len(collection_layout.all_files()) + + if not file_count: + continue + + display.info('Including collection: %s (%d files)' % (collection_layout.collection.full_name, file_count), verbosity=1) + collections.append(collection_layout) + + return collections + @staticmethod def __create_content_layout(layout_providers, # type: t.List[t.Type[LayoutProvider]] source_providers, # type: t.List[t.Type[SourceProvider]] diff --git a/test/lib/ansible_test/_internal/payload.py b/test/lib/ansible_test/_internal/payload.py index e81a0dc51f9..0a6754f5134 100644 --- a/test/lib/ansible_test/_internal/payload.py +++ b/test/lib/ansible_test/_internal/payload.py @@ -81,10 +81,11 @@ def create_payload(args, dst_path): # type: (CommonConfig, str) -> None # exclude built-in ansible modules when they are not needed files = [f for f in files if not is_subdir(f[1], 'lib/ansible/modules/') or f[1] == 'lib/ansible/modules/__init__.py'] - if data_context().content.collection: - # include collections content for testing - files.extend((os.path.join(data_context().content.root, path), os.path.join(data_context().content.collection.directory, path)) - for path in data_context().content.all_files()) + collection_layouts = data_context().create_collection_layouts() + + for layout in collection_layouts: + # include files from each collection in the same collection root as the content being tested + files.extend((os.path.join(layout.root, path), os.path.join(layout.collection.directory, path)) for path in layout.all_files()) for callback in data_context().payload_callbacks: callback(files) diff --git a/test/lib/ansible_test/_internal/provider/layout/__init__.py b/test/lib/ansible_test/_internal/provider/layout/__init__.py index 3e895efa6ad..47750329484 100644 --- a/test/lib/ansible_test/_internal/provider/layout/__init__.py +++ b/test/lib/ansible_test/_internal/provider/layout/__init__.py @@ -135,12 +135,12 @@ class CollectionDetail: name, # type: str namespace, # type: str root, # type: str - prefix, # type: str ): # type: (...) -> None self.name = name self.namespace = namespace self.root = root - self.prefix = prefix + self.full_name = '%s.%s' % (namespace, name) + self.prefix = '%s.' % self.full_name self.directory = os.path.join('ansible_collections', namespace, name) diff --git a/test/lib/ansible_test/_internal/provider/layout/collection.py b/test/lib/ansible_test/_internal/provider/layout/collection.py index a0633f4d514..9c07682fbd7 100644 --- a/test/lib/ansible_test/_internal/provider/layout/collection.py +++ b/test/lib/ansible_test/_internal/provider/layout/collection.py @@ -34,7 +34,6 @@ class CollectionLayout(LayoutProvider): collection_dir = os.path.relpath(root, collection_root) collection_namespace, collection_name = collection_dir.split(os.sep) - collection_prefix = '%s.%s.' % (collection_namespace, collection_name) collection_root = os.path.dirname(collection_root) return ContentLayout(root, @@ -44,7 +43,6 @@ class CollectionLayout(LayoutProvider): name=collection_name, namespace=collection_namespace, root=collection_root, - prefix=collection_prefix, ), unit_path='test/unit', unit_module_path='test/unit/plugins/modules',