Skip to main content

Parse and validate kustomize output

Project description

pytest_kustomize

Parse and validate kustomize output

Example

from pytest_kustomize import resolve_configmaps, extract_externalsecret_data
import pytest


@pytest.mark.parametrize("environment, value", [
    ("staging", "shared-db.staging.example.com"),
    ("production", "myservice-db.production.example.com"),
])
def test_database_matches_environment(kustomize_resources, environment, value):
    config = resolve_configmaps(kustomize_resources[environment])
    for deployment in ["webui", "api"]:
        assert config[deployment]["db_host"] == value


def test_production_has_no_staging_vault_paths(kustomize_resources):
    for secret in extract_externalsecret_data(kustomize_resources["production"]).values():
        assert "staging" not in secret["key"]


@pytest.mark.parametrize("environment", ["staging", "production"])
def test_jq(kustomize_jq, environment):
    for apiVersion in kustomize_jq(
        """.[] | select(."kind"=="ConfigMap").apiVersion""", environment
    ):
        assert apiVersion == "v1"

Usage

You can access the kustomize output as follows:

  • The kustomize_manifests fixture returns a dict[str, str], environment name: kustomize build output
  • The kustomize_resources fixture returns a dict[str, List[dict]], environment name: yaml-parsed output
  • The kustomize_jq fixture returns a query helper which applies a jq query string to the given environment and returns an instance of jq._ProgramWithInput. Only available by explicitly installing jq (or pytest_kustomize[jq])

Optionally, define the directory where kustomize manifests are looked up by providing this fixture

@pytest.fixture(scope="session")
def kustomize_root_directory():
    return "/path/to/kustomizations"

Default: ./k8s

Optionally, define for which subdirectories manifests should be generated by providing this fixture

@pytest.fixture(scope="session")
def kustomize_environment_names():
    return ["overlays/staging", "overlays/production"]

Default: All direct subdirectories that contain a kustomization.yaml

API

The package provides these helper functions:

  • resolve_configmaps() resolves envFrom references to ConfigMaps in Deployments. It takes a list of yaml dicts (usually kustomize_resources[environment]) and returns a dict[str, dict[str, str]], deployment+container name: {key: value}. The dict keys work as follows:
    • Containers are named {deployment}-{container}
    • Init Containers are named {deployment}-init-{container}
    • For convenience, the first container is also available just under the name {deployment}, and the first init container as {deployment}-init
  • extract_externalsecret_data() extracts the data sections of all ExternalSecret resources. It takes a list of yaml dicts and returns {"key_in_secret": {"key": "example/vault/path", "property": "token"}}

These take an optional name_transform callable which defaults to lambda x: x.rsplit('-', 1)[0], to normalize e.g. kustomize-generated ConfigMap names like project-44fb7dkk64.

pytest_kustomize Changes

1.3.0 (2025-12-08)

  • Make jq an optional dependency (jq)

1.2.0 (2025-10-02)

  • Allow querying manifests via jq

1.1.0 (2025-07-09)

  • Parse all containers and initContainers in resolve_configmaps (initcontainer)
  • Normalize names by splitting on the last dash, not the first (nametrans)

1.0.0 (2025-07-03)

  • Initial release (initial)

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_kustomize-1.3.0.tar.gz (57.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_kustomize-1.3.0-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file pytest_kustomize-1.3.0.tar.gz.

File metadata

  • Download URL: pytest_kustomize-1.3.0.tar.gz
  • Upload date:
  • Size: 57.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.15 {"installer":{"name":"uv","version":"0.9.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for pytest_kustomize-1.3.0.tar.gz
Algorithm Hash digest
SHA256 7aad2ff6957cb1da881c49b642ec48643d8609d2a8b3cf35a7948189bdbf35e7
MD5 5fe6e1fe90d3eeb7ec1b341c85589f3a
BLAKE2b-256 d44d67081e5c2ed0b08a86c17e35b4ec29b47f605b2153f566cf4977ea426b4e

See more details on using hashes here.

File details

Details for the file pytest_kustomize-1.3.0-py3-none-any.whl.

File metadata

  • Download URL: pytest_kustomize-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.15 {"installer":{"name":"uv","version":"0.9.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for pytest_kustomize-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8db7852e0c3a4233c98caad02cbadd03294f01614123bca4a113e14249d5dd0e
MD5 3100e5de8b3ddd06bc901315394543a7
BLAKE2b-256 57054ca8e0bd551f90484034e7692d6335fd6c26170b95d95cd5a337e66f258d

See more details on using hashes here.

Supported by

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