Skip to main content

Pytest plugin for checking charm relation interface protocol compliance.

Project description

pytest-interface-tester

This repository contains a library meant to facilitate compliance testing of charm relation interfaces. The problem is best stated as follows:

  • there is a repository containing relation interface specifications and tests (henceforth 'the spec repo'), such as charm-relation-interfaces.
  • there is a charm repository such as traefik-k8s that implements the provider side of the ingress relation interface (henceforth 'the charm repo').
  • The maintainers of the spec repo want to be able to automatically verify if all the registered charms that claim to implement a relation interface do in fact comply with its specification.
  • The maintainers of the charm repo want to be able to automatically verify that their implementation of the standardized relation interfaces are in fact compliant.

The interface tester package facilitates both these verification flows.

How to use the interface_tester in the spec repo

Follow the instructions here.

How to use the interface_tester in the charm repo

  1. Ensure that charm-relation-interfaces has one or more interface tests and a schema for the interface you want to test. If that is not the case, ask the maintainers of the interface (or its 'official' implementation) to add some.
  2. Install this package.
  3. Add a ...charm-root/tests/interface_tests/conftest.py file containing at least:
    import pytest
    from charm import MyCharm  # your charm class
    from interface_tester.plugin import InterfaceTester
    
    @pytest.fixture
    def interface_tester(interface_tester: InterfaceTester):
        interface_tester.configure(charm_type=MyCharm)
        yield interface_tester
    
    interface_tester is a pytest fixture exposed by this plugin. You are expected to override it with your own. The idea is that in doing this you:
    1. configure this fixture so that the spec repo will be able to find your charm location and name (it will default to whatever CharmBase subclass it can find in ...charm-root/src/charm.py, but it will give up soon if that is nonstandard or there are multiple charm classes).
    2. the test runner can be made aware of any special configuration that your charm needs in order to function. For example if your charm will do nothing unless it is configured in a certain way, a given file is present, a network is active, a container can connect, etc... then this is your chance to set your charm up so that it is ready for handling the necessary relation events.
    3. the runtime can be patched with anything necessary for your charm to function. For example, you can mock out any Popen calls, HTTP requests,

At this stage, if you commit this to your main branch, the spec repo will already be able to find your charm and run the interface tests against it.

The flow is (from the POV of the spec repo):

  • gather all tests, schemas and charms for an interface
  • for each charm:
    • clone the charm repo
    • fetch the interface_tester fixture from the charm's own tests, if any, otherwise assume no config is needed and try to grab the charm type off of src/charm.py
    • use the interface tester (configured or not) to run each test case:
      1. check that the scenario completes without uncaught charm errors, get the output state
      2. check whether the test case's own validator determines that the output state is valid
      3. check whether the relations in the output state are valid according to the schema

What if you want to run the tests yourself, charm-side, in CI, to catch potential issues before they trigger failures in the spec repo?

How to run the tests in the charm repo

A minimal example of a test is:

from interface_tester import InterfaceTester
from charm import MyCharm

def test_ingress_interface(interface_tester: InterfaceTester):
    interface_tester.configure(
      # you can skip this if your interface_tester fixture is already configured with the charm_type in conftest.py  
      charm_type=MyCharm, 
      # put here the interface that you wish to test. Omitting it will test for all interfaces that your charm supports.
      interface_name='ingress'
    )
    interface_tester.run()

If you have a conftest.py where you configured an interface_tester fixture and did all necessary mocking/patching already, then in principle you are good to go.

The flow is (from the POV of the spec repo):

  • clone the spec repo (or another)
  • gather all tests, schemas and charms for the interface you want to test
  • use the interface_tester fixture from the plugin (or one you override) to run the test cases
    • same 3 steps as above

Customizing the fixture's address

You can customize name and location of the fixture, but you will need to include that data when registering your charm with the interface. In interface.yaml, you can then specify:

  - name: my-charm-name  # required
    url: https://github.com/foo/my-charm-name  # required
    test_setup:  # optional
      location: path/to/file.py  # optional; default = tests/interface/conftest.py
      identifier: my_fixture_name # optional; default = interface_tester

Upgrading from v1

pytest-interface-tester supports both pydantic v1 and v2, but using v2 is recommended. You might need to adjust your tested charm to also support v2. See migration guide for more information.

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_interface_tester-3.4.0.tar.gz (27.9 kB view details)

Uploaded Source

Built Distribution

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

pytest_interface_tester-3.4.0-py3-none-any.whl (22.2 kB view details)

Uploaded Python 3

File details

Details for the file pytest_interface_tester-3.4.0.tar.gz.

File metadata

  • Download URL: pytest_interface_tester-3.4.0.tar.gz
  • Upload date:
  • Size: 27.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pytest_interface_tester-3.4.0.tar.gz
Algorithm Hash digest
SHA256 932a1811f8e3fc09607156018dd959bf29c235e75c47514cc252d7dbf5b83b21
MD5 4a446fbf2255d1ae2e4fc049fc810444
BLAKE2b-256 90afaeefdd291b46d81eeb25bea826bd47941b718f123ff7a5cc4779621855b7

See more details on using hashes here.

File details

Details for the file pytest_interface_tester-3.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_interface_tester-3.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8f16c6070fb7f7813e12a7b82f2fadc80675bedbca8344f9b041cfb6c1d5bd43
MD5 da9d7f811627bd4f0c8fefc1be647fc4
BLAKE2b-256 f0a9908cb5b464d3296a524d4538ffe3da202825bf10171e4ce5543ddb66e1c7

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