|
| 1 | +from unittest.mock import Mock |
| 2 | +from unittest.mock import call |
| 3 | + |
| 4 | +import pytest |
| 5 | +from ironicclient.common.apiclient import exceptions |
| 6 | +from tenacity import stop_after_attempt |
| 7 | +from tenacity import wait_none |
| 8 | + |
| 9 | +from understack_workflows.main import configure_snmpv3_user as cli |
| 10 | + |
| 11 | + |
| 12 | +def test_conflict_retries_the_same_request_without_lifecycle_calls(): |
| 13 | + client = Mock() |
| 14 | + client.client.node.vendor_passthru.side_effect = [ |
| 15 | + exceptions.Conflict("Node is busy"), |
| 16 | + {"configured": True}, |
| 17 | + ] |
| 18 | + # Tenacity adds retry_with at runtime but does not expose it in its types. |
| 19 | + configure = cli.configure_snmpv3_user.retry_with( # pyright: ignore[reportFunctionMemberAccess] |
| 20 | + wait=wait_none(), stop=stop_after_attempt(2) |
| 21 | + ) |
| 22 | + |
| 23 | + configure(client, "node-uuid", 5) |
| 24 | + |
| 25 | + request = call.client.node.vendor_passthru( |
| 26 | + "node-uuid", "configure_snmpv3_user", args={"slot": 5}, http_method="POST" |
| 27 | + ) |
| 28 | + assert client.mock_calls == [request, request] |
| 29 | + |
| 30 | + |
| 31 | +@pytest.mark.parametrize("error", [exceptions.BadRequest, exceptions.Forbidden]) |
| 32 | +def test_permanent_errors_are_not_retried(error): |
| 33 | + client = Mock() |
| 34 | + client.client.node.vendor_passthru.side_effect = error("Cannot configure") |
| 35 | + |
| 36 | + with pytest.raises(error): |
| 37 | + cli.configure_snmpv3_user(client, "node-uuid", 5) |
| 38 | + |
| 39 | + client.client.node.vendor_passthru.assert_called_once() |
| 40 | + |
| 41 | + |
| 42 | +def test_busy_node_eventually_fails(): |
| 43 | + client = Mock() |
| 44 | + client.client.node.vendor_passthru.side_effect = exceptions.Conflict("Busy") |
| 45 | + # Tenacity adds retry_with at runtime but does not expose it in its types. |
| 46 | + configure = cli.configure_snmpv3_user.retry_with( # pyright: ignore[reportFunctionMemberAccess] |
| 47 | + wait=wait_none(), stop=stop_after_attempt(2) |
| 48 | + ) |
| 49 | + |
| 50 | + with pytest.raises(exceptions.Conflict): |
| 51 | + configure(client, "node-uuid", 5) |
| 52 | + |
| 53 | + assert client.client.node.vendor_passthru.call_count == 2 |
| 54 | + |
| 55 | + |
| 56 | +def test_main_resolves_node_and_uses_monitoring_slot(mocker): |
| 57 | + mocker.patch("sys.argv", ["configure-snmpv3-user", "--node", "Dell-ABC1234"]) |
| 58 | + client = mocker.patch.object(cli, "IronicClient").return_value |
| 59 | + client.get_node.return_value.uuid = "node-uuid" |
| 60 | + configure = mocker.patch.object(cli, "configure_snmpv3_user") |
| 61 | + |
| 62 | + cli.main() |
| 63 | + |
| 64 | + configure.assert_called_once_with(client, "node-uuid", 5) |
| 65 | + |
| 66 | + |
| 67 | +@pytest.mark.parametrize("slot", ["2", "17", "invalid"]) |
| 68 | +def test_invalid_slot_is_rejected(slot): |
| 69 | + with pytest.raises(SystemExit): |
| 70 | + cli.argument_parser().parse_args(["--node", "node-uuid", "--slot", slot]) |
0 commit comments