canonical / ops-scenario

State-transition testing SDK for Operator Framework Juju charms.
Apache License 2.0
10 stars 7 forks source link

Consider allowing state.get_relation("some-endpoint-name") #155

Closed dimaqq closed 2 months ago

dimaqq commented 2 months ago

Today I have 2 options:

  1. iterate over state.relations and find my relation manually, or
  2. pin relation ids when state is created and .get_relation(0)

Given that first argument, the relation endpoint is allowed to be positional, consider expanding state.get_relation to allow a string argument, which would mean the endpoint name. The code would then, perhaps, ensure that there's only one relation like this.

tonyandrewmeyer commented 2 months ago

An endpoint name does not uniquely identify a relation. If you want to get the relations for a given endpoint, use get_relations. If you know there will only be one, you can make use of that knowledge when handling the response.

A charm can't add or remove relations, so the relation will exist in the input state. The expected pattern is:

rel = Relation(...)
state_in = State(relations={rel})
state_out = ctx.run(..., state_in)
assert state_out.get_relation(rel.id)...