| 1 |
# Copyright (c) 2021 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.hrobot.plugins.module_utils.robot import BASE_URL |
| 15 |
from ansible_collections.community.hrobot.plugins.modules import boot |
| 16 |
|
| 17 |
from ..data import ( |
| 18 |
SSH_PUBLIC_KEY_1, |
| 19 |
SSH_FINGERPRINT_1, |
| 20 |
) |
| 21 |
|
| 22 |
|
| 23 |
def _amend_server_data(data): |
| 24 |
data.update({ |
| 25 |
'server_ip': '123.123.123.123', |
| 26 |
'server_ipv6_net': '2a01:4f8:111:4221::', |
| 27 |
'server_number': 23, |
| 28 |
}) |
| 29 |
return data |
| 30 |
|
| 31 |
|
| 32 |
def create_rescue_inactive(): |
| 33 |
return _amend_server_data({ |
| 34 |
'active': False, |
| 35 |
'arch': [64, 32], |
| 36 |
'authorized_key': [], |
| 37 |
'boot_time': None, |
| 38 |
'host_key': [], |
| 39 |
'os': ['linux', 'linuxold', 'freebsd', 'freebsdbeta', 'freebsdax', 'freebsdbetaax', 'vkvm', 'vkvmold'], |
| 40 |
'password': None, |
| 41 |
}) |
| 42 |
|
| 43 |
|
| 44 |
def create_rescue_active(os='linux', arch=64, authorized_key=None, host_key=None): |
| 45 |
return _amend_server_data({ |
| 46 |
'active': True, |
| 47 |
'arch': arch, |
| 48 |
'authorized_key': authorized_key or [], |
| 49 |
'boot_time': None, |
| 50 |
'host_key': host_key or [], |
| 51 |
'os': os, |
| 52 |
'password': 'aBcDeFgHiJ1234', |
| 53 |
}) |
| 54 |
|
| 55 |
|
| 56 |
def create_linux_inactive(): |
| 57 |
return { |
| 58 |
'dist': [ |
| 59 |
'Arch Linux latest minimal', |
| 60 |
'CentOS 7.9 minimal', |
| 61 |
'CentOS 8.4 minimal', |
| 62 |
'Debian 10.10 LAMP', |
| 63 |
'Debian 10.10 minimal', |
| 64 |
'Debian 11 base', |
| 65 |
'Ubuntu 18.04.5 LTS minimal', |
| 66 |
'Ubuntu 18.04.5 LTS Nextcloud', |
| 67 |
'Ubuntu 20.04.1 LTS minimal', |
| 68 |
], |
| 69 |
'arch': [64], |
| 70 |
'lang': ['en'], |
| 71 |
'active': False, |
| 72 |
'password': None, |
| 73 |
'authorized_key': [], |
| 74 |
'host_key': [], |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
def create_linux_active(dist='Arch Linux latest minimal', arch=64, lang='en', authorized_key=None, host_key=None): |
| 79 |
return { |
| 80 |
'dist': dist, |
| 81 |
'arch': arch, |
| 82 |
'lang': lang, |
| 83 |
'active': True, |
| 84 |
'password': 'aBcDeFgHiJ1234', |
| 85 |
'authorized_key': authorized_key or [], |
| 86 |
'host_key': host_key or [], |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
def create_vnc_inactive(): |
| 91 |
return { |
| 92 |
'dist': ['CentOS-7.9', 'CentOS-8.4', 'Fedora-33', 'openSUSE-15.2'], |
| 93 |
'arch': [64], |
| 94 |
'lang': ['de_DE', 'en_US', 'fr_FR', 'ru_RU'], |
| 95 |
'active': False, |
| 96 |
'password': None, |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
def create_vnc_active(): |
| 101 |
return { |
| 102 |
"server_ip": "123.123.123.123", |
| 103 |
"server_ipv6_net": "2a01:4f8:111:4221::", |
| 104 |
"server_number": 321, |
| 105 |
"dist": "centOS-5.0", |
| 106 |
"arch": 32, |
| 107 |
"lang": "en_US", |
| 108 |
"active": True, |
| 109 |
"password": "jEt0dtUvomlyOwRr", |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
def create_cpanel_active(hostname): |
| 114 |
return { |
| 115 |
"server_ip": "123.123.123.123", |
| 116 |
"server_ipv6_net": "2a01:4f8:111:4221::", |
| 117 |
"server_number": 321, |
| 118 |
"dist": "CentOS 5.6 + cPanel", |
| 119 |
"arch": 64, |
| 120 |
"lang": "en", |
| 121 |
"active": True, |
| 122 |
"password": "ie8Nhz6R", |
| 123 |
"hostname": hostname, |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
def _amend_boot(data=None): |
| 128 |
if data is None: |
| 129 |
data = {} |
| 130 |
if 'rescue' not in data: |
| 131 |
data['rescue'] = create_rescue_inactive() |
| 132 |
if 'linux' not in data: |
| 133 |
data['linux'] = create_linux_inactive() |
| 134 |
if 'vnc' not in data: |
| 135 |
data['vnc'] = create_vnc_inactive() |
| 136 |
for section in ('windows', 'plesk', 'cpanel'): |
| 137 |
if section not in data: |
| 138 |
data[section] = None |
| 139 |
return { |
| 140 |
'boot': data, |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
class TestHetznerBoot(BaseTestModule): |
| 145 |
MOCK_ANSIBLE_MODULEUTILS_BASIC_ANSIBLEMODULE = 'ansible_collections.community.hrobot.plugins.modules.boot.AnsibleModule' |
| 146 |
MOCK_ANSIBLE_MODULEUTILS_URLS_FETCH_URL = 'ansible_collections.community.hrobot.plugins.module_utils.robot.fetch_url' |
| 147 |
|
| 148 |
def test_idempotent_regular(self, mocker): |
| 149 |
result = self.run_module_success(mocker, boot, { |
| 150 |
'hetzner_user': 'test', |
| 151 |
'hetzner_password': 'hunter2', |
| 152 |
'server_number': 23, |
| 153 |
'regular_boot': True, |
| 154 |
}, [ |
| 155 |
FetchUrlCall('GET', 200) |
| 156 |
.result_json(_amend_boot({ |
| 157 |
'rescue': create_linux_inactive(), |
| 158 |
})) |
| 159 |
.expect_basic_auth('test', 'hunter2') |
| 160 |
.expect_force_basic_auth(True) |
| 161 |
.expect_url('{0}/boot/23'.format(BASE_URL)), |
| 162 |
]) |
| 163 |
assert result['changed'] is False |
| 164 |
assert result['configuration_type'] == 'regular_boot' |
| 165 |
assert result['password'] is None |
| 166 |
|
| 167 |
def test_rescue_idempotent(self, mocker): |
| 168 |
result = self.run_module_success(mocker, boot, { |
| 169 |
'hetzner_user': '', |
| 170 |
'hetzner_password': '', |
| 171 |
'server_number': 23, |
| 172 |
'rescue': { |
| 173 |
'os': 'linux', |
| 174 |
}, |
| 175 |
}, [ |
| 176 |
FetchUrlCall('GET', 200) |
| 177 |
.result_json(_amend_boot({ |
| 178 |
'rescue': create_rescue_active(os='linux'), |
| 179 |
})) |
| 180 |
.expect_url('{0}/boot/23'.format(BASE_URL)), |
| 181 |
]) |
| 182 |
assert result['changed'] is False |
| 183 |
assert result['configuration_type'] == 'rescue' |
| 184 |
assert result['password'] == 'aBcDeFgHiJ1234' |
| 185 |
|
| 186 |
def test_rescue_idempotent_2(self, mocker): |
| 187 |
result = self.run_module_success(mocker, boot, { |
| 188 |
'hetzner_user': '', |
| 189 |
'hetzner_password': '', |
| 190 |
'server_number': 23, |
| 191 |
'rescue': { |
| 192 |
'os': 'linux', |
| 193 |
'arch': 32, |
| 194 |
'authorized_keys': [ |
| 195 |
SSH_PUBLIC_KEY_1, |
| 196 |
'aa:bb:cc:dd:ee:ff:00:11:22:33:44:55:66:77:88:99', |
| 197 |
'0f:1e:2d:3c:4b:5a:69:78:87:96:a5:b4:c3:d2:e1:f0', |
| 198 |
], |
| 199 |
}, |
| 200 |
}, [ |
| 201 |
FetchUrlCall('GET', 200) |
| 202 |
.result_json(_amend_boot({ |
| 203 |
'rescue': create_rescue_active(os='linux', arch=32, authorized_key=[ |
| 204 |
{ |
| 205 |
'key': { |
| 206 |
'fingerprint': SSH_FINGERPRINT_1, |
| 207 |
'name': 'baz', |
| 208 |
'size': 4096, |
| 209 |
'type': 'RSA', |
| 210 |
}, |
| 211 |
}, |
| 212 |
{ |
| 213 |
'key': { |
| 214 |
'fingerprint': 'aa:bb:cc:dd:ee:ff:00:11:22:33:44:55:66:77:88:99', |
| 215 |
'name': 'foo bar', |
| 216 |
'size': 2048, |
| 217 |
'type': 'RSA', |
| 218 |
}, |
| 219 |
}, |
| 220 |
{ |
| 221 |
'key': { |
| 222 |
'fingerprint': '0f:1e:2d:3c:4b:5a:69:78:87:96:a5:b4:c3:d2:e1:f0', |
| 223 |
'name': 'test', |
| 224 |
'size': 3072, |
| 225 |
'type': 'RSA', |
| 226 |
}, |
| 227 |
}, |
| 228 |
]), |
| 229 |
})) |
| 230 |
.expect_url('{0}/boot/23'.format(BASE_URL)), |
| 231 |
]) |
| 232 |
assert result['changed'] is False |
| 233 |
assert result['configuration_type'] == 'rescue' |
| 234 |
assert result['password'] == 'aBcDeFgHiJ1234' |
| 235 |
|
| 236 |
def test_rescue_deactivate(self, mocker): |
| 237 |
result = self.run_module_success(mocker, boot, { |
| 238 |
'hetzner_user': '', |
| 239 |
'hetzner_password': '', |
| 240 |
'server_number': 23, |
| 241 |
'regular_boot': True, |
| 242 |
}, [ |
| 243 |
FetchUrlCall('GET', 200) |
| 244 |
.result_json(_amend_boot({ |
| 245 |
'rescue': create_rescue_active(os='linux'), |
| 246 |
})) |
| 247 |
.expect_url('{0}/boot/23'.format(BASE_URL)), |
| 248 |
FetchUrlCall('DELETE', 200) |
| 249 |
.expect_url('{0}/boot/23/rescue'.format(BASE_URL)), |
| 250 |
]) |
| 251 |
assert result['changed'] is True |
| 252 |
assert result['configuration_type'] == 'regular_boot' |
| 253 |
assert result['password'] is None |
| 254 |
|
| 255 |
def test_rescue_deactivate_check_mode(self, mocker): |
| 256 |
result = self.run_module_success(mocker, boot, { |
| 257 |
'hetzner_user': '', |
| 258 |
'hetzner_password': '', |
| 259 |
'server_number': 23, |
| 260 |
'regular_boot': True, |
| 261 |
'_ansible_check_mode': True, |
| 262 |
}, [ |
| 263 |
FetchUrlCall('GET', 200) |
| 264 |
.result_json(_amend_boot({ |
| 265 |
'rescue': create_rescue_active(os='linux'), |
| 266 |
})) |
| 267 |
.expect_url('{0}/boot/23'.format(BASE_URL)), |
| 268 |
]) |
| 269 |
assert result['changed'] is True |
| 270 |
assert result['configuration_type'] == 'regular_boot' |
| 271 |
assert result['password'] is None |
| 272 |
|
| 273 |
def test_rescue_activate(self, mocker): |
| 274 |
result = self.run_module_success(mocker, boot, { |
| 275 |
'hetzner_user': '', |
| 276 |
'hetzner_password': '', |
| 277 |
'server_number': 23, |
| 278 |
'rescue': { |
| 279 |
'os': 'linux', |
| 280 |
}, |
| 281 |
}, [ |
| 282 |
FetchUrlCall('GET', 200) |
| 283 |
.result_json(_amend_boot()) |
| 284 |
.expect_url('{0}/boot/23'.format(BASE_URL)), |
| 285 |
FetchUrlCall('POST', 200) |
| 286 |
.expect_form_value('os', 'linux') |
| 287 |
.expect_form_value_absent('arch') |
| 288 |
.expect_form_value_absent('authorized_key') |
| 289 |
.result_json({ |
| 290 |
'rescue': create_rescue_active(os='linux'), |
| 291 |
}) |
| 292 |
.expect_url('{0}/boot/23/rescue'.format(BASE_URL)), |
| 293 |
]) |
| 294 |
assert result['changed'] is True |
| 295 |
assert result['configuration_type'] == 'rescue' |
| 296 |
assert result['password'] == 'aBcDeFgHiJ1234' |
| 297 |
|
| 298 |
def test_rescue_activate_check_mode(self, mocker): |
| 299 |
result = self.run_module_success(mocker, boot, { |
| 300 |
'hetzner_user': '', |
| 301 |
'hetzner_password': '', |
| 302 |
'server_number': 23, |
| 303 |
'rescue': { |
| 304 |
'os': 'linux', |
| 305 |
}, |
| 306 |
'_ansible_check_mode': True, |
| 307 |
}, [ |
| 308 |
FetchUrlCall('GET', 200) |
| 309 |
.result_json(_amend_boot()) |
| 310 |
.expect_url('{0}/boot/23'.format(BASE_URL)), |
| 311 |
]) |
| 312 |
assert result['changed'] is True |
| 313 |
assert result['configuration_type'] == 'rescue' |
| 314 |
assert result['password'] is None |
| 315 |
|
| 316 |
def test_rescue_reactivate(self, mocker): |
| 317 |
result = self.run_module_success(mocker, boot, { |
| 318 |
'hetzner_user': '', |
| 319 |
'hetzner_password': '', |
| 320 |
'server_number': 23, |
| 321 |
'rescue': { |
| 322 |
'os': 'linuxold', |
| 323 |
'arch': 32, |
| 324 |
}, |
| 325 |
}, [ |
| 326 |
FetchUrlCall('GET', 200) |
| 327 |
.result_json(_amend_boot({ |
| 328 |
'rescue': create_rescue_active(os='linux'), |
| 329 |
})) |
| 330 |
.expect_url('{0}/boot/23'.format(BASE_URL)), |
| 331 |
FetchUrlCall('DELETE', 200) |
| 332 |
.expect_url('{0}/boot/23/rescue'.format(BASE_URL)), |
| 333 |
FetchUrlCall('POST', 200) |
| 334 |
.expect_form_value('os', 'linuxold') |
| 335 |
.expect_form_value('arch', '32') |
| 336 |
.expect_form_value_absent('authorized_key') |
| 337 |
.result_json({ |
| 338 |
'rescue': create_rescue_active(os='linuxold', arch=32), |
| 339 |
}) |
| 340 |
.expect_url('{0}/boot/23/rescue'.format(BASE_URL)), |
| 341 |
]) |
| 342 |
assert result['changed'] is True |
| 343 |
assert result['configuration_type'] == 'rescue' |
| 344 |
assert result['password'] == 'aBcDeFgHiJ1234' |
| 345 |
|
| 346 |
def test_rescue_reactivate_check_mode(self, mocker): |
| 347 |
result = self.run_module_success(mocker, boot, { |
| 348 |
'hetzner_user': '', |
| 349 |
'hetzner_password': '', |
| 350 |
'server_number': 23, |
| 351 |
'rescue': { |
| 352 |
'os': 'linuxold', |
| 353 |
'arch': 32, |
| 354 |
}, |
| 355 |
'_ansible_check_mode': True, |
| 356 |
}, [ |
| 357 |
FetchUrlCall('GET', 200) |
| 358 |
.result_json(_amend_boot({ |
| 359 |
'rescue': create_rescue_active(os='linux'), |
| 360 |
})) |
| 361 |
]) |
| 362 |
assert result['changed'] is True |
| 363 |
assert result['configuration_type'] == 'rescue' |
| 364 |
assert result['password'] is None |
| 365 |
|
| 366 |
def test_install_linux_idempotent(self, mocker): |
| 367 |
result = self.run_module_success(mocker, boot, { |
| 368 |
'hetzner_user': '', |
| 369 |
'hetzner_password': '', |
| 370 |
'server_number': 23, |
| 371 |
'install_linux': { |
| 372 |
'dist': 'Arch Linux latest minimal', |
| 373 |
'lang': 'en', |
| 374 |
}, |
| 375 |
}, [ |
| 376 |
FetchUrlCall('GET', 200) |
| 377 |
.result_json(_amend_boot({ |
| 378 |
'linux': create_linux_active(dist='Arch Linux latest minimal', lang='en'), |
| 379 |
})) |
| 380 |
.expect_url('{0}/boot/23'.format(BASE_URL)), |
| 381 |
]) |
| 382 |
assert result['changed'] is False |
| 383 |
assert result['configuration_type'] == 'install_linux' |
| 384 |
assert result['password'] == 'aBcDeFgHiJ1234' |
| 385 |
|
| 386 |
def test_install_linux_idempotent_2(self, mocker): |
| 387 |
result = self.run_module_success(mocker, boot, { |
| 388 |
'hetzner_user': '', |
| 389 |
'hetzner_password': '', |
| 390 |
'server_number': 23, |
| 391 |
'install_linux': { |
| 392 |
'dist': 'Arch Linux latest minimal', |
| 393 |
'arch': 32, |
| 394 |
'lang': 'de', |
| 395 |
'authorized_keys': [ |
| 396 |
'e4:47:42:71:81:62:bf:06:1c:23:fa:f3:8f:7b:6f:d0', |
| 397 |
'0f:1e:2d:3c:4b:5a:69:78:87:96:a5:b4:c3:d2:e1:f0', |
| 398 |
'aa:bb:cc:dd:ee:ff:00:11:22:33:44:55:66:77:88:99', |
| 399 |
], |
| 400 |
}, |
| 401 |
}, [ |
| 402 |
FetchUrlCall('GET', 200) |
| 403 |
.result_json(_amend_boot({ |
| 404 |
'linux': create_linux_active(dist='Arch Linux latest minimal', arch=32, lang='de', authorized_key=[ |
| 405 |
{ |
| 406 |
'key': { |
| 407 |
'fingerprint': 'e4:47:42:71:81:62:bf:06:1c:23:fa:f3:8f:7b:6f:d0', |
| 408 |
'name': 'abc', |
| 409 |
'size': 4096, |
| 410 |
'type': 'RSA', |
| 411 |
}, |
| 412 |
}, |
| 413 |
{ |
| 414 |
'key': { |
| 415 |
'fingerprint': 'aa:bb:cc:dd:ee:ff:00:11:22:33:44:55:66:77:88:99', |
| 416 |
'name': 'buzz', |
| 417 |
'size': 2048, |
| 418 |
'type': 'RSA', |
| 419 |
}, |
| 420 |
}, |
| 421 |
{ |
| 422 |
'key': { |
| 423 |
'fingerprint': '0f:1e:2d:3c:4b:5a:69:78:87:96:a5:b4:c3:d2:e1:f0', |
| 424 |
'name': 'afz', |
| 425 |
'size': 2048, |
| 426 |
'type': 'RSA', |
| 427 |
}, |
| 428 |
}, |
| 429 |
]), |
| 430 |
})) |
| 431 |
.expect_url('{0}/boot/23'.format(BASE_URL)), |
| 432 |
]) |
| 433 |
assert result['changed'] is False |
| 434 |
assert result['configuration_type'] == 'install_linux' |
| 435 |
assert result['password'] == 'aBcDeFgHiJ1234' |
| 436 |
|
| 437 |
def test_install_linux_deactivate(self, mocker): |
| 438 |
result = self.run_module_success(mocker, boot, { |
| 439 |
'hetzner_user': '', |
| 440 |
'hetzner_password': '', |
| 441 |
'server_number': 23, |
| 442 |
'regular_boot': True, |
| 443 |
}, [ |
| 444 |
FetchUrlCall('GET', 200) |
| 445 |
.result_json(_amend_boot({ |
| 446 |
'linux': create_linux_active(dist='Arch Linux latest minimal'), |
| 447 |
})) |
| 448 |
.expect_url('{0}/boot/23'.format(BASE_URL)), |
| 449 |
FetchUrlCall('DELETE', 200) |
| 450 |
.expect_url('{0}/boot/23/linux'.format(BASE_URL)), |
| 451 |
]) |
| 452 |
assert result['changed'] is True |
| 453 |
assert result['configuration_type'] == 'regular_boot' |
| 454 |
assert result['password'] is None |
| 455 |
|
| 456 |
def test_install_linux_activate(self, mocker): |
| 457 |
result = self.run_module_success(mocker, boot, { |
| 458 |
'hetzner_user': '', |
| 459 |
'hetzner_password': '', |
| 460 |
'server_number': 23, |
| 461 |
'install_linux': { |
| 462 |
'dist': 'Arch Linux latest minimal', |
| 463 |
'lang': 'en', |
| 464 |
}, |
| 465 |
}, [ |
| 466 |
FetchUrlCall('GET', 200) |
| 467 |
.result_json(_amend_boot()) |
| 468 |
.expect_url('{0}/boot/23'.format(BASE_URL)), |
| 469 |
FetchUrlCall('POST', 200) |
| 470 |
.expect_form_value('dist', 'Arch Linux latest minimal') |
| 471 |
.expect_form_value_absent('arch') |
| 472 |
.expect_form_value_absent('authorized_key') |
| 473 |
.result_json({ |
| 474 |
'linux': create_linux_active(dist='Arch Linux latest minimal', lang='en'), |
| 475 |
}) |
| 476 |
.expect_url('{0}/boot/23/linux'.format(BASE_URL)), |
| 477 |
]) |
| 478 |
assert result['changed'] is True |
| 479 |
assert result['configuration_type'] == 'install_linux' |
| 480 |
assert result['password'] == 'aBcDeFgHiJ1234' |
| 481 |
|
| 482 |
def test_install_linux_reactivate(self, mocker): |
| 483 |
result = self.run_module_success(mocker, boot, { |
| 484 |
'hetzner_user': '', |
| 485 |
'hetzner_password': '', |
| 486 |
'server_number': 23, |
| 487 |
'install_linux': { |
| 488 |
'dist': 'Debian 11 base', |
| 489 |
'arch': 32, |
| 490 |
'lang': 'fr', |
| 491 |
'authorized_keys': [ |
| 492 |
'e4:47:42:71:81:62:bf:06:1c:23:fa:f3:8f:7b:6f:d0', |
| 493 |
'aa:bb:cc:dd:ee:ff:00:11:22:33:44:55:66:77:88:99', |
| 494 |
], |
| 495 |
}, |
| 496 |
}, [ |
| 497 |
FetchUrlCall('GET', 200) |
| 498 |
.result_json(_amend_boot({ |
| 499 |
'linux': create_linux_active(dist='Arch Linux latest minimal', lang='en'), |
| 500 |
})) |
| 501 |
.expect_url('{0}/boot/23'.format(BASE_URL)), |
| 502 |
FetchUrlCall('DELETE', 200) |
| 503 |
.expect_url('{0}/boot/23/linux'.format(BASE_URL)), |
| 504 |
FetchUrlCall('POST', 200) |
| 505 |
.expect_form_value('dist', 'Debian 11 base') |
| 506 |
.expect_form_value('arch', '32') |
| 507 |
.expect_form_value('lang', 'fr') |
| 508 |
.expect_form_present('authorized_key[]') |
| 509 |
.expect_form_values('authorized_key[]', [ |
| 510 |
'e4:47:42:71:81:62:bf:06:1c:23:fa:f3:8f:7b:6f:d0', |
| 511 |
'aa:bb:cc:dd:ee:ff:00:11:22:33:44:55:66:77:88:99', |
| 512 |
]) |
| 513 |
.result_json({ |
| 514 |
'linux': create_linux_active(dist='Debian 11 base', lang='fr', arch=32, authorized_key=[ |
| 515 |
{ |
| 516 |
'key': { |
| 517 |
'fingerprint': 'aa:bb:cc:dd:ee:ff:00:11:22:33:44:55:66:77:88:99', |
| 518 |
'name': 'foo bar', |
| 519 |
'size': 4096, |
| 520 |
'type': 'RSA', |
| 521 |
}, |
| 522 |
}, |
| 523 |
{ |
| 524 |
'key': { |
| 525 |
'fingerprint': 'e4:47:42:71:81:62:bf:06:1c:23:fa:f3:8f:7b:6f:d0', |
| 526 |
'name': 'bar', |
| 527 |
'size': 2048, |
| 528 |
'type': 'RSA', |
| 529 |
}, |
| 530 |
}, |
| 531 |
]), |
| 532 |
}) |
| 533 |
.expect_url('{0}/boot/23/linux'.format(BASE_URL)), |
| 534 |
]) |
| 535 |
assert result['changed'] is True |
| 536 |
assert result['configuration_type'] == 'install_linux' |
| 537 |
assert result['password'] == 'aBcDeFgHiJ1234' |
| 538 |
|
| 539 |
def test_install_vnc_deactivate(self, mocker): |
| 540 |
result = self.run_module_success(mocker, boot, { |
| 541 |
'hetzner_user': '', |
| 542 |
'hetzner_password': '', |
| 543 |
'server_number': 23, |
| 544 |
'regular_boot': True, |
| 545 |
}, [ |
| 546 |
FetchUrlCall('GET', 200) |
| 547 |
.result_json(_amend_boot({ |
| 548 |
'vnc': create_vnc_active(), |
| 549 |
})) |
| 550 |
.expect_url('{0}/boot/23'.format(BASE_URL)), |
| 551 |
FetchUrlCall('DELETE', 200) |
| 552 |
.expect_url('{0}/boot/23/vnc'.format(BASE_URL)), |
| 553 |
]) |
| 554 |
assert result['changed'] is True |
| 555 |
assert result['configuration_type'] == 'regular_boot' |
| 556 |
assert result['password'] is None |
| 557 |
|
| 558 |
def test_install_vnc_activate(self, mocker): |
| 559 |
result = self.run_module_success(mocker, boot, { |
| 560 |
'hetzner_user': '', |
| 561 |
'hetzner_password': '', |
| 562 |
'server_number': 23, |
| 563 |
'install_vnc': { |
| 564 |
'dist': 'Arch Linux latest minimal', |
| 565 |
'lang': 'en', |
| 566 |
}, |
| 567 |
}, [ |
| 568 |
FetchUrlCall('GET', 200) |
| 569 |
.result_json(_amend_boot()) |
| 570 |
.expect_url('{0}/boot/23'.format(BASE_URL)), |
| 571 |
FetchUrlCall('POST', 200) |
| 572 |
.expect_form_value('dist', 'Arch Linux latest minimal') |
| 573 |
.expect_form_value_absent('arch') |
| 574 |
.expect_form_value_absent('authorized_key') |
| 575 |
.result_json({ |
| 576 |
'vnc': create_vnc_active(), |
| 577 |
}) |
| 578 |
.expect_url('{0}/boot/23/vnc'.format(BASE_URL)), |
| 579 |
]) |
| 580 |
assert result['changed'] is True |
| 581 |
assert result['configuration_type'] == 'install_vnc' |
| 582 |
assert result['password'] == 'jEt0dtUvomlyOwRr' |
| 583 |
|
| 584 |
def test_install_cpanel_deactivate(self, mocker): |
| 585 |
result = self.run_module_success(mocker, boot, { |
| 586 |
'hetzner_user': '', |
| 587 |
'hetzner_password': '', |
| 588 |
'server_number': 23, |
| 589 |
'regular_boot': True, |
| 590 |
}, [ |
| 591 |
FetchUrlCall('GET', 200) |
| 592 |
.result_json(_amend_boot({ |
| 593 |
'cpanel': create_cpanel_active('foobar'), |
| 594 |
})) |
| 595 |
.expect_url('{0}/boot/23'.format(BASE_URL)), |
| 596 |
FetchUrlCall('DELETE', 200) |
| 597 |
.expect_url('{0}/boot/23/cpanel'.format(BASE_URL)), |
| 598 |
]) |
| 599 |
assert result['changed'] is True |
| 600 |
assert result['configuration_type'] == 'regular_boot' |
| 601 |
assert result['password'] is None |
| 602 |
|
| 603 |
def test_install_cpanel_activate(self, mocker): |
| 604 |
result = self.run_module_success(mocker, boot, { |
| 605 |
'hetzner_user': '', |
| 606 |
'hetzner_password': '', |
| 607 |
'server_number': 23, |
| 608 |
'install_cpanel': { |
| 609 |
'dist': 'Arch Linux latest minimal', |
| 610 |
'lang': 'en', |
| 611 |
'hostname': 'foobar', |
| 612 |
}, |
| 613 |
}, [ |
| 614 |
FetchUrlCall('GET', 200) |
| 615 |
.result_json(_amend_boot()) |
| 616 |
.expect_url('{0}/boot/23'.format(BASE_URL)), |
| 617 |
FetchUrlCall('POST', 200) |
| 618 |
.expect_form_value('dist', 'Arch Linux latest minimal') |
| 619 |
.expect_form_value_absent('arch') |
| 620 |
.expect_form_value_absent('authorized_key') |
| 621 |
.result_json({ |
| 622 |
'cpanel': create_cpanel_active('foobar'), |
| 623 |
}) |
| 624 |
.expect_url('{0}/boot/23/cpanel'.format(BASE_URL)), |
| 625 |
]) |
| 626 |
assert result['changed'] is True |
| 627 |
assert result['configuration_type'] == 'install_cpanel' |
| 628 |
assert result['password'] == 'ie8Nhz6R' |
| 629 |
|
| 630 |
def test_server_not_found(self, mocker): |
| 631 |
result = self.run_module_failed(mocker, boot, { |
| 632 |
'hetzner_user': '', |
| 633 |
'hetzner_password': '', |
| 634 |
'server_number': 23, |
| 635 |
'regular_boot': True, |
| 636 |
}, [ |
| 637 |
FetchUrlCall('GET', 404) |
| 638 |
.result_json({ |
| 639 |
'error': { |
| 640 |
'status': 404, |
| 641 |
'code': 'SERVER_NOT_FOUND', |
| 642 |
'message': 'Server not found', |
| 643 |
}, |
| 644 |
}) |
| 645 |
.expect_url('{0}/boot/23'.format(BASE_URL)), |
| 646 |
]) |
| 647 |
assert result['msg'] == 'This server does not exist, or you do not have access rights for it' |
| 648 |
|
| 649 |
def test_invalid_input(self, mocker): |
| 650 |
result = self.run_module_failed(mocker, boot, { |
| 651 |
'hetzner_user': '', |
| 652 |
'hetzner_password': '', |
| 653 |
'server_number': 23, |
| 654 |
'regular_boot': True, |
| 655 |
}, [ |
| 656 |
FetchUrlCall('GET', 404) |
| 657 |
.result_json({ |
| 658 |
'error': { |
| 659 |
'status': 404, |
| 660 |
'code': 'BOOT_NOT_AVAILABLE', |
| 661 |
'message': 'No boot configuration available for this server', |
| 662 |
}, |
| 663 |
}) |
| 664 |
.expect_url('{0}/boot/23'.format(BASE_URL)), |
| 665 |
]) |
| 666 |
assert result['msg'] == 'There is no boot configuration available for this server' |
| 667 |
|
| 668 |
def test_invalid_fingerprint(self, mocker): |
| 669 |
result = self.run_module_failed(mocker, boot, { |
| 670 |
'hetzner_user': '', |
| 671 |
'hetzner_password': '', |
| 672 |
'server_number': 23, |
| 673 |
'rescue': { |
| 674 |
'os': 'linux', |
| 675 |
'arch': 32, |
| 676 |
'authorized_keys': [ |
| 677 |
'asdf a-b', |
| 678 |
], |
| 679 |
}, |
| 680 |
}, [ |
| 681 |
FetchUrlCall('GET', 200) |
| 682 |
.result_json(_amend_boot({ |
| 683 |
'rescue': create_rescue_active(os='linux', arch=32, authorized_key=[]), |
| 684 |
})) |
| 685 |
.expect_url('{0}/boot/23'.format(BASE_URL)), |
| 686 |
]) |
| 687 |
assert result['msg'] == ( |
| 688 |
"Error while extracting fingerprint of rescue.authorized_keys[1]'s value 'asdf a-b':" |
| 689 |
" Error while extracting fingerprint from public key data: Incorrect padding" |
| 690 |
) |