Testing relations with Scenario

Scenario has just released with an update to the relation data model. From now on, you can enjoy true-to-juju relation modelling including ‘regular’, ‘peer’ and ‘subordinate relations’.

You can verify that your charm behaves the way it should in presence of a mix of the above:

from charm import MyCharm
from scenario import State, Relation, PeerRelation, SubordinateRelation

def test_foo():
    peer_relation = PeerRelation(
        'cluster',
        peers_data={
            32: {"foo": "bar"},
            42: {"baz": "qux"},
            })
    state_out = State(
        relations=[
            Relation('database'),
            peer_relation,
            SubordinateRelation('tracing')
        ]
    ).trigger(
        peer_relation.changed_event(remote_unit=42),
        charm_type=MyCharm,
        meta={
            "name": "my-app",
            "requires": {
                "database": {
                    "interface": "mongo"
                }
            },
            "provides": {
                "tracing": {
                    "interface": "foo",
                    "scope": "container"  # sub!
                }
            },
            "peers": {
                "cluster": {
                    "interface": "my-peer-interface"
                }
            }
        }
    )

Scenario will verify that the relations you are specifying match the ones declared in the metadata, to avoid any confusion.

The charm code can access relation data according to the Juju access rules.

2 Likes