Skip to main content

Pytest fixtures for testing with squid.

Project description

pytest-docker-squid-fixtures

Overview

Pytest fixtures to instantiate and utilize local squid docker containers, using lovely-pytest-docker and docker-py, for testing.

Getting Started

Update setup.py to include:

from distutils.core import setup

setup(
	tests_require=["pytest-docker-squid-fixtures"]
)

All fixtures should be automatically included via the pytest11 entry point.

import requests
import pytest
from pytest_docker_squid_fixtures import SquidInsecure, SquidSecure  # Optional, for typing

def test_squid_secure(squid_secure: SquidSecure):
    response = requests.head(f"https://{squid_secure.endpoint}/",
        headers=squid_secure.auth_header,
        verify=str(squid_secure.cacerts),
    )
    assert response.status_code == 200

def test_squid_insecure(squid_insecure: SquidInsecure):
    response = requests.head(f"http://{squid_insecure.endpoint}/")
    assert response.status_code == 200

The push_image mark can optionally be added to stage images in the squid prior to testing. See Markers for details.

Compatibility

  • Tested with python 3.8

Installation

From pypi.org

$ pip install pytest_docker_squid_fixtures

From source code

$ git clone https://github.com/crashvb/pytest-docker-squid-fixtures
$ cd pytest-docker-squid-fixtures
$ virtualenv env
$ source env/bin/activate
$ python -m pip install --editable .[dev]

Fixtures

squid_auth_header

Retrieves an HTTP basic authentication header that is populated with credentials that can access the secure squid service. The credentials are retrieved from the squid_password and squid_username fixtures. This fixture is used to replicate docker images into the secure squid service.

squid_cacerts

Locates 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 from certifi and the squid_certs fixture. This fixture is used to instantiate the secure squid service.

squid_certs

Returns 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 Fields

The 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 by pytest_docker_squid_fixtures.SquidCerts.

squid_htpasswd

Provides 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 the squid_password and squid_username fixtures. This fixture is used to instantiate the secure squid ervice.

squid_squidcfg_insecure

Provides 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_secure

Provides 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_insecure

Configures and instantiates a squid service without TLS or authentication.

import requests
from pytest_docker_squid_fixtures import SquidInsecure  # Optional, for typing

def test_squid_insecure(squid_insecure: SquidInsecure):
    response = requests.head(f"http://{squid_insecure.endpoint}/")
    assert response.status_code == 200

NamedTuple Fields

The 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 by pytest_docker_squid_fixtures.SquidInsecure.

squid_password

Provides 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_secure

Configures and instantiates a TLS enabled squid service with HTTP basic authorization.

import requests
from pytest_docker_squid_fixtures import SquidSecure  # Optional, for typing

def test_squid_secure(squid_secure: SquidSecure):
    response = requests.head(
        f"https://{squid_secure.endpoint}/",
        headers=squid_secure.auth_header,
        verify=str(squid_secure.cacerts),
    )
    assert response.status_code == 200

NamedTuple Fields

The following fields are defined in the tuple provided by this fixture:

  • auth_header - from squid_auth_header.
  • cacerts - from squid_cacerts.
  • certs - from squid_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 - from squid_htpasswd
  • password - from squid_password.
  • service_name - Name of the service within the docker-compose configuration.
  • ssl_context - from squid_ssl_context.
  • username - from squid_username.

Typing is provided by pytest_docker_squid_fixtures.SquidSecure.

squid_ssl_context

Provides an SSL context containing the CA trust store from the squid_cacerts fixture. This fixture is used to instantiate the secure squid service.

squid_username

Provides 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_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-squid-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 squid service. The configuration will be treated as a template; the $PATH_SQUIDCFG token will be populated with the absolute path provided by the squid_squidcfg 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-squid-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 squid service. The configuration will be treated as a template; the $PATH_CERTIFICATE, $PATH_HTPASSWD, $PATH_KEY, and $PATH_SQUIDCFG tokens will be populated with the absolute paths provided by the squid_certs, squid_htpasswd, and squid_squidcfg fixtures, as appropriate.

Enumerated Fixtures

It is possible to instantiate multiple squid instances using the corresponding enumerated fixtures. All fixtures listed above have _*list (e.g. squid_secure -> squid_secure_list) versions that will return enumerated lists of corresponding data type.

For example:

import requests
from typing import List  # Optional, for typing
from pytest_docker_squid_fixtures import SquidSecure  # Optional, for typing

def test_squid_secure_list(squid_secure_list: List[SquidSecure]):
    for squid_secure in squid_secure_list:
        response = requests.head(
            f"https://{squid_secure.endpoint}/",
            headers=squid_secure.auth_header,
            verify=str(squid_secure.cacerts),
        )
        assert response.status_code == 200

It 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 the pdhf_scale_factor fixture, as follows:

import pytest

@pytest.fixture(scope="session")
def pdhf_scale_factor() -> int:
    return 4

This fixture will be used to scale both the insecure and secure docker registries.

Limitations

  1. All the fixtures provided by this package are session scoped; and will only be executed once per test execution.
  2. At most 10 insecure and 10 secure squid instances are supported using the embedded docker compose.

External Debugging

While 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:$(docker inspect --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.1 200 Connection established

HTTP/2 200
content-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.

Development

Source Control

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_docker_squid_fixtures-0.1.2.tar.gz (18.3 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file pytest_docker_squid_fixtures-0.1.2.tar.gz.

File metadata

  • Download URL: pytest_docker_squid_fixtures-0.1.2.tar.gz
  • Upload date:
  • Size: 18.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.3

File hashes

Hashes for pytest_docker_squid_fixtures-0.1.2.tar.gz
Algorithm Hash digest
SHA256 c3aa644a36ee81ff694498ed284c835c71d805ed4c7597714f47b918904b7122
MD5 b05e2e778f67edc42069e87e6ae188d2
BLAKE2b-256 22eeb438486f41250e15975f3c84273a8ea65b316cb06e4e9514fa4721511cd2

See more details on using hashes here.

File details

Details for the file pytest_docker_squid_fixtures-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: pytest_docker_squid_fixtures-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 22.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.3

File hashes

Hashes for pytest_docker_squid_fixtures-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6f1c2c815ffe853cd268ba906a0f4d61613b9e5ba1459fd438fb107c51e5d408
MD5 f3e618d97ac93960f3e986f1b12c2c6a
BLAKE2b-256 efbda3fa1e225cde99bc168d341c6649ca54f23c11abba4fa6fd8787996a2ae8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page