diff --git a/plugins/modules/boot.py b/plugins/modules/boot.py index 69ac5ba..ff3f79e 100644 --- a/plugins/modules/boot.py +++ b/plugins/modules/boot.py @@ -438,7 +438,7 @@ def main(): if isinstance(has, list): has = sorted(has) if not isinstance(should, list): - should = [should] + should = [should] # pragma: no cover should = sorted(should) if should != has: needs_change = True diff --git a/plugins/modules/storagebox.py b/plugins/modules/storagebox.py index d8b4cb2..ef511c4 100644 --- a/plugins/modules/storagebox.py +++ b/plugins/modules/storagebox.py @@ -182,7 +182,8 @@ def main(): accept_errors=['INVALID_INPUT'], ) if error: - module.fail_json(msg='The values to update were invalid ({0})'.format(module.jsonify(changes))) + invalid = result['error'].get('invalid') or [] + module.fail_json(msg='The values to update were invalid ({0})'.format(', '.join(invalid))) after = extract(result) result = dict(after) diff --git a/plugins/modules/v_switch.py b/plugins/modules/v_switch.py index 5d043ca..5c80d54 100644 --- a/plugins/modules/v_switch.py +++ b/plugins/modules/v_switch.py @@ -283,7 +283,8 @@ def get_v_switch(module, id_, wait_condition=None): def print_list(possible_list): if isinstance(possible_list, list): - return [to_native(x) for x in possible_list] + return ', '.join([to_native(x) for x in possible_list]) + return repr(possible_list) def create_v_switch(module): diff --git a/tests/unit/plugins/inventory/test_robot.py b/tests/unit/plugins/inventory/test_robot.py index 5a3ec59..6524bb7 100644 --- a/tests/unit/plugins/inventory/test_robot.py +++ b/tests/unit/plugins/inventory/test_robot.py @@ -47,7 +47,7 @@ def access_mock(path, can_access=True): def access(f, m, *args, **kwargs): if to_native(f) == path: return can_access - return original_access(f, m, *args, **kwargs) + return original_access(f, m, *args, **kwargs) # pragma: no cover return access diff --git a/tests/unit/plugins/modules/test_boot.py b/tests/unit/plugins/modules/test_boot.py index 5326622..ebea833 100644 --- a/tests/unit/plugins/modules/test_boot.py +++ b/tests/unit/plugins/modules/test_boot.py @@ -97,6 +97,33 @@ def create_vnc_inactive(): } +def create_vnc_active(): + return { + "server_ip": "123.123.123.123", + "server_ipv6_net": "2a01:4f8:111:4221::", + "server_number": 321, + "dist": "centOS-5.0", + "arch": 32, + "lang": "en_US", + "active": True, + "password": "jEt0dtUvomlyOwRr", + } + + +def create_cpanel_active(hostname): + return { + "server_ip": "123.123.123.123", + "server_ipv6_net": "2a01:4f8:111:4221::", + "server_number": 321, + "dist": "CentOS 5.6 + cPanel", + "arch": 64, + "lang": "en", + "active": True, + "password": "ie8Nhz6R", + "hostname": hostname, + } + + def _amend_boot(data=None): if data is None: data = {} @@ -509,6 +536,97 @@ def test_install_linux_reactivate(self, mocker): assert result['configuration_type'] == 'install_linux' assert result['password'] == 'aBcDeFgHiJ1234' + def test_install_vnc_deactivate(self, mocker): + result = self.run_module_success(mocker, boot, { + 'hetzner_user': '', + 'hetzner_password': '', + 'server_number': 23, + 'regular_boot': True, + }, [ + FetchUrlCall('GET', 200) + .result_json(_amend_boot({ + 'vnc': create_vnc_active(), + })) + .expect_url('{0}/boot/23'.format(BASE_URL)), + FetchUrlCall('DELETE', 200) + .expect_url('{0}/boot/23/vnc'.format(BASE_URL)), + ]) + assert result['changed'] is True + assert result['configuration_type'] == 'regular_boot' + assert result['password'] is None + + def test_install_vnc_activate(self, mocker): + result = self.run_module_success(mocker, boot, { + 'hetzner_user': '', + 'hetzner_password': '', + 'server_number': 23, + 'install_vnc': { + 'dist': 'Arch Linux latest minimal', + 'lang': 'en', + }, + }, [ + FetchUrlCall('GET', 200) + .result_json(_amend_boot()) + .expect_url('{0}/boot/23'.format(BASE_URL)), + FetchUrlCall('POST', 200) + .expect_form_value('dist', 'Arch Linux latest minimal') + .expect_form_value_absent('arch') + .expect_form_value_absent('authorized_key') + .result_json({ + 'vnc': create_vnc_active(), + }) + .expect_url('{0}/boot/23/vnc'.format(BASE_URL)), + ]) + assert result['changed'] is True + assert result['configuration_type'] == 'install_vnc' + assert result['password'] == 'jEt0dtUvomlyOwRr' + + def test_install_cpanel_deactivate(self, mocker): + result = self.run_module_success(mocker, boot, { + 'hetzner_user': '', + 'hetzner_password': '', + 'server_number': 23, + 'regular_boot': True, + }, [ + FetchUrlCall('GET', 200) + .result_json(_amend_boot({ + 'cpanel': create_cpanel_active('foobar'), + })) + .expect_url('{0}/boot/23'.format(BASE_URL)), + FetchUrlCall('DELETE', 200) + .expect_url('{0}/boot/23/cpanel'.format(BASE_URL)), + ]) + assert result['changed'] is True + assert result['configuration_type'] == 'regular_boot' + assert result['password'] is None + + def test_install_cpanel_activate(self, mocker): + result = self.run_module_success(mocker, boot, { + 'hetzner_user': '', + 'hetzner_password': '', + 'server_number': 23, + 'install_cpanel': { + 'dist': 'Arch Linux latest minimal', + 'lang': 'en', + 'hostname': 'foobar', + }, + }, [ + FetchUrlCall('GET', 200) + .result_json(_amend_boot()) + .expect_url('{0}/boot/23'.format(BASE_URL)), + FetchUrlCall('POST', 200) + .expect_form_value('dist', 'Arch Linux latest minimal') + .expect_form_value_absent('arch') + .expect_form_value_absent('authorized_key') + .result_json({ + 'cpanel': create_cpanel_active('foobar'), + }) + .expect_url('{0}/boot/23/cpanel'.format(BASE_URL)), + ]) + assert result['changed'] is True + assert result['configuration_type'] == 'install_cpanel' + assert result['password'] == 'ie8Nhz6R' + def test_server_not_found(self, mocker): result = self.run_module_failed(mocker, boot, { 'hetzner_user': '', diff --git a/tests/unit/plugins/modules/test_ssh_key.py b/tests/unit/plugins/modules/test_ssh_key.py index e5f06e5..e998190 100644 --- a/tests/unit/plugins/modules/test_ssh_key.py +++ b/tests/unit/plugins/modules/test_ssh_key.py @@ -51,7 +51,7 @@ def test_absent_fp_idempotent(self, mocker): 'fingerprint': SSH_FINGERPRINT_1, }, [ FetchUrlCall('DELETE', 404) - .result_json({ + .result_error_json('Not found', { 'error': { 'status': 404, 'code': 'NOT_FOUND', diff --git a/tests/unit/plugins/modules/test_storagebox.py b/tests/unit/plugins/modules/test_storagebox.py index dac54af..87c76b7 100644 --- a/tests/unit/plugins/modules/test_storagebox.py +++ b/tests/unit/plugins/modules/test_storagebox.py @@ -145,6 +145,8 @@ def test_invalid_input(self, mocker): 'status': 400, 'code': 'INVALID_INPUT', 'message': 'Invalid input', + 'invalid': ['storagebox_name'], + 'missing': None, }, }) .expect_form_value('storagebox_name', 'Backup') @@ -155,7 +157,7 @@ def test_invalid_input(self, mocker): .expect_form_value_absent('zfs') .expect_url('{0}/storagebox/23'.format(BASE_URL)), ]) - assert result['msg'] == 'The values to update were invalid ({"storagebox_name": "Backup"})' + assert result['msg'] == 'The values to update were invalid (storagebox_name)' def test_change_name(self, mocker): updated = update_info(23, name='Backup') diff --git a/tests/unit/plugins/modules/test_v_switch.py b/tests/unit/plugins/modules/test_v_switch.py index eeded4e..6a4fb94 100644 --- a/tests/unit/plugins/modules/test_v_switch.py +++ b/tests/unit/plugins/modules/test_v_switch.py @@ -303,7 +303,7 @@ def test_v_switch_invalid_input_error(self, mocker): .expect_url('{0}/vswitch'.format(BASE_URL)), ], ) - assert result['msg'] == "vSwitch invalid parameter (['vlan'])" + assert result['msg'] == "vSwitch invalid parameter (vlan)" def test_delete(self, mocker): result = self.run_module_success( @@ -496,7 +496,7 @@ def test_delete_invalid_input(self, mocker): 'status': 400, 'code': 'INVALID_INPUT', 'message': 'Invalid input', - 'invalid': [], + 'invalid': 'foo', 'missing': None, }, }) @@ -504,7 +504,10 @@ def test_delete_invalid_input(self, mocker): ], ) - assert result['msg'] == 'vSwitch invalid parameter ([])' + assert result['msg'] in ( + "vSwitch invalid parameter ('foo')", + "vSwitch invalid parameter (u'foo')", + ) def test_create_with_server(self, mocker): result = self.run_module_success( @@ -1004,14 +1007,14 @@ def test_add_server_vlan_invalid_input(self, mocker): 'status': 400, 'code': 'INVALID_INPUT', 'message': 'Invalid input', - 'invalid': ['foobar'], + 'invalid': ['foobar', 'bazbam'], 'missing': None, }, }) .expect_url('{0}/vswitch/4321/server'.format(BASE_URL)), ], ) - assert result['msg'] == "Invalid parameter adding server (['foobar'])" + assert result['msg'] == "Invalid parameter adding server (foobar, bazbam)" def test_add_server_vlan_not_unique_error(self, mocker): result = self.run_module_failed(