Skip to content

Commit

Permalink
Use lists for Exec command prefix examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyandrewmeyer committed Aug 7, 2024
1 parent ee72367 commit a631927
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ def test_pebble_exec():
name='foo',
execs={
scenario.Exec(
command_prefix=('ls', '-ll'),
command_prefix=['ls', '-ll'],
return_code=0,
stdout=LS_LL,
),
Expand All @@ -663,7 +663,7 @@ def test_pebble_exec():
ctx.on.pebble_ready(container),
state_in,
)
assert state_out.containers["foo"].get_exec(('ls', '-ll')).stdin == "..."
assert state_out.containers["foo"].get_exec(['ls', '-ll']).stdin == "..."
```

Scenario will attempt to find the right `Exec` object by matching the provided
Expand Down
11 changes: 9 additions & 2 deletions scenario/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,13 @@ class Exec(_max_posargs(1)):
# change ID: used internally to keep track of mocked processes
_change_id: int = dataclasses.field(default_factory=_generate_new_change_id)

def __post_init__(self):
# The command prefix can be any sequence type, and a list is tidier to
# write when there's only one string. However, this object needs to be
# hashable, so can't contain a list. We 'freeze' the sequence to a tuple
# to support that.
object.__setattr__(self, "command_prefix", tuple(self.command_prefix))

def _run(self) -> int:
return self._change_id

Expand Down Expand Up @@ -844,9 +851,9 @@ class Container(_max_posargs(1)):
container = scenario.Container(
name='foo',
execs={
scenario.Exec(('whoami', ), return_code=0, stdout='ubuntu'),
scenario.Exec(['whoami'], return_code=0, stdout='ubuntu'),
scenario.Exec(
('dig', '+short', 'canonical.com'),
['dig', '+short', 'canonical.com'],
return_code=0,
stdout='185.125.190.20\\n185.125.190.21',
),
Expand Down
2 changes: 1 addition & 1 deletion tests/test_consistency_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def test_evt_bad_container_name():
def test_duplicate_execs_in_container():
container = Container(
"foo",
execs={Exec(("ls", "-l"), return_code=0), Exec(("ls", "-l"), return_code=1)},
execs={Exec(["ls", "-l"], return_code=0), Exec(["ls", "-l"], return_code=1)},
)
assert_inconsistent(
State(containers=[container]),
Expand Down
10 changes: 5 additions & 5 deletions tests/test_e2e/test_pebble.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def callback(self: CharmBase):
Container(
name="foo",
can_connect=True,
execs={Exec((cmd,), stdout=out)},
execs={Exec([cmd], stdout=out)},
)
}
),
Expand All @@ -224,7 +224,7 @@ def _on_ready(self, _):
proc.wait_output()

ctx = Context(MyCharm, meta={"name": "foo", "containers": {"foo": {}}})
exec = Exec((), stdout=LS)
exec = Exec([], stdout=LS)
container = Container(name="foo", can_connect=True, execs={exec})
state_out = ctx.run(
ctx.on.pebble_ready(container=container), State(containers={container})
Expand Down Expand Up @@ -332,7 +332,7 @@ def test_exec_wait_error(charm_cls):
Container(
name="foo",
can_connect=True,
execs={Exec(("foo",), stdout="hello pebble", return_code=1)},
execs={Exec(["foo"], stdout="hello pebble", return_code=1)},
)
}
)
Expand All @@ -352,7 +352,7 @@ def test_exec_wait_output(charm_cls):
Container(
name="foo",
can_connect=True,
execs={Exec(("foo",), stdout="hello pebble", stderr="oepsie")},
execs={Exec(["foo"], stdout="hello pebble", stderr="oepsie")},
)
}
)
Expand All @@ -372,7 +372,7 @@ def test_exec_wait_output_error(charm_cls):
Container(
name="foo",
can_connect=True,
execs={Exec(("foo",), stdout="hello pebble", return_code=1)},
execs={Exec(["foo"], stdout="hello pebble", return_code=1)},
)
}
)
Expand Down

0 comments on commit a631927

Please sign in to comment.