Skip to main content

Fixtures for Operators

Project description

pytest-operator

PyTest plugin to make it easy to create integration tests for Operator Charms.

Usage

Include pytest-operator in the deps of your tox.ini file:

[testenv]
deps =
  pytest
  pytest-operator

Then, just start using the ops_test fixture in your async tests. This module-scoped fixture provides a libjuju Model, helpers to build charms for testing, and the ability to abort testing so that the remaining tests in the module are automatically xfailed (you can also mark a test so that this happens automatically if the test fails; this is typically used on the initial deployment test, where subsequent tests depend on the deployment having succeeded).

As an additional nicety, you don't have to explicitly mark an async test with @pytest.mark.asyncio; if it's a test function / method and it's async, it will be marked automatically.

Example:

import pytest


@pytest.mark.abort_on_fail
async def test_build_and_deploy(ops_test):
    my_charm = await ops_test.build_charm(".")
    await ops_test.model.deploy(my_charm)
    await ops_test.model.wait_for_idle()


async def test_status(ops_test):
    assert ops_test.model.applications["my-charm"].units[0].workload_status == "active"

Building/Downloading Charm Resources

Quite often, when charms are preparing for integration tests, the charms may need to attach resources to the charm for it to function. In these cases, the integration code must either build the resources or pull those from external resources.

Example:

async def test_build_and_deploy(ops_test):
    charm = await ops_test.build_charm(".")

    build_script = Path.cwd() / "build-charm-resources.sh"
    resources = await ops_test.build_resources(build_script)

    if resources:
        # created a dict from list of a filenames
        resources = {rsc.stem: rsc for rsc in resources}
    else:
        arch_resources = ops_test.arch_specific_resources(charm)
        resources = await ops_test.download_resources(
            charm, resources=arch_resources
        )
        
    assert resources, "Failed to build or download charm resources."
    
    log.info("Build Bundle...")
    bundle = ops_test.render_bundle(
        "tests/data/bundle.yaml", charm=charm, **resources
    )

    log.info("Deploy Bundle...")
    juju_cmd = ["deploy", "-m", ops_test.model_full_name, str(bundle)]
    rc, stdout, stderr = await ops_test.juju(*juju_cmd)
    assert rc == 0, f"Bundle deploy failed: {(stderr or stdout).strip()}"

    await ops_test.model.wait_for_idle()
    ...

Reference

More details can be found in the reference docs.

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-operator-0.17.0.tar.gz (20.7 kB view details)

Uploaded Source

Built Distribution

pytest_operator-0.17.0-py3-none-any.whl (20.6 kB view details)

Uploaded Python 3

File details

Details for the file pytest-operator-0.17.0.tar.gz.

File metadata

  • Download URL: pytest-operator-0.17.0.tar.gz
  • Upload date:
  • Size: 20.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.7

File hashes

Hashes for pytest-operator-0.17.0.tar.gz
Algorithm Hash digest
SHA256 d36fbeef38c644b696e417c23660a2d7abed43ba653465971b066473fd82dfa2
MD5 06adbc18ca62f202f09c1cc72cab255b
BLAKE2b-256 76b411e9c83aa64505b2df4dd6d68acbc0ec2c2736eb3a6321444de620eb96b3

See more details on using hashes here.

File details

Details for the file pytest_operator-0.17.0-py3-none-any.whl.

File metadata

  • Download URL: pytest_operator-0.17.0-py3-none-any.whl
  • Upload date:
  • Size: 20.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.7

File hashes

Hashes for pytest_operator-0.17.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a9d957b1e36f43bf92fd3ed3a4ff8bd67fab0232d5acbcbb1eec215db47d9884
MD5 1b4b869fe05dc10cc2faddddc70f7a12
BLAKE2b-256 849807c17e9b9d44cf84b14654dd99e96cbcc963fe7e3dc565515a6c380348c7

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