Package: pytest-docker-squid-fixtures
pytest-docker-squid-fixturesOverviewPytest fixtures to instantiate and utilize local squid docker containers, usinglovely-pytest-dockeranddocker-py, for testing.Getting StartedUpdatesetup.pyto include:fromdistutils.coreimportsetupsetup(tests_require=["pytest-docker-squid-fixtures"])All fixtures should be automatically included via thepytest11entry point.importrequestsimportpytestfrompytest_docker_squid_fixturesimportSquidInsecure,SquidSecure# Optional, for typingdeftest_squid_secure(squid_secure:SquidSecure):response=requests.head(f"https://{squid_secure.endpoint}/",headers=squid_secure.auth_header,verify=str(squid_secure.cacerts),)assertresponse.status_code==200deftest_squid_insecure(squid_insecure:SquidInsecure):response=requests.head(f"http://{squid_insecure.endpoint}/")assertresponse.status_code==200Thepush_imagemark can optionally be added to stage images in the squid prior to testing. SeeMarkersfor details.CompatibilityTested with python 3.8InstallationFrompypi.org$ pip install pytest_docker_squid_fixturesFrom source code$gitclonehttps://github.com/crashvb/pytest-docker-squid-fixtures
$cdpytest-docker-squid-fixtures
$virtualenvenv
$sourceenv/bin/activate
$python-mpipinstall--editable.[dev]Fixturessquid_auth_headerRetrieves an HTTP basic authentication header that is populated with credentials that can access the secure squid service. The credentials are retrieved from thesquid_passwordandsquid_usernamefixtures. This fixture is used to replicate docker images into the secure squid service.squid_cacertsLocates a user-defined CA trust store (tests/cacerts) to use to verify connections to the secure squid service. If one cannot be located, a temporary trust store is created containing certificates fromcertifiand thesquid_certsfixture. This fixture is used to instantiate the secure squid service.squid_certsReturns the paths of the self-signed certificate authority certificate, certificate, and private key that are used by the secure squid service. This fixture is used to instantiate the secure squid 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_squid_fixtures.SquidCerts.squid_htpasswdProvides the path to a htpasswd file that is used by the secure squid service. If a user-defined htpasswd file (tests/htpasswd) can be located, it is used. Otherwise, a temporary htpasswd file is created using credentials from thesquid_passwordandsquid_usernamefixtures. This fixture is used to instantiate the secure squid ervice.squid_squidcfg_insecureProvides the path to an insecure squid.cfg file that is used by the insecure squid service. If a user-defined squid.cfg file (tests/squid.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 squid service.squid_squidcfg_secureProvides the path to a secure squid.cfg file that is used by the secure squid service. If a user-defined squid.cfg file (tests/squid.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 squid service.squid_insecureConfigures and instantiates a squid service without TLS or authentication.importrequestsfrompytest_docker_squid_fixturesimportSquidInsecure# Optional, for typingdeftest_squid_insecure(squid_insecure:SquidInsecure):response=requests.head(f"http://{squid_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 squid service.endpoint_name- Endpoint of the insecure squid service, by service name.service_name- Name of the service within the docker-compose configuration.Typing is provided bypytest_docker_squid_fixtures.SquidInsecure.squid_passwordProvides a generated password to use for authentication to the secure squid service. This fixture is used to replicate docker images into the secure squid service.squid_secureConfigures and instantiates a TLS enabled squid service with HTTP basic authorization.importrequestsfrompytest_docker_squid_fixturesimportSquidSecure# Optional, for typingdeftest_squid_secure(squid_secure:SquidSecure):response=requests.head(f"https://{squid_secure.endpoint}/",headers=squid_secure.auth_header,verify=str(squid_secure.cacerts),)assertresponse.status_code==200NamedTuple FieldsThe following fields are defined in the tuple provided by this fixture:auth_header- fromsquid_auth_header.cacerts- fromsquid_cacerts.certs- fromsquid_certs.docker_compose- Path to the fully instantiated docker-compose configuration.endpoint- Endpoint of the secure squid service.endpoint_name- Endpoint of the secure squid service, by service name.htpasswd- fromsquid_htpasswdpassword- fromsquid_password.service_name- Name of the service within the docker-compose configuration.ssl_context- fromsquid_ssl_context.username- fromsquid_username.Typing is provided bypytest_docker_squid_fixtures.SquidSecure.squid_ssl_contextProvides an SSL context containing the CA trust store from thesquid_cacertsfixture. This fixture is used to instantiate the secure squid service.squid_usernameProvides a generated username to use for authentication to the secure squid service. This fixture is used to replicate docker images into the secure squid 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-squid-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 squid service. The configuration will be treated as a template; the$PATH_SQUIDCFGtoken will be populated with the absolute path provided by thesquid_squidcfgfixture.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-squid-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 squid service. The configuration will be treated as a template; the$PATH_CERTIFICATE,$PATH_HTPASSWD,$PATH_KEY, and$PATH_SQUIDCFGtokens will be populated with the absolute paths provided by thesquid_certs,squid_htpasswd, andsquid_squidcfgfixtures, as appropriate.Enumerated FixturesIt is possible to instantiate multiple squid instances using the corresponding enumerated fixtures. Allfixtureslisted above have _*list(e.g.squid_secure->squid_secure_list) versions that will return enumerated lists of corresponding data type.For example:importrequestsfromtypingimportList# Optional, for typingfrompytest_docker_squid_fixturesimportSquidSecure# Optional, for typingdeftest_squid_secure_list(squid_secure_list:List[SquidSecure]):forsquid_secureinsquid_secure_list:response=requests.head(f"https://{squid_secure.endpoint}/",headers=squid_secure.auth_header,verify=str(squid_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. squid_secure == squid_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 squid instances are supported using the embedded docker compose.External DebuggingWhile all the metadata needed to interact with the proxy is available for consumption via fixtures, sometimes it is desirable to interact with the instantiated service outside of the test context.If pytest is executed with--keepalive, it is possible to connect to the proxy using external tooling both during and after testing has completed:$https_proxy=https://127.0.0.1:$(dockerinspect--format='{{ (index (index .NetworkSettings.Ports "3129/tcp") 0).HostPort }}'pytest-squid-secure-0)\curl--head--proxy-insecure--proxy-user"pytest.username...:pytest.password..."https://www.google.com/
HTTP/1.1200Connectionestablished

HTTP/2200content-type:text/html;charset=ISO-8859-1
...You can also retrieve additional, transient, configuration files, such as the CA certificate or proxy configuration, from/tmp/pytest-of-${USER}/pytest-current/...or by inspecting the running container.DevelopmentSource Control