Skip to content

Commit

Permalink
Improving code coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfontein committed Jan 4, 2025
1 parent 1f799fb commit 72aeb7e
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 11 deletions.
2 changes: 1 addition & 1 deletion plugins/modules/boot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion plugins/modules/storagebox.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)))

Check warning on line 186 in plugins/modules/storagebox.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/storagebox.py#L185-L186

Added lines #L185 - L186 were not covered by tests
after = extract(result)

result = dict(after)
Expand Down
3 changes: 2 additions & 1 deletion plugins/modules/v_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check warning on line 287 in plugins/modules/v_switch.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/v_switch.py#L287

Added line #L287 was not covered by tests


def create_v_switch(module):
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/plugins/inventory/test_robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
118 changes: 118 additions & 0 deletions tests/unit/plugins/modules/test_boot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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': '',
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/plugins/modules/test_ssh_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/plugins/modules/test_storagebox.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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')
Expand Down
13 changes: 8 additions & 5 deletions tests/unit/plugins/modules/test_v_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -496,15 +496,18 @@ def test_delete_invalid_input(self, mocker):
'status': 400,
'code': 'INVALID_INPUT',
'message': 'Invalid input',
'invalid': [],
'invalid': 'foo',
'missing': None,
},
})
.expect_url('{0}/vswitch/4321'.format(BASE_URL)),
],
)

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(
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 72aeb7e

Please sign in to comment.