mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Add small unit test framework for testing fetch_url() based modules (#171)
* Convert integrated simple fetch_url() checking framework from hetzner_firewall tests to proper framework which can also be used by other modules. * Linting. * One more. * Use community.internal_test_tools collection.
This commit is contained in:
parent
23de3feedd
commit
703daa9500
4 changed files with 1349 additions and 1558 deletions
|
@ -7,6 +7,7 @@ unit_tests_dependencies:
|
||||||
- ansible.netcommon
|
- ansible.netcommon
|
||||||
- ansible.posix
|
- ansible.posix
|
||||||
- cisco.intersight
|
- cisco.intersight
|
||||||
|
- community.internal_test_tools
|
||||||
- community.kubernetes
|
- community.kubernetes
|
||||||
- google.cloud
|
- google.cloud
|
||||||
- ovirt.ovirt
|
- ovirt.ovirt
|
||||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -5,235 +5,236 @@ from __future__ import (absolute_import, division, print_function)
|
||||||
__metaclass__ = type
|
__metaclass__ = type
|
||||||
|
|
||||||
|
|
||||||
import pytest
|
from ansible_collections.community.internal_test_tools.tests.unit.utils.fetch_url_module_framework import (
|
||||||
|
FetchUrlCall,
|
||||||
|
BaseTestModule,
|
||||||
|
)
|
||||||
|
|
||||||
from ansible_collections.community.general.plugins.module_utils.hetzner import BASE_URL
|
from ansible_collections.community.general.plugins.module_utils.hetzner import BASE_URL
|
||||||
from ansible_collections.community.general.plugins.modules.net_tools import hetzner_firewall_info
|
from ansible_collections.community.general.plugins.modules.net_tools import hetzner_firewall_info
|
||||||
from .test_hetzner_firewall import FetchUrlCall, run_module_success, run_module_failed
|
|
||||||
|
|
||||||
|
|
||||||
# Tests for state (absent and present)
|
class TestHetznerFirewallInfo(BaseTestModule):
|
||||||
|
MOCK_ANSIBLE_MODULEUTILS_BASIC_ANSIBLEMODULE = 'ansible_collections.community.general.plugins.modules.net_tools.hetzner_firewall_info.AnsibleModule'
|
||||||
|
MOCK_ANSIBLE_MODULEUTILS_URLS_FETCH_URL = 'ansible_collections.community.general.plugins.module_utils.hetzner.fetch_url'
|
||||||
|
|
||||||
|
# Tests for state (absent and present)
|
||||||
|
|
||||||
def test_absent(mocker):
|
def test_absent(self, mocker):
|
||||||
result = run_module_success(mocker, hetzner_firewall_info, {
|
result = self.run_module_success(mocker, hetzner_firewall_info, {
|
||||||
'hetzner_user': '',
|
'hetzner_user': '',
|
||||||
'hetzner_password': '',
|
'hetzner_password': '',
|
||||||
'server_ip': '1.2.3.4',
|
'server_ip': '1.2.3.4',
|
||||||
}, [
|
}, [
|
||||||
FetchUrlCall('GET', 200)
|
FetchUrlCall('GET', 200)
|
||||||
.result_json({
|
.result_json({
|
||||||
'firewall': {
|
'firewall': {
|
||||||
'server_ip': '1.2.3.4',
|
'server_ip': '1.2.3.4',
|
||||||
'server_number': 1,
|
'server_number': 1,
|
||||||
'status': 'disabled',
|
'status': 'disabled',
|
||||||
'whitelist_hos': False,
|
'whitelist_hos': False,
|
||||||
'port': 'main',
|
'port': 'main',
|
||||||
'rules': {
|
'rules': {
|
||||||
'input': [],
|
'input': [],
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
})
|
||||||
})
|
.expect_url('{0}/firewall/1.2.3.4'.format(BASE_URL)),
|
||||||
.expect_url('{0}/firewall/1.2.3.4'.format(BASE_URL)),
|
])
|
||||||
])
|
assert result['changed'] is False
|
||||||
assert result['changed'] is False
|
assert result['firewall']['status'] == 'disabled'
|
||||||
assert result['firewall']['status'] == 'disabled'
|
assert result['firewall']['server_ip'] == '1.2.3.4'
|
||||||
assert result['firewall']['server_ip'] == '1.2.3.4'
|
assert result['firewall']['server_number'] == 1
|
||||||
assert result['firewall']['server_number'] == 1
|
|
||||||
|
|
||||||
|
def test_present(self, mocker):
|
||||||
def test_present(mocker):
|
result = self.run_module_success(mocker, hetzner_firewall_info, {
|
||||||
result = run_module_success(mocker, hetzner_firewall_info, {
|
'hetzner_user': '',
|
||||||
'hetzner_user': '',
|
'hetzner_password': '',
|
||||||
'hetzner_password': '',
|
'server_ip': '1.2.3.4',
|
||||||
'server_ip': '1.2.3.4',
|
}, [
|
||||||
}, [
|
FetchUrlCall('GET', 200)
|
||||||
FetchUrlCall('GET', 200)
|
.result_json({
|
||||||
.result_json({
|
'firewall': {
|
||||||
'firewall': {
|
'server_ip': '1.2.3.4',
|
||||||
'server_ip': '1.2.3.4',
|
'server_number': 1,
|
||||||
'server_number': 1,
|
'status': 'active',
|
||||||
'status': 'active',
|
'whitelist_hos': False,
|
||||||
'whitelist_hos': False,
|
'port': 'main',
|
||||||
'port': 'main',
|
'rules': {
|
||||||
'rules': {
|
'input': [],
|
||||||
'input': [],
|
},
|
||||||
},
|
},
|
||||||
},
|
})
|
||||||
})
|
.expect_url('{0}/firewall/1.2.3.4'.format(BASE_URL)),
|
||||||
.expect_url('{0}/firewall/1.2.3.4'.format(BASE_URL)),
|
])
|
||||||
])
|
assert result['changed'] is False
|
||||||
assert result['changed'] is False
|
assert result['firewall']['status'] == 'active'
|
||||||
assert result['firewall']['status'] == 'active'
|
assert result['firewall']['server_ip'] == '1.2.3.4'
|
||||||
assert result['firewall']['server_ip'] == '1.2.3.4'
|
assert result['firewall']['server_number'] == 1
|
||||||
assert result['firewall']['server_number'] == 1
|
assert len(result['firewall']['rules']['input']) == 0
|
||||||
assert len(result['firewall']['rules']['input']) == 0
|
|
||||||
|
|
||||||
|
def test_present_w_rules(self, mocker):
|
||||||
def test_present_w_rules(mocker):
|
result = self.run_module_success(mocker, hetzner_firewall_info, {
|
||||||
result = run_module_success(mocker, hetzner_firewall_info, {
|
'hetzner_user': '',
|
||||||
'hetzner_user': '',
|
'hetzner_password': '',
|
||||||
'hetzner_password': '',
|
'server_ip': '1.2.3.4',
|
||||||
'server_ip': '1.2.3.4',
|
}, [
|
||||||
}, [
|
FetchUrlCall('GET', 200)
|
||||||
FetchUrlCall('GET', 200)
|
.result_json({
|
||||||
.result_json({
|
'firewall': {
|
||||||
'firewall': {
|
'server_ip': '1.2.3.4',
|
||||||
'server_ip': '1.2.3.4',
|
'server_number': 1,
|
||||||
'server_number': 1,
|
'status': 'active',
|
||||||
'status': 'active',
|
'whitelist_hos': False,
|
||||||
'whitelist_hos': False,
|
'port': 'main',
|
||||||
'port': 'main',
|
'rules': {
|
||||||
'rules': {
|
'input': [
|
||||||
'input': [
|
{
|
||||||
{
|
'name': 'Accept HTTPS traffic',
|
||||||
'name': 'Accept HTTPS traffic',
|
'ip_version': 'ipv4',
|
||||||
'ip_version': 'ipv4',
|
'dst_ip': None,
|
||||||
'dst_ip': None,
|
'dst_port': '443',
|
||||||
'dst_port': '443',
|
'src_ip': None,
|
||||||
'src_ip': None,
|
'src_port': None,
|
||||||
'src_port': None,
|
'protocol': 'tcp',
|
||||||
'protocol': 'tcp',
|
'tcp_flags': None,
|
||||||
'tcp_flags': None,
|
'action': 'accept',
|
||||||
'action': 'accept',
|
},
|
||||||
},
|
{
|
||||||
{
|
'name': None,
|
||||||
'name': None,
|
'ip_version': 'ipv4',
|
||||||
'ip_version': 'ipv4',
|
'dst_ip': None,
|
||||||
'dst_ip': None,
|
'dst_port': None,
|
||||||
'dst_port': None,
|
'src_ip': None,
|
||||||
'src_ip': None,
|
'src_port': None,
|
||||||
'src_port': None,
|
'protocol': None,
|
||||||
'protocol': None,
|
'tcp_flags': None,
|
||||||
'tcp_flags': None,
|
'action': 'discard',
|
||||||
'action': 'discard',
|
}
|
||||||
}
|
],
|
||||||
],
|
},
|
||||||
},
|
},
|
||||||
},
|
})
|
||||||
})
|
.expect_url('{0}/firewall/1.2.3.4'.format(BASE_URL)),
|
||||||
.expect_url('{0}/firewall/1.2.3.4'.format(BASE_URL)),
|
])
|
||||||
])
|
assert result['changed'] is False
|
||||||
assert result['changed'] is False
|
assert result['firewall']['status'] == 'active'
|
||||||
assert result['firewall']['status'] == 'active'
|
assert result['firewall']['server_ip'] == '1.2.3.4'
|
||||||
assert result['firewall']['server_ip'] == '1.2.3.4'
|
assert result['firewall']['server_number'] == 1
|
||||||
assert result['firewall']['server_number'] == 1
|
assert len(result['firewall']['rules']['input']) == 2
|
||||||
assert len(result['firewall']['rules']['input']) == 2
|
assert result['firewall']['rules']['input'][0]['name'] == 'Accept HTTPS traffic'
|
||||||
assert result['firewall']['rules']['input'][0]['name'] == 'Accept HTTPS traffic'
|
assert result['firewall']['rules']['input'][0]['dst_port'] == '443'
|
||||||
assert result['firewall']['rules']['input'][0]['dst_port'] == '443'
|
assert result['firewall']['rules']['input'][0]['action'] == 'accept'
|
||||||
assert result['firewall']['rules']['input'][0]['action'] == 'accept'
|
assert result['firewall']['rules']['input'][1]['dst_port'] is None
|
||||||
assert result['firewall']['rules']['input'][1]['dst_port'] is None
|
assert result['firewall']['rules']['input'][1]['action'] == 'discard'
|
||||||
assert result['firewall']['rules']['input'][1]['action'] == 'discard'
|
|
||||||
|
|
||||||
|
# Tests for wait_for_configured in getting status
|
||||||
|
|
||||||
# Tests for wait_for_configured in getting status
|
def test_wait_get(self, mocker):
|
||||||
|
mocker.patch('time.sleep', lambda duration: None)
|
||||||
|
result = self.run_module_success(mocker, hetzner_firewall_info, {
|
||||||
def test_wait_get(mocker):
|
'hetzner_user': '',
|
||||||
result = run_module_success(mocker, hetzner_firewall_info, {
|
'hetzner_password': '',
|
||||||
'hetzner_user': '',
|
'server_ip': '1.2.3.4',
|
||||||
'hetzner_password': '',
|
'wait_for_configured': True,
|
||||||
'server_ip': '1.2.3.4',
|
}, [
|
||||||
'wait_for_configured': True,
|
FetchUrlCall('GET', 200)
|
||||||
}, [
|
.result_json({
|
||||||
FetchUrlCall('GET', 200)
|
'firewall': {
|
||||||
.result_json({
|
'server_ip': '1.2.3.4',
|
||||||
'firewall': {
|
'server_number': 1,
|
||||||
'server_ip': '1.2.3.4',
|
'status': 'in process',
|
||||||
'server_number': 1,
|
'whitelist_hos': False,
|
||||||
'status': 'in process',
|
'port': 'main',
|
||||||
'whitelist_hos': False,
|
'rules': {
|
||||||
'port': 'main',
|
'input': [],
|
||||||
'rules': {
|
},
|
||||||
'input': [],
|
|
||||||
},
|
},
|
||||||
},
|
})
|
||||||
})
|
.expect_url('{0}/firewall/1.2.3.4'.format(BASE_URL)),
|
||||||
.expect_url('{0}/firewall/1.2.3.4'.format(BASE_URL)),
|
FetchUrlCall('GET', 200)
|
||||||
FetchUrlCall('GET', 200)
|
.result_json({
|
||||||
.result_json({
|
'firewall': {
|
||||||
'firewall': {
|
'server_ip': '1.2.3.4',
|
||||||
'server_ip': '1.2.3.4',
|
'server_number': 1,
|
||||||
'server_number': 1,
|
'status': 'active',
|
||||||
'status': 'active',
|
'whitelist_hos': False,
|
||||||
'whitelist_hos': False,
|
'port': 'main',
|
||||||
'port': 'main',
|
'rules': {
|
||||||
'rules': {
|
'input': [],
|
||||||
'input': [],
|
},
|
||||||
},
|
},
|
||||||
},
|
})
|
||||||
})
|
.expect_url('{0}/firewall/1.2.3.4'.format(BASE_URL)),
|
||||||
.expect_url('{0}/firewall/1.2.3.4'.format(BASE_URL)),
|
])
|
||||||
])
|
assert result['changed'] is False
|
||||||
assert result['changed'] is False
|
assert result['firewall']['status'] == 'active'
|
||||||
assert result['firewall']['status'] == 'active'
|
assert result['firewall']['server_ip'] == '1.2.3.4'
|
||||||
assert result['firewall']['server_ip'] == '1.2.3.4'
|
assert result['firewall']['server_number'] == 1
|
||||||
assert result['firewall']['server_number'] == 1
|
|
||||||
|
|
||||||
|
def test_wait_get_timeout(self, mocker):
|
||||||
def test_wait_get_timeout(mocker):
|
mocker.patch('time.sleep', lambda duration: None)
|
||||||
result = run_module_failed(mocker, hetzner_firewall_info, {
|
result = self.run_module_failed(mocker, hetzner_firewall_info, {
|
||||||
'hetzner_user': '',
|
'hetzner_user': '',
|
||||||
'hetzner_password': '',
|
'hetzner_password': '',
|
||||||
'server_ip': '1.2.3.4',
|
'server_ip': '1.2.3.4',
|
||||||
'wait_for_configured': True,
|
'wait_for_configured': True,
|
||||||
'timeout': 0,
|
'timeout': 0,
|
||||||
}, [
|
}, [
|
||||||
FetchUrlCall('GET', 200)
|
FetchUrlCall('GET', 200)
|
||||||
.result_json({
|
.result_json({
|
||||||
'firewall': {
|
'firewall': {
|
||||||
'server_ip': '1.2.3.4',
|
'server_ip': '1.2.3.4',
|
||||||
'server_number': 1,
|
'server_number': 1,
|
||||||
'status': 'in process',
|
'status': 'in process',
|
||||||
'whitelist_hos': False,
|
'whitelist_hos': False,
|
||||||
'port': 'main',
|
'port': 'main',
|
||||||
'rules': {
|
'rules': {
|
||||||
'input': [],
|
'input': [],
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
})
|
||||||
})
|
.expect_url('{0}/firewall/1.2.3.4'.format(BASE_URL)),
|
||||||
.expect_url('{0}/firewall/1.2.3.4'.format(BASE_URL)),
|
FetchUrlCall('GET', 200)
|
||||||
FetchUrlCall('GET', 200)
|
.result_json({
|
||||||
.result_json({
|
'firewall': {
|
||||||
'firewall': {
|
'server_ip': '1.2.3.4',
|
||||||
'server_ip': '1.2.3.4',
|
'server_number': 1,
|
||||||
'server_number': 1,
|
'status': 'in process',
|
||||||
'status': 'in process',
|
'whitelist_hos': False,
|
||||||
'whitelist_hos': False,
|
'port': 'main',
|
||||||
'port': 'main',
|
'rules': {
|
||||||
'rules': {
|
'input': [],
|
||||||
'input': [],
|
},
|
||||||
},
|
},
|
||||||
},
|
})
|
||||||
})
|
.expect_url('{0}/firewall/1.2.3.4'.format(BASE_URL)),
|
||||||
.expect_url('{0}/firewall/1.2.3.4'.format(BASE_URL)),
|
])
|
||||||
])
|
assert result['msg'] == 'Timeout while waiting for firewall to be configured.'
|
||||||
assert result['msg'] == 'Timeout while waiting for firewall to be configured.'
|
|
||||||
|
|
||||||
|
def test_nowait_get(self, mocker):
|
||||||
def test_nowait_get(mocker):
|
result = self.run_module_success(mocker, hetzner_firewall_info, {
|
||||||
result = run_module_success(mocker, hetzner_firewall_info, {
|
'hetzner_user': '',
|
||||||
'hetzner_user': '',
|
'hetzner_password': '',
|
||||||
'hetzner_password': '',
|
'server_ip': '1.2.3.4',
|
||||||
'server_ip': '1.2.3.4',
|
'wait_for_configured': False,
|
||||||
'wait_for_configured': False,
|
}, [
|
||||||
}, [
|
FetchUrlCall('GET', 200)
|
||||||
FetchUrlCall('GET', 200)
|
.result_json({
|
||||||
.result_json({
|
'firewall': {
|
||||||
'firewall': {
|
'server_ip': '1.2.3.4',
|
||||||
'server_ip': '1.2.3.4',
|
'server_number': 1,
|
||||||
'server_number': 1,
|
'status': 'in process',
|
||||||
'status': 'in process',
|
'whitelist_hos': False,
|
||||||
'whitelist_hos': False,
|
'port': 'main',
|
||||||
'port': 'main',
|
'rules': {
|
||||||
'rules': {
|
'input': [],
|
||||||
'input': [],
|
},
|
||||||
},
|
},
|
||||||
},
|
})
|
||||||
})
|
.expect_url('{0}/firewall/1.2.3.4'.format(BASE_URL)),
|
||||||
.expect_url('{0}/firewall/1.2.3.4'.format(BASE_URL)),
|
])
|
||||||
])
|
assert result['changed'] is False
|
||||||
assert result['changed'] is False
|
assert result['firewall']['status'] == 'in process'
|
||||||
assert result['firewall']['status'] == 'in process'
|
assert result['firewall']['server_ip'] == '1.2.3.4'
|
||||||
assert result['firewall']['server_ip'] == '1.2.3.4'
|
assert result['firewall']['server_number'] == 1
|
||||||
assert result['firewall']['server_number'] == 1
|
|
||||||
|
|
|
@ -60,6 +60,7 @@ retry ansible-galaxy -vvv collection install ansible.posix
|
||||||
# https://github.com/CiscoDevNet/ansible-intersight/issues/9
|
# https://github.com/CiscoDevNet/ansible-intersight/issues/9
|
||||||
retry ansible-galaxy -vvv collection install cisco.intersight:1.0.4
|
retry ansible-galaxy -vvv collection install cisco.intersight:1.0.4
|
||||||
retry ansible-galaxy -vvv collection install community.crypto
|
retry ansible-galaxy -vvv collection install community.crypto
|
||||||
|
retry ansible-galaxy -vvv collection install community.internal_test_tools
|
||||||
retry ansible-galaxy -vvv collection install community.kubernetes
|
retry ansible-galaxy -vvv collection install community.kubernetes
|
||||||
retry ansible-galaxy -vvv collection install google.cloud
|
retry ansible-galaxy -vvv collection install google.cloud
|
||||||
retry ansible-galaxy -vvv collection install ovirt.ovirt
|
retry ansible-galaxy -vvv collection install ovirt.ovirt
|
||||||
|
|
Loading…
Reference in a new issue