| 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 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_snapshot |
| 19 |
|
| 20 |
LEGACY_CREATED_SNAPSHOT = { |
| 21 |
'snapshot': { |
| 22 |
'name': '2025-03-28T15-20-51', |
| 23 |
'timestamp': '2025-03-28T16:20:51+01:00', |
| 24 |
'size': 0 |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
LEGACY_EXISTING_SNAPSHOTS = [ |
| 29 |
{ |
| 30 |
'snapshot': { |
| 31 |
'name': '2015-12-21T12-40-38', |
| 32 |
'timestamp': '2015-12-21T13:40:38+00:00', |
| 33 |
'size': 400, |
| 34 |
'filesystem_size': 12345, |
| 35 |
'automatic': False, |
| 36 |
'comment': 'Test-Snapshot 1' |
| 37 |
} |
| 38 |
}, |
| 39 |
{ |
| 40 |
'snapshot': { |
| 41 |
'name': '2025-03-28T15-20-51', |
| 42 |
'timestamp': '2025-03-28T15:19:30+00:00', |
| 43 |
'size': 10000, |
| 44 |
'filesystem_size': 22345, |
| 45 |
'automatic': False, |
| 46 |
'comment': 'Test-Snapshot 2' |
| 47 |
} |
| 48 |
} |
| 49 |
] |
| 50 |
|
| 51 |
|
| 52 |
STORAGEBOX_SNAPSHOTS = { |
| 53 |
1: { |
| 54 |
"id": 1, |
| 55 |
"stats": { |
| 56 |
"size": 2097152, |
| 57 |
"size_filesystem": 1048576 |
| 58 |
}, |
| 59 |
"is_automatic": False, |
| 60 |
"name": "2015-12-21T12-40-38", |
| 61 |
"description": "Test-Snapshot 1", |
| 62 |
"created": "2025-02-12T11:35:19.000Z", |
| 63 |
"storage_box": 23, |
| 64 |
"labels": {} |
| 65 |
}, |
| 66 |
2: { |
| 67 |
"id": 2, |
| 68 |
"stats": { |
| 69 |
"size": 2097152, |
| 70 |
"size_filesystem": 1048576 |
| 71 |
}, |
| 72 |
"is_automatic": False, |
| 73 |
"name": "2025-03-28T15-20-51", |
| 74 |
"description": "Test-Snapshot 2", |
| 75 |
"created": "2025-02-22:00:02.000Z", |
| 76 |
"storage_box": 23, |
| 77 |
"labels": {} |
| 78 |
}, |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
class TestHetznerStorageboxSnapshotPlanInfoLegacy(BaseTestModule): |
| 83 |
MOCK_ANSIBLE_MODULEUTILS_BASIC_ANSIBLEMODULE = 'ansible_collections.community.hrobot.plugins.modules.storagebox_snapshot.AnsibleModule' |
| 84 |
MOCK_ANSIBLE_MODULEUTILS_URLS_FETCH_URL = 'ansible_collections.community.hrobot.plugins.module_utils.robot.fetch_url' |
| 85 |
|
| 86 |
def test_create_snapshot(self, mocker): |
| 87 |
result = self.run_module_success(mocker, storagebox_snapshot, { |
| 88 |
'hetzner_user': 'test', |
| 89 |
'hetzner_password': 'hunter2', |
| 90 |
'storagebox_id': 23}, [ |
| 91 |
FetchUrlCall('POST', 200) |
| 92 |
.expect_basic_auth('test', 'hunter2') |
| 93 |
.expect_force_basic_auth(True) |
| 94 |
.result_json(LEGACY_CREATED_SNAPSHOT) |
| 95 |
.expect_url(BASE_URL + '/storagebox/23/snapshot') |
| 96 |
]) |
| 97 |
assert result['changed'] is True |
| 98 |
assert result['snapshot'] == LEGACY_CREATED_SNAPSHOT['snapshot'] |
| 99 |
|
| 100 |
def test_create_snapshot_check_mode(self, mocker): |
| 101 |
result = self.run_module_success(mocker, storagebox_snapshot, { |
| 102 |
'hetzner_user': 'test', |
| 103 |
'hetzner_password': 'hunter2', |
| 104 |
'storagebox_id': 23, |
| 105 |
'_ansible_check_mode': True}, [ |
| 106 |
]) |
| 107 |
assert result['changed'] is True |
| 108 |
|
| 109 |
def test_create_snapshot_with_comment(self, mocker): |
| 110 |
result = self.run_module_success(mocker, storagebox_snapshot, { |
| 111 |
'hetzner_user': 'test', |
| 112 |
'hetzner_password': 'hunter2', |
| 113 |
'storagebox_id': 23, |
| 114 |
'snapshot_comment': 'On Creation Comment'}, [ |
| 115 |
FetchUrlCall('POST', 200) |
| 116 |
.expect_basic_auth('test', 'hunter2') |
| 117 |
.expect_force_basic_auth(True) |
| 118 |
.result_json(LEGACY_CREATED_SNAPSHOT) |
| 119 |
.expect_url(BASE_URL + '/storagebox/23/snapshot'), |
| 120 |
FetchUrlCall("POST", 200) |
| 121 |
.expect_url('{0}/storagebox/23/snapshot/{1}/comment'.format(BASE_URL, LEGACY_CREATED_SNAPSHOT['snapshot']['name'])) |
| 122 |
.result_json({ |
| 123 |
'snapshot': { |
| 124 |
'name': '2025-03-28T15-20-51', |
| 125 |
'timestamp': '2025-03-28T16:20:51+01:00', |
| 126 |
'size': 0, |
| 127 |
'comment': 'On Creation Comment' |
| 128 |
}}) |
| 129 |
]) |
| 130 |
assert result['changed'] is True |
| 131 |
assert result['snapshot']['comment'] == 'On Creation Comment' |
| 132 |
|
| 133 |
def test_comment_snapshot(self, mocker): |
| 134 |
self.run_module_success(mocker, storagebox_snapshot, { |
| 135 |
'hetzner_user': 'test', |
| 136 |
'hetzner_password': 'hunter2', |
| 137 |
'storagebox_id': 23, |
| 138 |
'snapshot_name': '2025-03-28T15-20-51', |
| 139 |
'snapshot_comment': 'Changing Comment', |
| 140 |
}, [ |
| 141 |
FetchUrlCall('GET', 200) |
| 142 |
.result_json(LEGACY_EXISTING_SNAPSHOTS) |
| 143 |
.expect_url(BASE_URL + '/storagebox/23/snapshot'), |
| 144 |
FetchUrlCall('POST', 200) |
| 145 |
.expect_basic_auth('test', 'hunter2') |
| 146 |
.expect_force_basic_auth(True) |
| 147 |
.expect_url('{0}/storagebox/23/snapshot/{1}/comment'.format(BASE_URL, '2025-03-28T15-20-51')) |
| 148 |
]) |
| 149 |
|
| 150 |
def test_same_comment_snapshot(self, mocker): |
| 151 |
self.run_module_success(mocker, storagebox_snapshot, { |
| 152 |
'hetzner_user': 'test', |
| 153 |
'hetzner_password': 'hunter2', |
| 154 |
'storagebox_id': 23, |
| 155 |
'snapshot_name': '2025-03-28T15-20-51', |
| 156 |
'snapshot_comment': 'Test-Snapshot 2', |
| 157 |
}, [ |
| 158 |
FetchUrlCall('GET', 200) |
| 159 |
.result_json(LEGACY_EXISTING_SNAPSHOTS) |
| 160 |
.expect_url(BASE_URL + '/storagebox/23/snapshot') |
| 161 |
]) |
| 162 |
|
| 163 |
def test_comment_snapshot_check_mode(self, mocker): |
| 164 |
result = self.run_module_success(mocker, storagebox_snapshot, { |
| 165 |
'hetzner_user': 'test', |
| 166 |
'hetzner_password': 'hunter2', |
| 167 |
'storagebox_id': 23, |
| 168 |
'snapshot_name': '2025-03-28T15-20-51', |
| 169 |
'snapshot_comment': 'Changing Comment', |
| 170 |
'_ansible_check_mode': True |
| 171 |
}, [ |
| 172 |
FetchUrlCall('GET', 200) |
| 173 |
.result_json(LEGACY_EXISTING_SNAPSHOTS) |
| 174 |
.expect_url(BASE_URL + '/storagebox/23/snapshot'), |
| 175 |
]) |
| 176 |
assert result['changed'] is True |
| 177 |
|
| 178 |
def test_comment_snapshot_nonexistent_storagebox(self, mocker): |
| 179 |
self.run_module_failed(mocker, storagebox_snapshot, { |
| 180 |
'hetzner_user': 'test', |
| 181 |
'hetzner_password': 'hunter2', |
| 182 |
'storagebox_id': 54, |
| 183 |
'snapshot_name': '2025-03-28T15-20-51', |
| 184 |
'snapshot_comment': 'Changing Comment', |
| 185 |
}, [ |
| 186 |
FetchUrlCall('GET', 404) |
| 187 |
.result_json({ |
| 188 |
'error': { |
| 189 |
'status': 404, |
| 190 |
'code': 'STORAGEBOX_NOT_FOUND', |
| 191 |
'message': 'Storagebox with ID 54 does not exist', |
| 192 |
} |
| 193 |
}) |
| 194 |
.expect_url(BASE_URL + '/storagebox/54/snapshot') |
| 195 |
]) |
| 196 |
|
| 197 |
def test_delete_snapshot(self, mocker): |
| 198 |
self.run_module_success(mocker, storagebox_snapshot, { |
| 199 |
'hetzner_user': 'test', |
| 200 |
'hetzner_password': 'hunter2', |
| 201 |
'storagebox_id': 23, |
| 202 |
'snapshot_name': '2025-03-28T15-20-51', |
| 203 |
'state': 'absent' |
| 204 |
}, [ |
| 205 |
FetchUrlCall('GET', 200) |
| 206 |
.result_json(LEGACY_EXISTING_SNAPSHOTS) |
| 207 |
.expect_url(BASE_URL + '/storagebox/23/snapshot'), |
| 208 |
FetchUrlCall('DELETE', 200) |
| 209 |
.expect_basic_auth('test', 'hunter2') |
| 210 |
.expect_force_basic_auth(True) |
| 211 |
.expect_url('{0}/storagebox/23/snapshot/{1}'.format(BASE_URL, '2025-03-28T15-20-51')) |
| 212 |
]) |
| 213 |
|
| 214 |
def test_delete_snapshot_check_mode(self, mocker): |
| 215 |
result = self.run_module_success(mocker, storagebox_snapshot, { |
| 216 |
'hetzner_user': 'test', |
| 217 |
'hetzner_password': 'hunter2', |
| 218 |
'storagebox_id': 23, |
| 219 |
'snapshot_name': '2025-03-28T15-20-51', |
| 220 |
'state': 'absent', |
| 221 |
'_ansible_check_mode': True |
| 222 |
}, [ |
| 223 |
FetchUrlCall('GET', 200) |
| 224 |
.result_json(LEGACY_EXISTING_SNAPSHOTS) |
| 225 |
.expect_url(BASE_URL + '/storagebox/23/snapshot') |
| 226 |
]) |
| 227 |
assert result['changed'] is True |
| 228 |
|
| 229 |
def test_create_limit_exceeded(self, mocker): |
| 230 |
resutl = self.run_module_failed(mocker, storagebox_snapshot, { |
| 231 |
'hetzner_user': 'test', |
| 232 |
'hetzner_password': 'hunter2', |
| 233 |
'storagebox_id': 23, |
| 234 |
}, [ |
| 235 |
FetchUrlCall('POST', 409) |
| 236 |
.expect_basic_auth('test', 'hunter2') |
| 237 |
.expect_force_basic_auth(True) |
| 238 |
.expect_url('{0}/storagebox/23/snapshot'.format(BASE_URL)) |
| 239 |
]) |
| 240 |
resutl['msg'] == 'Snapshot limit exceeded' |
| 241 |
|
| 242 |
def test_create_with_state_present(self, mocker): |
| 243 |
result = self.run_module_success(mocker, storagebox_snapshot, { |
| 244 |
'hetzner_user': 'test', |
| 245 |
'hetzner_password': 'hunter2', |
| 246 |
'storagebox_id': 23, |
| 247 |
'state': 'present'}, [ |
| 248 |
FetchUrlCall('POST', 200) |
| 249 |
.expect_basic_auth('test', 'hunter2') |
| 250 |
.expect_force_basic_auth(True) |
| 251 |
.result_json(LEGACY_CREATED_SNAPSHOT) |
| 252 |
.expect_url(BASE_URL + '/storagebox/23/snapshot') |
| 253 |
]) |
| 254 |
assert result['changed'] is True |
| 255 |
assert result['snapshot'] == LEGACY_CREATED_SNAPSHOT['snapshot'] |
| 256 |
|
| 257 |
def test_delete_nonexistent_snapshot(self, mocker): |
| 258 |
result = self.run_module_success(mocker, storagebox_snapshot, { |
| 259 |
'hetzner_user': 'test', |
| 260 |
'hetzner_password': 'hunter2', |
| 261 |
'storagebox_id': 23, |
| 262 |
'snapshot_name': 'does-not-exist', |
| 263 |
'state': 'absent' |
| 264 |
}, [ |
| 265 |
FetchUrlCall("GET", 200) |
| 266 |
.expect_url(BASE_URL + '/storagebox/23/snapshot') |
| 267 |
.result_json(LEGACY_EXISTING_SNAPSHOTS) |
| 268 |
]) |
| 269 |
assert result['changed'] is False |
| 270 |
|
| 271 |
def test_delete_snapshot_nonexistent_storagebox(self, mocker): |
| 272 |
result = self.run_module_failed(mocker, storagebox_snapshot, { |
| 273 |
'hetzner_user': 'test', |
| 274 |
'hetzner_password': 'hunter2', |
| 275 |
'storagebox_id': 54, |
| 276 |
'snapshot_name': '2025-03-28T15-20-51', |
| 277 |
'state': 'absent' |
| 278 |
}, [ |
| 279 |
FetchUrlCall("GET", 404) |
| 280 |
.result_json({ |
| 281 |
'error': { |
| 282 |
'status': 404, |
| 283 |
'code': 'STORAGEBOX_NOT_FOUND', |
| 284 |
'message': 'Storagebox with ID 54 does not exist', |
| 285 |
} |
| 286 |
}) |
| 287 |
.expect_url(BASE_URL + '/storagebox/54/snapshot') |
| 288 |
]) |
| 289 |
assert result['msg'] == 'Storagebox with ID 54 does not exist' |
| 290 |
|
| 291 |
def test_storagebox_id_unknown(self, mocker): |
| 292 |
result = self.run_module_failed(mocker, storagebox_snapshot, { |
| 293 |
'hetzner_user': '', |
| 294 |
'hetzner_password': '', |
| 295 |
'storagebox_id': 54 |
| 296 |
}, [ |
| 297 |
FetchUrlCall('POST', 404) |
| 298 |
.result_json({ |
| 299 |
'error': { |
| 300 |
'status': 404, |
| 301 |
'code': 'STORAGEBOX_NOT_FOUND', |
| 302 |
'message': 'Storagebox with ID 54 does not exist', |
| 303 |
} |
| 304 |
}) |
| 305 |
.expect_url(BASE_URL + '/storagebox/54/snapshot') |
| 306 |
]) |
| 307 |
assert result['msg'] == 'Storagebox with ID 54 does not exist' |
| 308 |
|
| 309 |
def test_snapshot_name_unknown(self, mocker): |
| 310 |
result = self.run_module_failed(mocker, storagebox_snapshot, { |
| 311 |
'hetzner_user': '', |
| 312 |
'hetzner_password': '', |
| 313 |
'storagebox_id': 23, |
| 314 |
'snapshot_name': '2038-01-19T03:14:17', |
| 315 |
'snapshot_comment': 'Test comment' |
| 316 |
}, [ |
| 317 |
FetchUrlCall('GET', 200) |
| 318 |
.result_json(LEGACY_EXISTING_SNAPSHOTS) |
| 319 |
.expect_url(BASE_URL + '/storagebox/23/snapshot') |
| 320 |
]) |
| 321 |
assert result['msg'] == 'Snapshot with name 2038-01-19T03:14:17 does not exist' |
| 322 |
|
| 323 |
def test_snapshot_name_with_state_present(self, mocker): |
| 324 |
result = self.run_module_failed(mocker, storagebox_snapshot, { |
| 325 |
'hetzner_user': '', |
| 326 |
'hetzner_password': '', |
| 327 |
'storagebox_id': 23, |
| 328 |
'snapshot_name': '2038-01-19T03:14:17', |
| 329 |
'state': 'present' |
| 330 |
}, []) |
| 331 |
assert result['msg'] == "snapshot_comment is required when updating a snapshot" |
| 332 |
|
| 333 |
|
| 334 |
class TestHetznerStorageboxSnapshotPlanInfo(BaseTestModule): |
| 335 |
MOCK_ANSIBLE_MODULEUTILS_BASIC_ANSIBLEMODULE = 'ansible_collections.community.hrobot.plugins.modules.storagebox_snapshot.AnsibleModule' |
| 336 |
MOCK_ANSIBLE_MODULEUTILS_URLS_FETCH_URL = 'ansible_collections.community.hrobot.plugins.module_utils.api.fetch_url' |
| 337 |
|
| 338 |
def test_create_snapshot(self, mocker): |
| 339 |
sleep_mock = MagicMock() |
| 340 |
mocker.patch('time.sleep', sleep_mock) |
| 341 |
result = self.run_module_success(mocker, storagebox_snapshot, { |
| 342 |
'hetzner_token': 'asdf', |
| 343 |
'storagebox_id': 23, |
| 344 |
}, [ |
| 345 |
FetchUrlCall('POST', 200) |
| 346 |
.expect_json_value_absent(['description']) |
| 347 |
.result_json({ |
| 348 |
"snapshot": { |
| 349 |
"id": 2, |
| 350 |
"storage_box": 23, |
| 351 |
}, |
| 352 |
"action": { |
| 353 |
"id": 13, |
| 354 |
"command": "create_snapshot", |
| 355 |
"status": "running", |
| 356 |
"progress": 0, |
| 357 |
"started": "2016-01-30T23:50:00+00:00", |
| 358 |
"finished": None, |
| 359 |
"resources": [ |
| 360 |
{ |
| 361 |
"id": 23, |
| 362 |
"type": "storage_box", |
| 363 |
}, |
| 364 |
{ |
| 365 |
"id": 2, |
| 366 |
"type": "storage_box_snapshot", |
| 367 |
}, |
| 368 |
], |
| 369 |
"error": None, |
| 370 |
} |
| 371 |
}) |
| 372 |
.expect_url('{0}/v1/storage_boxes/23/snapshots'.format(API_BASE_URL)), |
| 373 |
FetchUrlCall('GET', 200) |
| 374 |
.result_json({ |
| 375 |
"action": { |
| 376 |
"id": 13, |
| 377 |
"command": "update_access_settings", |
| 378 |
"status": "success", |
| 379 |
"progress": 0, |
| 380 |
"started": "2016-01-30T23:50:00+00:00", |
| 381 |
"finished": "2016-01-30T23:50:00+00:00", |
| 382 |
"resources": [ |
| 383 |
{ |
| 384 |
"id": 23, |
| 385 |
"type": "storage_box", |
| 386 |
}, |
| 387 |
{ |
| 388 |
"id": 2, |
| 389 |
"type": "storage_box_snapshot", |
| 390 |
}, |
| 391 |
], |
| 392 |
"error": None, |
| 393 |
} |
| 394 |
}) |
| 395 |
.expect_url('{0}/v1/storage_boxes/actions/13'.format(API_BASE_URL)), |
| 396 |
FetchUrlCall('GET', 200) |
| 397 |
.result_json({ |
| 398 |
"snapshot": STORAGEBOX_SNAPSHOTS[2], |
| 399 |
}) |
| 400 |
.expect_url('{0}/v1/storage_boxes/23/snapshots/2'.format(API_BASE_URL)), |
| 401 |
]) |
| 402 |
assert result['changed'] is True |
| 403 |
assert result['snapshot'] == { |
| 404 |
'comment': 'Test-Snapshot 2', |
| 405 |
'filesystem_size': 1, |
| 406 |
'id': 2, |
| 407 |
'name': '2025-03-28T15-20-51', |
| 408 |
'size': 2, |
| 409 |
'timestamp': '2025-02-22:00:02.000Z', |
| 410 |
} |
| 411 |
|
| 412 |
def test_create_snapshot_check_mode(self, mocker): |
| 413 |
result = self.run_module_success(mocker, storagebox_snapshot, { |
| 414 |
'hetzner_token': '', |
| 415 |
'storagebox_id': 23, |
| 416 |
'_ansible_check_mode': True}, [ |
| 417 |
]) |
| 418 |
assert result['changed'] is True |
| 419 |
|
| 420 |
def test_create_snapshot_with_comment(self, mocker): |
| 421 |
sleep_mock = MagicMock() |
| 422 |
mocker.patch('time.sleep', sleep_mock) |
| 423 |
result = self.run_module_success(mocker, storagebox_snapshot, { |
| 424 |
'hetzner_token': '', |
| 425 |
'storagebox_id': 23, |
| 426 |
'snapshot_comment': 'On Creation Comment', |
| 427 |
}, [ |
| 428 |
FetchUrlCall('POST', 200) |
| 429 |
.expect_json_value(['description'], 'On Creation Comment') |
| 430 |
.result_json({ |
| 431 |
"snapshot": { |
| 432 |
"id": 1, |
| 433 |
"storage_box": 23, |
| 434 |
}, |
| 435 |
"action": { |
| 436 |
"id": 13, |
| 437 |
"command": "create_snapshot", |
| 438 |
"status": "running", |
| 439 |
"progress": 0, |
| 440 |
"started": "2016-01-30T23:50:00+00:00", |
| 441 |
"finished": None, |
| 442 |
"resources": [ |
| 443 |
{ |
| 444 |
"id": 23, |
| 445 |
"type": "storage_box", |
| 446 |
}, |
| 447 |
{ |
| 448 |
"id": 1, |
| 449 |
"type": "storage_box_snapshot", |
| 450 |
}, |
| 451 |
], |
| 452 |
"error": None, |
| 453 |
} |
| 454 |
}) |
| 455 |
.expect_url('{0}/v1/storage_boxes/23/snapshots'.format(API_BASE_URL)), |
| 456 |
FetchUrlCall('GET', 200) |
| 457 |
.result_json({ |
| 458 |
"action": { |
| 459 |
"id": 13, |
| 460 |
"command": "update_access_settings", |
| 461 |
"status": "success", |
| 462 |
"progress": 0, |
| 463 |
"started": "2016-01-30T23:50:00+00:00", |
| 464 |
"finished": "2016-01-30T23:50:00+00:00", |
| 465 |
"resources": [ |
| 466 |
{ |
| 467 |
"id": 23, |
| 468 |
"type": "storage_box", |
| 469 |
}, |
| 470 |
{ |
| 471 |
"id": 1, |
| 472 |
"type": "storage_box_snapshot", |
| 473 |
}, |
| 474 |
], |
| 475 |
"error": None, |
| 476 |
} |
| 477 |
}) |
| 478 |
.expect_url('{0}/v1/storage_boxes/actions/13'.format(API_BASE_URL)), |
| 479 |
FetchUrlCall('GET', 200) |
| 480 |
.result_json({ |
| 481 |
"snapshot": STORAGEBOX_SNAPSHOTS[1], |
| 482 |
}) |
| 483 |
.expect_url('{0}/v1/storage_boxes/23/snapshots/1'.format(API_BASE_URL)), |
| 484 |
]) |
| 485 |
assert result['changed'] is True |
| 486 |
assert result['snapshot'] == { |
| 487 |
'comment': 'Test-Snapshot 1', |
| 488 |
'filesystem_size': 1, |
| 489 |
'id': 1, |
| 490 |
'name': '2015-12-21T12-40-38', |
| 491 |
'size': 2, |
| 492 |
'timestamp': '2025-02-12T11:35:19.000Z', |
| 493 |
} |
| 494 |
|
| 495 |
def test_create_snapshot_failed(self, mocker): |
| 496 |
sleep_mock = MagicMock() |
| 497 |
mocker.patch('time.sleep', sleep_mock) |
| 498 |
result = self.run_module_failed(mocker, storagebox_snapshot, { |
| 499 |
'hetzner_token': 'asdf', |
| 500 |
'storagebox_id': 23, |
| 501 |
}, [ |
| 502 |
FetchUrlCall('POST', 200) |
| 503 |
.expect_json_value_absent(['description']) |
| 504 |
.result_json({ |
| 505 |
"snapshot": { |
| 506 |
"id": 42, |
| 507 |
"storage_box": 23, |
| 508 |
}, |
| 509 |
"action": { |
| 510 |
"id": 13, |
| 511 |
"command": "create_snapshot", |
| 512 |
"status": "running", |
| 513 |
"progress": 0, |
| 514 |
"started": "2016-01-30T23:50:00+00:00", |
| 515 |
"finished": None, |
| 516 |
"resources": [ |
| 517 |
{ |
| 518 |
"id": 23, |
| 519 |
"type": "storage_box", |
| 520 |
}, |
| 521 |
], |
| 522 |
"error": None, |
| 523 |
} |
| 524 |
}) |
| 525 |
.expect_url('{0}/v1/storage_boxes/23/snapshots'.format(API_BASE_URL)), |
| 526 |
FetchUrlCall('GET', 200) |
| 527 |
.result_json({ |
| 528 |
"action": { |
| 529 |
"id": 13, |
| 530 |
"command": "update_access_settings", |
| 531 |
"status": "error", |
| 532 |
"progress": 0, |
| 533 |
"started": "2016-01-30T23:50:00+00:00", |
| 534 |
"finished": "2016-01-30T23:50:00+00:00", |
| 535 |
"resources": [ |
| 536 |
{ |
| 537 |
"id": 23, |
| 538 |
"type": "storage_box", |
| 539 |
}, |
| 540 |
{ |
| 541 |
"id": 42, |
| 542 |
"type": "storage_box_snapshot", |
| 543 |
}, |
| 544 |
], |
| 545 |
"error": { |
| 546 |
"code": "action_failed", |
| 547 |
"message": "Action failed", |
| 548 |
}, |
| 549 |
} |
| 550 |
}) |
| 551 |
.expect_url('{0}/v1/storage_boxes/actions/13'.format(API_BASE_URL)), |
| 552 |
]) |
| 553 |
assert result['msg'] == 'Error while creating snapshot: [action_failed] Action failed' |
| 554 |
|
| 555 |
def test_comment_snapshot(self, mocker): |
| 556 |
updated = dict(STORAGEBOX_SNAPSHOTS[2]) |
| 557 |
updated['description'] = 'Changing Comment' |
| 558 |
result = self.run_module_success(mocker, storagebox_snapshot, { |
| 559 |
'hetzner_token': '', |
| 560 |
'storagebox_id': 23, |
| 561 |
'snapshot_name': '2025-03-28T15-20-51', |
| 562 |
'snapshot_comment': 'Changing Comment', |
| 563 |
}, [ |
| 564 |
FetchUrlCall('GET', 200) |
| 565 |
.result_json({ |
| 566 |
"snapshots": [ |
| 567 |
STORAGEBOX_SNAPSHOTS[2], |
| 568 |
], |
| 569 |
}) |
| 570 |
.expect_url('{0}/v1/storage_boxes/23/snapshots?name=2025-03-28T15-20-51'.format(API_BASE_URL)), |
| 571 |
FetchUrlCall('PUT', 200) |
| 572 |
.result_json({ |
| 573 |
"snapshot": updated, |
| 574 |
}) |
| 575 |
.expect_json_value(['description'], 'Changing Comment') |
| 576 |
.expect_url('{0}/v1/storage_boxes/23/snapshots/2'.format(API_BASE_URL)), |
| 577 |
]) |
| 578 |
assert result['snapshot']['comment'] == 'Changing Comment' |
| 579 |
|
| 580 |
def test_same_comment_snapshot(self, mocker): |
| 581 |
self.run_module_success(mocker, storagebox_snapshot, { |
| 582 |
'hetzner_token': '', |
| 583 |
'storagebox_id': 23, |
| 584 |
'snapshot_name': '2025-03-28T15-20-51', |
| 585 |
'snapshot_comment': 'Test-Snapshot 2', |
| 586 |
}, [ |
| 587 |
FetchUrlCall('GET', 200) |
| 588 |
.result_json({ |
| 589 |
"snapshots": [ |
| 590 |
STORAGEBOX_SNAPSHOTS[2], |
| 591 |
], |
| 592 |
}) |
| 593 |
.expect_url('{0}/v1/storage_boxes/23/snapshots?name=2025-03-28T15-20-51'.format(API_BASE_URL)), |
| 594 |
]) |
| 595 |
|
| 596 |
def test_comment_snapshot_check_mode(self, mocker): |
| 597 |
result = self.run_module_success(mocker, storagebox_snapshot, { |
| 598 |
'hetzner_token': '', |
| 599 |
'storagebox_id': 23, |
| 600 |
'snapshot_name': '2025-03-28T15-20-51', |
| 601 |
'snapshot_comment': 'Changing Comment', |
| 602 |
'_ansible_check_mode': True |
| 603 |
}, [ |
| 604 |
FetchUrlCall('GET', 200) |
| 605 |
.result_json({ |
| 606 |
"snapshots": [ |
| 607 |
STORAGEBOX_SNAPSHOTS[2], |
| 608 |
], |
| 609 |
}) |
| 610 |
.expect_url('{0}/v1/storage_boxes/23/snapshots?name=2025-03-28T15-20-51'.format(API_BASE_URL)), |
| 611 |
]) |
| 612 |
assert result['changed'] is True |
| 613 |
|
| 614 |
def test_comment_snapshot_nonexistent_storagebox(self, mocker): |
| 615 |
self.run_module_failed(mocker, storagebox_snapshot, { |
| 616 |
'hetzner_token': '', |
| 617 |
'storagebox_id': 54, |
| 618 |
'snapshot_name': '2025-03-28T15-20-51', |
| 619 |
'snapshot_comment': 'Changing Comment', |
| 620 |
}, [ |
| 621 |
FetchUrlCall('GET', 404) |
| 622 |
.result_json({ |
| 623 |
"error": { |
| 624 |
'code': 'not_found', |
| 625 |
'message': 'Storage Box not found', |
| 626 |
'details': {}, |
| 627 |
}, |
| 628 |
}) |
| 629 |
.expect_url('{0}/v1/storage_boxes/54/snapshots?name=2025-03-28T15-20-51'.format(API_BASE_URL)), |
| 630 |
]) |
| 631 |
|
| 632 |
def test_delete_snapshot(self, mocker): |
| 633 |
sleep_mock = MagicMock() |
| 634 |
mocker.patch('time.sleep', sleep_mock) |
| 635 |
self.run_module_success(mocker, storagebox_snapshot, { |
| 636 |
'hetzner_token': '', |
| 637 |
'storagebox_id': 23, |
| 638 |
'snapshot_name': '2025-03-28T15-20-51', |
| 639 |
'state': 'absent' |
| 640 |
}, [ |
| 641 |
FetchUrlCall('GET', 200) |
| 642 |
.result_json({ |
| 643 |
"snapshots": [ |
| 644 |
STORAGEBOX_SNAPSHOTS[2], |
| 645 |
], |
| 646 |
}) |
| 647 |
.expect_url('{0}/v1/storage_boxes/23/snapshots?name=2025-03-28T15-20-51'.format(API_BASE_URL)), |
| 648 |
FetchUrlCall('DELETE', 200) |
| 649 |
.result_json({ |
| 650 |
"action": { |
| 651 |
"id": 13, |
| 652 |
"command": "delete_snapshot", |
| 653 |
"status": "running", |
| 654 |
"progress": 0, |
| 655 |
"started": "2016-01-30T23:50:00+00:00", |
| 656 |
"finished": None, |
| 657 |
"resources": [ |
| 658 |
{ |
| 659 |
"id": 23, |
| 660 |
"type": "storage_box", |
| 661 |
}, |
| 662 |
{ |
| 663 |
"id": 2, |
| 664 |
"type": "storage_box_snapshot", |
| 665 |
}, |
| 666 |
], |
| 667 |
"error": None, |
| 668 |
} |
| 669 |
}) |
| 670 |
.expect_url('{0}/v1/storage_boxes/23/snapshots/2'.format(API_BASE_URL)), |
| 671 |
FetchUrlCall('GET', 200) |
| 672 |
.result_json({ |
| 673 |
"action": { |
| 674 |
"id": 13, |
| 675 |
"command": "delete_snapshot", |
| 676 |
"status": "success", |
| 677 |
"progress": 0, |
| 678 |
"started": "2016-01-30T23:50:00+00:00", |
| 679 |
"finished": "2016-01-30T23:50:00+00:00", |
| 680 |
"resources": [ |
| 681 |
{ |
| 682 |
"id": 23, |
| 683 |
"type": "storage_box", |
| 684 |
}, |
| 685 |
{ |
| 686 |
"id": 2, |
| 687 |
"type": "storage_box_snapshot", |
| 688 |
}, |
| 689 |
], |
| 690 |
"error": None, |
| 691 |
} |
| 692 |
}) |
| 693 |
.expect_url('{0}/v1/storage_boxes/actions/13'.format(API_BASE_URL)), |
| 694 |
]) |
| 695 |
|
| 696 |
def test_delete_snapshot_check_mode(self, mocker): |
| 697 |
result = self.run_module_success(mocker, storagebox_snapshot, { |
| 698 |
'hetzner_token': '', |
| 699 |
'storagebox_id': 23, |
| 700 |
'snapshot_name': '2025-03-28T15-20-51', |
| 701 |
'state': 'absent', |
| 702 |
'_ansible_check_mode': True |
| 703 |
}, [ |
| 704 |
FetchUrlCall('GET', 200) |
| 705 |
.result_json({ |
| 706 |
"snapshots": [ |
| 707 |
STORAGEBOX_SNAPSHOTS[2], |
| 708 |
], |
| 709 |
}) |
| 710 |
.expect_url('{0}/v1/storage_boxes/23/snapshots?name=2025-03-28T15-20-51'.format(API_BASE_URL)), |
| 711 |
]) |
| 712 |
assert result['changed'] is True |
| 713 |
|
| 714 |
def test_delete_snapshot_failed(self, mocker): |
| 715 |
sleep_mock = MagicMock() |
| 716 |
mocker.patch('time.sleep', sleep_mock) |
| 717 |
result = self.run_module_failed(mocker, storagebox_snapshot, { |
| 718 |
'hetzner_token': '', |
| 719 |
'storagebox_id': 23, |
| 720 |
'snapshot_name': '2025-03-28T15-20-51', |
| 721 |
'state': 'absent' |
| 722 |
}, [ |
| 723 |
FetchUrlCall('GET', 200) |
| 724 |
.result_json({ |
| 725 |
"snapshots": [ |
| 726 |
STORAGEBOX_SNAPSHOTS[2], |
| 727 |
], |
| 728 |
}) |
| 729 |
.expect_url('{0}/v1/storage_boxes/23/snapshots?name=2025-03-28T15-20-51'.format(API_BASE_URL)), |
| 730 |
FetchUrlCall('DELETE', 200) |
| 731 |
.result_json({ |
| 732 |
"action": { |
| 733 |
"id": 13, |
| 734 |
"command": "delete_snapshot", |
| 735 |
"status": "running", |
| 736 |
"progress": 0, |
| 737 |
"started": "2016-01-30T23:50:00+00:00", |
| 738 |
"finished": None, |
| 739 |
"resources": [ |
| 740 |
{ |
| 741 |
"id": 23, |
| 742 |
"type": "storage_box", |
| 743 |
}, |
| 744 |
{ |
| 745 |
"id": 2, |
| 746 |
"type": "storage_box_snapshot", |
| 747 |
}, |
| 748 |
], |
| 749 |
"error": None, |
| 750 |
} |
| 751 |
}) |
| 752 |
.expect_url('{0}/v1/storage_boxes/23/snapshots/2'.format(API_BASE_URL)), |
| 753 |
FetchUrlCall('GET', 200) |
| 754 |
.result_json({ |
| 755 |
"action": { |
| 756 |
"id": 13, |
| 757 |
"command": "delete_snapshot", |
| 758 |
"status": "error", |
| 759 |
"progress": 0, |
| 760 |
"started": "2016-01-30T23:50:00+00:00", |
| 761 |
"finished": "2016-01-30T23:50:00+00:00", |
| 762 |
"resources": [ |
| 763 |
{ |
| 764 |
"id": 23, |
| 765 |
"type": "storage_box", |
| 766 |
}, |
| 767 |
{ |
| 768 |
"id": 2, |
| 769 |
"type": "storage_box_snapshot", |
| 770 |
}, |
| 771 |
], |
| 772 |
"error": { |
| 773 |
"code": "action_failed", |
| 774 |
"message": "Action failed", |
| 775 |
}, |
| 776 |
} |
| 777 |
}) |
| 778 |
.expect_url('{0}/v1/storage_boxes/actions/13'.format(API_BASE_URL)), |
| 779 |
]) |
| 780 |
assert result['msg'] == 'Error while deleting snapshot: [action_failed] Action failed' |
| 781 |
|
| 782 |
def test_create_limit_exceeded(self, mocker): |
| 783 |
result = self.run_module_failed(mocker, storagebox_snapshot, { |
| 784 |
'hetzner_token': '', |
| 785 |
'storagebox_id': 23, |
| 786 |
}, [ |
| 787 |
FetchUrlCall('POST', 400) |
| 788 |
.expect_json_value_absent(['description']) |
| 789 |
.result_json({ |
| 790 |
"error": { |
| 791 |
"code": "resource_limit_exceeded", |
| 792 |
"message": "snapshot limit exceeded", |
| 793 |
"details": { |
| 794 |
"limits": [ |
| 795 |
{ |
| 796 |
"name": "snapshot_limit" |
| 797 |
}, |
| 798 |
], |
| 799 |
}, |
| 800 |
}, |
| 801 |
}) |
| 802 |
.expect_url('{0}/v1/storage_boxes/23/snapshots'.format(API_BASE_URL)), |
| 803 |
]) |
| 804 |
assert result['msg'] in ( |
| 805 |
"Request POST https://api.hetzner.com/v1/storage_boxes/23/snapshots failed:" |
| 806 |
" [resource_limit_exceeded] snapshot limit exceeded. Details: {'limits': [{'name': 'snapshot_limit'}]}", |
| 807 |
"Request POST https://api.hetzner.com/v1/storage_boxes/23/snapshots failed:" |
| 808 |
" [resource_limit_exceeded] snapshot limit exceeded. Details: {u'limits': [{u'name': u'snapshot_limit'}]}", |
| 809 |
) |
| 810 |
|
| 811 |
def test_delete_nonexistent_snapshot(self, mocker): |
| 812 |
result = self.run_module_success(mocker, storagebox_snapshot, { |
| 813 |
'hetzner_token': '', |
| 814 |
'storagebox_id': 23, |
| 815 |
'snapshot_name': 'does not exist', |
| 816 |
'state': 'absent' |
| 817 |
}, [ |
| 818 |
FetchUrlCall('GET', 200) |
| 819 |
.result_json({ |
| 820 |
"snapshots": [ |
| 821 |
STORAGEBOX_SNAPSHOTS[1], |
| 822 |
], |
| 823 |
}) |
| 824 |
.expect_url('{0}/v1/storage_boxes/23/snapshots?name=does+not+exist'.format(API_BASE_URL)), |
| 825 |
]) |
| 826 |
assert result['changed'] is False |
| 827 |
|
| 828 |
def test_delete_snapshot_nonexistent_storagebox(self, mocker): |
| 829 |
result = self.run_module_failed(mocker, storagebox_snapshot, { |
| 830 |
'hetzner_token': '', |
| 831 |
'storagebox_id': 54, |
| 832 |
'snapshot_name': '2025-03-28T15-20-51', |
| 833 |
'state': 'absent' |
| 834 |
}, [ |
| 835 |
FetchUrlCall('GET', 404) |
| 836 |
.result_json({ |
| 837 |
"error": { |
| 838 |
'code': 'not_found', |
| 839 |
'message': 'Storage Box not found', |
| 840 |
'details': {}, |
| 841 |
}, |
| 842 |
}) |
| 843 |
.expect_url('{0}/v1/storage_boxes/54/snapshots?name=2025-03-28T15-20-51'.format(API_BASE_URL)), |
| 844 |
]) |
| 845 |
assert result['msg'] == 'Storagebox with ID 54 does not exist' |
| 846 |
|
| 847 |
def test_storagebox_id_unknown(self, mocker): |
| 848 |
result = self.run_module_failed(mocker, storagebox_snapshot, { |
| 849 |
'hetzner_token': '', |
| 850 |
'storagebox_id': 54 |
| 851 |
}, [ |
| 852 |
FetchUrlCall('POST', 404) |
| 853 |
.result_json({ |
| 854 |
"error": { |
| 855 |
'code': 'not_found', |
| 856 |
'message': 'Storage Box not found', |
| 857 |
'details': {}, |
| 858 |
}, |
| 859 |
}) |
| 860 |
.expect_url('{0}/v1/storage_boxes/54/snapshots'.format(API_BASE_URL)), |
| 861 |
]) |
| 862 |
assert result['msg'] == 'Storagebox with ID 54 does not exist' |
| 863 |
|
| 864 |
def test_snapshot_name_unknown(self, mocker): |
| 865 |
result = self.run_module_failed(mocker, storagebox_snapshot, { |
| 866 |
'hetzner_token': '', |
| 867 |
'storagebox_id': 23, |
| 868 |
'snapshot_name': '2038-01-19T03:14:17', |
| 869 |
'snapshot_comment': 'Test comment' |
| 870 |
}, [ |
| 871 |
FetchUrlCall('GET', 200) |
| 872 |
.result_json({ |
| 873 |
"snapshots": [], |
| 874 |
}) |
| 875 |
.expect_url('{0}/v1/storage_boxes/23/snapshots?name=2038-01-19T03%3A14%3A17'.format(API_BASE_URL)), |
| 876 |
]) |
| 877 |
assert result['msg'] == 'Snapshot with name 2038-01-19T03:14:17 does not exist' |
| 878 |
|
| 879 |
def test_snapshot_name_with_state_present(self, mocker): |
| 880 |
result = self.run_module_failed(mocker, storagebox_snapshot, { |
| 881 |
'hetzner_token': '', |
| 882 |
'storagebox_id': 23, |
| 883 |
'snapshot_name': '2038-01-19T03:14:17', |
| 884 |
'state': 'present' |
| 885 |
}, []) |
| 886 |
assert result['msg'] == "snapshot_comment is required when updating a snapshot" |
| 887 |
|
| 888 |
def test_multiple_names(self, mocker): |
| 889 |
result = self.run_module_failed(mocker, storagebox_snapshot, { |
| 890 |
'hetzner_token': '', |
| 891 |
'storagebox_id': 23, |
| 892 |
'snapshot_name': '2025-03-28T15-20-51', |
| 893 |
'state': 'absent' |
| 894 |
}, [ |
| 895 |
FetchUrlCall('GET', 200) |
| 896 |
.result_json({ |
| 897 |
"snapshots": [ |
| 898 |
STORAGEBOX_SNAPSHOTS[2], |
| 899 |
STORAGEBOX_SNAPSHOTS[2], |
| 900 |
], |
| 901 |
}) |
| 902 |
.expect_url('{0}/v1/storage_boxes/23/snapshots?name=2025-03-28T15-20-51'.format(API_BASE_URL)), |
| 903 |
]) |
| 904 |
assert result['msg'] == "Found 2 snapshots with name '2025-03-28T15-20-51'" |