| 1 |
# Copyright (c) 2025 Felix Fontein <[email protected]> |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
from __future__ import (absolute_import, division, print_function) |
| 6 |
__metaclass__ = type |
| 7 |
|
| 8 |
|
| 9 |
from ansible_collections.community.internal_test_tools.tests.unit.utils.fetch_url_module_framework import ( |
| 10 |
FetchUrlCall, |
| 11 |
BaseTestModule, |
| 12 |
) |
| 13 |
|
| 14 |
from ansible_collections.community.internal_test_tools.tests.unit.compat.mock import call, MagicMock |
| 15 |
|
| 16 |
from ansible_collections.community.hrobot.plugins.module_utils.api import API_BASE_URL |
| 17 |
from ansible_collections.community.hrobot.plugins.module_utils.robot import BASE_URL |
| 18 |
from ansible_collections.community.hrobot.plugins.modules import storagebox_info |
| 19 |
|
| 20 |
|
| 21 |
STORAGEBOX_MINIMUM_DATA = [ |
| 22 |
{ |
| 23 |
'storagebox': { |
| 24 |
'id': 123456, |
| 25 |
'login': 'u12345', |
| 26 |
'name': 'Backup Server 1', |
| 27 |
'product': 'BX60', |
| 28 |
'cancelled': False, |
| 29 |
'locked': False, |
| 30 |
'location': 'FSN1', |
| 31 |
'linked_server': 1234567, |
| 32 |
'paid_until': '2015-10-23', |
| 33 |
}, |
| 34 |
}, |
| 35 |
{ |
| 36 |
'storagebox': { |
| 37 |
'id': 23, |
| 38 |
'login': 'u23', |
| 39 |
'name': 'Backup Server 2', |
| 40 |
'product': 'BX11', |
| 41 |
'cancelled': True, |
| 42 |
'locked': False, |
| 43 |
'location': 'HEL1', |
| 44 |
'linked_server': None, |
| 45 |
'paid_until': '2025-01-31', |
| 46 |
}, |
| 47 |
}, |
| 48 |
] |
| 49 |
|
| 50 |
|
| 51 |
STORAGEBOX_DETAIL_DATA = { |
| 52 |
23: { |
| 53 |
'storagebox': { |
| 54 |
'id': 23, |
| 55 |
'login': 'u23', |
| 56 |
'name': 'Backup Server 2', |
| 57 |
'product': 'BX11', |
| 58 |
'cancelled': True, |
| 59 |
'locked': False, |
| 60 |
'location': 'HEL1', |
| 61 |
'linked_server': None, |
| 62 |
'paid_until': '2025-01-31', |
| 63 |
'disk_quota': 1234, |
| 64 |
'disk_usage': 123, |
| 65 |
'disk_usage_data': 50, |
| 66 |
'disk_usage_snapshots': 73, |
| 67 |
'webdav': False, |
| 68 |
'samba': False, |
| 69 |
'ssh': True, |
| 70 |
'external_reachability': True, |
| 71 |
'zfs': False, |
| 72 |
'server': 'u23.your-storagebox.de', |
| 73 |
'host_system': 'HEL1-FOOBAR' |
| 74 |
}, |
| 75 |
}, |
| 76 |
123456: { |
| 77 |
'storagebox': { |
| 78 |
'id': 123456, |
| 79 |
'login': 'u12345', |
| 80 |
'name': 'Backup Server 1', |
| 81 |
'product': 'BX60', |
| 82 |
'cancelled': False, |
| 83 |
'locked': False, |
| 84 |
'location': 'FSN1', |
| 85 |
'linked_server': 1234567, |
| 86 |
'paid_until': '2015-10-23', |
| 87 |
'disk_quota': 10240000, |
| 88 |
'disk_usage': 900, |
| 89 |
'disk_usage_data': 500, |
| 90 |
'disk_usage_snapshots': 400, |
| 91 |
'webdav': True, |
| 92 |
'samba': True, |
| 93 |
'ssh': True, |
| 94 |
'external_reachability': True, |
| 95 |
'zfs': False, |
| 96 |
'server': 'u12345.your-storagebox.de', |
| 97 |
'host_system': 'FSN1-BX355' |
| 98 |
}, |
| 99 |
}, |
| 100 |
} |
| 101 |
|
| 102 |
STORAGEBOX_API_DATA = { |
| 103 |
23: { |
| 104 |
'id': 23, |
| 105 |
'username': 'u23', |
| 106 |
'status': 'active', |
| 107 |
'name': 'string', |
| 108 |
'storage_box_type': { |
| 109 |
'name': 'bx11', |
| 110 |
'description': 'BX11', |
| 111 |
'snapshot_limit': 10, |
| 112 |
'automatic_snapshot_limit': 10, |
| 113 |
'subaccounts_limit': 200, |
| 114 |
'size': 1073741824, |
| 115 |
'prices': [ |
| 116 |
{ |
| 117 |
'location': 'fsn1', |
| 118 |
'price_hourly': { |
| 119 |
'net': '1.0000', |
| 120 |
'gross': '1.1900', |
| 121 |
}, |
| 122 |
'price_monthly': { |
| 123 |
'net': '1.0000', |
| 124 |
'gross': '1.1900', |
| 125 |
}, |
| 126 |
'setup_fee': { |
| 127 |
'net': '1.0000', |
| 128 |
'gross': '1.1900', |
| 129 |
}, |
| 130 |
}, |
| 131 |
], |
| 132 |
'deprecation': { |
| 133 |
'unavailable_after': '2023-09-01T00:00:00+00:00', |
| 134 |
'announced': '2023-06-01T00:00:00+00:00', |
| 135 |
}, |
| 136 |
}, |
| 137 |
'location': { |
| 138 |
'id': 42, |
| 139 |
'name': 'fsn1', |
| 140 |
'description': 'Falkenstein DC Park 1', |
| 141 |
'country': 'DE', |
| 142 |
'city': 'Falkenstein', |
| 143 |
'latitude': 50.47612, |
| 144 |
'longitude': 12.370071, |
| 145 |
'network_zone': 'eu-central', |
| 146 |
}, |
| 147 |
'access_settings': { |
| 148 |
'reachable_externally': False, |
| 149 |
'samba_enabled': False, |
| 150 |
'ssh_enabled': True, |
| 151 |
'webdav_enabled': False, |
| 152 |
'zfs_enabled': False, |
| 153 |
}, |
| 154 |
'server': 'u23.your-storagebox.de', |
| 155 |
'system': 'FSN1-BX355', |
| 156 |
'stats': { |
| 157 |
'size': 0, |
| 158 |
'size_data': 0, |
| 159 |
'size_snapshots': 0, |
| 160 |
}, |
| 161 |
'labels': { |
| 162 |
'environment': 'prod', |
| 163 |
'example.com/my': 'label', |
| 164 |
'just-a-key': '', |
| 165 |
}, |
| 166 |
'protection': { |
| 167 |
'delete': False, |
| 168 |
}, |
| 169 |
'snapshot_plan': { |
| 170 |
'max_snapshots': 0, |
| 171 |
'minute': None, |
| 172 |
'hour': None, |
| 173 |
'day_of_week': None, |
| 174 |
'day_of_month': None, |
| 175 |
}, |
| 176 |
'created': '2016-01-30T23:55:00+00:00', |
| 177 |
}, |
| 178 |
123456: { |
| 179 |
'id': 123456, |
| 180 |
'username': 'u123456', |
| 181 |
'status': 'active', |
| 182 |
'name': 'string', |
| 183 |
'storage_box_type': { |
| 184 |
'name': 'bx11', |
| 185 |
'description': 'BX11', |
| 186 |
'snapshot_limit': 10, |
| 187 |
'automatic_snapshot_limit': 10, |
| 188 |
'subaccounts_limit': 200, |
| 189 |
'size': 1073741824, |
| 190 |
'prices': [ |
| 191 |
{ |
| 192 |
'location': 'fsn1', |
| 193 |
'price_hourly': { |
| 194 |
'net': '1.0000', |
| 195 |
'gross': '1.1900', |
| 196 |
}, |
| 197 |
'price_monthly': { |
| 198 |
'net': '1.0000', |
| 199 |
'gross': '1.1900', |
| 200 |
}, |
| 201 |
'setup_fee': { |
| 202 |
'net': '1.0000', |
| 203 |
'gross': '1.1900', |
| 204 |
}, |
| 205 |
}, |
| 206 |
], |
| 207 |
'deprecation': { |
| 208 |
'unavailable_after': '2023-09-01T00:00:00+00:00', |
| 209 |
'announced': '2023-06-01T00:00:00+00:00', |
| 210 |
}, |
| 211 |
}, |
| 212 |
'location': { |
| 213 |
'id': 23, |
| 214 |
'name': 'hel1', |
| 215 |
'description': 'Helsinki somewhere', |
| 216 |
'country': 'FI', |
| 217 |
'city': 'Helsinki', |
| 218 |
'latitude': 60.17, |
| 219 |
'longitude': 24.93, |
| 220 |
'network_zone': 'eu-central', |
| 221 |
}, |
| 222 |
'access_settings': { |
| 223 |
'reachable_externally': False, |
| 224 |
'samba_enabled': True, |
| 225 |
'ssh_enabled': True, |
| 226 |
'webdav_enabled': True, |
| 227 |
'zfs_enabled': True, |
| 228 |
}, |
| 229 |
'server': 'u123456.your-storagebox.de', |
| 230 |
'system': 'FSN1-BX355', |
| 231 |
'stats': { |
| 232 |
'size': 0, |
| 233 |
'size_data': 0, |
| 234 |
'size_snapshots': 0, |
| 235 |
}, |
| 236 |
'labels': { |
| 237 |
}, |
| 238 |
'protection': { |
| 239 |
'delete': False, |
| 240 |
}, |
| 241 |
'snapshot_plan': { |
| 242 |
'max_snapshots': 0, |
| 243 |
'minute': None, |
| 244 |
'hour': None, |
| 245 |
'day_of_week': None, |
| 246 |
'day_of_month': None, |
| 247 |
}, |
| 248 |
'created': '2025-06-28T23:55:00+00:00', |
| 249 |
}, |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
class TestHetznerStorageboxInfoLegacy(BaseTestModule): |
| 254 |
MOCK_ANSIBLE_MODULEUTILS_BASIC_ANSIBLEMODULE = 'ansible_collections.community.hrobot.plugins.modules.storagebox_info.AnsibleModule' |
| 255 |
MOCK_ANSIBLE_MODULEUTILS_URLS_FETCH_URL = 'ansible_collections.community.hrobot.plugins.module_utils.robot.fetch_url' |
| 256 |
|
| 257 |
def test_storagebox_id(self, mocker): |
| 258 |
result = self.run_module_success(mocker, storagebox_info, { |
| 259 |
'hetzner_user': 'test', |
| 260 |
'hetzner_password': 'hunter2', |
| 261 |
'storagebox_id': 23, |
| 262 |
}, [ |
| 263 |
FetchUrlCall('GET', 200) |
| 264 |
.expect_basic_auth('test', 'hunter2') |
| 265 |
.expect_force_basic_auth(True) |
| 266 |
.result_json(STORAGEBOX_DETAIL_DATA[23]) |
| 267 |
.expect_url('{0}/storagebox/23'.format(BASE_URL)), |
| 268 |
]) |
| 269 |
assert result['changed'] is False |
| 270 |
assert len(result['storageboxes']) == 1 |
| 271 |
assert result['storageboxes'][0] == STORAGEBOX_DETAIL_DATA[23]['storagebox'] |
| 272 |
|
| 273 |
def test_server_number_unknown(self, mocker): |
| 274 |
result = self.run_module_success(mocker, storagebox_info, { |
| 275 |
'hetzner_user': '', |
| 276 |
'hetzner_password': '', |
| 277 |
'storagebox_id': 1, |
| 278 |
}, [ |
| 279 |
FetchUrlCall('GET', 404) |
| 280 |
.result_json({ |
| 281 |
'error': { |
| 282 |
'status': 404, |
| 283 |
'code': 'STORAGEBOX_NOT_FOUND', |
| 284 |
'message': 'server not found', |
| 285 |
}, |
| 286 |
}) |
| 287 |
.expect_url('{0}/storagebox/1'.format(BASE_URL)), |
| 288 |
]) |
| 289 |
assert result['changed'] is False |
| 290 |
assert len(result['storageboxes']) == 0 |
| 291 |
|
| 292 |
def test_all(self, mocker): |
| 293 |
result = self.run_module_success(mocker, storagebox_info, { |
| 294 |
'hetzner_user': '', |
| 295 |
'hetzner_password': '', |
| 296 |
}, [ |
| 297 |
FetchUrlCall('GET', 200) |
| 298 |
.result_json(STORAGEBOX_MINIMUM_DATA) |
| 299 |
.expect_url('{0}/storagebox'.format(BASE_URL)), |
| 300 |
]) |
| 301 |
assert result['changed'] is False |
| 302 |
assert len(result['storageboxes']) == 2 |
| 303 |
assert result['storageboxes'][0] == STORAGEBOX_MINIMUM_DATA[0]['storagebox'] |
| 304 |
assert result['storageboxes'][1] == STORAGEBOX_MINIMUM_DATA[1]['storagebox'] |
| 305 |
|
| 306 |
def test_linked_server_number(self, mocker): |
| 307 |
result = self.run_module_success(mocker, storagebox_info, { |
| 308 |
'hetzner_user': '', |
| 309 |
'hetzner_password': '', |
| 310 |
'linked_server_number': 1234567, |
| 311 |
}, [ |
| 312 |
FetchUrlCall('GET', 200) |
| 313 |
.result_json(STORAGEBOX_MINIMUM_DATA[0]) |
| 314 |
.expect_form_value('linked_server', '1234567') |
| 315 |
.expect_url('{0}/storagebox'.format(BASE_URL)), |
| 316 |
]) |
| 317 |
assert result['changed'] is False |
| 318 |
assert len(result['storageboxes']) == 1 |
| 319 |
assert result['storageboxes'][0] == STORAGEBOX_MINIMUM_DATA[0]['storagebox'] |
| 320 |
|
| 321 |
def test_linked_server_number_unknown(self, mocker): |
| 322 |
result = self.run_module_success(mocker, storagebox_info, { |
| 323 |
'hetzner_user': '', |
| 324 |
'hetzner_password': '', |
| 325 |
'linked_server_number': 1, |
| 326 |
}, [ |
| 327 |
FetchUrlCall('GET', 404) |
| 328 |
.result_json({ |
| 329 |
'error': { |
| 330 |
'status': 404, |
| 331 |
'code': 'STORAGEBOX_NOT_FOUND', |
| 332 |
'message': 'server not found', |
| 333 |
}, |
| 334 |
}) |
| 335 |
.expect_form_value('linked_server', '1') |
| 336 |
.expect_url('{0}/storagebox'.format(BASE_URL)), |
| 337 |
]) |
| 338 |
assert result['changed'] is False |
| 339 |
assert len(result['storageboxes']) == 0 |
| 340 |
|
| 341 |
def test_all_full_info(self, mocker): |
| 342 |
result = self.run_module_success(mocker, storagebox_info, { |
| 343 |
'hetzner_user': '', |
| 344 |
'hetzner_password': '', |
| 345 |
'full_info': True, |
| 346 |
}, [ |
| 347 |
FetchUrlCall('GET', 200) |
| 348 |
.result_json(STORAGEBOX_MINIMUM_DATA) |
| 349 |
.expect_url('{0}/storagebox'.format(BASE_URL)), |
| 350 |
FetchUrlCall('GET', 200) |
| 351 |
.result_json(STORAGEBOX_DETAIL_DATA[123456]) |
| 352 |
.expect_url('{0}/storagebox/123456'.format(BASE_URL)), |
| 353 |
FetchUrlCall('GET', 200) |
| 354 |
.result_json(STORAGEBOX_DETAIL_DATA[23]) |
| 355 |
.expect_url('{0}/storagebox/23'.format(BASE_URL)), |
| 356 |
]) |
| 357 |
assert result['changed'] is False |
| 358 |
assert len(result['storageboxes']) == 2 |
| 359 |
assert result['storageboxes'][0] == STORAGEBOX_DETAIL_DATA[123456]['storagebox'] |
| 360 |
assert result['storageboxes'][1] == STORAGEBOX_DETAIL_DATA[23]['storagebox'] |
| 361 |
|
| 362 |
def test_all_none(self, mocker): |
| 363 |
result = self.run_module_success(mocker, storagebox_info, { |
| 364 |
'hetzner_user': '', |
| 365 |
'hetzner_password': '', |
| 366 |
}, [ |
| 367 |
FetchUrlCall('GET', 200) |
| 368 |
.result_json([]) |
| 369 |
.expect_url('{0}/storagebox'.format(BASE_URL)), |
| 370 |
]) |
| 371 |
assert result['changed'] is False |
| 372 |
assert len(result['storageboxes']) == 0 |
| 373 |
|
| 374 |
def test_all_none_error(self, mocker): |
| 375 |
# According to the API docs, when no storagebox is found this API can return 404. |
| 376 |
result = self.run_module_success(mocker, storagebox_info, { |
| 377 |
'hetzner_user': '', |
| 378 |
'hetzner_password': '', |
| 379 |
}, [ |
| 380 |
FetchUrlCall('GET', 404) |
| 381 |
.result_json({ |
| 382 |
'error': { |
| 383 |
'status': 404, |
| 384 |
'code': 'STORAGEBOX_NOT_FOUND', |
| 385 |
'message': 'server not found', |
| 386 |
}, |
| 387 |
}) |
| 388 |
.expect_url('{0}/storagebox'.format(BASE_URL)), |
| 389 |
]) |
| 390 |
assert result['changed'] is False |
| 391 |
assert len(result['storageboxes']) == 0 |
| 392 |
|
| 393 |
|
| 394 |
class TestHetznerStorageboxInfo(BaseTestModule): |
| 395 |
MOCK_ANSIBLE_MODULEUTILS_BASIC_ANSIBLEMODULE = 'ansible_collections.community.hrobot.plugins.modules.storagebox_info.AnsibleModule' |
| 396 |
MOCK_ANSIBLE_MODULEUTILS_URLS_FETCH_URL = 'ansible_collections.community.hrobot.plugins.module_utils.api.fetch_url' |
| 397 |
|
| 398 |
def test_auth_error(self, mocker): |
| 399 |
result = self.run_module_failed(mocker, storagebox_info, { |
| 400 |
'hetzner_token': 'asdf', |
| 401 |
'storagebox_id': 23, |
| 402 |
}, [ |
| 403 |
FetchUrlCall('GET', 401) |
| 404 |
.expect_header("Authorization", "Bearer asdf") |
| 405 |
.result_json({ |
| 406 |
"error": { |
| 407 |
"code": "unauthorized", |
| 408 |
"details": None, |
| 409 |
"message": "unable to authenticate", |
| 410 |
}, |
| 411 |
}) |
| 412 |
.expect_url('{0}/v1/storage_boxes/23'.format(API_BASE_URL)), |
| 413 |
]) |
| 414 |
assert result['msg'] == 'Request GET https://api.hetzner.com/v1/storage_boxes/23 failed: [unauthorized] unable to authenticate' |
| 415 |
|
| 416 |
def test_auth_error_w_details(self, mocker): |
| 417 |
result = self.run_module_failed(mocker, storagebox_info, { |
| 418 |
'hetzner_token': 'asdf', |
| 419 |
'storagebox_id': 23, |
| 420 |
}, [ |
| 421 |
FetchUrlCall('GET', 401) |
| 422 |
.expect_header("Authorization", "Bearer asdf") |
| 423 |
.result_json({ |
| 424 |
"error": { |
| 425 |
"code": "unauthorized", |
| 426 |
"details": { |
| 427 |
"username": "invalid", |
| 428 |
}, |
| 429 |
"message": "unable to authenticate", |
| 430 |
}, |
| 431 |
}) |
| 432 |
.expect_url('{0}/v1/storage_boxes/23'.format(API_BASE_URL)), |
| 433 |
]) |
| 434 |
assert result['msg'] in ( |
| 435 |
"Request GET https://api.hetzner.com/v1/storage_boxes/23 failed: [unauthorized]" |
| 436 |
" unable to authenticate. Details: {'username': 'invalid'}", |
| 437 |
"Request GET https://api.hetzner.com/v1/storage_boxes/23 failed: [unauthorized]" |
| 438 |
" unable to authenticate. Details: {u'username': u'invalid'}", |
| 439 |
) |
| 440 |
|
| 441 |
def test_empty_result(self, mocker): |
| 442 |
result = self.run_module_failed(mocker, storagebox_info, { |
| 443 |
'hetzner_token': 'asdf', |
| 444 |
'storagebox_id': 23, |
| 445 |
}, [ |
| 446 |
FetchUrlCall('GET', 200) |
| 447 |
.expect_header("Authorization", "Bearer asdf") |
| 448 |
.expect_url('{0}/v1/storage_boxes/23'.format(API_BASE_URL)), |
| 449 |
]) |
| 450 |
assert result['msg'] == 'Cannot retrieve content from GET https://api.hetzner.com/v1/storage_boxes/23, HTTP status code 200 (None)' |
| 451 |
|
| 452 |
def test_empty_result_rate_limited(self, mocker): |
| 453 |
sleep_mock = MagicMock() |
| 454 |
mocker.patch('time.sleep', sleep_mock) |
| 455 |
result = self.run_module_failed(mocker, storagebox_info, { |
| 456 |
'hetzner_token': 'asdf', |
| 457 |
'storagebox_id': 23, |
| 458 |
'rate_limit_retry_timeout': 1, |
| 459 |
}, [ |
| 460 |
FetchUrlCall('GET', 429) |
| 461 |
.expect_header("Authorization", "Bearer asdf") |
| 462 |
.result_error_json('Not found', { |
| 463 |
'error': { |
| 464 |
'code': 'rate_limit_exceeded', |
| 465 |
'message': 'Rate limit exceeded', |
| 466 |
'details': {}, |
| 467 |
}, |
| 468 |
}) |
| 469 |
.return_header('RateLimit-Limit', '23') |
| 470 |
.return_header('RateLimit-Remaining', '0') |
| 471 |
.return_header('RateLimit-Reset', '1234567890') |
| 472 |
.expect_url('{0}/v1/storage_boxes/23'.format(API_BASE_URL)), |
| 473 |
FetchUrlCall('GET', 200) |
| 474 |
.expect_header("Authorization", "Bearer asdf") |
| 475 |
.expect_url('{0}/v1/storage_boxes/23'.format(API_BASE_URL)), |
| 476 |
]) |
| 477 |
assert result['msg'] == 'Cannot retrieve content from GET https://api.hetzner.com/v1/storage_boxes/23, HTTP status code 200 (None)' |
| 478 |
# TODO: only works with Python 3+ |
| 479 |
# sleep_mock.assert_called_once() |
| 480 |
|
| 481 |
def test_empty_result_rate_limited_error(self, mocker): |
| 482 |
sleep_mock = MagicMock() |
| 483 |
mocker.patch('time.sleep', sleep_mock) |
| 484 |
result = self.run_module_failed(mocker, storagebox_info, { |
| 485 |
'hetzner_token': 'asdf', |
| 486 |
'storagebox_id': 23, |
| 487 |
'rate_limit_retry_timeout': 0, |
| 488 |
}, [ |
| 489 |
FetchUrlCall('GET', 429) |
| 490 |
.result_error_json('Not found', { |
| 491 |
'error': { |
| 492 |
'code': 'rate_limit_exceeded', |
| 493 |
'message': 'Rate limit exceeded', |
| 494 |
'details': {}, |
| 495 |
}, |
| 496 |
}) |
| 497 |
.return_header('RateLimit-Limit', '23') |
| 498 |
.return_header('RateLimit-Remaining', '0') |
| 499 |
.return_header('RateLimit-Reset', '1234567890') |
| 500 |
.expect_url('{0}/v1/storage_boxes/23'.format(API_BASE_URL)), |
| 501 |
]) |
| 502 |
assert result['msg'] == 'Request GET https://api.hetzner.com/v1/storage_boxes/23 failed: [rate_limit_exceeded] Rate limit exceeded' |
| 503 |
sleep_mock.assert_has_calls([]) |
| 504 |
|
| 505 |
def test_bad_json(self, mocker): |
| 506 |
result = self.run_module_failed(mocker, storagebox_info, { |
| 507 |
'hetzner_token': 'asdf', |
| 508 |
'storagebox_id': 23, |
| 509 |
}, [ |
| 510 |
FetchUrlCall('GET', 200) |
| 511 |
.expect_header("Authorization", "Bearer asdf") |
| 512 |
.result_str("{") |
| 513 |
.expect_url('{0}/v1/storage_boxes/23'.format(API_BASE_URL)), |
| 514 |
]) |
| 515 |
assert result['msg'] == 'Cannot decode content retrieved from GET https://api.hetzner.com/v1/storage_boxes/23' |
| 516 |
|
| 517 |
def test_storagebox_id(self, mocker): |
| 518 |
result = self.run_module_success(mocker, storagebox_info, { |
| 519 |
'hetzner_token': 'asdf', |
| 520 |
'storagebox_id': 23, |
| 521 |
}, [ |
| 522 |
FetchUrlCall('GET', 200) |
| 523 |
.expect_header("Authorization", "Bearer asdf") |
| 524 |
.result_json({ |
| 525 |
"storage_box": STORAGEBOX_API_DATA[23], |
| 526 |
}) |
| 527 |
.expect_url('{0}/v1/storage_boxes/23'.format(API_BASE_URL)), |
| 528 |
]) |
| 529 |
assert result['changed'] is False |
| 530 |
assert len(result['storageboxes']) == 1 |
| 531 |
assert result['storageboxes'][0] == storagebox_info.add_hrobot_compat_shim(STORAGEBOX_API_DATA[23]) |
| 532 |
|
| 533 |
def test_server_number_unknown(self, mocker): |
| 534 |
result = self.run_module_success(mocker, storagebox_info, { |
| 535 |
'hetzner_token': '', |
| 536 |
'storagebox_id': 1, |
| 537 |
}, [ |
| 538 |
FetchUrlCall('GET', 404) |
| 539 |
.result_json({ |
| 540 |
'error': { |
| 541 |
'code': 'not_found', |
| 542 |
'message': 'Storage Box not found', |
| 543 |
'details': {}, |
| 544 |
}, |
| 545 |
}) |
| 546 |
.expect_url('{0}/v1/storage_boxes/1'.format(API_BASE_URL)), |
| 547 |
]) |
| 548 |
assert result['changed'] is False |
| 549 |
assert len(result['storageboxes']) == 0 |
| 550 |
|
| 551 |
def test_all(self, mocker): |
| 552 |
result = self.run_module_success(mocker, storagebox_info, { |
| 553 |
'hetzner_token': '', |
| 554 |
}, [ |
| 555 |
FetchUrlCall('GET', 200) |
| 556 |
.result_json({ |
| 557 |
"storage_boxes": [ |
| 558 |
STORAGEBOX_API_DATA[23], |
| 559 |
STORAGEBOX_API_DATA[123456], |
| 560 |
], |
| 561 |
"meta": { |
| 562 |
"pagination": { |
| 563 |
"page": 1, |
| 564 |
"per_page": 100, |
| 565 |
"previous_page": None, |
| 566 |
"next_page": None, |
| 567 |
"last_page": 1, |
| 568 |
"total_entries": 2, |
| 569 |
}, |
| 570 |
}, |
| 571 |
}) |
| 572 |
.expect_url('{0}/v1/storage_boxes?page=1&per_page=100'.format(API_BASE_URL)), |
| 573 |
]) |
| 574 |
assert result['changed'] is False |
| 575 |
assert len(result['storageboxes']) == 2 |
| 576 |
assert result['storageboxes'][0] == storagebox_info.add_hrobot_compat_shim(STORAGEBOX_API_DATA[23]) |
| 577 |
assert result['storageboxes'][1] == storagebox_info.add_hrobot_compat_shim(STORAGEBOX_API_DATA[123456]) |
| 578 |
|
| 579 |
def test_all_paginated(self, mocker): |
| 580 |
# This is kind of faked, since we ask for 100 entries per page, but get one entry per page... |
| 581 |
result = self.run_module_success(mocker, storagebox_info, { |
| 582 |
'hetzner_token': '', |
| 583 |
}, [ |
| 584 |
FetchUrlCall('GET', 200) |
| 585 |
.result_json({ |
| 586 |
"storage_boxes": [ |
| 587 |
STORAGEBOX_API_DATA[23], |
| 588 |
], |
| 589 |
"meta": { |
| 590 |
"pagination": { |
| 591 |
"page": 1, |
| 592 |
"per_page": 100, |
| 593 |
"previous_page": None, |
| 594 |
"next_page": 2, |
| 595 |
"last_page": 2, |
| 596 |
"total_entries": 2, |
| 597 |
}, |
| 598 |
}, |
| 599 |
}) |
| 600 |
.expect_url('{0}/v1/storage_boxes?page=1&per_page=100'.format(API_BASE_URL)), |
| 601 |
FetchUrlCall('GET', 200) |
| 602 |
.result_json({ |
| 603 |
"storage_boxes": [ |
| 604 |
STORAGEBOX_API_DATA[123456], |
| 605 |
], |
| 606 |
"meta": { |
| 607 |
"pagination": { |
| 608 |
"page": 2, |
| 609 |
"per_page": 100, |
| 610 |
"previous_page": 1, |
| 611 |
"next_page": None, |
| 612 |
"last_page": 2, |
| 613 |
"total_entries": 2, |
| 614 |
}, |
| 615 |
}, |
| 616 |
}) |
| 617 |
.expect_url('{0}/v1/storage_boxes?page=2&per_page=100'.format(API_BASE_URL)), |
| 618 |
]) |
| 619 |
assert result['changed'] is False |
| 620 |
assert len(result['storageboxes']) == 2 |
| 621 |
assert result['storageboxes'][0] == storagebox_info.add_hrobot_compat_shim(STORAGEBOX_API_DATA[23]) |
| 622 |
assert result['storageboxes'][1] == storagebox_info.add_hrobot_compat_shim(STORAGEBOX_API_DATA[123456]) |
| 623 |
|
| 624 |
def test_all_paginated_strange(self, mocker): |
| 625 |
# This is really faked and simply tests more edge cases that should never happen with the real API |
| 626 |
sleep_mock = MagicMock() |
| 627 |
mocker.patch('time.sleep', sleep_mock) |
| 628 |
result = self.run_module_success(mocker, storagebox_info, { |
| 629 |
'hetzner_token': '', |
| 630 |
'rate_limit_retry_timeout': -1, |
| 631 |
}, [ |
| 632 |
FetchUrlCall('GET', 429) |
| 633 |
.result_json({ |
| 634 |
'error': { |
| 635 |
'code': 'rate_limit_exceeded', |
| 636 |
'message': 'Rate limit exceeded', |
| 637 |
'details': {}, |
| 638 |
}, |
| 639 |
}) |
| 640 |
.return_header('RateLimit-Limit', '23') |
| 641 |
.return_header('RateLimit-Remaining', '0') |
| 642 |
.return_header('RateLimit-Reset', '1234567890') |
| 643 |
.expect_url('{0}/v1/storage_boxes?page=1&per_page=100'.format(API_BASE_URL)), |
| 644 |
FetchUrlCall('GET', 429) |
| 645 |
.result_json({ |
| 646 |
'error': { |
| 647 |
'code': 'rate_limit_exceeded', |
| 648 |
'message': 'Rate limit exceeded', |
| 649 |
'details': {}, |
| 650 |
}, |
| 651 |
}) |
| 652 |
.return_header('RateLimit-Limit', '23') |
| 653 |
.return_header('RateLimit-Remaining', '0') |
| 654 |
.return_header('RateLimit-Reset', '1234567891') |
| 655 |
.expect_url('{0}/v1/storage_boxes?page=1&per_page=100'.format(API_BASE_URL)), |
| 656 |
FetchUrlCall('GET', 200) |
| 657 |
.result_json({ |
| 658 |
"storage_boxes": [ |
| 659 |
STORAGEBOX_API_DATA[23], |
| 660 |
STORAGEBOX_API_DATA[123456], |
| 661 |
], |
| 662 |
}) |
| 663 |
.expect_url('{0}/v1/storage_boxes?page=1&per_page=100'.format(API_BASE_URL)), |
| 664 |
FetchUrlCall('GET', 200) |
| 665 |
.result_json({ |
| 666 |
"storage_boxes": None, |
| 667 |
"meta": { |
| 668 |
"pagination": {}, |
| 669 |
}, |
| 670 |
}) |
| 671 |
.expect_url('{0}/v1/storage_boxes?page=2&per_page=100'.format(API_BASE_URL)), |
| 672 |
FetchUrlCall('GET', 200) |
| 673 |
.result_json({}) |
| 674 |
.expect_url('{0}/v1/storage_boxes?page=3&per_page=100'.format(API_BASE_URL)), |
| 675 |
]) |
| 676 |
assert result['changed'] is False |
| 677 |
assert len(result['storageboxes']) == 2 |
| 678 |
assert result['storageboxes'][0] == storagebox_info.add_hrobot_compat_shim(STORAGEBOX_API_DATA[23]) |
| 679 |
assert result['storageboxes'][1] == storagebox_info.add_hrobot_compat_shim(STORAGEBOX_API_DATA[123456]) |
| 680 |
sleep_mock.assert_has_calls([ |
| 681 |
call(5), |
| 682 |
call(5), |
| 683 |
]) |
| 684 |
|
| 685 |
def test_all_none(self, mocker): |
| 686 |
result = self.run_module_success(mocker, storagebox_info, { |
| 687 |
'hetzner_token': '', |
| 688 |
}, [ |
| 689 |
FetchUrlCall('GET', 200) |
| 690 |
.result_json({ |
| 691 |
"storage_boxes": [], |
| 692 |
"meta": { |
| 693 |
"pagination": { |
| 694 |
"page": 1, |
| 695 |
"per_page": 100, |
| 696 |
"previous_page": None, |
| 697 |
"next_page": None, |
| 698 |
"last_page": 1, |
| 699 |
"total_entries": 0, |
| 700 |
}, |
| 701 |
}, |
| 702 |
}) |
| 703 |
.expect_url('{0}/v1/storage_boxes?page=1&per_page=100'.format(API_BASE_URL)), |
| 704 |
]) |
| 705 |
assert result['changed'] is False |
| 706 |
assert len(result['storageboxes']) == 0 |