From 9ce2cb1ef03dfcb00f6b9c339ac2d0f034709dcf Mon Sep 17 00:00:00 2001 From: Richard Davis Date: Tue, 1 Feb 2022 17:24:23 -0500 Subject: [PATCH] Prefix fixtures to prevent conflicts until pytest-dev/pytest#3966 is resolved. --- README.md | 16 +- pytest_docker_haproxy_fixtures/__init__.py | 2 +- pytest_docker_haproxy_fixtures/fixtures.py | 280 ++++++++++----------- tests/test_fixtures.py | 64 ++--- 4 files changed, 181 insertions(+), 181 deletions(-) diff --git a/README.md b/README.md index 8ee97fa..9f38635 100644 --- a/README.md +++ b/README.md @@ -58,14 +58,6 @@ $ python -m pip install --editable .[dev] ## Fixtures -### 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-haproxy-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 haproxy service. The configuration will be treated as a template; the $PATH_HAPROXYCFG token will be populated with the absolute path provided by the [haproxy_haproxycfg](#haproxy_haproxycfg) fixture. - -### 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-haproxy-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 haproxy service. The configuration will be treated as a template; the $PATH_CERTIFICATE, $PATH_HAPROXYCFG, and $PATH_KEY tokens will be populated with the absolute paths provided by the [haproxy_certs](#haproxy_certs) and [haproxy_haproxycfg](#haproxy_haproxycfg) fixtures, as appropriate. - ### haproxy_auth_header Retrieves an HTTP basic authentication header that is populated with credentials that can access the secure docker haproxy service. The credentials are retrieved from the [haproxy_password](#haproxy_password) and [haproxy_username](#haproxy_username) fixtures. This fixture is used to replicate docker images into the secure docker haproxy service. @@ -165,6 +157,14 @@ Provides an SSL context containing the CA trust store from the [haproxy_cacerts Provides a generated username to use for authentication to the secure docker haproxy service. This fixture is used to replicate docker images into the secure docker haproxy service. +### pdhf_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-haproxy-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 haproxy service. The configuration will be treated as a template; the $PATH_HAPROXYCFG token will be populated with the absolute path provided by the [haproxy_haproxycfg](#haproxy_haproxycfg) fixture. + +### pdhf_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-haproxy-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 haproxy service. The configuration will be treated as a template; the $PATH_CERTIFICATE, $PATH_HAPROXYCFG, and $PATH_KEY tokens will be populated with the absolute paths provided by the [haproxy_certs](#haproxy_certs) and [haproxy_haproxycfg](#haproxy_haproxycfg) fixtures, as appropriate. + ## Enumerated Fixtures It is possible to instantiate multiple haproxy instances using the corresponding enumerated fixtures. All [fixtures](#fixtures) listed above have _*_list_ (e.g. `haproxy_secure` -> `haproxy_secure_list`) versions that will return enumerated lists of corresponding data type. diff --git a/pytest_docker_haproxy_fixtures/__init__.py b/pytest_docker_haproxy_fixtures/__init__.py index 289beb4..288ac59 100644 --- a/pytest_docker_haproxy_fixtures/__init__.py +++ b/pytest_docker_haproxy_fixtures/__init__.py @@ -4,4 +4,4 @@ from .fixtures import * -__version__ = "0.1.3.dev0" +__version__ = "0.2.0.dev0" diff --git a/pytest_docker_haproxy_fixtures/fixtures.py b/pytest_docker_haproxy_fixtures/fixtures.py index 93c56b6..d016d4e 100644 --- a/pytest_docker_haproxy_fixtures/fixtures.py +++ b/pytest_docker_haproxy_fixtures/fixtures.py @@ -70,136 +70,6 @@ class HAProxySecure(NamedTuple): username: str -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 haproxy 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 = HAPROXY_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 haproxy 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], - pdhf_scale_factor: int, - tmp_path_factory: TempPathFactory, -) -> Generator[List[Path], None, None]: - """ - Provides the location of the docker-compose configuration file containing the insecure haproxy service. - """ - yield from _docker_compose_insecure( - docker_compose_files=docker_compose_files, - scale_factor=pdhf_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 haproxy - 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 = HAPROXY_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 haproxy - 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], - pdhf_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 haproxy - service. - """ - yield from _docker_compose_secure( - docker_compose_files=docker_compose_files, - scale_factor=pdhf_scale_factor, - tmp_path_factory=tmp_path_factory, - ) - - def _haproxy_auth_header( *, haproxy_password_list: List[str], @@ -556,14 +426,14 @@ def _haproxy_insecure( @pytest.fixture(scope="session") def haproxy_insecure( - docker_compose_insecure: Path, docker_services: Services, haproxy_haproxycfg_insecure: Path, + pdhf_docker_compose_insecure: Path, tmp_path_factory: TempPathFactory, ) -> Generator[HAProxyInsecure, None, None]: """Provides the endpoint of a local, insecure, haproxy.""" for lst in _haproxy_insecure( - docker_compose_insecure_list=[docker_compose_insecure], + docker_compose_insecure_list=[pdhf_docker_compose_insecure], docker_services=docker_services, haproxy_haproxycfg_insecure_list=[haproxy_haproxycfg_insecure], scale_factor=1, @@ -574,15 +444,15 @@ def haproxy_insecure( @pytest.fixture(scope="session") def haproxy_insecure_list( - docker_compose_insecure_list: List[Path], docker_services: Services, haproxy_haproxycfg_insecure_list: List[Path], + pdhf_docker_compose_insecure_list: List[Path], pdhf_scale_factor: int, tmp_path_factory: TempPathFactory, ) -> Generator[List[HAProxyInsecure], None, None]: """Provides the endpoint of a local, insecure, haproxy.""" yield from _haproxy_insecure( - docker_compose_insecure_list=docker_compose_insecure_list, + docker_compose_insecure_list=pdhf_docker_compose_insecure_list, docker_services=docker_services, haproxy_haproxycfg_insecure_list=haproxy_haproxycfg_insecure_list, scale_factor=pdhf_scale_factor, @@ -698,7 +568,7 @@ def _haproxy_secure( @pytest.fixture(scope="session") def haproxy_secure( - docker_compose_secure: Path, + docker_services: Services, haproxy_auth_header, haproxy_cacerts: Path, haproxy_certs: HAProxyCerts, @@ -706,12 +576,12 @@ def haproxy_secure( haproxy_password: str, haproxy_ssl_context: SSLContext, haproxy_username: str, - docker_services: Services, + pdhf_docker_compose_secure: Path, tmp_path_factory: TempPathFactory, ) -> Generator[HAProxySecure, None, None]: """Provides the endpoint of a local, secure, haproxy.""" for lst in _haproxy_secure( - docker_compose_secure_list=[docker_compose_secure], + docker_compose_secure_list=[pdhf_docker_compose_secure], haproxy_auth_header_list=[haproxy_auth_header], haproxy_cacerts_list=[haproxy_cacerts], haproxy_certs_list=[haproxy_certs], @@ -728,7 +598,7 @@ def haproxy_secure( @pytest.fixture(scope="session") def haproxy_secure_list( - docker_compose_secure_list: List[Path], + docker_services: Services, haproxy_auth_header_list, haproxy_cacerts_list: List[Path], haproxy_certs_list: List[HAProxyCerts], @@ -736,13 +606,13 @@ def haproxy_secure_list( haproxy_password_list: List[str], haproxy_ssl_context_list: List[SSLContext], haproxy_username_list: List[str], - docker_services: Services, + pdhf_docker_compose_secure_list: List[Path], pdhf_scale_factor: int, tmp_path_factory: TempPathFactory, ) -> Generator[List[HAProxySecure], None, None]: """Provides the endpoint of a local, secure, haproxy.""" yield from _haproxy_secure( - docker_compose_secure_list=docker_compose_secure_list, + docker_compose_secure_list=pdhf_docker_compose_secure_list, haproxy_auth_header_list=haproxy_auth_header_list, haproxy_cacerts_list=haproxy_cacerts_list, haproxy_certs_list=haproxy_certs_list, @@ -828,6 +698,136 @@ def haproxy_username_list( return _haproxy_username(scale_factor=pdhf_scale_factor) +def _pdhf_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 haproxy service. + """ + cache_key = _pdhf_docker_compose_insecure.__name__ + result = CACHE.get(cache_key, []) + for i in range(scale_factor): + if i < len(result): + continue + + service_name = HAPROXY_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 pdhf_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 haproxy service. + """ + for lst in _pdhf_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 pdhf_docker_compose_insecure_list( + docker_compose_files: List[str], + pdhf_scale_factor: int, + tmp_path_factory: TempPathFactory, +) -> Generator[List[Path], None, None]: + """ + Provides the location of the docker-compose configuration file containing the insecure haproxy service. + """ + yield from _pdhf_docker_compose_insecure( + docker_compose_files=docker_compose_files, + scale_factor=pdhf_scale_factor, + tmp_path_factory=tmp_path_factory, + ) + + +def _pdhf_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 haproxy + service. + """ + cache_key = _pdhf_docker_compose_secure.__name__ + result = CACHE.get(cache_key, []) + for i in range(scale_factor): + if i < len(result): + continue + + service_name = HAPROXY_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 pdhf_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 haproxy + service. + """ + for lst in _pdhf_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 pdhf_docker_compose_secure_list( + docker_compose_files: List[str], + pdhf_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 haproxy + service. + """ + yield from _pdhf_docker_compose_secure( + docker_compose_files=docker_compose_files, + scale_factor=pdhf_scale_factor, + tmp_path_factory=tmp_path_factory, + ) + + @pytest.fixture(scope="session") def pdhf_scale_factor() -> int: """Provides the number enumerated instances to be instantiated.""" diff --git a/tests/test_fixtures.py b/tests/test_fixtures.py index e59443e..70afd68 100644 --- a/tests/test_fixtures.py +++ b/tests/test_fixtures.py @@ -51,38 +51,6 @@ def no_duplicates(lst: List) -> bool: return len(lst) == len(set(lst)) -def test_docker_compose_insecure(docker_compose_insecure: Path): - """Test that the embedded docker-compose for insecure haproxy can be copied to a temporary file.""" - service_name = HAPROXY_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], pdhf_scale_factor: int -): - """Test that the embedded docker-compose for insecure haproxy can be copied to a temporary file.""" - for i in range(pdhf_scale_factor): - service_name = HAPROXY_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 haproxy can be copied to a temporary file.""" - service_name = HAPROXY_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], pdhf_scale_factor: int -): - """Test that the embedded docker-compose for secure haproxy can be copied to a temporary file.""" - for i in range(pdhf_scale_factor): - service_name = HAPROXY_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_haproxy_auth_header( haproxy_auth_header, haproxy_password: str, @@ -499,3 +467,35 @@ def test_haproxy_username_list( for i in range(pdhf_scale_factor): assert haproxy_username_list[i] assert no_duplicates(haproxy_username_list) + + +def test_pdhf_docker_compose_insecure(pdhf_docker_compose_insecure: Path): + """Test that the embedded docker-compose for insecure haproxy can be copied to a temporary file.""" + service_name = HAPROXY_SERVICE_PATTERN.format("insecure", 0) + assert service_name in pdhf_docker_compose_insecure.read_text() + + +def test_pdhf_docker_compose_insecure_list( + pdhf_docker_compose_insecure_list: List[Path], pdhf_scale_factor: int +): + """Test that the embedded docker-compose for insecure haproxy can be copied to a temporary file.""" + for i in range(pdhf_scale_factor): + service_name = HAPROXY_SERVICE_PATTERN.format("insecure", i) + assert service_name in pdhf_docker_compose_insecure_list[i].read_text() + assert no_duplicates(pdhf_docker_compose_insecure_list) + + +def test_pdhf_docker_compose_secure(pdhf_docker_compose_secure: Path): + """Test that the embedded docker-compose for secure haproxy can be copied to a temporary file.""" + service_name = HAPROXY_SERVICE_PATTERN.format("secure", 0) + assert service_name in pdhf_docker_compose_secure.read_text() + + +def test_pdhf_docker_compose_secure_list( + pdhf_docker_compose_secure_list: List[Path], pdhf_scale_factor: int +): + """Test that the embedded docker-compose for secure haproxy can be copied to a temporary file.""" + for i in range(pdhf_scale_factor): + service_name = HAPROXY_SERVICE_PATTERN.format("secure", i) + assert service_name in pdhf_docker_compose_secure_list[i].read_text() + assert no_duplicates(pdhf_docker_compose_secure_list)