| 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 |
| 19 |
|
| 20 |
|
| 21 |
STORAGEBOX_LEGACY_KEYS = ('name', 'webdav', 'samba', 'ssh', 'external_reachability', 'zfs') |
| 22 |
|
| 23 |
STORAGEBOX_LEGACY_DETAIL_DATA = { |
| 24 |
23: { |
| 25 |
'storagebox': { |
| 26 |
'id': 23, |
| 27 |
'login': 'u23', |
| 28 |
'name': 'Backup Server 2', |
| 29 |
'product': 'BX11', |
| 30 |
'cancelled': True, |
| 31 |
'locked': False, |
| 32 |
'location': 'HEL1', |
| 33 |
'linked_server': None, |
| 34 |
'paid_until': '2025-01-31', |
| 35 |
'disk_quota': 1234, |
| 36 |
'disk_usage': 123, |
| 37 |
'disk_usage_data': 50, |
| 38 |
'disk_usage_snapshots': 73, |
| 39 |
'webdav': False, |
| 40 |
'samba': False, |
| 41 |
'ssh': True, |
| 42 |
'external_reachability': True, |
| 43 |
'zfs': False, |
| 44 |
'server': 'u23.your-storagebox.de', |
| 45 |
'host_system': 'HEL1-FOOBAR' |
| 46 |
}, |
| 47 |
}, |
| 48 |
123456: { |
| 49 |
'storagebox': { |
| 50 |
'id': 123456, |
| 51 |
'login': 'u12345', |
| 52 |
'name': 'Backup Server 1', |
| 53 |
'product': 'BX60', |
| 54 |
'cancelled': False, |
| 55 |
'locked': False, |
| 56 |
'location': 'FSN1', |
| 57 |
'linked_server': 1234567, |
| 58 |
'paid_until': '2015-10-23', |
| 59 |
'disk_quota': 10240000, |
| 60 |
'disk_usage': 900, |
| 61 |
'disk_usage_data': 500, |
| 62 |
'disk_usage_snapshots': 400, |
| 63 |
'webdav': True, |
| 64 |
'samba': True, |
| 65 |
'ssh': True, |
| 66 |
'external_reachability': True, |
| 67 |
'zfs': False, |
| 68 |
'server': 'u12345.your-storagebox.de', |
| 69 |
'host_system': 'FSN1-BX355' |
| 70 |
}, |
| 71 |
}, |
| 72 |
} |
| 73 |
|
| 74 |
STORAGEBOX_KEYS = { |
| 75 |
'name': ['name'], |
| 76 |
'webdav': ['access_settings', 'webdav_enabled'], |
| 77 |
'samba': ['access_settings', 'samba_enabled'], |
| 78 |
'ssh': ['access_settings', 'ssh_enabled'], |
| 79 |
'external_reachability': ['access_settings', 'reachable_externally'], |
| 80 |
'zfs': ['access_settings', 'zfs_enabled'], |
| 81 |
} |
| 82 |
|
| 83 |
STORAGEBOX_DETAIL_DATA = { |
| 84 |
23: { |
| 85 |
'id': 23, |
| 86 |
'username': 'u23', |
| 87 |
'status': 'active', |
| 88 |
'name': 'Backup Server 2', |
| 89 |
'storage_box_type': { |
| 90 |
'name': 'bx11', |
| 91 |
'description': 'BX11', |
| 92 |
'snapshot_limit': 10, |
| 93 |
'automatic_snapshot_limit': 10, |
| 94 |
'subaccounts_limit': 200, |
| 95 |
'size': 1073741824, |
| 96 |
'prices': [ |
| 97 |
{ |
| 98 |
'location': 'fsn1', |
| 99 |
'price_hourly': { |
| 100 |
'net': '1.0000', |
| 101 |
'gross': '1.1900', |
| 102 |
}, |
| 103 |
'price_monthly': { |
| 104 |
'net': '1.0000', |
| 105 |
'gross': '1.1900', |
| 106 |
}, |
| 107 |
'setup_fee': { |
| 108 |
'net': '1.0000', |
| 109 |
'gross': '1.1900', |
| 110 |
}, |
| 111 |
}, |
| 112 |
], |
| 113 |
'deprecation': { |
| 114 |
'unavailable_after': '2023-09-01T00:00:00+00:00', |
| 115 |
'announced': '2023-06-01T00:00:00+00:00', |
| 116 |
}, |
| 117 |
}, |
| 118 |
'location': { |
| 119 |
'id': 42, |
| 120 |
'name': 'fsn1', |
| 121 |
'description': 'Falkenstein DC Park 1', |
| 122 |
'country': 'DE', |
| 123 |
'city': 'Falkenstein', |
| 124 |
'latitude': 50.47612, |
| 125 |
'longitude': 12.370071, |
| 126 |
'network_zone': 'eu-central', |
| 127 |
}, |
| 128 |
'access_settings': { |
| 129 |
'reachable_externally': True, |
| 130 |
'samba_enabled': False, |
| 131 |
'ssh_enabled': True, |
| 132 |
'webdav_enabled': False, |
| 133 |
'zfs_enabled': True, |
| 134 |
}, |
| 135 |
'server': 'u23.your-storagebox.de', |
| 136 |
'system': 'FSN1-BX355', |
| 137 |
'stats': { |
| 138 |
'size': 0, |
| 139 |
'size_data': 0, |
| 140 |
'size_snapshots': 0, |
| 141 |
}, |
| 142 |
'labels': { |
| 143 |
'environment': 'prod', |
| 144 |
'example.com/my': 'label', |
| 145 |
'just-a-key': '', |
| 146 |
}, |
| 147 |
'protection': { |
| 148 |
'delete': False, |
| 149 |
}, |
| 150 |
'snapshot_plan': { |
| 151 |
'max_snapshots': 0, |
| 152 |
'minute': None, |
| 153 |
'hour': None, |
| 154 |
'day_of_week': None, |
| 155 |
'day_of_month': None, |
| 156 |
}, |
| 157 |
'created': '2016-01-30T23:55:00+00:00', |
| 158 |
}, |
| 159 |
123456: { |
| 160 |
'id': 123456, |
| 161 |
'username': 'u123456', |
| 162 |
'status': 'active', |
| 163 |
'name': 'Backup Server 1', |
| 164 |
'storage_box_type': { |
| 165 |
'name': 'bx11', |
| 166 |
'description': 'BX11', |
| 167 |
'snapshot_limit': 10, |
| 168 |
'automatic_snapshot_limit': 10, |
| 169 |
'subaccounts_limit': 200, |
| 170 |
'size': 1073741824, |
| 171 |
'prices': [ |
| 172 |
{ |
| 173 |
'location': 'fsn1', |
| 174 |
'price_hourly': { |
| 175 |
'net': '1.0000', |
| 176 |
'gross': '1.1900', |
| 177 |
}, |
| 178 |
'price_monthly': { |
| 179 |
'net': '1.0000', |
| 180 |
'gross': '1.1900', |
| 181 |
}, |
| 182 |
'setup_fee': { |
| 183 |
'net': '1.0000', |
| 184 |
'gross': '1.1900', |
| 185 |
}, |
| 186 |
}, |
| 187 |
], |
| 188 |
'deprecation': { |
| 189 |
'unavailable_after': '2023-09-01T00:00:00+00:00', |
| 190 |
'announced': '2023-06-01T00:00:00+00:00', |
| 191 |
}, |
| 192 |
}, |
| 193 |
'location': { |
| 194 |
'id': 23, |
| 195 |
'name': 'hel1', |
| 196 |
'description': 'Helsinki somewhere', |
| 197 |
'country': 'FI', |
| 198 |
'city': 'Helsinki', |
| 199 |
'latitude': 60.17, |
| 200 |
'longitude': 24.93, |
| 201 |
'network_zone': 'eu-central', |
| 202 |
}, |
| 203 |
'access_settings': { |
| 204 |
'reachable_externally': True, |
| 205 |
'samba_enabled': True, |
| 206 |
'ssh_enabled': True, |
| 207 |
'webdav_enabled': True, |
| 208 |
'zfs_enabled': False, |
| 209 |
}, |
| 210 |
'server': 'u123456.your-storagebox.de', |
| 211 |
'system': 'FSN1-BX355', |
| 212 |
'stats': { |
| 213 |
'size': 0, |
| 214 |
'size_data': 0, |
| 215 |
'size_snapshots': 0, |
| 216 |
}, |
| 217 |
'labels': { |
| 218 |
}, |
| 219 |
'protection': { |
| 220 |
'delete': False, |
| 221 |
}, |
| 222 |
'snapshot_plan': { |
| 223 |
'max_snapshots': 0, |
| 224 |
'minute': None, |
| 225 |
'hour': None, |
| 226 |
'day_of_week': None, |
| 227 |
'day_of_month': None, |
| 228 |
}, |
| 229 |
'created': '2025-06-28T23:55:00+00:00', |
| 230 |
}, |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
def legacy_update_info(id, **updates): |
| 235 |
result = { |
| 236 |
'storagebox': dict(STORAGEBOX_LEGACY_DETAIL_DATA[id]['storagebox']), |
| 237 |
} |
| 238 |
result['storagebox'].update(updates) |
| 239 |
return result |
| 240 |
|
| 241 |
|
| 242 |
def update_info(id, **updates): |
| 243 |
result = { |
| 244 |
'storage_box': dict(STORAGEBOX_DETAIL_DATA[id]), |
| 245 |
} |
| 246 |
for k, v in updates.items(): |
| 247 |
keys = STORAGEBOX_KEYS[k] |
| 248 |
dest = result['storage_box'] |
| 249 |
for key in keys[:-1]: |
| 250 |
dest, old_dest = dict(dest[key]), dest |
| 251 |
old_dest[key] = dest |
| 252 |
dest[keys[-1]] = v |
| 253 |
return result |
| 254 |
|
| 255 |
|
| 256 |
def get_key(data, key): |
| 257 |
for k in key: |
| 258 |
data = data[k] |
| 259 |
return data |
| 260 |
|
| 261 |
|
| 262 |
class TestHetznerStorageboxLegacy(BaseTestModule): |
| 263 |
MOCK_ANSIBLE_MODULEUTILS_BASIC_ANSIBLEMODULE = 'ansible_collections.community.hrobot.plugins.modules.storagebox.AnsibleModule' |
| 264 |
MOCK_ANSIBLE_MODULEUTILS_URLS_FETCH_URL = 'ansible_collections.community.hrobot.plugins.module_utils.robot.fetch_url' |
| 265 |
|
| 266 |
def test_idempotent(self, mocker): |
| 267 |
updated = legacy_update_info(23) |
| 268 |
result = self.run_module_success(mocker, storagebox, { |
| 269 |
'hetzner_user': 'test', |
| 270 |
'hetzner_password': 'hunter2', |
| 271 |
'id': 23, |
| 272 |
'name': 'Backup Server 2', |
| 273 |
'webdav': False, |
| 274 |
'samba': False, |
| 275 |
'ssh': True, |
| 276 |
'external_reachability': True, |
| 277 |
}, [ |
| 278 |
FetchUrlCall('GET', 200) |
| 279 |
.expect_basic_auth('test', 'hunter2') |
| 280 |
.expect_force_basic_auth(True) |
| 281 |
.result_json(STORAGEBOX_LEGACY_DETAIL_DATA[23]) |
| 282 |
.expect_url('{0}/storagebox/23'.format(BASE_URL)), |
| 283 |
]) |
| 284 |
assert result['changed'] is False |
| 285 |
for key in STORAGEBOX_LEGACY_KEYS: |
| 286 |
assert result[key] == updated['storagebox'][key], "Unexpected difference for {0!r}".format(key) |
| 287 |
|
| 288 |
def test_id_unknown(self, mocker): |
| 289 |
result = self.run_module_failed(mocker, storagebox, { |
| 290 |
'hetzner_user': '', |
| 291 |
'hetzner_password': '', |
| 292 |
'id': 1, |
| 293 |
}, [ |
| 294 |
FetchUrlCall('GET', 404) |
| 295 |
.expect_basic_auth('', '') |
| 296 |
.expect_force_basic_auth(True) |
| 297 |
.result_json({ |
| 298 |
'error': { |
| 299 |
'status': 404, |
| 300 |
'code': 'STORAGEBOX_NOT_FOUND', |
| 301 |
'message': 'Storagebox not found', |
| 302 |
}, |
| 303 |
}) |
| 304 |
.expect_url('{0}/storagebox/1'.format(BASE_URL)), |
| 305 |
]) |
| 306 |
assert result['msg'] == 'Storagebox with ID 1 does not exist' |
| 307 |
|
| 308 |
def test_invalid_input(self, mocker): |
| 309 |
result = self.run_module_failed(mocker, storagebox, { |
| 310 |
'hetzner_user': '', |
| 311 |
'hetzner_password': '', |
| 312 |
'id': 23, |
| 313 |
'name': 'Backup', |
| 314 |
}, [ |
| 315 |
FetchUrlCall('GET', 200) |
| 316 |
.expect_basic_auth('', '') |
| 317 |
.expect_force_basic_auth(True) |
| 318 |
.result_json(STORAGEBOX_LEGACY_DETAIL_DATA[23]) |
| 319 |
.expect_url('{0}/storagebox/23'.format(BASE_URL)), |
| 320 |
FetchUrlCall('POST', 400) |
| 321 |
.expect_basic_auth('', '') |
| 322 |
.expect_force_basic_auth(True) |
| 323 |
.result_json({ |
| 324 |
'error': { |
| 325 |
'status': 400, |
| 326 |
'code': 'INVALID_INPUT', |
| 327 |
'message': 'Invalid input', |
| 328 |
'invalid': ['storagebox_name'], |
| 329 |
'missing': None, |
| 330 |
}, |
| 331 |
}) |
| 332 |
.expect_form_value('storagebox_name', 'Backup') |
| 333 |
.expect_form_value_absent('webdav') |
| 334 |
.expect_form_value_absent('samba') |
| 335 |
.expect_form_value_absent('ssh') |
| 336 |
.expect_form_value_absent('external_reachability') |
| 337 |
.expect_form_value_absent('zfs') |
| 338 |
.expect_url('{0}/storagebox/23'.format(BASE_URL)), |
| 339 |
]) |
| 340 |
assert result['msg'] == 'The values to update were invalid (storagebox_name)' |
| 341 |
|
| 342 |
def test_change_name(self, mocker): |
| 343 |
updated = legacy_update_info(23, name='Backup') |
| 344 |
result = self.run_module_success(mocker, storagebox, { |
| 345 |
'hetzner_user': 'test', |
| 346 |
'hetzner_password': 'hunter2', |
| 347 |
'id': 23, |
| 348 |
'name': 'Backup', |
| 349 |
'ssh': True, |
| 350 |
}, [ |
| 351 |
FetchUrlCall('GET', 200) |
| 352 |
.expect_basic_auth('test', 'hunter2') |
| 353 |
.expect_force_basic_auth(True) |
| 354 |
.result_json(STORAGEBOX_LEGACY_DETAIL_DATA[23]) |
| 355 |
.expect_url('{0}/storagebox/23'.format(BASE_URL)), |
| 356 |
FetchUrlCall('POST', 200) |
| 357 |
.result_json(updated) |
| 358 |
.expect_form_value('storagebox_name', 'Backup') |
| 359 |
.expect_form_value_absent('webdav') |
| 360 |
.expect_form_value_absent('samba') |
| 361 |
.expect_form_value_absent('ssh') |
| 362 |
.expect_form_value_absent('external_reachability') |
| 363 |
.expect_form_value_absent('zfs') |
| 364 |
.expect_url('{0}/storagebox/23'.format(BASE_URL)), |
| 365 |
]) |
| 366 |
assert result['changed'] is True |
| 367 |
for key in STORAGEBOX_LEGACY_KEYS: |
| 368 |
assert result[key] == updated['storagebox'][key], "Unexpected difference for {0!r}".format(key) |
| 369 |
|
| 370 |
def test_change_name_rate_limit_fail(self, mocker): |
| 371 |
result = self.run_module_failed(mocker, storagebox, { |
| 372 |
'hetzner_user': 'test', |
| 373 |
'hetzner_password': 'hunter2', |
| 374 |
'id': 23, |
| 375 |
'name': 'Backup', |
| 376 |
'ssh': True, |
| 377 |
'rate_limit_retry_timeout': 0, |
| 378 |
}, [ |
| 379 |
FetchUrlCall('GET', 200) |
| 380 |
.expect_basic_auth('test', 'hunter2') |
| 381 |
.expect_force_basic_auth(True) |
| 382 |
.result_json(STORAGEBOX_LEGACY_DETAIL_DATA[23]) |
| 383 |
.expect_url('{0}/storagebox/23'.format(BASE_URL)), |
| 384 |
FetchUrlCall('POST', 403) |
| 385 |
.result_json({ |
| 386 |
'error': { |
| 387 |
'status': 403, |
| 388 |
'code': 'RATE_LIMIT_EXCEEDED', |
| 389 |
'message': 'Rate limit exceeded', |
| 390 |
'interval': 5, |
| 391 |
'max_request': 1, |
| 392 |
}, |
| 393 |
}) |
| 394 |
.expect_form_value('storagebox_name', 'Backup') |
| 395 |
.expect_form_value_absent('webdav') |
| 396 |
.expect_form_value_absent('samba') |
| 397 |
.expect_form_value_absent('ssh') |
| 398 |
.expect_form_value_absent('external_reachability') |
| 399 |
.expect_form_value_absent('zfs') |
| 400 |
.expect_url('{0}/storagebox/23'.format(BASE_URL)), |
| 401 |
]) |
| 402 |
assert result['msg'] == ( |
| 403 |
'Request failed: 403 RATE_LIMIT_EXCEEDED (Rate limit exceeded).' |
| 404 |
' Maximum allowed requests: 1. Time interval in seconds: 5' |
| 405 |
) |
| 406 |
|
| 407 |
def test_change_name_rate_limit(self, mocker): |
| 408 |
sleep_mock = MagicMock() |
| 409 |
mocker.patch('time.sleep', sleep_mock) |
| 410 |
updated = legacy_update_info(23, name='Backup') |
| 411 |
result = self.run_module_success(mocker, storagebox, { |
| 412 |
'hetzner_user': 'test', |
| 413 |
'hetzner_password': 'hunter2', |
| 414 |
'id': 23, |
| 415 |
'name': 'Backup', |
| 416 |
'ssh': True, |
| 417 |
'rate_limit_retry_timeout': -1, |
| 418 |
}, [ |
| 419 |
FetchUrlCall('GET', 200) |
| 420 |
.expect_basic_auth('test', 'hunter2') |
| 421 |
.expect_force_basic_auth(True) |
| 422 |
.result_json(STORAGEBOX_LEGACY_DETAIL_DATA[23]) |
| 423 |
.expect_url('{0}/storagebox/23'.format(BASE_URL)), |
| 424 |
FetchUrlCall('POST', 403) |
| 425 |
.result_json({ |
| 426 |
'error': { |
| 427 |
'status': 403, |
| 428 |
'code': 'RATE_LIMIT_EXCEEDED', |
| 429 |
'message': 'Rate limit exceeded', |
| 430 |
'interval': 5, |
| 431 |
'max_request': 1, |
| 432 |
}, |
| 433 |
}) |
| 434 |
.expect_form_value('storagebox_name', 'Backup') |
| 435 |
.expect_form_value_absent('webdav') |
| 436 |
.expect_form_value_absent('samba') |
| 437 |
.expect_form_value_absent('ssh') |
| 438 |
.expect_form_value_absent('external_reachability') |
| 439 |
.expect_form_value_absent('zfs') |
| 440 |
.expect_url('{0}/storagebox/23'.format(BASE_URL)), |
| 441 |
FetchUrlCall('POST', 403) |
| 442 |
.result_json({ |
| 443 |
'error': { |
| 444 |
'status': 403, |
| 445 |
'code': 'RATE_LIMIT_EXCEEDED', |
| 446 |
'message': 'Rate limit exceeded', |
| 447 |
'interval': 3, |
| 448 |
'max_request': 1, |
| 449 |
}, |
| 450 |
}) |
| 451 |
.expect_form_value('storagebox_name', 'Backup') |
| 452 |
.expect_form_value_absent('webdav') |
| 453 |
.expect_form_value_absent('samba') |
| 454 |
.expect_form_value_absent('ssh') |
| 455 |
.expect_form_value_absent('external_reachability') |
| 456 |
.expect_form_value_absent('zfs') |
| 457 |
.expect_url('{0}/storagebox/23'.format(BASE_URL)), |
| 458 |
FetchUrlCall('POST', 403) |
| 459 |
.result_json({ |
| 460 |
'error': { |
| 461 |
'status': 403, |
| 462 |
'code': 'RATE_LIMIT_EXCEEDED', |
| 463 |
'message': 'Rate limit exceeded', |
| 464 |
'interval': 4, |
| 465 |
'max_request': 1, |
| 466 |
}, |
| 467 |
}) |
| 468 |
.expect_form_value('storagebox_name', 'Backup') |
| 469 |
.expect_form_value_absent('webdav') |
| 470 |
.expect_form_value_absent('samba') |
| 471 |
.expect_form_value_absent('ssh') |
| 472 |
.expect_form_value_absent('external_reachability') |
| 473 |
.expect_form_value_absent('zfs') |
| 474 |
.expect_url('{0}/storagebox/23'.format(BASE_URL)), |
| 475 |
FetchUrlCall('POST', 403) |
| 476 |
.result_json({ |
| 477 |
'error': { |
| 478 |
'status': 403, |
| 479 |
'code': 'RATE_LIMIT_EXCEEDED', |
| 480 |
'message': 'Rate limit exceeded', |
| 481 |
}, |
| 482 |
}) |
| 483 |
.expect_form_value('storagebox_name', 'Backup') |
| 484 |
.expect_form_value_absent('webdav') |
| 485 |
.expect_form_value_absent('samba') |
| 486 |
.expect_form_value_absent('ssh') |
| 487 |
.expect_form_value_absent('external_reachability') |
| 488 |
.expect_form_value_absent('zfs') |
| 489 |
.expect_url('{0}/storagebox/23'.format(BASE_URL)), |
| 490 |
FetchUrlCall('POST', 200) |
| 491 |
.result_json(updated) |
| 492 |
.expect_form_value('storagebox_name', 'Backup') |
| 493 |
.expect_form_value_absent('webdav') |
| 494 |
.expect_form_value_absent('samba') |
| 495 |
.expect_form_value_absent('ssh') |
| 496 |
.expect_form_value_absent('external_reachability') |
| 497 |
.expect_form_value_absent('zfs') |
| 498 |
.expect_url('{0}/storagebox/23'.format(BASE_URL)), |
| 499 |
]) |
| 500 |
assert result['changed'] is True |
| 501 |
for key in STORAGEBOX_LEGACY_KEYS: |
| 502 |
assert result[key] == updated['storagebox'][key], "Unexpected difference for {0!r}".format(key) |
| 503 |
sleep_mock.assert_has_calls([ |
| 504 |
call(5), |
| 505 |
call(3), |
| 506 |
call(3), |
| 507 |
call(3), |
| 508 |
]) |
| 509 |
|
| 510 |
def test_change_name_rate_limit_timeout(self, mocker): |
| 511 |
elapsed = [123.4] |
| 512 |
|
| 513 |
def sleep(duration): |
| 514 |
elapsed[0] += duration |
| 515 |
print('sleep', duration, '->', elapsed[0]) |
| 516 |
|
| 517 |
def get_time(): |
| 518 |
elapsed[0] += 0.03 |
| 519 |
print('get', elapsed[0]) |
| 520 |
return elapsed[0] |
| 521 |
|
| 522 |
mocker.patch('time.sleep', sleep) |
| 523 |
mocker.patch('time.time', get_time) |
| 524 |
result = self.run_module_failed(mocker, storagebox, { |
| 525 |
'hetzner_user': 'test', |
| 526 |
'hetzner_password': 'hunter2', |
| 527 |
'id': 23, |
| 528 |
'name': 'Backup', |
| 529 |
'ssh': True, |
| 530 |
'rate_limit_retry_timeout': 7, |
| 531 |
}, [ |
| 532 |
FetchUrlCall('GET', 200) |
| 533 |
.expect_basic_auth('test', 'hunter2') |
| 534 |
.expect_force_basic_auth(True) |
| 535 |
.result_json(STORAGEBOX_LEGACY_DETAIL_DATA[23]) |
| 536 |
.expect_url('{0}/storagebox/23'.format(BASE_URL)), |
| 537 |
FetchUrlCall('POST', 403) |
| 538 |
.result_json({ |
| 539 |
'error': { |
| 540 |
'status': 403, |
| 541 |
'code': 'RATE_LIMIT_EXCEEDED', |
| 542 |
'message': 'Rate limit exceeded', |
| 543 |
'interval': 5, |
| 544 |
'max_request': 1, |
| 545 |
}, |
| 546 |
}) |
| 547 |
.expect_form_value('storagebox_name', 'Backup') |
| 548 |
.expect_form_value_absent('webdav') |
| 549 |
.expect_form_value_absent('samba') |
| 550 |
.expect_form_value_absent('ssh') |
| 551 |
.expect_form_value_absent('external_reachability') |
| 552 |
.expect_form_value_absent('zfs') |
| 553 |
.expect_url('{0}/storagebox/23'.format(BASE_URL)), |
| 554 |
FetchUrlCall('POST', 403) |
| 555 |
.result_json({ |
| 556 |
'error': { |
| 557 |
'status': 403, |
| 558 |
'code': 'RATE_LIMIT_EXCEEDED', |
| 559 |
'message': 'Rate limit exceeded', |
| 560 |
'interval': 5, |
| 561 |
'max_request': 1, |
| 562 |
}, |
| 563 |
}) |
| 564 |
.expect_form_value('storagebox_name', 'Backup') |
| 565 |
.expect_form_value_absent('webdav') |
| 566 |
.expect_form_value_absent('samba') |
| 567 |
.expect_form_value_absent('ssh') |
| 568 |
.expect_form_value_absent('external_reachability') |
| 569 |
.expect_form_value_absent('zfs') |
| 570 |
.expect_url('{0}/storagebox/23'.format(BASE_URL)), |
| 571 |
FetchUrlCall('POST', 403) |
| 572 |
.result_json({ |
| 573 |
'error': { |
| 574 |
'status': 403, |
| 575 |
'code': 'RATE_LIMIT_EXCEEDED', |
| 576 |
'message': 'Rate limit exceeded', |
| 577 |
'interval': 5, |
| 578 |
'max_request': 1, |
| 579 |
}, |
| 580 |
}) |
| 581 |
.expect_form_value('storagebox_name', 'Backup') |
| 582 |
.expect_form_value_absent('webdav') |
| 583 |
.expect_form_value_absent('samba') |
| 584 |
.expect_form_value_absent('ssh') |
| 585 |
.expect_form_value_absent('external_reachability') |
| 586 |
.expect_form_value_absent('zfs') |
| 587 |
.expect_url('{0}/storagebox/23'.format(BASE_URL)), |
| 588 |
]) |
| 589 |
assert result['msg'] == ( |
| 590 |
'Request failed: 403 RATE_LIMIT_EXCEEDED (Rate limit exceeded).' |
| 591 |
' Maximum allowed requests: 1. Time interval in seconds: 5.' |
| 592 |
' Waited a total of 5.1 seconds for rate limit errors to go away' |
| 593 |
) |
| 594 |
|
| 595 |
def test_change_ssh(self, mocker): |
| 596 |
updated = legacy_update_info(23, ssh=False) |
| 597 |
result = self.run_module_success(mocker, storagebox, { |
| 598 |
'hetzner_user': 'test', |
| 599 |
'hetzner_password': 'hunter2', |
| 600 |
'id': 23, |
| 601 |
'name': 'Backup Server 2', |
| 602 |
'ssh': False, |
| 603 |
}, [ |
| 604 |
FetchUrlCall('GET', 200) |
| 605 |
.expect_basic_auth('test', 'hunter2') |
| 606 |
.expect_force_basic_auth(True) |
| 607 |
.result_json(STORAGEBOX_LEGACY_DETAIL_DATA[23]) |
| 608 |
.expect_url('{0}/storagebox/23'.format(BASE_URL)), |
| 609 |
FetchUrlCall('POST', 200) |
| 610 |
.result_json(updated) |
| 611 |
.expect_form_value_absent('storagebox_name') |
| 612 |
.expect_form_value_absent('webdav') |
| 613 |
.expect_form_value_absent('samba') |
| 614 |
.expect_form_value('ssh', 'false') |
| 615 |
.expect_form_value_absent('external_reachability') |
| 616 |
.expect_form_value_absent('zfs') |
| 617 |
.expect_url('{0}/storagebox/23'.format(BASE_URL)), |
| 618 |
]) |
| 619 |
assert result['changed'] is True |
| 620 |
for key in STORAGEBOX_LEGACY_KEYS: |
| 621 |
assert result[key] == updated['storagebox'][key], "Unexpected difference for {0!r}".format(key) |
| 622 |
|
| 623 |
|
| 624 |
class TestHetznerStoragebox(BaseTestModule): |
| 625 |
MOCK_ANSIBLE_MODULEUTILS_BASIC_ANSIBLEMODULE = 'ansible_collections.community.hrobot.plugins.modules.storagebox.AnsibleModule' |
| 626 |
MOCK_ANSIBLE_MODULEUTILS_URLS_FETCH_URL = 'ansible_collections.community.hrobot.plugins.module_utils.api.fetch_url' |
| 627 |
|
| 628 |
def test_idempotent(self, mocker): |
| 629 |
updated = update_info(23) |
| 630 |
result = self.run_module_success(mocker, storagebox, { |
| 631 |
'hetzner_token': 'asdf', |
| 632 |
'id': 23, |
| 633 |
'name': 'Backup Server 2', |
| 634 |
'webdav': False, |
| 635 |
'samba': False, |
| 636 |
'ssh': True, |
| 637 |
'external_reachability': True, |
| 638 |
}, [ |
| 639 |
FetchUrlCall('GET', 200) |
| 640 |
.expect_header("Authorization", "Bearer asdf") |
| 641 |
.result_json({'storage_box': STORAGEBOX_DETAIL_DATA[23]}) |
| 642 |
.expect_url('{0}/v1/storage_boxes/23'.format(API_BASE_URL)), |
| 643 |
]) |
| 644 |
assert result['changed'] is False |
| 645 |
for key, keys in STORAGEBOX_KEYS.items(): |
| 646 |
assert result[key] == get_key(updated['storage_box'], keys), "Unexpected difference for {0!r}".format(key) |
| 647 |
|
| 648 |
def test_id_unknown(self, mocker): |
| 649 |
result = self.run_module_failed(mocker, storagebox, { |
| 650 |
'hetzner_token': '', |
| 651 |
'id': 1, |
| 652 |
}, [ |
| 653 |
FetchUrlCall('GET', 404) |
| 654 |
.expect_header("Authorization", "Bearer ") |
| 655 |
.result_json({ |
| 656 |
'error': { |
| 657 |
'code': 'not_found', |
| 658 |
'message': 'Storage Box not found', |
| 659 |
'details': {}, |
| 660 |
}, |
| 661 |
}) |
| 662 |
.expect_url('{0}/v1/storage_boxes/1'.format(API_BASE_URL)), |
| 663 |
]) |
| 664 |
assert result['msg'] == 'Storagebox with ID 1 does not exist' |
| 665 |
|
| 666 |
def test_invalid_input(self, mocker): |
| 667 |
result = self.run_module_failed(mocker, storagebox, { |
| 668 |
'hetzner_token': '', |
| 669 |
'id': 23, |
| 670 |
'name': 'Backup', |
| 671 |
}, [ |
| 672 |
FetchUrlCall('GET', 200) |
| 673 |
.result_json({'storage_box': STORAGEBOX_DETAIL_DATA[23]}) |
| 674 |
.expect_url('{0}/v1/storage_boxes/23'.format(API_BASE_URL)), |
| 675 |
FetchUrlCall('PUT', 400) |
| 676 |
.result_json({ |
| 677 |
'error': { |
| 678 |
'code': 'invalid_input', |
| 679 |
'message': "invalid input in field 'name': is too long", |
| 680 |
'details': { |
| 681 |
'fields': [ |
| 682 |
{ |
| 683 |
'name': 'name', |
| 684 |
'message': 'is too long', |
| 685 |
}, |
| 686 |
], |
| 687 |
}, |
| 688 |
}, |
| 689 |
}) |
| 690 |
.expect_json_value(['name'], 'Backup') |
| 691 |
.expect_json_value_absent(['access_settings']) |
| 692 |
.expect_json_value_absent(['webdav_enabled']) |
| 693 |
.expect_json_value_absent(['samba_enabled']) |
| 694 |
.expect_json_value_absent(['ssh_enabled']) |
| 695 |
.expect_json_value_absent(['zfs_enabled']) |
| 696 |
.expect_url('{0}/v1/storage_boxes/23'.format(API_BASE_URL)), |
| 697 |
]) |
| 698 |
assert result['msg'] == 'The values to update were invalid (name: is too long)' |
| 699 |
|
| 700 |
def test_change_name(self, mocker): |
| 701 |
updated = update_info(23, name='Backup') |
| 702 |
result = self.run_module_success(mocker, storagebox, { |
| 703 |
'hetzner_token': '', |
| 704 |
'id': 23, |
| 705 |
'name': 'Backup', |
| 706 |
'ssh': True, |
| 707 |
}, [ |
| 708 |
FetchUrlCall('GET', 200) |
| 709 |
.result_json({'storage_box': STORAGEBOX_DETAIL_DATA[23]}) |
| 710 |
.expect_url('{0}/v1/storage_boxes/23'.format(API_BASE_URL)), |
| 711 |
FetchUrlCall('PUT', 200) |
| 712 |
.result_json(updated) |
| 713 |
.expect_json_value(['name'], 'Backup') |
| 714 |
.expect_json_value_absent(['access_settings']) |
| 715 |
.expect_json_value_absent(['webdav_enabled']) |
| 716 |
.expect_json_value_absent(['samba_enabled']) |
| 717 |
.expect_json_value_absent(['ssh_enabled']) |
| 718 |
.expect_json_value_absent(['zfs_enabled']) |
| 719 |
.expect_url('{0}/v1/storage_boxes/23'.format(API_BASE_URL)), |
| 720 |
]) |
| 721 |
assert result['changed'] is True |
| 722 |
for key, keys in STORAGEBOX_KEYS.items(): |
| 723 |
assert result[key] == get_key(updated['storage_box'], keys), "Unexpected difference for {0!r}".format(key) |
| 724 |
|
| 725 |
def test_change_name_rate_limit_fail(self, mocker): |
| 726 |
result = self.run_module_failed(mocker, storagebox, { |
| 727 |
'hetzner_token': '', |
| 728 |
'id': 23, |
| 729 |
'name': 'Backup', |
| 730 |
'ssh': True, |
| 731 |
'rate_limit_retry_timeout': 0, |
| 732 |
}, [ |
| 733 |
FetchUrlCall('GET', 200) |
| 734 |
.result_json({'storage_box': STORAGEBOX_DETAIL_DATA[23]}) |
| 735 |
.expect_url('{0}/v1/storage_boxes/23'.format(API_BASE_URL)), |
| 736 |
FetchUrlCall('PUT', 429) |
| 737 |
.result_json({ |
| 738 |
'error': { |
| 739 |
'code': 'rate_limit_exceeded', |
| 740 |
'message': 'Rate limit exceeded', |
| 741 |
'details': {}, |
| 742 |
}, |
| 743 |
}) |
| 744 |
.expect_json_value(['name'], 'Backup') |
| 745 |
.expect_json_value_absent(['access_settings']) |
| 746 |
.expect_json_value_absent(['webdav_enabled']) |
| 747 |
.expect_json_value_absent(['samba_enabled']) |
| 748 |
.expect_json_value_absent(['ssh_enabled']) |
| 749 |
.expect_json_value_absent(['zfs_enabled']) |
| 750 |
.expect_url('{0}/v1/storage_boxes/23'.format(API_BASE_URL)), |
| 751 |
]) |
| 752 |
assert result['msg'] == 'Request PUT https://api.hetzner.com/v1/storage_boxes/23 failed: [rate_limit_exceeded] Rate limit exceeded' |
| 753 |
|
| 754 |
def test_change_name_rate_limit(self, mocker): |
| 755 |
sleep_mock = MagicMock() |
| 756 |
mocker.patch('time.sleep', sleep_mock) |
| 757 |
updated = update_info(23, name='Backup') |
| 758 |
result = self.run_module_success(mocker, storagebox, { |
| 759 |
'hetzner_token': '', |
| 760 |
'id': 23, |
| 761 |
'name': 'Backup', |
| 762 |
'ssh': True, |
| 763 |
'rate_limit_retry_timeout': -1, |
| 764 |
}, [ |
| 765 |
FetchUrlCall('GET', 200) |
| 766 |
.result_json({'storage_box': STORAGEBOX_DETAIL_DATA[23]}) |
| 767 |
.expect_url('{0}/v1/storage_boxes/23'.format(API_BASE_URL)), |
| 768 |
FetchUrlCall('PUT', 429) |
| 769 |
.result_json({ |
| 770 |
'error': { |
| 771 |
'code': 'rate_limit_exceeded', |
| 772 |
'message': 'Rate limit exceeded', |
| 773 |
'details': {}, |
| 774 |
}, |
| 775 |
}) |
| 776 |
.expect_json_value(['name'], 'Backup') |
| 777 |
.expect_json_value_absent(['access_settings']) |
| 778 |
.expect_json_value_absent(['webdav_enabled']) |
| 779 |
.expect_json_value_absent(['samba_enabled']) |
| 780 |
.expect_json_value_absent(['ssh_enabled']) |
| 781 |
.expect_json_value_absent(['zfs_enabled']) |
| 782 |
.expect_url('{0}/v1/storage_boxes/23'.format(API_BASE_URL)), |
| 783 |
FetchUrlCall('PUT', 429) |
| 784 |
.result_json({ |
| 785 |
'error': { |
| 786 |
'code': 'rate_limit_exceeded', |
| 787 |
'message': 'Rate limit exceeded', |
| 788 |
'details': {}, |
| 789 |
}, |
| 790 |
}) |
| 791 |
.expect_json_value(['name'], 'Backup') |
| 792 |
.expect_json_value_absent(['access_settings']) |
| 793 |
.expect_json_value_absent(['webdav_enabled']) |
| 794 |
.expect_json_value_absent(['samba_enabled']) |
| 795 |
.expect_json_value_absent(['ssh_enabled']) |
| 796 |
.expect_json_value_absent(['zfs_enabled']) |
| 797 |
.expect_url('{0}/v1/storage_boxes/23'.format(API_BASE_URL)), |
| 798 |
FetchUrlCall('PUT', 429) |
| 799 |
.result_json({ |
| 800 |
'error': { |
| 801 |
'code': 'rate_limit_exceeded', |
| 802 |
'message': 'Rate limit exceeded', |
| 803 |
'details': {}, |
| 804 |
}, |
| 805 |
}) |
| 806 |
.expect_json_value(['name'], 'Backup') |
| 807 |
.expect_json_value_absent(['access_settings']) |
| 808 |
.expect_json_value_absent(['webdav_enabled']) |
| 809 |
.expect_json_value_absent(['samba_enabled']) |
| 810 |
.expect_json_value_absent(['ssh_enabled']) |
| 811 |
.expect_json_value_absent(['zfs_enabled']) |
| 812 |
.expect_url('{0}/v1/storage_boxes/23'.format(API_BASE_URL)), |
| 813 |
FetchUrlCall('PUT', 429) |
| 814 |
.result_json({ |
| 815 |
'error': { |
| 816 |
'code': 'rate_limit_exceeded', |
| 817 |
'message': 'Rate limit exceeded', |
| 818 |
'details': {}, |
| 819 |
}, |
| 820 |
}) |
| 821 |
.expect_json_value(['name'], 'Backup') |
| 822 |
.expect_json_value_absent(['access_settings']) |
| 823 |
.expect_json_value_absent(['webdav_enabled']) |
| 824 |
.expect_json_value_absent(['samba_enabled']) |
| 825 |
.expect_json_value_absent(['ssh_enabled']) |
| 826 |
.expect_json_value_absent(['zfs_enabled']) |
| 827 |
.expect_url('{0}/v1/storage_boxes/23'.format(API_BASE_URL)), |
| 828 |
FetchUrlCall('PUT', 200) |
| 829 |
.result_json(updated) |
| 830 |
.expect_json_value(['name'], 'Backup') |
| 831 |
.expect_json_value_absent(['access_settings']) |
| 832 |
.expect_json_value_absent(['webdav_enabled']) |
| 833 |
.expect_json_value_absent(['samba_enabled']) |
| 834 |
.expect_json_value_absent(['ssh_enabled']) |
| 835 |
.expect_json_value_absent(['zfs_enabled']) |
| 836 |
.expect_url('{0}/v1/storage_boxes/23'.format(API_BASE_URL)), |
| 837 |
]) |
| 838 |
assert result['changed'] is True |
| 839 |
for key, keys in STORAGEBOX_KEYS.items(): |
| 840 |
assert result[key] == get_key(updated['storage_box'], keys), "Unexpected difference for {0!r}".format(key) |
| 841 |
sleep_mock.assert_has_calls([ |
| 842 |
call(5), |
| 843 |
call(5), |
| 844 |
call(5), |
| 845 |
call(5), |
| 846 |
]) |
| 847 |
|
| 848 |
def test_change_name_rate_limit_timeout(self, mocker): |
| 849 |
elapsed = [123.4] |
| 850 |
|
| 851 |
def sleep(duration): |
| 852 |
elapsed[0] += duration |
| 853 |
print('sleep', duration, '->', elapsed[0]) |
| 854 |
|
| 855 |
def get_time(): |
| 856 |
elapsed[0] += 0.03 |
| 857 |
print('get', elapsed[0]) |
| 858 |
return elapsed[0] |
| 859 |
|
| 860 |
mocker.patch('time.sleep', sleep) |
| 861 |
mocker.patch('time.time', get_time) |
| 862 |
result = self.run_module_failed(mocker, storagebox, { |
| 863 |
'hetzner_token': '', |
| 864 |
'id': 23, |
| 865 |
'name': 'Backup', |
| 866 |
'ssh': True, |
| 867 |
'rate_limit_retry_timeout': 7, |
| 868 |
}, [ |
| 869 |
FetchUrlCall('GET', 200) |
| 870 |
.result_json({'storage_box': STORAGEBOX_DETAIL_DATA[23]}) |
| 871 |
.expect_url('{0}/v1/storage_boxes/23'.format(API_BASE_URL)), |
| 872 |
FetchUrlCall('PUT', 429) |
| 873 |
.result_json({ |
| 874 |
'error': { |
| 875 |
'code': 'rate_limit_exceeded', |
| 876 |
'message': 'Rate limit exceeded', |
| 877 |
'details': {}, |
| 878 |
}, |
| 879 |
}) |
| 880 |
.expect_json_value(['name'], 'Backup') |
| 881 |
.expect_json_value_absent(['access_settings']) |
| 882 |
.expect_json_value_absent(['webdav_enabled']) |
| 883 |
.expect_json_value_absent(['samba_enabled']) |
| 884 |
.expect_json_value_absent(['ssh_enabled']) |
| 885 |
.expect_json_value_absent(['zfs_enabled']) |
| 886 |
.expect_url('{0}/v1/storage_boxes/23'.format(API_BASE_URL)), |
| 887 |
FetchUrlCall('PUT', 429) |
| 888 |
.result_json({ |
| 889 |
'error': { |
| 890 |
'code': 'rate_limit_exceeded', |
| 891 |
'message': 'Rate limit exceeded', |
| 892 |
'details': {}, |
| 893 |
}, |
| 894 |
}) |
| 895 |
.expect_json_value(['name'], 'Backup') |
| 896 |
.expect_json_value_absent(['access_settings']) |
| 897 |
.expect_json_value_absent(['webdav_enabled']) |
| 898 |
.expect_json_value_absent(['samba_enabled']) |
| 899 |
.expect_json_value_absent(['ssh_enabled']) |
| 900 |
.expect_json_value_absent(['zfs_enabled']) |
| 901 |
.expect_url('{0}/v1/storage_boxes/23'.format(API_BASE_URL)), |
| 902 |
FetchUrlCall('PUT', 429) |
| 903 |
.result_json({ |
| 904 |
'error': { |
| 905 |
'code': 'rate_limit_exceeded', |
| 906 |
'message': 'Rate limit exceeded', |
| 907 |
'details': {}, |
| 908 |
}, |
| 909 |
}) |
| 910 |
.expect_json_value(['name'], 'Backup') |
| 911 |
.expect_json_value_absent(['access_settings']) |
| 912 |
.expect_json_value_absent(['webdav_enabled']) |
| 913 |
.expect_json_value_absent(['samba_enabled']) |
| 914 |
.expect_json_value_absent(['ssh_enabled']) |
| 915 |
.expect_json_value_absent(['zfs_enabled']) |
| 916 |
.expect_url('{0}/v1/storage_boxes/23'.format(API_BASE_URL)), |
| 917 |
]) |
| 918 |
assert result['msg'] == 'Request PUT https://api.hetzner.com/v1/storage_boxes/23 failed: [rate_limit_exceeded] Rate limit exceeded' |
| 919 |
|
| 920 |
def test_change_ssh(self, mocker): |
| 921 |
sleep_mock = MagicMock() |
| 922 |
mocker.patch('time.sleep', sleep_mock) |
| 923 |
updated = update_info(23, ssh=False) |
| 924 |
result = self.run_module_success(mocker, storagebox, { |
| 925 |
'hetzner_token': '', |
| 926 |
'id': 23, |
| 927 |
'ssh': False, |
| 928 |
}, [ |
| 929 |
FetchUrlCall('GET', 200) |
| 930 |
.result_json({'storage_box': STORAGEBOX_DETAIL_DATA[23]}) |
| 931 |
.expect_url('{0}/v1/storage_boxes/23'.format(API_BASE_URL)), |
| 932 |
FetchUrlCall('POST', 200) |
| 933 |
.result_json({ |
| 934 |
"action": { |
| 935 |
"id": 13, |
| 936 |
"command": "update_access_settings", |
| 937 |
"status": "running", |
| 938 |
"progress": 0, |
| 939 |
"started": "2016-01-30T23:50:00+00:00", |
| 940 |
"finished": None, |
| 941 |
"resources": [ |
| 942 |
{ |
| 943 |
"id": 23, |
| 944 |
"type": "storage_box", |
| 945 |
}, |
| 946 |
], |
| 947 |
"error": None, |
| 948 |
} |
| 949 |
}) |
| 950 |
.expect_json_value_absent(['name']) |
| 951 |
.expect_json_value_absent(['access_settings']) |
| 952 |
.expect_json_value_absent(['webdav_enabled']) |
| 953 |
.expect_json_value_absent(['samba_enabled']) |
| 954 |
.expect_json_value(['ssh_enabled'], False) |
| 955 |
.expect_json_value_absent(['zfs_enabled']) |
| 956 |
.expect_url('{0}/v1/storage_boxes/23/actions/update_access_settings'.format(API_BASE_URL)), |
| 957 |
FetchUrlCall('GET', 200) |
| 958 |
.result_json({ |
| 959 |
"action": { |
| 960 |
"id": 13, |
| 961 |
"command": "update_access_settings", |
| 962 |
"status": "success", |
| 963 |
"progress": 0, |
| 964 |
"started": "2016-01-30T23:50:00+00:00", |
| 965 |
"finished": "2016-01-30T23:50:00+00:00", |
| 966 |
"resources": [ |
| 967 |
{ |
| 968 |
"id": 23, |
| 969 |
"type": "storage_box", |
| 970 |
}, |
| 971 |
], |
| 972 |
"error": None, |
| 973 |
} |
| 974 |
}) |
| 975 |
.expect_url('{0}/v1/storage_boxes/actions/13'.format(API_BASE_URL)), |
| 976 |
]) |
| 977 |
assert result['changed'] is True |
| 978 |
for key, keys in STORAGEBOX_KEYS.items(): |
| 979 |
assert result[key] == get_key(updated['storage_box'], keys), "Unexpected difference for {0!r}".format(key) |
| 980 |
sleep_mock.assert_has_calls([ |
| 981 |
call(1), |
| 982 |
]) |
| 983 |
|
| 984 |
def test_change_ssh_fail(self, mocker): |
| 985 |
sleep_mock = MagicMock() |
| 986 |
mocker.patch('time.sleep', sleep_mock) |
| 987 |
result = self.run_module_failed(mocker, storagebox, { |
| 988 |
'hetzner_token': '', |
| 989 |
'id': 23, |
| 990 |
'name': 'Backup Server 2', |
| 991 |
'ssh': False, |
| 992 |
}, [ |
| 993 |
FetchUrlCall('GET', 200) |
| 994 |
.result_json({'storage_box': STORAGEBOX_DETAIL_DATA[23]}) |
| 995 |
.expect_url('{0}/v1/storage_boxes/23'.format(API_BASE_URL)), |
| 996 |
FetchUrlCall('POST', 200) |
| 997 |
.result_json({ |
| 998 |
"action": { |
| 999 |
"id": 13, |
| 1000 |
"command": "update_access_settings", |
| 1001 |
"status": "running", |
| 1002 |
"progress": 0, |
| 1003 |
"started": "2016-01-30T23:50:00+00:00", |
| 1004 |
"finished": None, |
| 1005 |
"resources": [ |
| 1006 |
{ |
| 1007 |
"id": 23, |
| 1008 |
"type": "storage_box", |
| 1009 |
}, |
| 1010 |
], |
| 1011 |
"error": None, |
| 1012 |
} |
| 1013 |
}) |
| 1014 |
.expect_json_value_absent(['name']) |
| 1015 |
.expect_json_value_absent(['access_settings']) |
| 1016 |
.expect_json_value_absent(['webdav_enabled']) |
| 1017 |
.expect_json_value_absent(['samba_enabled']) |
| 1018 |
.expect_json_value(['ssh_enabled'], False) |
| 1019 |
.expect_json_value_absent(['zfs_enabled']) |
| 1020 |
.expect_url('{0}/v1/storage_boxes/23/actions/update_access_settings'.format(API_BASE_URL)), |
| 1021 |
FetchUrlCall('GET', 200) |
| 1022 |
.result_json({ |
| 1023 |
"action": { |
| 1024 |
"id": 13, |
| 1025 |
"command": "update_access_settings", |
| 1026 |
"status": "error", |
| 1027 |
"progress": 0, |
| 1028 |
"started": "2016-01-30T23:50:00+00:00", |
| 1029 |
"finished": "2016-01-30T23:50:00+00:00", |
| 1030 |
"resources": [ |
| 1031 |
{ |
| 1032 |
"id": 23, |
| 1033 |
"type": "storage_box", |
| 1034 |
}, |
| 1035 |
], |
| 1036 |
"error": { |
| 1037 |
"code": "action_failed", |
| 1038 |
"message": "Action failed", |
| 1039 |
}, |
| 1040 |
} |
| 1041 |
}) |
| 1042 |
.expect_url('{0}/v1/storage_boxes/actions/13'.format(API_BASE_URL)), |
| 1043 |
]) |
| 1044 |
assert result['msg'] == 'Error while updating access settings: [action_failed] Action failed' |
| 1045 |
sleep_mock.assert_has_calls([ |
| 1046 |
call(1), |
| 1047 |
]) |
| 1048 |
|
| 1049 |
def test_change_ssh_fail_unknown(self, mocker): |
| 1050 |
sleep_mock = MagicMock() |
| 1051 |
mocker.patch('time.sleep', sleep_mock) |
| 1052 |
result = self.run_module_failed(mocker, storagebox, { |
| 1053 |
'hetzner_token': '', |
| 1054 |
'id': 23, |
| 1055 |
'name': 'Backup Server 2', |
| 1056 |
'ssh': False, |
| 1057 |
}, [ |
| 1058 |
FetchUrlCall('GET', 200) |
| 1059 |
.result_json({'storage_box': STORAGEBOX_DETAIL_DATA[23]}) |
| 1060 |
.expect_url('{0}/v1/storage_boxes/23'.format(API_BASE_URL)), |
| 1061 |
FetchUrlCall('POST', 200) |
| 1062 |
.result_json({ |
| 1063 |
"action": { |
| 1064 |
"id": 13, |
| 1065 |
"command": "update_access_settings", |
| 1066 |
"status": "error", |
| 1067 |
"progress": 0, |
| 1068 |
"started": "2016-01-30T23:50:00+00:00", |
| 1069 |
"finished": None, |
| 1070 |
"resources": [ |
| 1071 |
{ |
| 1072 |
"id": 23, |
| 1073 |
"type": "storage_box", |
| 1074 |
}, |
| 1075 |
], |
| 1076 |
"error": None, |
| 1077 |
} |
| 1078 |
}) |
| 1079 |
.expect_json_value_absent(['name']) |
| 1080 |
.expect_json_value_absent(['access_settings']) |
| 1081 |
.expect_json_value_absent(['webdav_enabled']) |
| 1082 |
.expect_json_value_absent(['samba_enabled']) |
| 1083 |
.expect_json_value(['ssh_enabled'], False) |
| 1084 |
.expect_json_value_absent(['zfs_enabled']) |
| 1085 |
.expect_url('{0}/v1/storage_boxes/23/actions/update_access_settings'.format(API_BASE_URL)), |
| 1086 |
]) |
| 1087 |
assert result['msg'] == 'Error while updating access settings: Unknown error' |
| 1088 |
sleep_mock.assert_has_calls([]) |
| 1089 |
|
| 1090 |
def test_change_ssh_timeout(self, mocker): |
| 1091 |
elapsed = [123.4] |
| 1092 |
|
| 1093 |
def sleep(duration): |
| 1094 |
elapsed[0] += duration |
| 1095 |
print('sleep', duration, '->', elapsed[0]) |
| 1096 |
|
| 1097 |
def get_time(): |
| 1098 |
elapsed[0] += 0.03 |
| 1099 |
print('get', elapsed[0]) |
| 1100 |
return elapsed[0] |
| 1101 |
|
| 1102 |
mocker.patch('time.sleep', sleep) |
| 1103 |
mocker.patch('time.time', get_time) |
| 1104 |
updated = update_info(23, ssh=False) |
| 1105 |
unfinished_action = { |
| 1106 |
"action": { |
| 1107 |
"id": 13, |
| 1108 |
"command": "update_access_settings", |
| 1109 |
"status": "running", |
| 1110 |
"progress": 0, |
| 1111 |
"started": "2016-01-30T23:50:00+00:00", |
| 1112 |
"finished": None, |
| 1113 |
"resources": [ |
| 1114 |
{ |
| 1115 |
"id": 23, |
| 1116 |
"type": "storage_box", |
| 1117 |
}, |
| 1118 |
], |
| 1119 |
"error": None, |
| 1120 |
} |
| 1121 |
} |
| 1122 |
http_calls = [ |
| 1123 |
FetchUrlCall('GET', 200) |
| 1124 |
.result_json({'storage_box': STORAGEBOX_DETAIL_DATA[23]}) |
| 1125 |
.expect_url('{0}/v1/storage_boxes/23'.format(API_BASE_URL)), |
| 1126 |
FetchUrlCall('POST', 200) |
| 1127 |
.result_json(unfinished_action) |
| 1128 |
.expect_json_value_absent(['name']) |
| 1129 |
.expect_json_value_absent(['access_settings']) |
| 1130 |
.expect_json_value_absent(['webdav_enabled']) |
| 1131 |
.expect_json_value_absent(['samba_enabled']) |
| 1132 |
.expect_json_value(['ssh_enabled'], False) |
| 1133 |
.expect_json_value_absent(['zfs_enabled']) |
| 1134 |
.expect_url('{0}/v1/storage_boxes/23/actions/update_access_settings'.format(API_BASE_URL)), |
| 1135 |
] |
| 1136 |
for i in range(57): |
| 1137 |
http_calls.append( |
| 1138 |
FetchUrlCall('GET', 200) |
| 1139 |
.result_json(unfinished_action) |
| 1140 |
.expect_url('{0}/v1/storage_boxes/actions/13'.format(API_BASE_URL)) |
| 1141 |
) |
| 1142 |
result = self.run_module_failed(mocker, storagebox, { |
| 1143 |
'hetzner_token': '', |
| 1144 |
'id': 23, |
| 1145 |
'ssh': False, |
| 1146 |
}, http_calls) |
| 1147 |
assert result['msg'] == 'Error while updating access settings: Timeout' |
| 1148 |
|
| 1149 |
def test_change_name_and_ssh(self, mocker): |
| 1150 |
sleep_mock = MagicMock() |
| 1151 |
mocker.patch('time.sleep', sleep_mock) |
| 1152 |
updated = update_info(23, ssh=False, name="Backup") |
| 1153 |
updated_int = update_info(23, name="Backup") |
| 1154 |
result = self.run_module_success(mocker, storagebox, { |
| 1155 |
'hetzner_token': '', |
| 1156 |
'id': 23, |
| 1157 |
'name': 'Backup', |
| 1158 |
'ssh': False, |
| 1159 |
}, [ |
| 1160 |
FetchUrlCall('GET', 200) |
| 1161 |
.result_json({'storage_box': STORAGEBOX_DETAIL_DATA[23]}) |
| 1162 |
.expect_url('{0}/v1/storage_boxes/23'.format(API_BASE_URL)), |
| 1163 |
FetchUrlCall('PUT', 200) |
| 1164 |
.result_json(updated_int) |
| 1165 |
.expect_json_value(['name'], 'Backup') |
| 1166 |
.expect_json_value_absent(['access_settings']) |
| 1167 |
.expect_json_value_absent(['webdav_enabled']) |
| 1168 |
.expect_json_value_absent(['samba_enabled']) |
| 1169 |
.expect_json_value_absent(['ssh_enabled']) |
| 1170 |
.expect_json_value_absent(['zfs_enabled']) |
| 1171 |
.expect_url('{0}/v1/storage_boxes/23'.format(API_BASE_URL)), |
| 1172 |
FetchUrlCall('POST', 200) |
| 1173 |
.result_json({ |
| 1174 |
"action": { |
| 1175 |
"id": 13, |
| 1176 |
"command": "update_access_settings", |
| 1177 |
"status": "running", |
| 1178 |
"progress": 0, |
| 1179 |
"started": "2016-01-30T23:50:00+00:00", |
| 1180 |
"finished": None, |
| 1181 |
"resources": [ |
| 1182 |
{ |
| 1183 |
"id": 23, |
| 1184 |
"type": "storage_box", |
| 1185 |
}, |
| 1186 |
], |
| 1187 |
"error": None, |
| 1188 |
} |
| 1189 |
}) |
| 1190 |
.expect_json_value_absent(['name']) |
| 1191 |
.expect_json_value_absent(['access_settings']) |
| 1192 |
.expect_json_value_absent(['webdav_enabled']) |
| 1193 |
.expect_json_value_absent(['samba_enabled']) |
| 1194 |
.expect_json_value(['ssh_enabled'], False) |
| 1195 |
.expect_json_value_absent(['zfs_enabled']) |
| 1196 |
.expect_url('{0}/v1/storage_boxes/23/actions/update_access_settings'.format(API_BASE_URL)), |
| 1197 |
FetchUrlCall('GET', 200) |
| 1198 |
.result_json({ |
| 1199 |
"action": { |
| 1200 |
"id": 13, |
| 1201 |
"command": "update_access_settings", |
| 1202 |
"status": "running", |
| 1203 |
"progress": 0, |
| 1204 |
"started": "2016-01-30T23:50:00+00:00", |
| 1205 |
"finished": None, |
| 1206 |
"resources": [ |
| 1207 |
{ |
| 1208 |
"id": 23, |
| 1209 |
"type": "storage_box", |
| 1210 |
}, |
| 1211 |
], |
| 1212 |
"error": None, |
| 1213 |
} |
| 1214 |
}) |
| 1215 |
.expect_url('{0}/v1/storage_boxes/actions/13'.format(API_BASE_URL)), |
| 1216 |
FetchUrlCall('GET', 200) |
| 1217 |
.result_json({ |
| 1218 |
"action": { |
| 1219 |
"id": 13, |
| 1220 |
"command": "update_access_settings", |
| 1221 |
"status": "success", |
| 1222 |
"progress": 0, |
| 1223 |
"started": "2016-01-30T23:50:00+00:00", |
| 1224 |
"finished": "2016-01-30T23:50:00+00:00", |
| 1225 |
"resources": [ |
| 1226 |
{ |
| 1227 |
"id": 23, |
| 1228 |
"type": "storage_box", |
| 1229 |
}, |
| 1230 |
], |
| 1231 |
"error": None, |
| 1232 |
} |
| 1233 |
}) |
| 1234 |
.expect_url('{0}/v1/storage_boxes/actions/13'.format(API_BASE_URL)), |
| 1235 |
]) |
| 1236 |
assert result['changed'] is True |
| 1237 |
for key, keys in STORAGEBOX_KEYS.items(): |
| 1238 |
assert result[key] == get_key(updated['storage_box'], keys), "Unexpected difference for {0!r}".format(key) |
| 1239 |
sleep_mock.assert_has_calls([ |
| 1240 |
call(1), |
| 1241 |
call(1), |
| 1242 |
]) |