Debian Codegraph @

ansible_collections/hetzner/hcloud/tests/unit/module_utils/test_hcloud.py

raw
1 from __future__ import annotations
2
3 import traceback
4 from datetime import datetime, timezone
5
6 import pytest
7
8 from plugins.module_utils._base import AnsibleHCloud
9 from plugins.module_utils._vendor.hcloud import (
10 APIException,
11 )
12 from plugins.module_utils._vendor.hcloud.actions import (
13 Action,
14 ActionException,
15 ActionFailedException,
16 ActionTimeoutException,
17 )
18
19
20 def test_hcloud_fail_json_hcloud(module):
21 AnsibleHCloud.represent = "hcloud_test"
22 hcloud = AnsibleHCloud(module)
23
24 try:
25 raise APIException(
26 code="invalid_input",
27 message="invalid input in fields 'server', 'home_location'",
28 details={
29 "fields": [
30 {"messages": ["either server or home_location must be provided"], "name": "server"},
31 {"messages": ["either server or home_location must be provided"], "name": "home_location"},
32 ]
33 },
34 )
35 except APIException as exception:
36 hcloud.fail_json_hcloud(exception)
37 # pylint: disable=unreachable
38 module.fail_json.assert_called_with(
39 msg="invalid input in fields 'server', 'home_location' (invalid_input)",
40 exception=traceback.format_exc(),
41 failure={
42 "message": "invalid input in fields 'server', 'home_location'",
43 "code": "invalid_input",
44 "details": {
45 "fields": [
46 {"messages": ["either server or home_location must be provided"], "name": "server"},
47 {"messages": ["either server or home_location must be provided"], "name": "home_location"},
48 ]
49 },
50 },
51 )
52
53 try:
54 raise ActionFailedException(
55 action=Action(
56 **{
57 "id": 1084730887,
58 "command": "change_server_type",
59 "status": "error",
60 "progress": 100,
61 "resources": [{"id": 34574042, "type": "server"}],
62 "error": {"code": "server_does_not_exist_anymore", "message": "Server does not exist anymore"},
63 "started": "2023-07-06T14:52:42+00:00",
64 "finished": "2023-07-06T14:53:08+00:00",
65 }
66 )
67 )
68 except ActionException as exception:
69 hcloud.fail_json_hcloud(exception)
70 # pylint: disable=unreachable
71 module.fail_json.assert_called_with(
72 msg="The pending action failed: Server does not exist anymore (server_does_not_exist_anymore, 1084730887)",
73 exception=traceback.format_exc(),
74 failure={
75 "action": {
76 "id": 1084730887,
77 "command": "change_server_type",
78 "status": "error",
79 "progress": 100,
80 "resources": [{"id": 34574042, "type": "server"}],
81 "error": {"code": "server_does_not_exist_anymore", "message": "Server does not exist anymore"},
82 "started": datetime(2023, 7, 6, 14, 52, 42, tzinfo=timezone.utc),
83 "finished": datetime(2023, 7, 6, 14, 53, 8, tzinfo=timezone.utc),
84 }
85 },
86 )
87
88 try:
89 raise ActionTimeoutException(
90 action=Action(
91 **{
92 "id": 1084659545,
93 "command": "create_server",
94 "status": "running",
95 "progress": 50,
96 "started": "2023-07-06T13:58:38+00:00",
97 "finished": None,
98 "resources": [{"id": 34572291, "type": "server"}],
99 "error": None,
100 }
101 )
102 )
103 except ActionException as exception:
104 hcloud.fail_json_hcloud(exception)
105 # pylint: disable=unreachable
106 module.fail_json.assert_called_with(
107 msg="The pending action timed out (create_server, 1084659545)",
108 exception=traceback.format_exc(),
109 failure={
110 "action": {
111 "id": 1084659545,
112 "command": "create_server",
113 "status": "running",
114 "progress": 50,
115 "resources": [{"id": 34572291, "type": "server"}],
116 "error": None,
117 "started": datetime(2023, 7, 6, 13, 58, 38, tzinfo=timezone.utc),
118 "finished": None,
119 }
120 },
121 )
122
123
124 @pytest.mark.parametrize(
125 ("kwargs", "msg"),
126 [
127 ({"required": ["key1"]}, None),
128 ({"required": ["missing"]}, "missing required arguments: missing"),
129 ({"required_one_of": [["key1", "missing"]]}, None),
130 ({"required_one_of": [["missing1", "missing2"]]}, "one of the following is required: missing1, missing2"),
131 ],
132 )
133 def test_hcloud_fail_on_invalid_params(module, kwargs, msg):
134 AnsibleHCloud.represent = "hcloud_test"
135 hcloud = AnsibleHCloud(module)
136
137 module.params = {
138 "key1": "value",
139 "key2": "value",
140 }
141
142 hcloud.fail_on_invalid_params(**kwargs)
143 if msg is None:
144 module.fail_json.assert_not_called()
145 else:
146 module.fail_json.assert_called_with(msg=msg)