|
| 1 | +from itertools import count |
| 2 | +from unittest.mock import Mock, call, ANY |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from tests.installer import MultiStepAction, Step, AbortAction, SkipStep, InstallerError, CommandFailed |
| 7 | + |
| 8 | + |
| 9 | +@pytest.fixture |
| 10 | +def step_mock(): |
| 11 | + yield Mock() |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture |
| 15 | +def step_factory(step_mock): |
| 16 | + idx = count() |
| 17 | + |
| 18 | + def _create_mock(**kwargs): |
| 19 | + attrs = {} |
| 20 | + step_idx = next(idx) |
| 21 | + for step_attr in ("pre_execute", "execute", "on_action_success", "on_action_fail"): |
| 22 | + mock = Mock(side_effect=kwargs.pop(step_attr, None)) |
| 23 | + step_mock.attach_mock(mock, f"step_{step_idx}_{step_attr}") |
| 24 | + attrs[step_attr] = mock |
| 25 | + |
| 26 | + attrs.update(kwargs) |
| 27 | + |
| 28 | + return type(f"TestStep{step_idx}", (Step,), attrs) |
| 29 | + |
| 30 | + return _create_mock |
| 31 | + |
| 32 | + |
| 33 | +@pytest.mark.unit |
| 34 | +@pytest.mark.parametrize( |
| 35 | + "step_1_args", |
| 36 | + ({}, {"execute": ValueError, "required": False}, {"execute": SkipStep}), |
| 37 | + ids=("regular execution", "step skipped", "non required step fails"), |
| 38 | +) |
| 39 | +def test_execute(step_1_args, step_mock, step_factory, args_mock): |
| 40 | + class TestMSAction(MultiStepAction): |
| 41 | + steps = ( |
| 42 | + step_factory(), |
| 43 | + step_factory(**step_1_args), |
| 44 | + step_factory(), |
| 45 | + ) |
| 46 | + |
| 47 | + action = TestMSAction() |
| 48 | + action.execute(args_mock) |
| 49 | + |
| 50 | + step_mock.assert_has_calls( |
| 51 | + [ |
| 52 | + call.step_0_pre_execute(ANY, args_mock), |
| 53 | + call.step_1_pre_execute(ANY, args_mock), |
| 54 | + call.step_2_pre_execute(ANY, args_mock), |
| 55 | + call.step_0_execute(ANY, args_mock), |
| 56 | + call.step_1_execute(ANY, args_mock), |
| 57 | + call.step_2_execute(ANY, args_mock), |
| 58 | + call.step_2_on_action_success(ANY, args_mock), |
| 59 | + call.step_1_on_action_success(ANY, args_mock), |
| 60 | + call.step_0_on_action_success(ANY, args_mock), |
| 61 | + ] |
| 62 | + ) |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.unit |
| 66 | +def test_execute_not_required(step_mock, step_factory, args_mock): |
| 67 | + class TestMSAction(MultiStepAction): |
| 68 | + steps = ( |
| 69 | + step_factory(), |
| 70 | + step_factory(), |
| 71 | + step_factory(), |
| 72 | + ) |
| 73 | + |
| 74 | + action = TestMSAction() |
| 75 | + action.execute(args_mock) |
| 76 | + |
| 77 | + step_mock.assert_has_calls( |
| 78 | + [ |
| 79 | + call.step_0_pre_execute(ANY, args_mock), |
| 80 | + call.step_1_pre_execute(ANY, args_mock), |
| 81 | + call.step_2_pre_execute(ANY, args_mock), |
| 82 | + call.step_0_execute(ANY, args_mock), |
| 83 | + call.step_1_execute(ANY, args_mock), |
| 84 | + call.step_2_execute(ANY, args_mock), |
| 85 | + call.step_2_on_action_success(ANY, args_mock), |
| 86 | + call.step_1_on_action_success(ANY, args_mock), |
| 87 | + call.step_0_on_action_success(ANY, args_mock), |
| 88 | + ] |
| 89 | + ) |
| 90 | + |
| 91 | + |
| 92 | +@pytest.mark.unit |
| 93 | +def test_execute_post_hook_fail(step_mock, step_factory, args_mock): |
| 94 | + class TestMSAction(MultiStepAction): |
| 95 | + steps = ( |
| 96 | + step_factory(on_action_success=RuntimeError), |
| 97 | + step_factory(execute=SkipStep, on_action_success=ValueError), |
| 98 | + step_factory(on_action_success=InstallerError), |
| 99 | + ) |
| 100 | + |
| 101 | + action = TestMSAction() |
| 102 | + action.execute(args_mock) |
| 103 | + |
| 104 | + step_mock.assert_has_calls( |
| 105 | + [ |
| 106 | + call.step_0_pre_execute(ANY, args_mock), |
| 107 | + call.step_1_pre_execute(ANY, args_mock), |
| 108 | + call.step_2_pre_execute(ANY, args_mock), |
| 109 | + call.step_0_execute(ANY, args_mock), |
| 110 | + call.step_1_execute(ANY, args_mock), |
| 111 | + call.step_2_execute(ANY, args_mock), |
| 112 | + call.step_2_on_action_success(ANY, args_mock), |
| 113 | + call.step_1_on_action_success(ANY, args_mock), |
| 114 | + call.step_0_on_action_success(ANY, args_mock), |
| 115 | + ] |
| 116 | + ) |
| 117 | + |
| 118 | + |
| 119 | +@pytest.mark.unit |
| 120 | +@pytest.mark.parametrize("exc_class", (AbortAction, InstallerError, ValueError, CommandFailed)) |
| 121 | +def test_execute_fail(exc_class, step_mock, step_factory, args_mock): |
| 122 | + abort_exc = exc_class() |
| 123 | + |
| 124 | + class TestMSAction(MultiStepAction): |
| 125 | + steps = ( |
| 126 | + step_factory(), |
| 127 | + step_factory(execute=abort_exc), |
| 128 | + step_factory(on_action_fail=RuntimeError), |
| 129 | + ) |
| 130 | + |
| 131 | + action = TestMSAction() |
| 132 | + |
| 133 | + with pytest.raises(AbortAction if exc_class == AbortAction else InstallerError) as exc_info: |
| 134 | + action.execute(args_mock) |
| 135 | + |
| 136 | + step_mock.assert_has_calls( |
| 137 | + [ |
| 138 | + call.step_0_pre_execute(ANY, args_mock), |
| 139 | + call.step_1_pre_execute(ANY, args_mock), |
| 140 | + call.step_2_pre_execute(ANY, args_mock), |
| 141 | + call.step_0_execute(ANY, args_mock), |
| 142 | + call.step_1_execute(ANY, args_mock), |
| 143 | + call.step_2_on_action_fail(ANY, args_mock), |
| 144 | + call.step_1_on_action_fail(ANY, args_mock), |
| 145 | + call.step_0_on_action_fail(ANY, args_mock), |
| 146 | + ] |
| 147 | + ) |
| 148 | + |
| 149 | + assert exc_info.value.__cause__ == abort_exc |
| 150 | + |
| 151 | + |
| 152 | +@pytest.mark.unit |
| 153 | +@pytest.mark.parametrize("exc_class", (AbortAction, InstallerError, ValueError, CommandFailed)) |
| 154 | +def test_pre_execute_fail(exc_class, step_mock, step_factory, args_mock): |
| 155 | + abort_exc = exc_class() |
| 156 | + |
| 157 | + class TestMSAction(MultiStepAction): |
| 158 | + steps = ( |
| 159 | + step_factory(), |
| 160 | + step_factory(execute=abort_exc), |
| 161 | + step_factory(), |
| 162 | + ) |
| 163 | + |
| 164 | + action = TestMSAction() |
| 165 | + |
| 166 | + with pytest.raises(AbortAction if exc_class == AbortAction else InstallerError) as exc_info: |
| 167 | + action.execute(args_mock) |
| 168 | + |
| 169 | + step_mock.assert_has_calls( |
| 170 | + [ |
| 171 | + call.step_0_pre_execute(ANY, args_mock), |
| 172 | + call.step_1_pre_execute(ANY, args_mock), |
| 173 | + ] |
| 174 | + ) |
| 175 | + |
| 176 | + assert exc_info.value.__cause__ == abort_exc |
0 commit comments