harness-extensions

Harness Extensions

Channel Revision Published Runs on
latest/beta 3 05 Jul 2022
Ubuntu 20.04
juju deploy harness-extensions --channel beta
Show information

Platform:

Ubuntu
20.04

charms.harness_extensions.v0.harness_ctx

This is a library providing a sample implementation for more realistic Harness-event interactions. First and foremost, it generates a single-use Harness that gets discarded after each event execution, thereby forcing a charm init at each event. Secondly, it emits framework.commit() events after each hook, which Harness doesn't (yet). This lib will likely become redundant once https://github.com/canonical/operator/issues/736 is solved; until then, this allows us to experiment with the concept.

Basic usage:

class MyCharm(CharmBase): def init(self, framework: Framework, key: typing.Optional = None): super().init(framework, key) self.framework.observe(self.on.update_status, self._listen) self.framework.observe(self.framework.on.commit, self._listen)

def _listen(self, e):
    self.event = e

with HarnessCtx(MyCharm, "update-status") as h: event = h.emit() assert event.handle.kind == "update_status"

assert h.harness.charm.event.handle.kind == "commit"


class Emitter

Description

Event emitter. None

Methods

Emitter. __init__( self , harness: Harness , emit )

Emitter. emitted( self )

Description

Has the event been emitted already? None

Emitter. emit( self )

Emit the event.

Description

Will get called automatically when HarnessCtx exits if you didn't call it already.

class HarnessCtx

Harness-based context for emitting a single event.

Description

Example usage:

class MyCharm(CharmBase): def init(self, framework: Framework, key: typing.Optional = None): super().init(framework, key) self.framework.observe(self.on.update_status, self._listen) self.framework.observe(self.framework.on.commit, self._listen)

def _listen(self, e):
    self.event = e

with HarnessCtx(MyCharm, "update-status") as h: event = h.emit() assert event.handle.kind == "update_status"

assert h.harness.charm.event.handle.kind == "commit"

Methods

HarnessCtx. __init__( self , charm , event_name: str , emitter )

HarnessCtx. __enter__( self )

HarnessCtx. __exit__( self , exc_type , exc_val , exc_tb )