Debian Codegraph @

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

raw
1 from __future__ import annotations
2
3 from datetime import datetime, timedelta, timezone
4 from unittest import mock
5
6 import pytest
7
8 from plugins.module_utils._deprecation import (
9 deprecated_server_type_warning,
10 )
11 from plugins.module_utils._vendor.hcloud.locations import (
12 BoundLocation,
13 )
14 from plugins.module_utils._vendor.hcloud.server_types import (
15 BoundServerType,
16 )
17
18 PAST = datetime.now(timezone.utc) - timedelta(days=14)
19 FUTURE = datetime.now(timezone.utc) + timedelta(days=14)
20
21 LOCATION_FSN = {
22 "id": 1,
23 "name": "fsn1",
24 "available": False,
25 "recommended": False,
26 }
27 LOCATION_NBG = {
28 "id": 2,
29 "name": "nbg1",
30 "available": True,
31 "recommended": True,
32 }
33 DEPRECATION_NONE = {
34 "deprecation": None,
35 }
36 DEPRECATION_DEPRECATED = {
37 "deprecation": {
38 "announced": PAST.isoformat(),
39 "unavailable_after": FUTURE.isoformat(),
40 },
41 }
42 DEPRECATION_UNAVAILABLE = {
43 "deprecation": {
44 "announced": PAST.isoformat(),
45 "unavailable_after": PAST.isoformat(),
46 },
47 }
48
49
50 @pytest.mark.parametrize(
51 ("server_type", "location", "calls"),
52 [
53 (
54 BoundServerType(
55 mock.Mock(),
56 {"name": "cx22", "locations": []},
57 ),
58 BoundLocation(mock.Mock(), LOCATION_FSN),
59 [],
60 ),
61 # - Deprecated (backward compatible)
62 (
63 BoundServerType(
64 mock.Mock(),
65 {"name": "cx22", **DEPRECATION_DEPRECATED},
66 ),
67 None,
68 [
69 mock.call(
70 "Server type cx22 is deprecated in all locations and will no longer "
71 f"be available for order as of {FUTURE.strftime('%Y-%m-%d')}. "
72 "Existing servers of that type will continue to work as before and "
73 "no action is required on your part."
74 )
75 ],
76 ),
77 # - Unavailable (backward compatible)
78 (
79 BoundServerType(
80 mock.Mock(),
81 {"name": "cx22", **DEPRECATION_UNAVAILABLE},
82 ),
83 None,
84 [
85 mock.call(
86 "Server type cx22 is unavailable in all locations and can no longer "
87 "be ordered. Existing servers of that type will continue to work as "
88 "before and no action is required on your part."
89 )
90 ],
91 ),
92 # - SOME locations are deprecated
93 # - Given location is NOT deprecated
94 (
95 BoundServerType(
96 mock.Mock(),
97 {
98 "name": "cx22",
99 "locations": [
100 {**LOCATION_FSN, **DEPRECATION_NONE},
101 {**LOCATION_NBG, **DEPRECATION_DEPRECATED},
102 ],
103 },
104 ),
105 BoundLocation(mock.Mock(), LOCATION_FSN),
106 [],
107 ),
108 # - SOME locations are deprecated
109 # - Given location is deprecated
110 (
111 BoundServerType(
112 mock.Mock(),
113 {
114 "name": "cx22",
115 "locations": [
116 {**LOCATION_FSN, **DEPRECATION_NONE},
117 {**LOCATION_NBG, **DEPRECATION_DEPRECATED},
118 ],
119 },
120 ),
121 BoundLocation(mock.Mock(), LOCATION_NBG),
122 [
123 mock.call(
124 "Server type cx22 is deprecated in nbg1 and will no longer be available "
125 f"for order as of {FUTURE.strftime('%Y-%m-%d')}. Existing servers of "
126 "that type will continue to work as before and no action is required "
127 "on your part."
128 )
129 ],
130 ),
131 # - SOME locations are unavailable
132 # - Given location is unavailable
133 (
134 BoundServerType(
135 mock.Mock(),
136 {
137 "name": "cx22",
138 "locations": [
139 {**LOCATION_FSN, **DEPRECATION_NONE},
140 {**LOCATION_NBG, **DEPRECATION_UNAVAILABLE},
141 ],
142 },
143 ),
144 BoundLocation(mock.Mock(), LOCATION_NBG),
145 [
146 mock.call(
147 "Server type cx22 is unavailable in nbg1 and can no longer be ordered. "
148 "Existing servers of that type will continue to work as before and no "
149 "action is required on your part."
150 )
151 ],
152 ),
153 # - SOME locations are deprecated
154 # - Location is not given
155 (
156 BoundServerType(
157 mock.Mock(),
158 {
159 "name": "cx22",
160 "locations": [
161 {**LOCATION_FSN, **DEPRECATION_NONE},
162 {**LOCATION_NBG, **DEPRECATION_DEPRECATED},
163 ],
164 },
165 ),
166 None,
167 [],
168 ),
169 # - SOME locations are unavailable
170 # - Location is not given
171 (
172 BoundServerType(
173 mock.Mock(),
174 {
175 "name": "cx22",
176 "locations": [
177 {**LOCATION_FSN, **DEPRECATION_NONE},
178 {**LOCATION_NBG, **DEPRECATION_UNAVAILABLE},
179 ],
180 },
181 ),
182 None,
183 [],
184 ),
185 # - SOME locations are deprecated
186 # - SOME locations are unavailable
187 # - Location is not given
188 (
189 BoundServerType(
190 mock.Mock(),
191 {
192 "name": "cx22",
193 "locations": [
194 {**LOCATION_FSN, **DEPRECATION_DEPRECATED},
195 {**LOCATION_NBG, **DEPRECATION_UNAVAILABLE},
196 ],
197 },
198 ),
199 None,
200 [
201 mock.call(
202 "Server type cx22 is deprecated in all locations (fsn1,nbg1) and "
203 "can no longer be ordered in some locations (nbg1). Existing servers"
204 " of that type will continue to work as before and no action is "
205 "required on your part."
206 )
207 ],
208 ),
209 # - ALL locations are deprecated
210 # - Location is not given
211 (
212 BoundServerType(
213 mock.Mock(),
214 {
215 "name": "cx22",
216 "locations": [
217 {**LOCATION_FSN, **DEPRECATION_DEPRECATED},
218 {**LOCATION_NBG, **DEPRECATION_DEPRECATED},
219 ],
220 },
221 ),
222 None,
223 [
224 mock.call(
225 "Server type cx22 is deprecated in all locations (fsn1,nbg1) and "
226 "will no longer be available for order. Existing servers of that "
227 "type will continue to work as before and no action is required on "
228 "your part."
229 )
230 ],
231 ),
232 # - ALL locations are unavailable
233 # - Location is not given
234 (
235 BoundServerType(
236 mock.Mock(),
237 {
238 "name": "cx22",
239 "locations": [
240 {**LOCATION_FSN, **DEPRECATION_UNAVAILABLE},
241 {**LOCATION_NBG, **DEPRECATION_UNAVAILABLE},
242 ],
243 },
244 ),
245 None,
246 [
247 mock.call(
248 "Server type cx22 is unavailable in all locations (fsn1,nbg1) and "
249 "can no longer be ordered. Existing servers of that type will "
250 "continue to work as before and no action is required on your part."
251 )
252 ],
253 ),
254 ],
255 )
256 def test_deprecated_server_type_warning(server_type, location, calls):
257 m = mock.Mock()
258 deprecated_server_type_warning(m, server_type, location)
259 m.warn.assert_has_calls(calls)