Skip to main content

Pytest fixtures for testing with haproxy.

Project description

pytest-docker-haproxy-fixtures

pypi version build status coverage status python versions linting code style license

Overview

Pytest fixtures to instantiate and utilize local haproxy 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-haproxy-fixtures"]
)

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

import requests
import pytest
from pytest_docker_haproxy_fixtures import HAProxyInsecure, HAProxySecure  # Optional, for typing

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

def test_haproxy_insecure(haproxy_insecure: HAProxyInsecure):
    response = requests.head(f"http://{haproxy_insecure.endpoint}/")
    assert response.status_code == 200

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

Installation

From pypi.org

$ pip install pytest_docker_haproxy_fixtures

From source code

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

Fixtures

haproxy_auth_header

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

haproxy_cacerts

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

haproxy_certs

Returns 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 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_haproxy_fixtures.HAProxyCerts.

haproxy_haproxycfg_insecure

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

Provides 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 $PASSWORD and $USERNAME tokens will be populated with values provided by the haproxy_password and haproxy_username fixtures, as appropriate.

haproxy_insecure

Configures and instantiates a haproxy service without TLS or authentication.

import requests
from pytest_docker_haproxy_fixtures import HAProxyInsecure  # Optional, for typing

def test_haproxy_insecure(haproxy_insecure: HAProxyInsecure):
    response = requests.head(f"http://{haproxy_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 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 by pytest_docker_haproxy_fixtures.HAProxyInsecure.

haproxy_password

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

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

import requests
from pytest_docker_haproxy_fixtures import HAProxySecure  # Optional, for typing

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

NamedTuple Fields

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

Typing is provided by pytest_docker_haproxy_fixtures.HAProxySecure.

haproxy_ssl_context

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

haproxy_username

Provides 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_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 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 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 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 and haproxy_haproxycfg fixtures, as appropriate.

Enumerated Fixtures

It is possible to instantiate multiple haproxy instances using the corresponding enumerated fixtures. All fixtures listed above have _*list (e.g. haproxy_secure -> haproxy_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_haproxy_fixtures import HAProxySecure  # Optional, for typing

def test_haproxy_secure_list(haproxy_secure_list: List[HAProxySecure]):
    for haproxy_secure in haproxy_secure_list:
        response = requests.head(
            f"https://{haproxy_secure.endpoint}/",
            headers=haproxy_secure.auth_header,
            verify=str(haproxy_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. 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 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 haproxy instances are supported using the embedded docker compose.

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_haproxy_fixtures-1.0.0.tar.gz (21.5 kB view details)

Uploaded Source

Built Distribution

pytest_docker_haproxy_fixtures-1.0.0-py3-none-any.whl (23.3 kB view details)

Uploaded Python 3

File details

Details for the file pytest_docker_haproxy_fixtures-1.0.0.tar.gz.

File metadata

File hashes

Hashes for pytest_docker_haproxy_fixtures-1.0.0.tar.gz
Algorithm Hash digest
SHA256 79be8d49398165c19d37cb303669e075af0bfdc946876836f296833917f4d0b4
MD5 5cea18d1ee8cf1c63bd5a4f5de1f497b
BLAKE2b-256 6e6428220257e95d98f7bb1ac19fcb35e8d18039723af77536f00b284b62bca3

See more details on using hashes here.

File details

Details for the file pytest_docker_haproxy_fixtures-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_docker_haproxy_fixtures-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7bbe12d6a028894a8b053d07e07ddf935f4a2520325dc0d0ef90ef8647513aa1
MD5 89ba7955cab75a199e5e93d2beeebc0d
BLAKE2b-256 80d1bc34f831d002f130eb3154feb4eb786bdffd59f30d8cd17096436fbc1c21

See more details on using hashes here.

Supported by

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