Skip to main content

Python library providing a state-transition testing API for Operator Framework charms.

Project description

ops-scenario, the unit testing framework for ops charms

ops-scenario is a Python library that provides state-transition testing for Ops charms. These tests are higher level than typical unit tests, but run at similar speeds and are the recommended approach for testing charms within requiring a full Juju installation.

Test are written in the arrange/act/assert pattern, arranging an object representing the current Juju state, acting by emulating an event from Juju, and then asserting on the (simulated) output Juju state.

Writing tests

Here's a test that verifies that a unit is active after the start event, with a very minimal initial state:

from ops import testing

# 'src/charm.py' typically contains the charm class.
from charm import MyCharm

def test_start():
    ctx = testing.Context(MyCharm)
    state_in = testing.State()
    state_out = ctx.run(ctx.on.start(), state_in)
    assert state_out.unit_status == testing.ActiveStatus()

More comprehensive tests will include relations, containers, secrets, and other components in the input state, and assertions against both the output state and the context. The 'act' stage remains a simple single call, although additional arguments may be required for the event, such as the relation or container that triggered it. For example:

import pytest
from ops import testing

from charm import MyCharm

@pytest.mark.parametrize(
    'leader',
    [pytest.param(True, id='leader'), pytest.param(False, id='non-leader')],
)
def test_(leader: bool):
    # Arrange:
    ctx = testing.Context(MyCharm)
    relation = testing.Relation('db', local_app_data={'hostname': 'example.com'})
    peer_relation = testing.PeerRelation('peer')
    container = testing.Container('workload', can_connect=True)
    relation_secret = testing.Secret({'certificate': 'xxxxxxxx'})
    user_secret = testing.Secret({'username': 'admin', 'password': 'xxxxxxxx'})
    config = {'port': 8443, 'admin-credentials': 'secret:1234'}
    state_in = testing.State(
        leader=leader,
        config=config,
        relations={relation, peer_relation},
        containers={container},
        secrets={relation_secret, user_secret},
        unit_status=testing.BlockedStatus(),
        workload_version='1.0.1',
    )

    # Act:
    state_out = ctx.run(ctx.on.relation_changed(relation), state_in)

    # Assert:
    assert testing.JujuLogLine(level='INFO', message='Distributing secret.') in ctx.juju_log
    peer_relation_out = state_out.get_relation(peer_relation.id)
    assert peer_relation_out.peers_data[0] == {'secret_id': relation_secret.id}

You don't have to use pytest for your charm tests, but it's what we recommend. pytest's assert-based approach is a straightforward way to write tests, and its fixtures are helpful for structuring setup and teardown.

Installation

For charm tests, install the testing framework by adding the testing extra of ops in your unit testing environment. For example, in pyproject.toml:

[dependency-groups]
test = ['ops[testing]<4.0']

Ops checks if ops-scenario is installed, and, if so, makes the classes (such as Context, State, and Relation) available in the ops.testing namespace. Use from ops import testing rather than importing the scenario package.

ops-scenario supports the same platforms and Python versions as ops itself.

Documentation

  • To get started, work through our 'Write your first Kubernetes charm' tutorial, following the instructions for adding unit tests at the end of each chapter.
  • When you need to write a test that involves specific ops functionality, refer to our how-to guides which all conclude with examples of tests of the ops functionality.
  • Use our extensive reference documentation when you need to know how each testing object works. These docs are also available via the standard Python help() functionality and in your IDE.

Read the full documentation

Community

ops-scenario is a member of the Charming family. It's an open source project that warmly welcomes community contributions, suggestions, fixes and constructive feedback.

  • Read our code of conduct: As a community we adhere to the Ubuntu code of conduct.
  • Get support: Discourse is the go-to forum for all Ops-related discussions, including around testing.
  • Join our online chat: Meet us in the #charmhub-charmdev channel on Matrix.
  • Report bugs: We want to know about the problems so we can fix them.
  • Contribute docs: Get started on GitHub.

Contributing and developing

Anyone can contribute to ops and ops-scenario. It's best to start by opening an issue with a clear description of the problem or feature request, but you can also open a pull request directly.

Read our guide for more details on how to work on and contribute to ops-scenario.

Currently, releases of ops-scenario are done in lockstep with releases of ops itself, with matching minor and bugfix release numbers. The ops documentation outlines how to create a new release.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ops_scenario-8.7.1.tar.gz (78.6 kB view details)

Uploaded Source

Built Distribution

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

ops_scenario-8.7.1-py3-none-any.whl (69.4 kB view details)

Uploaded Python 3

File details

Details for the file ops_scenario-8.7.1.tar.gz.

File metadata

  • Download URL: ops_scenario-8.7.1.tar.gz
  • Upload date:
  • Size: 78.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for ops_scenario-8.7.1.tar.gz
Algorithm Hash digest
SHA256 25b24c7c612b8e089ad05f24a47c138999d1cc0a2683e0f7c74e0520c7bb6043
MD5 a4a2c8c30937bf59b7574c9f615f603b
BLAKE2b-256 4f06705b452b9145107978eb0400a1e751839ed1ec59dc152fa45a2e5fba0ea3

See more details on using hashes here.

Provenance

The following attestation bundles were made for ops_scenario-8.7.1.tar.gz:

Publisher: publish.yaml on canonical/operator

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ops_scenario-8.7.1-py3-none-any.whl.

File metadata

  • Download URL: ops_scenario-8.7.1-py3-none-any.whl
  • Upload date:
  • Size: 69.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for ops_scenario-8.7.1-py3-none-any.whl
Algorithm Hash digest
SHA256 01a8cee03c57a826b2dd9692571731d6307327cace7ac7865d9420e1136e7860
MD5 f50f9413673b870455d470cfbdc0ce3b
BLAKE2b-256 1545c78501e1b8203207b7c43f62233b9fea3be5015f5230ce53d7deceb5d7a3

See more details on using hashes here.

Provenance

The following attestation bundles were made for ops_scenario-8.7.1-py3-none-any.whl:

Publisher: publish.yaml on canonical/operator

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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