diff --git a/README.md b/README.md index a088325e..3d313b7d 100644 --- a/README.md +++ b/README.md @@ -664,8 +664,8 @@ def test_pebble_exec(): ctx.on.pebble_ready(container), state_in, ) - assert ctx.exec_history[container.name].command == ['ls', '-ll'] - assert ctx.exec_history[container.name].stdin == "..." + assert ctx.exec_history[container.name][0].command == ['ls', '-ll'] + assert ctx.exec_history[container.name][0].stdin == "..." ``` Scenario will attempt to find the right `Exec` object by matching the provided diff --git a/scenario/context.py b/scenario/context.py index 760bacf0..0f7ca1e1 100644 --- a/scenario/context.py +++ b/scenario/context.py @@ -1,13 +1,13 @@ #!/usr/bin/env python3 # Copyright 2023 Canonical Ltd. # See LICENSE file for licensing details. -import collections import tempfile from contextlib import contextmanager from pathlib import Path from typing import TYPE_CHECKING, Any, Dict, List, Optional, Type, Union, cast from ops import CharmBase, EventBase +from ops.testing import ExecArgs from scenario.logger import logger as scenario_logger from scenario.runtime import Runtime @@ -451,7 +451,7 @@ def __init__( self.juju_log: List["JujuLogLine"] = [] self.app_status_history: List["_EntityStatus"] = [] self.unit_status_history: List["_EntityStatus"] = [] - self.exec_history = collections.defaultdict(list) + self.exec_history: Dict[str, List[ExecArgs]] = {} self.workload_version_history: List[str] = [] self.removed_secret_revisions: List[int] = [] self.emitted_events: List[EventBase] = [] diff --git a/scenario/mocking.py b/scenario/mocking.py index 508af7fc..f906d083 100644 --- a/scenario/mocking.py +++ b/scenario/mocking.py @@ -844,19 +844,22 @@ def exec( stderr.write(handler.stderr) args = ExecArgs( - command, - environment or {}, - working_dir, - timeout, - user_id, - user, - group_id, - group, - stdin, # type:ignore # If None, will be replaced by proc_stdin.read() later. - encoding, - combine_stderr, + command=command, + environment=environment or {}, + working_dir=working_dir, + timeout=timeout, + user_id=user_id, + user=user, + group_id=group_id, + group=group, + stdin=stdin, # type:ignore # If None, will be replaced by proc_stdin.read() later. + encoding=encoding, + combine_stderr=combine_stderr, ) - self._context.exec_history[self._container_name].append(args) + try: + self._context.exec_history[self._container_name].append(args) + except KeyError: + self._context.exec_history[self._container_name] = [args] change_id = handler._run() return cast(