Skip to main content

Pytest fixtures for testing with git scm.

Project description

pytest-docker-git-fixtures

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

Overview

Pytest fixtures to instantiate and populated local GIT SCMs, using lovely-pytest-docker, for testing.

Getting Started

Update setup.py to include:

from distutils.core import setup

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

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

import pytest
import subprocess
from pathlib import Path
from pytest_docker_git_fixtures import GITInsecure, GITSecure  # Optional, for typing

@pytest.mark.create_repo("test_git_secure")
@pytest.mark.mirror_repo("https://github.com/crashvb/shim-bind.git")
def test_git_secure(git_secure: GITSecure, tmp_path: Path):
    uri = f"https://{git_secure.endpoint}/secure/shim-bind.git"
    path = tmp_path.joinpath("local-clone")
    subprocess.run(
        ["git", "clone", uri, str(path)],
        check=True,
        cwd=str(tmp_path),
        stderr=subprocess.STDOUT,
    )
    assert path.joinpath("README.md").exists()

@pytest.mark.create_repo("test_git_insecure")
def test_git_insecure(git_insecure: GITInsecure, tmp_path: Path):
    uri = f"https://{git_insecure.endpoint}/insecure/test_git_insecure.git"
    path = tmp_path.joinpath("local-clone")
    subprocess.run(
        ["git", "clone", uri, str(path)],
        check=True,
        cwd=str(tmp_path),
        stderr=subprocess.STDOUT,
    )
    assert path.exists()

The create_repo and mirror_repo marks can optionally be added to stage repositories in the GIT prior to testing. See Markers for details.

Installation

From pypi.org

$ pip install pytest_git_fixtures

From source code

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

Fixtures

git_auth_header

Retrieves an HTTP basic authentication header that is populated with credentials that can access the secure GIT service. The credentials are retrieved from the git_password and git_username fixtures.

git_cacerts

Locates a user-defined CA trust store (tests/cacerts) to use to verify connections to the secure GIT service. If one cannot be located, a temporary trust store is created containing certificates from certifi and the git_certs fixture. This fixture is used to instantiate the secure GIT service.

git_certs

Returns the paths of the self-signed certificate authority certificate, certificate, and private key that are used by the secure GIT service. This fixture is used to instantiate the secure GIT 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_git_fixtures.GITCerts.

git_htpasswd

Provides the path to a htpasswd file that is used by the secure GIT 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 git_password and git_username fixtures. This fixture is used to instantiate the secure GIT service.

git_insecure

Configures and instantiates a GIT without TLS or authentication.

import pytest
import subprocess
from pathlib import Path
from pytest_docker_git_fixtures import GITInsecure  # Optional, for typing

@pytest.mark.create_repo("test_git_insecure")
def test_git_insecure(git_insecure: GITInsecure, tmp_path: Path):
    uri = f"https://{git_insecure.endpoint}/insecure/test_git_insecure.git"
    path = tmp_path.joinpath("local-clone")
    subprocess.run(
        ["git", "clone", uri, str(path)],
        check=True,
        cwd=str(tmp_path),
        stderr=subprocess.STDOUT,
    )
    assert path.exists()

NamedTuple Fields

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

  • created_repos - The list of created repositories.
  • docker_compose - Path to the fully instantiated docker-compose configuration.
  • endpoint - Endpoint of the insecure GIT service.
  • endpoint_name - Endpoint of the insecure GIT service, by service name.
  • mirrored_repos - The list of mirrored repositories.
  • service_name - Name of the service within the docker-compose configuration.

Typing is provided by pytest_git_fixtures.GITInsecure.

git_password

Provides a generated password to use for authentication to the secure GIT service.

git_secure

Configures and instantiates a TLS enabled GIT with HTTP basic authorization.

import pytest
import subprocess
from pathlib import Path
from pytest_docker_git_fixtures import GITSecure  # Optional, for typing

@pytest.mark.mirror_repo("https://github.com/crashvb/shim-bind.git")
def test_git_secure(git_secure: GITSecure, tmp_path: Path):
    uri = f"https://{git_secure.endpoint}/secure/shim-bind.git"
    path = tmp_path.joinpath("local-clone")
    subprocess.run(
        ["git", "clone", uri, str(path)],
        check=True,
        cwd=str(tmp_path),
        stderr=subprocess.STDOUT,
    )
    assert path.joinpath("README.md").exists()

NamedTuple Fields

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

  • auth_header - from git_auth_header.
  • cacerts - from git_cacerts.
  • certs - from git_certs.
  • created_repos - The list of created repositories.
  • docker_compose - Path to the fully instantiated docker-compose configuration.
  • endpoint - Endpoint of the secure GIT service.
  • endpoint_name - Endpoint of the secure GIT service, by service name.
  • htpasswd - from git_htpasswd
  • mirrored_repos - The list of mirrored repositories.
  • password - from git_password.
  • service_name - Name of the service within the docker-compose configuration.
  • ssl_context - from git_ssl_context.
  • username - from git_username.

Typing is provided by pytest_git_fixtures.GITSecure.

git_ssl_context

Provides an SSL context containing the CA trust store from the git_cacerts fixture.

git_username

Provides a generated username to use for authentication to the secure GIT service.

pdgf_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-git-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 GIT service.

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-git-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 GIT service; however, unlike the configuration returned by the pdgf_docker_compose_insecure fixture, this configuration will be treated as a template; the $PATH_CERTIFICATE, $PATH_HTPASSWD, and $PATH_KEY tokens will be populated with the absolute paths provided by the git_certs and git_htpasswd fixtures, as appropriate.

Markers

pytest.mark.create_repo

This marker specifies the GIT repository(ies) that should be initialized within the GIT service(s) prior to testing. It can ...

... decorate individual tests:

import pytest
from pytest_docker_git_fixtures import GITSecure  # Optional, for typing

@pytest.mark.create_repo("test_git_secure")
def test_git_secure(git_secure: GITSecure):
	...

... be specified in the pytestmark list at the module level:

#!/usr/bin/env python

import pytest

pytestmark = [pytest.mark.create_repo("test_generic_repo")]

...

... or be provided via the corresponding --create-repo command-line argument:

python -m pytest --create-repo repo0 --create-repo repo1 --create-repo repo2,repo3 ...

This marker supports being specified multiple times, and removes duplicate repository names (see Limitations below).

A helper function, get_created_repos, is included for test scenarios that wish to inspect the maker directly:

import pytest
from pytest_docker_git_fixtures import GITSecure, get_created_repos

@pytest.mark.create_repo("test_git_secure")
def test_git_secure(git_secure: GITSecure, request):
    name = get_created_repos(request)[0]

pytest.mark.mirror_repo

Similarly to create_repo, this marker specifies the GIT repository(ies) that should be replicated to the GIT service(s) prior to testing.

Likewise, there is a get_mirrored_repos helper function.

Enumerated Fixtures

It is possible to instantiate multiple GIT instances using the corresponding enumerated fixtures. All fixtures listed above have _*list (e.g. git_secure -> git_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_git_fixtures import GITSecure  # Optional, for typing

def test_git_secure_list(git_secure_list: List[GITSecure]):
    for git_secure in git_secure_list:
        # Default listener ...
        response = requests.get(
            f"https://{git_secure.endpoint}/",
            headers=git_secure.auth_header,
            verify=str(git_secure.cacerts),
        )
        assert response.status_code == 200
        assert response.content == b"pytest-docker-git-fixtures-docker\n"

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. git_secure == git_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 pdgf_scale_factor fixture, as follows:

import pytest

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

This fixture will be used to scale both the insecure and secure GIT SCMs.

Limitations

  1. All the fixtures provided by this package are session scoped; and will only be executed once per test execution.
  2. The create_repo, and mirror_repo markers are processed as part of the git_insecure and git_secure fixtures. As such:
  • all markers will be aggregated during initialization of the session, and processed prior test execution.
  • Initialized and mirror repositories will be applied to both the insecure and secure GIT SCMs, if both are instantiated.
  1. At most 10 insecure and 10 secure GIT SCMs are supported using the embedded docker compose.
  2. It is not currently possible to specify into which enumerated SCM instances repositories should be applied. As such, and for backwards compatibility, they will only be applied into the first instance of each of the insecure and secure GIT SCMs.

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_git_fixtures-1.0.0.tar.gz (24.4 kB view hashes)

Uploaded Source

Built Distribution

pytest_docker_git_fixtures-1.0.0-py3-none-any.whl (24.8 kB view hashes)

Uploaded Python 3

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