Package: pytest-docker-git-fixtures
pytest-docker-git-fixturesOverviewPytest fixtures to instantiate and populated local GIT SCMs, usinglovely-pytest-docker, for testing.Getting StartedUpdatesetup.pyto include:fromdistutils.coreimportsetupsetup(tests_require=["pytest-docker-git-fixtures"])All fixtures should be automatically included via thepytest11entry point.importpytestimportsubprocessfrompathlibimportPathfrompytest_docker_git_fixturesimportGITInsecure,GITSecure# Optional, for typing@pytest.mark.create_repo("test_git_secure")@pytest.mark.mirror_repo("https://github.com/crashvb/shim-bind.git")deftest_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,)assertpath.joinpath("README.md").exists()@pytest.mark.create_repo("test_git_insecure")deftest_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,)assertpath.exists()Thecreate_repoandmirror_repomarks can optionally be added to stage repositories in the GIT prior to testing. SeeMarkersfor details.CompatibilityTested with python 3.8InstallationFrompypi.org$ pip install pytest_git_fixturesFrom source code$gitclonehttps://github.com/crashvb/pytest-docker-git-fixtures
$cdpytest-docker-git-fixtures
$virtualenvenv
$sourceenv/bin/activate
$python-mpipinstall--editable.[dev]Fixturesgit_auth_headerRetrieves an HTTP basic authentication header that is populated with credentials that can access the secure GIT service. The credentials are retrieved from thegit_passwordandgit_usernamefixtures.git_cacertsLocates 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 fromcertifiand thegit_certsfixture. This fixture is used to instantiate the secure GIT service.git_certsReturns 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 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_git_fixtures.GITCerts.git_htpasswdProvides 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 thegit_passwordandgit_usernamefixtures. This fixture is used to instantiate the secure GIT service.git_insecureConfigures and instantiates a GIT without TLS or authentication.importpytestimportsubprocessfrompathlibimportPathfrompytest_docker_git_fixturesimportGITInsecure# Optional, for typing@pytest.mark.create_repo("test_git_insecure")deftest_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,)assertpath.exists()NamedTuple FieldsThe 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 bypytest_git_fixtures.GITInsecure.git_passwordProvides a generated password to use for authentication to the secure GIT service.git_secureConfigures and instantiates a TLS enabled GIT with HTTP basic authorization.importpytestimportsubprocessfrompathlibimportPathfrompytest_docker_git_fixturesimportGITSecure# Optional, for typing@pytest.mark.mirror_repo("https://github.com/crashvb/shim-bind.git")deftest_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,)assertpath.joinpath("README.md").exists()NamedTuple FieldsThe following fields are defined in the tuple provided by this fixture:auth_header- fromgit_auth_header.cacerts- fromgit_cacerts.certs- fromgit_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- fromgit_htpasswdmirrored_repos- The list of mirrored repositories.password- fromgit_password.service_name- Name of the service within the docker-compose configuration.ssl_context- fromgit_ssl_context.username- fromgit_username.Typing is provided bypytest_git_fixtures.GITSecure.git_ssl_contextProvides an SSL context containing the CA trust store from thegit_cacertsfixture.git_usernameProvides a generated username to use for authentication to the secure GIT service.pdgf_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-git-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 GIT service.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-git-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 GIT service; however, unlike the configuration returned by thepdgf_docker_compose_insecurefixture, this configuration will be treated as a template; the$PATH_CERTIFICATE,$PATH_HTPASSWD, and$PATH_KEYtokens will be populated with the absolute paths provided by thegit_certsandgit_htpasswdfixtures, as appropriate.Markerspytest.mark.create_repoThis marker specifies the GIT repository(ies) that should be initialized within the GIT service(s) prior to testing. It can ...... decorate individual tests:importpytestfrompytest_docker_git_fixturesimportGITSecure# Optional, for typing@pytest.mark.create_repo("test_git_secure")deftest_git_secure(git_secure:GITSecure):...... be specified in thepytestmarklist at the module level:#!/usr/bin/env pythonimportpytestpytestmark=[pytest.mark.create_repo("test_generic_repo")]...... or be provided via the corresponding--create-repocommand-line argument:python-mpytest--create-reporepo0--create-reporepo1--create-reporepo2,repo3...This marker supports being specified multiple times, and removes duplicate repository names (seeLimitationsbelow).A helper function,get_created_repos, is included for test scenarios that  wish to inspect the maker directly:importpytestfrompytest_docker_git_fixturesimportGITSecure,get_created_repos@pytest.mark.create_repo("test_git_secure")deftest_git_secure(git_secure:GITSecure,request):name=get_created_repos(request)[0]pytest.mark.mirror_repoSimilarly 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 aget_mirrored_reposhelper function.Enumerated FixturesIt is possible to instantiate multiple GIT instances using the corresponding enumerated fixtures. Allfixtureslisted above have _*list(e.g.git_secure->git_secure_list) versions that will return enumerated lists of corresponding data type.For example:importrequestsfromtypingimportList# Optional, for typingfrompytest_docker_git_fixturesimportGITSecure# Optional, for typingdeftest_git_secure_list(git_secure_list:List[GITSecure]):forgit_secureingit_secure_list:# Default listener ...response=requests.get(f"https://{git_secure.endpoint}/",headers=git_secure.auth_header,verify=str(git_secure.cacerts),)assertresponse.status_code==200assertresponse.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 thepdgf_scale_factorfixture, as follows:importpytest@pytest.fixture(scope="session")defpdgf_scale_factor()->int:return4This fixture will be used to scale both the insecure and secure GIT SCMs.LimitationsAll the fixtures provided by this package aresessionscoped; and will only be executed once per test execution.Thecreate_repo, andmirror_repomarkers are processed as part of thegit_insecureandgit_securefixtures. As such:allmarkers 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.At most 10 insecure and 10 secure GIT SCMs are supported using the embedded docker compose.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.DevelopmentSource Control