Skip to main content

No project description provided

Project description

pytest-kubernetes

pytest-kubernetes is a lightweight pytest plugin that makes managing (local) Kubernetes clusters a breeze. You can easily spin up a Kubernetes cluster with one pytest fixure and remove them again. The fixture comes with some simple functions to interact with the cluster, for example kubectl(...) that allows you to run typical kubectl commands against this cluster without worring about the kubeconfig on the test machine.

Features:

  • Set up and tear down (local) Kubernetes clusters with minikube, k3d and kind
  • Configure the cluster to recreate for each test case (default), or keep it across multiple test cases
  • Automatic management of the kubeconfig
  • Simple functions to run kubectl commands (with dict output), reading logs and load custom container images
  • Management utils for custom pytest-fixtures (for example pre-provisioned clusters)

Installation

This plugin can be installed from PyPI:

  • pip install pytest-kubernetes
  • poetry add -D pytest-kubernetes

Note that this package provides entrypoint hooks to be automatically loaded with pytest.

Requirements

pytest-kubernetes expects the following components to be available on the test machine:

  • kubectl
  • minikube (optional for minikube-based clusters)
  • k3d (optional for k3d-based clusters)
  • kind (optional for kind-based clusters)
  • Docker (optional for Docker-based Kubernetes clusters)

Please make sure they are installed to run pytest-kubernetes properly.

Reference

Fixture

k8s

The k8s fixture provides access to an automatically selected Kubernetes provider (depending on the availability on the host). The priority is: k3d, kind, minikube-docker and minikube-kvm2.

The fixture passes a manager object of type AClusterManager. It provides the following interface:

  • kubectl(...): Execute kubectl command against this cluster
  • apply(...): Apply resources to this cluster, either from YAML file, or Python dict
  • load_image(...): Load a container image into this cluster
  • logs(...): Get the logs of a pod
  • version(): Get the Kubernetes version of this cluster
  • create(...): Create this cluster
  • delete(): Delete this cluster
  • reset(): Delete this cluster (if it exists) and create it again

The interface provides proper typing and should be easy to work with.

Example

def test_a_feature_with_k3d(k8s: AClusterManager):
    k8s.create()
    k8s.apply(
        {
            "apiVersion": "v1",
            "kind": "ConfigMap",
            "data": {"key": "value"},
            "metadata": {"name": "myconfigmap"},
        },
    )
    k8s.apply("./dependencies.yaml")
    k8s.load_image("my-container-image:latest")
    k8s.kubectl(
        [
            "run",
            "test",
            "--image",
            "my-container-image:latest",
            "--restart=Never",
            "--image-pull-policy=Never",
        ]
    )

This cluster will be deleted once the test case is over.

Please note that you need to set "--image-pull-policy=Never" for images that you loaded into the cluster via the k8s.load(name: str) function (see example above).

Marks

pytest-kubernetes uses pytest marks for specifying the cluster configuration for a test case

Currently the following settings are supported:

  • provider (str): request a specific Kubernetes provider for the test case
  • cluster_name (str): request a specific cluster name
  • keep (bool): keep the cluster across multiple test cases

Example

@pytest.mark.k8s(provider="minikube", cluster_name="test1", keep=True)
def test_a_feature_in_minikube(k8s: AClusterManager):
    ...

Examples

Please find more examples in tests/vendor.py in this repository. These test cases are written as you would write test cases in your project.

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_kubernetes-0.1.0.tar.gz (13.8 kB view details)

Uploaded Source

Built Distribution

pytest_kubernetes-0.1.0-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

Details for the file pytest_kubernetes-0.1.0.tar.gz.

File metadata

  • Download URL: pytest_kubernetes-0.1.0.tar.gz
  • Upload date:
  • Size: 13.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.8.10 Linux/5.15.0-60-generic

File hashes

Hashes for pytest_kubernetes-0.1.0.tar.gz
Algorithm Hash digest
SHA256 cbaea3b0a522c1d78c5464cf1e905efc5323c56923dccf9a6d963fcecfb868a5
MD5 4186467c6cb6808b741d59fb5de642e2
BLAKE2b-256 e2b7f3a12654b14d71122f82d641449e51b0febacb1739bf6455ef51d20ac522

See more details on using hashes here.

File details

Details for the file pytest_kubernetes-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pytest_kubernetes-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.8.10 Linux/5.15.0-60-generic

File hashes

Hashes for pytest_kubernetes-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4e76d50f93971860776f0a8e2af41a5f9bcbae08f24e4bbe9acba010ae778c32
MD5 aa88d5aa744e02bb2436d55eb3c3e844
BLAKE2b-256 b72cd78a8cbbc483dc46bb4ed5f06c8e794b6844d73f7930a1647199b42a1b8b

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