canonical / ops-scenario

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

Add support for testing pebble services interaction #34

Open ghislainbourgeois opened 1 year ago

ghislainbourgeois commented 1 year ago

There is currently no way in ops-scenario to test a charm interaction with a pebble service, like restarting it for example: self._container.restart(self._service_name).

The only way to accomplish this is to mock ops.model.Container.restart.

It would be nice to have a better way to do this, maybe as an automatic mock in the output state?

...

state_out = self.ctx.run(container.pebble_ready_event, state_in)

assert state_out.containers[0].services["ausf"].restarted
PietroPasotti commented 1 year ago

I'm hesitant to patch extra names into Container.services because at the moment scenario is using 'native' ops.pebble data structures internally, (Plan, ServiceInfo, etc...) so it'd be unexpected to have a different api in different contexts, when the objects are the same. We could add something to Container:

assert state_out.containers[0].restarted('ausf')
assert state_out.containers[0].replanned
assert state_out.containers[0].sent_signals == ((9, "ausf", "foo"), ("SIGKILL", "bar-server"))

@benhoyt WDYT?

PietroPasotti commented 1 year ago

PS in light of #37, probably this kind of container-facing output mocks should end up in scenario.outputs as they are not quite persisted in the state. Much like juju-log and action results.

PietroPasotti commented 2 months ago

@tonyandrewmeyer perhaps now we're using our own data structures instead of importing them from ops, this becomes more easy to implement in a clean way? will draft something on top of v7 as soon as we release it