From 0f2140706d6b79f04ea79f03696f3b8eb34cb72e Mon Sep 17 00:00:00 2001 From: Richard Davis Date: Tue, 1 Feb 2022 17:25:00 -0500 Subject: [PATCH] Prefix fixtures to prevent conflicts until pytest-dev/pytest#3966 is resolved. --- README.md | 16 +- pytest_docker_registry_fixtures/__init__.py | 2 +- pytest_docker_registry_fixtures/fixtures.py | 276 ++++++++++---------- tests/test_fixtures.py | 64 ++--- 4 files changed, 179 insertions(+), 179 deletions(-) diff --git a/README.md b/README.md index 479e15c..59fbd1a 100644 --- a/README.md +++ b/README.md @@ -70,14 +70,6 @@ def test_docker_pull(docker_client: DockerClient): image = docker_client.image.pull("busybox:1.30.1") ``` -### docker_compose_insecure - -This fixture uses the `docker_compose_files` fixture to locate a user-defined docker-compose configuration file (typically tests/docker-compose.yml) that contains the pytest-docker-registry-insecure service. If one cannot be located, an embedded configuration is copied to a temporary location and returned. This fixture is used to instantiate the insecure docker registry service. - -### docker_compose_secure - -This fixture uses the `docker_compose_files` fixture to locate a user-defined docker-compose configuration file (typically tests/docker-compose.yml) that contains the pytest-docker-registry-secure service. If one cannot be located, an embedded configuration is copied to a temporary location and returned. This fixture is used to instantiate the secure docker registry service; however, unlike the configuration returned by the [docker_compose_insecure](#docker_compose_insecure) fixture, this configuration will be treated as a template; the $PATH_CERTIFICATE, $PATH_HTPASSWD, and $PATH_KEY tokens will be populated with the absolute paths provided by the [docker_registry_certs](#docker_registry_certs) and [docker_registry_htpasswd](#docker_registry_htpasswd) fixtures, as appropriate. - ### docker_registry_auth_header Retrieves an HTTP basic authentication header that is populated with credentials that can access the secure docker registry service. The credentials are retrieved from the [docker_registry_password](#docker_registry_password) and [docker_registry_username](#docker_registry_username) fixtures. This fixture is used to replicate docker images into the secure docker registry service. @@ -184,6 +176,14 @@ Provides an SSL context containing the CA trust store from the [docker_registry Provides a generated username to use for authentication to the secure docker registry service. This fixture is used to replicate docker images into the secure docker registry service. +### pdrf_docker_compose_insecure + +This fixture uses the `docker_compose_files` fixture to locate a user-defined docker-compose configuration file (typically tests/docker-compose.yml) that contains the pytest-docker-registry-insecure service. If one cannot be located, an embedded configuration is copied to a temporary location and returned. This fixture is used to instantiate the insecure docker registry service. + +### pdrf_docker_compose_secure + +This fixture uses the `docker_compose_files` fixture to locate a user-defined docker-compose configuration file (typically tests/docker-compose.yml) that contains the pytest-docker-registry-secure service. If one cannot be located, an embedded configuration is copied to a temporary location and returned. This fixture is used to instantiate the secure docker registry service; however, unlike the configuration returned by the [pdrf_docker_compose_insecure](#pdrf_docker_compose_insecure) fixture, this configuration will be treated as a template; the $PATH_CERTIFICATE, $PATH_HTPASSWD, and $PATH_KEY tokens will be populated with the absolute paths provided by the [docker_registry_certs](#docker_registry_certs) and [docker_registry_htpasswd](#docker_registry_htpasswd) fixtures, as appropriate. + ## Markers ### pytest.mark.push_image diff --git a/pytest_docker_registry_fixtures/__init__.py b/pytest_docker_registry_fixtures/__init__.py index 80ff206..5e28e31 100644 --- a/pytest_docker_registry_fixtures/__init__.py +++ b/pytest_docker_registry_fixtures/__init__.py @@ -6,4 +6,4 @@ from .fixtures import * from .utils import get_pushed_images, replicate_manifest_list -__version__ = "0.2.2.dev0" +__version__ = "0.3.0.dev0" diff --git a/pytest_docker_registry_fixtures/fixtures.py b/pytest_docker_registry_fixtures/fixtures.py index 232aa8e..a98f62f 100644 --- a/pytest_docker_registry_fixtures/fixtures.py +++ b/pytest_docker_registry_fixtures/fixtures.py @@ -85,136 +85,6 @@ def docker_client() -> DockerClient: return from_env() -def _docker_compose_insecure( - *, - docker_compose_files: List[str], - scale_factor: int, - tmp_path_factory: TempPathFactory, -) -> Generator[List[Path], None, None]: - """ - Provides the location of the docker-compose configuration file containing the insecure docker registry service. - """ - cache_key = _docker_compose_insecure.__name__ - result = CACHE.get(cache_key, []) - for i in range(scale_factor): - if i < len(result): - continue - - service_name = DOCKER_REGISTRY_SERVICE_PATTERN.format("insecure", i) - chain = itertools.chain( - get_docker_compose_user_defined(docker_compose_files, service_name), - # TODO: lovely-docker-compose uses the file for teardown ... - get_embedded_file( - tmp_path_factory, delete_after=False, name="docker-compose.yml" - ), - ) - for path in chain: - result.append(path) - break - else: - LOGGER.warning("Unable to find docker compose for: %s", service_name) - result.append("-unknown-") - CACHE[cache_key] = result - yield result - - -@pytest.fixture(scope="session") -def docker_compose_insecure( - docker_compose_files: List[str], tmp_path_factory: TempPathFactory -) -> Generator[Path, None, None]: - """ - Provides the location of the docker-compose configuration file containing the insecure docker registry service. - """ - for lst in _docker_compose_insecure( - docker_compose_files=docker_compose_files, - scale_factor=1, - tmp_path_factory=tmp_path_factory, - ): - yield lst[0] - - -@pytest.fixture(scope="session") -def docker_compose_insecure_list( - docker_compose_files: List[str], - pdrf_scale_factor: int, - tmp_path_factory: TempPathFactory, -) -> Generator[List[Path], None, None]: - """ - Provides the location of the docker-compose configuration file containing the insecure docker registry service. - """ - yield from _docker_compose_insecure( - docker_compose_files=docker_compose_files, - scale_factor=pdrf_scale_factor, - tmp_path_factory=tmp_path_factory, - ) - - -def _docker_compose_secure( - *, - docker_compose_files: List[str], - scale_factor: int, - tmp_path_factory: TempPathFactory, -) -> Generator[List[Path], None, None]: - """ - Provides the location of the templated docker-compose configuration file containing the secure docker registry - service. - """ - cache_key = _docker_compose_secure.__name__ - result = CACHE.get(cache_key, []) - for i in range(scale_factor): - if i < len(result): - continue - - service_name = DOCKER_REGISTRY_SERVICE_PATTERN.format("secure", i) - chain = itertools.chain( - get_docker_compose_user_defined(docker_compose_files, service_name), - get_embedded_file( - tmp_path_factory, delete_after=False, name="docker-compose.yml" - ), - ) - for path in chain: - result.append(path) - break - else: - LOGGER.warning("Unable to find docker compose for: %s", service_name) - result.append("-unknown-") - CACHE[cache_key] = result - yield result - - -@pytest.fixture(scope="session") -def docker_compose_secure( - docker_compose_files: List[str], tmp_path_factory: TempPathFactory -) -> Generator[Path, None, None]: - """ - Provides the location of the templated docker-compose configuration file containing the secure docker registry - service. - """ - for lst in _docker_compose_secure( - docker_compose_files=docker_compose_files, - scale_factor=1, - tmp_path_factory=tmp_path_factory, - ): - yield lst[0] - - -@pytest.fixture(scope="session") -def docker_compose_secure_list( - docker_compose_files: List[str], - pdrf_scale_factor: int, - tmp_path_factory: TempPathFactory, -) -> Generator[List[Path], None, None]: - """ - Provides the location of the templated docker-compose configuration file containing the secure docker registry - service. - """ - yield from _docker_compose_secure( - docker_compose_files=docker_compose_files, - scale_factor=pdrf_scale_factor, - tmp_path_factory=tmp_path_factory, - ) - - def _docker_registry_auth_header( *, docker_registry_password_list: List[str], @@ -525,15 +395,15 @@ def _docker_registry_insecure( @pytest.fixture(scope="session") def docker_registry_insecure( docker_client: DockerClient, - docker_compose_insecure: Path, docker_services: Services, + pdrf_docker_compose_insecure: Path, request, tmp_path_factory: TempPathFactory, ) -> Generator[DockerRegistryInsecure, None, None]: """Provides the endpoint of a local, mutable, insecure, docker registry.""" for lst in _docker_registry_insecure( docker_client=docker_client, - docker_compose_insecure_list=[docker_compose_insecure], + docker_compose_insecure_list=[pdrf_docker_compose_insecure], docker_services=docker_services, request=request, scale_factor=1, @@ -545,8 +415,8 @@ def docker_registry_insecure( @pytest.fixture(scope="session") def docker_registry_insecure_list( docker_client: DockerClient, - docker_compose_insecure_list: List[Path], docker_services: Services, + pdrf_docker_compose_insecure_list: List[Path], pdrf_scale_factor: int, request, tmp_path_factory: TempPathFactory, @@ -554,7 +424,7 @@ def docker_registry_insecure_list( """Provides the endpoint of a local, mutable, insecure, docker registry.""" yield from _docker_registry_insecure( docker_client=docker_client, - docker_compose_insecure_list=docker_compose_insecure_list, + docker_compose_insecure_list=pdrf_docker_compose_insecure_list, docker_services=docker_services, request=request, scale_factor=pdrf_scale_factor, @@ -691,7 +561,6 @@ def _docker_registry_secure( @pytest.fixture(scope="session") def docker_registry_secure( docker_client: DockerClient, - docker_compose_secure: Path, docker_registry_auth_header: Dict[str, str], docker_registry_cacerts: Path, docker_registry_certs: DockerRegistryCerts, @@ -700,13 +569,14 @@ def docker_registry_secure( docker_registry_ssl_context: SSLContext, docker_registry_username: str, docker_services: Services, + pdrf_docker_compose_secure: Path, request, tmp_path_factory: TempPathFactory, ) -> Generator[DockerRegistrySecure, None, None]: """Provides the endpoint of a local, mutable, secure, docker registry.""" for lst in _docker_registry_secure( docker_client=docker_client, - docker_compose_secure_list=[docker_compose_secure], + docker_compose_secure_list=[pdrf_docker_compose_secure], docker_registry_auth_header_list=[docker_registry_auth_header], docker_registry_cacerts_list=[docker_registry_cacerts], docker_registry_certs_list=[docker_registry_certs], @@ -725,7 +595,6 @@ def docker_registry_secure( @pytest.fixture(scope="session") def docker_registry_secure_list( docker_client: DockerClient, - docker_compose_secure_list: List[Path], docker_registry_auth_header_list: List[Dict[str, str]], docker_registry_cacerts_list: List[Path], docker_registry_certs_list: List[DockerRegistryCerts], @@ -734,6 +603,7 @@ def docker_registry_secure_list( docker_registry_ssl_context_list: List[SSLContext], docker_registry_username_list: List[str], docker_services: Services, + pdrf_docker_compose_secure_list: List[Path], pdrf_scale_factor: int, request, tmp_path_factory: TempPathFactory, @@ -741,7 +611,7 @@ def docker_registry_secure_list( """Provides the endpoint of a local, mutable, secure, docker registry.""" yield from _docker_registry_secure( docker_client=docker_client, - docker_compose_secure_list=docker_compose_secure_list, + docker_compose_secure_list=pdrf_docker_compose_secure_list, docker_registry_auth_header_list=docker_registry_auth_header_list, docker_registry_cacerts_list=docker_registry_cacerts_list, docker_registry_certs_list=docker_registry_certs_list, @@ -830,6 +700,136 @@ def docker_registry_username_list( return _docker_registry_username(scale_factor=pdrf_scale_factor) +def _pdrf_docker_compose_insecure( + *, + docker_compose_files: List[str], + scale_factor: int, + tmp_path_factory: TempPathFactory, +) -> Generator[List[Path], None, None]: + """ + Provides the location of the docker-compose configuration file containing the insecure docker registry service. + """ + cache_key = _pdrf_docker_compose_insecure.__name__ + result = CACHE.get(cache_key, []) + for i in range(scale_factor): + if i < len(result): + continue + + service_name = DOCKER_REGISTRY_SERVICE_PATTERN.format("insecure", i) + chain = itertools.chain( + get_docker_compose_user_defined(docker_compose_files, service_name), + # TODO: lovely-docker-compose uses the file for teardown ... + get_embedded_file( + tmp_path_factory, delete_after=False, name="docker-compose.yml" + ), + ) + for path in chain: + result.append(path) + break + else: + LOGGER.warning("Unable to find docker compose for: %s", service_name) + result.append("-unknown-") + CACHE[cache_key] = result + yield result + + +@pytest.fixture(scope="session") +def pdrf_docker_compose_insecure( + docker_compose_files: List[str], tmp_path_factory: TempPathFactory +) -> Generator[Path, None, None]: + """ + Provides the location of the docker-compose configuration file containing the insecure docker registry service. + """ + for lst in _pdrf_docker_compose_insecure( + docker_compose_files=docker_compose_files, + scale_factor=1, + tmp_path_factory=tmp_path_factory, + ): + yield lst[0] + + +@pytest.fixture(scope="session") +def pdrf_docker_compose_insecure_list( + docker_compose_files: List[str], + pdrf_scale_factor: int, + tmp_path_factory: TempPathFactory, +) -> Generator[List[Path], None, None]: + """ + Provides the location of the docker-compose configuration file containing the insecure docker registry service. + """ + yield from _pdrf_docker_compose_insecure( + docker_compose_files=docker_compose_files, + scale_factor=pdrf_scale_factor, + tmp_path_factory=tmp_path_factory, + ) + + +def _pdrf_docker_compose_secure( + *, + docker_compose_files: List[str], + scale_factor: int, + tmp_path_factory: TempPathFactory, +) -> Generator[List[Path], None, None]: + """ + Provides the location of the templated docker-compose configuration file containing the secure docker registry + service. + """ + cache_key = _pdrf_docker_compose_secure.__name__ + result = CACHE.get(cache_key, []) + for i in range(scale_factor): + if i < len(result): + continue + + service_name = DOCKER_REGISTRY_SERVICE_PATTERN.format("secure", i) + chain = itertools.chain( + get_docker_compose_user_defined(docker_compose_files, service_name), + get_embedded_file( + tmp_path_factory, delete_after=False, name="docker-compose.yml" + ), + ) + for path in chain: + result.append(path) + break + else: + LOGGER.warning("Unable to find docker compose for: %s", service_name) + result.append("-unknown-") + CACHE[cache_key] = result + yield result + + +@pytest.fixture(scope="session") +def pdrf_docker_compose_secure( + docker_compose_files: List[str], tmp_path_factory: TempPathFactory +) -> Generator[Path, None, None]: + """ + Provides the location of the templated docker-compose configuration file containing the secure docker registry + service. + """ + for lst in _pdrf_docker_compose_secure( + docker_compose_files=docker_compose_files, + scale_factor=1, + tmp_path_factory=tmp_path_factory, + ): + yield lst[0] + + +@pytest.fixture(scope="session") +def pdrf_docker_compose_secure_list( + docker_compose_files: List[str], + pdrf_scale_factor: int, + tmp_path_factory: TempPathFactory, +) -> Generator[List[Path], None, None]: + """ + Provides the location of the templated docker-compose configuration file containing the secure docker registry + service. + """ + yield from _pdrf_docker_compose_secure( + docker_compose_files=docker_compose_files, + scale_factor=pdrf_scale_factor, + tmp_path_factory=tmp_path_factory, + ) + + @pytest.fixture(scope="session") def pdrf_scale_factor() -> int: """Provides the number enumerated instances to be instantiated.""" diff --git a/tests/test_fixtures.py b/tests/test_fixtures.py index 9044c21..6a5d86a 100644 --- a/tests/test_fixtures.py +++ b/tests/test_fixtures.py @@ -46,38 +46,6 @@ def test_docker_client(docker_client: DockerClient): assert docker_client -def test_docker_compose_insecure(docker_compose_insecure: Path): - """Test that the embedded docker-compose for insecure registries can be copied to a temporary file.""" - service_name = DOCKER_REGISTRY_SERVICE_PATTERN.format("insecure", 0) - assert service_name in docker_compose_insecure.read_text() - - -def test_docker_compose_insecure_list( - docker_compose_insecure_list: List[Path], pdrf_scale_factor: int -): - """Test that the embedded docker-compose for insecure registries can be copied to a temporary file.""" - for i in range(pdrf_scale_factor): - service_name = DOCKER_REGISTRY_SERVICE_PATTERN.format("insecure", i) - assert service_name in docker_compose_insecure_list[i].read_text() - assert no_duplicates(docker_compose_insecure_list) - - -def test_docker_compose_secure(docker_compose_secure: Path): - """Test that the embedded docker-compose for secure registries can be copied to a temporary file.""" - service_name = DOCKER_REGISTRY_SERVICE_PATTERN.format("secure", 0) - assert service_name in docker_compose_secure.read_text() - - -def test_docker_compose_secure_list( - docker_compose_secure_list: List[Path], pdrf_scale_factor: int -): - """Test that the embedded docker-compose for secure registries can be copied to a temporary file.""" - for i in range(pdrf_scale_factor): - service_name = DOCKER_REGISTRY_SERVICE_PATTERN.format("secure", i) - assert service_name in docker_compose_secure_list[i].read_text() - assert no_duplicates(docker_compose_secure_list) - - def test_docker_registry_auth_header( docker_registry_auth_header: Dict[str, str], docker_registry_password: str, @@ -441,3 +409,35 @@ def test_docker_registry_username_list( for i in range(pdrf_scale_factor): assert docker_registry_username_list[i] assert no_duplicates(docker_registry_username_list) + + +def test_pdrf_docker_compose_insecure(pdrf_docker_compose_insecure: Path): + """Test that the embedded docker-compose for insecure registries can be copied to a temporary file.""" + service_name = DOCKER_REGISTRY_SERVICE_PATTERN.format("insecure", 0) + assert service_name in pdrf_docker_compose_insecure.read_text() + + +def test_pdrf_docker_compose_insecure_list( + pdrf_docker_compose_insecure_list: List[Path], pdrf_scale_factor: int +): + """Test that the embedded docker-compose for insecure registries can be copied to a temporary file.""" + for i in range(pdrf_scale_factor): + service_name = DOCKER_REGISTRY_SERVICE_PATTERN.format("insecure", i) + assert service_name in pdrf_docker_compose_insecure_list[i].read_text() + assert no_duplicates(pdrf_docker_compose_insecure_list) + + +def test_pdrf_docker_compose_secure(pdrf_docker_compose_secure: Path): + """Test that the embedded docker-compose for secure registries can be copied to a temporary file.""" + service_name = DOCKER_REGISTRY_SERVICE_PATTERN.format("secure", 0) + assert service_name in pdrf_docker_compose_secure.read_text() + + +def test_pdrf_docker_compose_secure_list( + pdrf_docker_compose_secure_list: List[Path], pdrf_scale_factor: int +): + """Test that the embedded docker-compose for secure registries can be copied to a temporary file.""" + for i in range(pdrf_scale_factor): + service_name = DOCKER_REGISTRY_SERVICE_PATTERN.format("secure", i) + assert service_name in pdrf_docker_compose_secure_list[i].read_text() + assert no_duplicates(pdrf_docker_compose_secure_list)