|
| 1 | +# |
| 2 | +# Copyright 2025 FMR LLC <opensource@fidelity.com> |
| 3 | +# |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | +# |
| 6 | + |
| 7 | +# pylint: disable=redefined-outer-name,missing-docstring |
| 8 | + |
| 9 | +import pytest |
| 10 | +from awsrun.runner import AccountRunner, Command |
| 11 | +from awsrun.session import SessionProvider |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture |
| 15 | +def runner(mocker): |
| 16 | + session_provider = mocker.MagicMock(spec=SessionProvider) |
| 17 | + return AccountRunner(session_provider) |
| 18 | + |
| 19 | + |
| 20 | +def test_lifecycle_methods(mocker, runner): |
| 21 | + command = mocker.MagicMock(spec=Command) |
| 22 | + runner.run(command, ["a", "b"]) |
| 23 | + command.pre_hook_with_context.assert_called() |
| 24 | + assert command.execute.call_count == 2 |
| 25 | + command.collect_results.assert_called() |
| 26 | + command.post_hook.assert_called() |
| 27 | + |
| 28 | + |
| 29 | +def test_lifecycle_methods_with_no_accts_to_process(mocker, runner): |
| 30 | + command = mocker.MagicMock(spec=Command) |
| 31 | + runner.run(command, []) |
| 32 | + command.pre_hook_with_context.assert_called() |
| 33 | + command.execute.assert_not_called() |
| 34 | + command.collect_results.assert_not_called() |
| 35 | + command.post_hook.assert_called() |
| 36 | + |
| 37 | + |
| 38 | +def test_pre_hook_with_context(mocker, runner): |
| 39 | + command = Command() |
| 40 | + command.pre_hook_with_context = mocker.MagicMock() |
| 41 | + runner.run(command, [], context="Hello") |
| 42 | + command.pre_hook_with_context.assert_called_with("Hello") |
| 43 | + |
| 44 | + |
| 45 | +# If no pre_hook_with_context is explicitly defined in a Command, then the |
| 46 | +# default implementation should invoke the older pre_hook method for |
| 47 | +# backwards compatibility. |
| 48 | +def test_backwards_compat_lifecycle_pre_hook(mocker, runner): |
| 49 | + command = Command() |
| 50 | + command.pre_hook = mocker.MagicMock() |
| 51 | + runner.run(command, []) |
| 52 | + command.pre_hook.assert_called() |
0 commit comments