Skip to content

Commit

Permalink
feat: use :memory: as the unit state storage location for ops.testing (
Browse files Browse the repository at this point in the history
…#1494)

This PR changes Scenario to always use `:memory:` as the location for
the unit state database, to speed up running tests.

In most cases, the charm root location is a `TemporaryDirectory`, so the
unit state database only exists during the event emission anyway. The
database could be accessed if a specific root directory was provided (or
inside the context manager context) but the correct way to interact with
the database is through the State API (deferred events, saved state
objects) so we don't want to leave that open for users.

Timing (best of 3):

|Suite|main|branch|
|-|-|-|
|operator unit tests (no concurrency)|4m11.923s|3m41.354s|
|traefik scenario tests|3m49.479s|1m56.795s|
|kafka-k8s-operator scenario tests|0m13.463s|0m13.236s|

Note that this [is already the case for
Harness](https://github.com/canonical/operator/blob/0ad731a6a9118dfac240e84681f5c41810c4f3f2/ops/_private/harness.py#L309).

Refs #1434
  • Loading branch information
tonyandrewmeyer authored Dec 11, 2024
1 parent 0ad731a commit 9d476ba
Showing 1 changed file with 1 addition and 3 deletions.
4 changes: 1 addition & 3 deletions testing/src/scenario/_ops_main_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,7 @@ def _setup_root_logging(self):

def _make_storage(self, _: _Dispatcher):
# TODO: add use_juju_for_storage support
# TODO: Pass a charm_state_path that is ':memory:' when appropriate.
charm_state_path = self._charm_root / self._charm_state_path
storage = ops.storage.SQLiteStorage(charm_state_path)
storage = ops.storage.SQLiteStorage(":memory:")
logger.info("Copying input state to storage.")
self.store = UnitStateDB(storage)
self.store.apply_state(self.state)
Expand Down

0 comments on commit 9d476ba

Please sign in to comment.