Package: pytest-docker-haproxy-fixtures
pytest-docker-haproxy-fixturesOverviewPytest fixtures to instantiate and utilize local haproxy docker containers, usinglovely-pytest-dockeranddocker-py, for testing.Getting StartedUpdatesetup.pyto include:fromdistutils.coreimportsetupsetup(tests_require=["pytest-docker-haproxy-fixtures"])All fixtures should be automatically included via thepytest11entry point.importrequestsimportpytestfrompytest_docker_haproxy_fixturesimportHAProxyInsecure,HAProxySecure# Optional, for typingdeftest_haproxy_secure(haproxy_secure:HAProxySecure):response=requests.head(f"https://{haproxy_secure.endpoint}/",headers=haproxy_secure.auth_header,verify=str(haproxy_secure.cacerts),)assertresponse.status_code==200deftest_haproxy_insecure(haproxy_insecure:HAProxyInsecure):response=requests.head(f"http://{haproxy_insecure.endpoint}/")assertresponse.status_code==200Thepush_imagemark can optionally be added to stage images in the haproxy prior to testing. SeeMarkersfor details.CompatibilityTested with python 3.8InstallationFrompypi.org$ pip install pytest_docker_haproxy_fixturesFrom source code$gitclonehttps://github.com/crashvb/pytest-docker-haproxy-fixtures
$cdpytest-docker-haproxy-fixtures
$virtualenvenv
$sourceenv/bin/activate
$python-mpipinstall--editable.[dev]Fixtureshaproxy_auth_headerRetrieves an HTTP basic authentication header that is populated with credentials that can access the secure haproxy service. The credentials are retrieved from thehaproxy_passwordandhaproxy_usernamefixtures. This fixture is used to replicate docker images into the secure haproxy service.haproxy_cacertsLocates a user-defined CA trust store (tests/cacerts) to use to verify connections to the secure haproxy service. If one cannot be located, a temporary trust store is created containing certificates fromcertifiand thehaproxy_certsfixture. This fixture is used to instantiate the secure haproxy service.haproxy_certsReturns the paths of the self-signed certificate authority certificate, certificate, and private key that are used by the secure haproxy service. This fixture is used to instantiate the secure haproxy service.NamedTuple FieldsThe following fields are defined in the tuple provided by this fixture:ca_certificate- Path to the self-signed certificate authority certificate.ca_private_key- Path to the self-signed certificate authority private key.certificate- Path to the certificate.private_key- Path to the private key.Typing is provided bypytest_docker_haproxy_fixtures.HAProxyCerts.haproxy_haproxycfg_insecureProvides the path to an insecure haproxy.cfg file that is used by the insecure haproxy service. If a user-defined haproxy.cfg file (tests/haproxy.insecure.cfg) can be located, it is used. Otherwise, an embedded configuration is copied to a temporary location and returned. This fixture is used to instantiate the insecure haproxy service.haproxy_haproxycfg_secureProvides the path to a secure haproxy.cfg file that is used by the secure haproxy service. If a user-defined haproxy.cfg file (tests/haproxy.secure.cfg) can be located, it is used. Otherwise, an embedded configuration is copied to a temporary location and returned. This fixture is used to instantiate the secure haproxy service. The configuration will be treated as a template; the$PASSWORDand$USERNAMEtokens will be populated with values provided by thehaproxy_passwordandhaproxy_usernamefixtures, as appropriate.haproxy_insecureConfigures and instantiates a haproxy service without TLS or authentication.importrequestsfrompytest_docker_haproxy_fixturesimportHAProxyInsecure# Optional, for typingdeftest_haproxy_insecure(haproxy_insecure:HAProxyInsecure):response=requests.head(f"http://{haproxy_insecure.endpoint}/")assertresponse.status_code==200NamedTuple FieldsThe following fields are defined in the tuple provided by this fixture:docker_compose- Path to the fully instantiated docker-compose configuration.endpoint- Endpoint of the insecure haproxy service.endpoint_name- Endpoint of the insecure haproxy service, by server name.service_name- Name of the service within the docker-compose configuration.Typing is provided bypytest_docker_haproxy_fixtures.HAProxyInsecure.haproxy_passwordProvides a generated password to use for authentication to the secure haproxy service. This fixture is used to replicate docker images into the secure haproxy service.haproxy_secureConfigures and instantiates a TLS enabled haproxy service with HTTP basic authorization.importrequestsfrompytest_docker_haproxy_fixturesimportHAProxySecure# Optional, for typingdeftest_haproxy_secure(haproxy_secure:HAProxySecure):response=requests.head(f"https://{haproxy_secure.endpoint}/",headers=haproxy_secure.auth_header,verify=str(haproxy_secure.cacerts),)assertresponse.status_code==200NamedTuple FieldsThe following fields are defined in the tuple provided by this fixture:auth_header- fromhaproxy_auth_header.cacerts- fromhaproxy_cacerts.certs- fromhaproxy_certs.docker_compose- Path to the fully instantiated docker-compose configuration.endpoint- Endpoint of the secure haproxy service.endpoint_name- Endpoint of the secure haproxy service, by server name.password- fromhaproxy_password.service_name- Name of the service within the docker-compose configuration.ssl_context- fromhaproxy_ssl_context.username- fromhaproxy_username.Typing is provided bypytest_docker_haproxy_fixtures.HAProxySecure.haproxy_ssl_contextProvides an SSL context containing the CA trust store from thehaproxy_cacertsfixture. This fixture is used to instantiate the secure haproxy service.haproxy_usernameProvides a generated username to use for authentication to the secure haproxy service. This fixture is used to replicate docker images into the secure haproxy service.pdhf_docker_compose_insecureThis fixture uses thedocker_compose_filesfixture to locate a user-defined docker-compose configuration file (typicallytests/docker-compose.yml) that contains thepytest-docker-haproxy-insecureservice. If one cannot be located, an embedded configuration is copied to a temporary location and returned. This fixture is used to instantiate the insecure haproxy service. The configuration will be treated as a template; the$PATH_HAPROXYCFGtoken will be populated with the absolute path provided by thehaproxy_haproxycfgfixture.pdhf_docker_compose_secureThis fixture uses thedocker_compose_filesfixture to locate a user-defined docker-compose configuration file (typicallytests/docker-compose.yml) that contains thepytest-docker-haproxy-secureservice. If one cannot be located, an embedded configuration is copied to a temporary location and returned. This fixture is used to instantiate the secure haproxy service. The configuration will be treated as a template; the$PATH_CERTIFICATE,$PATH_HAPROXYCFG, and$PATH_KEYtokens will be populated with the absolute paths provided by thehaproxy_certsandhaproxy_haproxycfgfixtures, as appropriate.Enumerated FixturesIt is possible to instantiate multiple haproxy instances using the corresponding enumerated fixtures. Allfixtureslisted above have _*list(e.g.haproxy_secure->haproxy_secure_list) versions that will return enumerated lists of corresponding data type.For example:importrequestsfromtypingimportList# Optional, for typingfrompytest_docker_haproxy_fixturesimportHAProxySecure# Optional, for typingdeftest_haproxy_secure_list(haproxy_secure_list:List[HAProxySecure]):forhaproxy_secureinhaproxy_secure_list:response=requests.head(f"https://{haproxy_secure.endpoint}/",headers=haproxy_secure.auth_header,verify=str(haproxy_secure.cacerts),)assertresponse.status_code==200It is possible to use both singular and enumerated fixtures within the same test context; however, the same values will be returned for the singular fixture as the first enumerated list value (i.e. haproxy_secure == haproxy_secure_list[0]). To avoid complications with lower layers, mainly docker-compose, and to allow for this interchangeability, caching is used internally.By default, the scale factor of the enumerated instances is set to one (n=1). This value can be changed by overriding thepdhf_scale_factorfixture, as follows:importpytest@pytest.fixture(scope="session")defpdhf_scale_factor()->int:return4This fixture will be used to scale both the insecure and secure docker registries.LimitationsAll the fixtures provided by this package aresessionscoped; and will only be executed once per test execution.At most 10 insecure and 10 secure haproxy instances are supported using the embedded docker compose.DevelopmentSource Control