Skip to content

Commit

Permalink
Increase coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfontein committed Jan 3, 2025
1 parent 157a09d commit cc023a0
Show file tree
Hide file tree
Showing 2 changed files with 226 additions and 3 deletions.
214 changes: 212 additions & 2 deletions tests/unit/plugins/modules/test_v_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ def create_v_switch_data(vlan, name, server=[]):
}


def create_v_switches_data(vlan, name):
def create_v_switches_data(vlan, name, id_=4321):
return [
{
'id': 4321,
'id': id_,
'name': name,
'vlan': vlan,
'cancelled': False,
Expand Down Expand Up @@ -86,6 +86,57 @@ def test_idempotent(self, mocker):
assert result['changed'] is False
assert result['v_switch'] == create_v_switch_data(4010, 'foo')

def test_not_found_after_list(self, mocker):
result = self.run_module_failed(
mocker,
v_switch,
{
'hetzner_user': 'test',
'hetzner_password': 'hunter2',
'vlan': 4010,
'name': 'foo',
},
[
FetchUrlCall('GET', 200)
.expect_basic_auth('test', 'hunter2')
.expect_force_basic_auth(True)
.result_json(create_v_switches_data(4010, 'foo'))
.expect_url('{0}/vswitch'.format(BASE_URL)),
FetchUrlCall('GET', 404)
.expect_basic_auth('test', 'hunter2')
.expect_force_basic_auth(True)
.result_json({
'error': {
'status': 404,
'code': 'NOT_FOUND',
'message': 'Cannot find vswitch',
},
})
.expect_url('{0}/vswitch/4321'.format(BASE_URL)),
],
)
assert result['msg'] == 'vSwitch not found.'

def test_not_unique(self, mocker):
result = self.run_module_failed(
mocker,
v_switch,
{
'hetzner_user': 'test',
'hetzner_password': 'hunter2',
'vlan': 4010,
'name': 'foo',
},
[
FetchUrlCall('GET', 200)
.expect_basic_auth('test', 'hunter2')
.expect_force_basic_auth(True)
.result_json(create_v_switches_data(4010, 'foo') + create_v_switches_data(4010, 'foo', id_=1234))
.expect_url('{0}/vswitch'.format(BASE_URL)),
],
)
assert result['msg'] == 'Multiple vSwitches with same name and VLAN ID in non cancelled status. Clean it.'

def test_create(self, mocker):
result = self.run_module_success(
mocker,
Expand Down Expand Up @@ -264,6 +315,125 @@ def test_delete(self, mocker):

assert result['changed'] is True

def test_delete_not_found(self, mocker):
result = self.run_module_failed(
mocker,
v_switch,
{
'hetzner_user': 'test',
'hetzner_password': 'hunter2',
'vlan': 4010,
'name': 'foo',
'state': 'absent',
},
[
FetchUrlCall('GET', 200)
.expect_basic_auth('test', 'hunter2')
.expect_force_basic_auth(True)
.result_json(create_v_switches_data(4010, 'foo'))
.expect_url('{0}/vswitch'.format(BASE_URL)),
FetchUrlCall('GET', 200)
.expect_basic_auth('test', 'hunter2')
.expect_force_basic_auth(True)
.result_json(create_v_switch_data(4010, 'foo'))
.expect_url('{0}/vswitch/4321'.format(BASE_URL)),
FetchUrlCall('DELETE', 404)
.expect_basic_auth('test', 'hunter2')
.expect_force_basic_auth(True)
.expect_form_value('cancellation_date', datetime.now().strftime('%y-%m-%d'))
.result_json({
'error': {
'status': 404,
'code': 'NOT_FOUND',
'message': 'not found',
},
})
.expect_url('{0}/vswitch/4321'.format(BASE_URL)),
],
)

assert result['msg'] == 'vSwitch not found to delete'

def test_delete_conflict(self, mocker):
result = self.run_module_failed(
mocker,
v_switch,
{
'hetzner_user': 'test',
'hetzner_password': 'hunter2',
'vlan': 4010,
'name': 'foo',
'state': 'absent',
},
[
FetchUrlCall('GET', 200)
.expect_basic_auth('test', 'hunter2')
.expect_force_basic_auth(True)
.result_json(create_v_switches_data(4010, 'foo'))
.expect_url('{0}/vswitch'.format(BASE_URL)),
FetchUrlCall('GET', 200)
.expect_basic_auth('test', 'hunter2')
.expect_force_basic_auth(True)
.result_json(create_v_switch_data(4010, 'foo'))
.expect_url('{0}/vswitch/4321'.format(BASE_URL)),
FetchUrlCall('DELETE', 404)
.expect_basic_auth('test', 'hunter2')
.expect_force_basic_auth(True)
.expect_form_value('cancellation_date', datetime.now().strftime('%y-%m-%d'))
.result_json({
'error': {
'status': 409,
'code': 'CONFLICT',
'message': 'Already cancelled',
},
})
.expect_url('{0}/vswitch/4321'.format(BASE_URL)),
],
)

assert result['msg'] == 'The vSwitch is already cancelled'

def test_delete_invalid_input(self, mocker):
result = self.run_module_failed(
mocker,
v_switch,
{
'hetzner_user': 'test',
'hetzner_password': 'hunter2',
'vlan': 4010,
'name': 'foo',
'state': 'absent',
},
[
FetchUrlCall('GET', 200)
.expect_basic_auth('test', 'hunter2')
.expect_force_basic_auth(True)
.result_json(create_v_switches_data(4010, 'foo'))
.expect_url('{0}/vswitch'.format(BASE_URL)),
FetchUrlCall('GET', 200)
.expect_basic_auth('test', 'hunter2')
.expect_force_basic_auth(True)
.result_json(create_v_switch_data(4010, 'foo'))
.expect_url('{0}/vswitch/4321'.format(BASE_URL)),
FetchUrlCall('DELETE', 404)
.expect_basic_auth('test', 'hunter2')
.expect_force_basic_auth(True)
.expect_form_value('cancellation_date', datetime.now().strftime('%y-%m-%d'))
.result_json({
'error': {
'status': 400,
'code': 'INVALID_INPUT',
'message': 'Invalid input',
'invalid': [],
'missing': None,
},
})
.expect_url('{0}/vswitch/4321'.format(BASE_URL)),
],
)

assert result['msg'] == 'vSwitch invalid parameter ([])'

def test_create_with_server(self, mocker):
result = self.run_module_success(
mocker,
Expand Down Expand Up @@ -702,6 +872,46 @@ def test_add_server_server_not_found_error(self, mocker):
)
assert result['msg'] == 'server "123.123.123.123" not found'

def test_add_server_vlan_invalid_input(self, mocker):
result = self.run_module_failed(
mocker,
v_switch,
{
'hetzner_user': 'test',
'hetzner_password': 'hunter2',
'vlan': 4010,
'name': 'foo',
'servers': ['123.123.123.123'],
},
[
FetchUrlCall('GET', 200)
.expect_basic_auth('test', 'hunter2')
.expect_force_basic_auth(True)
.result_json(create_v_switches_data(4010, 'foo'))
.expect_url('{0}/vswitch'.format(BASE_URL)),
FetchUrlCall('GET', 200)
.expect_basic_auth('test', 'hunter2')
.expect_force_basic_auth(True)
.result_json(create_v_switch_data(4010, 'foo'))
.expect_url('{0}/vswitch/4321'.format(BASE_URL)),
FetchUrlCall('POST', 201)
.expect_basic_auth('test', 'hunter2')
.expect_force_basic_auth(True)
.expect_form_value('server[]', '123.123.123.123')
.result_json({
'error': {
'status': 400,
'code': 'INVALID_INPUT',
'message': 'Invalid input',
'invalid': ['foobar'],
'missing': None,
},
})
.expect_url('{0}/vswitch/4321/server'.format(BASE_URL)),
],
)
assert result['msg'] == "Invalid parameter adding server (['foobar'])"

def test_add_server_vlan_not_unique_error(self, mocker):
result = self.run_module_failed(
mocker,
Expand Down
15 changes: 14 additions & 1 deletion tests/unit/plugins/plugin_utils/test_unsafe.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

import pytest

from ansible.utils.unsafe_proxy import AnsibleUnsafe
from ansible.utils.unsafe_proxy import (
AnsibleUnsafe,
wrap_var as ansible_make_unsafe,
)

from ansible_collections.community.hrobot.plugins.plugin_utils.unsafe import (
make_unsafe,
Expand Down Expand Up @@ -99,6 +102,16 @@ def test_make_unsafe(value, check_unsafe_paths, check_safe_paths):
assert not isinstance(obj, AnsibleUnsafe)


def test_make_unsafe_idempotence():
assert make_unsafe(None) is None

unsafe_str = ansible_make_unsafe('{{test}}')
assert id(make_unsafe(unsafe_str)) == id(unsafe_str)

safe_str = '{{test}}'
assert id(make_unsafe(safe_str)) != id(safe_str)


def test_make_unsafe_dict_key():
value = {
b'test': 1,
Expand Down

0 comments on commit cc023a0

Please sign in to comment.