From d5d24302b67677a8d27372b481cfd7c15d44fde5 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Mon, 16 Nov 2020 08:17:55 +0100 Subject: [PATCH] Add hetzner_failover_ip* tests (#1301) (#1310) * Add tests for hetzner_failover_ip_info. * Add basic tests for hetzner_failover_ip. (cherry picked from commit 41550b5205276c78f5c38f1f0c27af0e2f220f8b) Co-authored-by: Felix Fontein --- .../net_tools/test_hetzner_failover_ip.py | 219 ++++++++++++++++++ .../test_hetzner_failover_ip_info.py | 71 ++++++ 2 files changed, 290 insertions(+) create mode 100644 tests/unit/plugins/modules/net_tools/test_hetzner_failover_ip.py create mode 100644 tests/unit/plugins/modules/net_tools/test_hetzner_failover_ip_info.py diff --git a/tests/unit/plugins/modules/net_tools/test_hetzner_failover_ip.py b/tests/unit/plugins/modules/net_tools/test_hetzner_failover_ip.py new file mode 100644 index 0000000000..47c076054d --- /dev/null +++ b/tests/unit/plugins/modules/net_tools/test_hetzner_failover_ip.py @@ -0,0 +1,219 @@ +# (c) 2020 Felix Fontein +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + + +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.modules.net_tools import hetzner_failover_ip + + +class TestHetznerFailoverIP(BaseTestModule): + MOCK_ANSIBLE_MODULEUTILS_BASIC_ANSIBLEMODULE = 'ansible_collections.community.general.plugins.modules.net_tools.hetzner_failover_ip.AnsibleModule' + MOCK_ANSIBLE_MODULEUTILS_URLS_FETCH_URL = 'ansible_collections.community.general.plugins.module_utils.hetzner.fetch_url' + + # Tests for state idempotence (routed and unrouted) + + def test_unrouted(self, mocker): + result = self.run_module_success(mocker, hetzner_failover_ip, { + 'hetzner_user': '', + 'hetzner_password': '', + 'failover_ip': '1.2.3.4', + 'state': 'unrouted', + }, [ + FetchUrlCall('GET', 200) + .result_json({ + 'failover': { + 'ip': '1.2.3.4', + 'netmask': '255.255.255.255', + 'server_ip': '2.3.4.5', + 'server_number': 2345, + 'active_server_ip': None, + }, + }) + .expect_url('{0}/failover/1.2.3.4'.format(BASE_URL)), + ]) + assert result['changed'] is False + assert result['value'] is None + assert result['state'] == 'unrouted' + + def test_routed(self, mocker): + result = self.run_module_success(mocker, hetzner_failover_ip, { + 'hetzner_user': '', + 'hetzner_password': '', + 'failover_ip': '1.2.3.4', + 'state': 'routed', + 'value': '4.3.2.1', + }, [ + FetchUrlCall('GET', 200) + .result_json({ + 'failover': { + 'ip': '1.2.3.4', + 'netmask': '255.255.255.255', + 'server_ip': '2.3.4.5', + 'server_number': 2345, + 'active_server_ip': '4.3.2.1', + }, + }) + .expect_url('{0}/failover/1.2.3.4'.format(BASE_URL)), + ]) + assert result['changed'] is False + assert result['value'] == '4.3.2.1' + assert result['state'] == 'routed' + + # Tests for changing state (unrouted to routed, vice versa) + + def test_unrouted_to_routed(self, mocker): + result = self.run_module_success(mocker, hetzner_failover_ip, { + 'hetzner_user': '', + 'hetzner_password': '', + 'failover_ip': '1.2.3.4', + 'state': 'routed', + 'value': '4.3.2.1', + }, [ + FetchUrlCall('GET', 200) + .result_json({ + 'failover': { + 'ip': '1.2.3.4', + 'netmask': '255.255.255.255', + 'server_ip': '2.3.4.5', + 'server_number': 2345, + 'active_server_ip': None, + }, + }) + .expect_url('{0}/failover/1.2.3.4'.format(BASE_URL)), + FetchUrlCall('POST', 200) + .result_json({ + 'failover': { + 'ip': '1.2.3.4', + 'netmask': '255.255.255.255', + 'server_ip': '2.3.4.5', + 'server_number': 2345, + 'active_server_ip': '4.3.2.1', + }, + }) + .expect_form_value('active_server_ip', '4.3.2.1') + .expect_url('{0}/failover/1.2.3.4'.format(BASE_URL)), + ]) + assert result['changed'] is True + assert result['value'] == '4.3.2.1' + assert result['state'] == 'routed' + + def test_routed_to_unrouted(self, mocker): + result = self.run_module_success(mocker, hetzner_failover_ip, { + 'hetzner_user': '', + 'hetzner_password': '', + 'failover_ip': '1.2.3.4', + 'state': 'unrouted', + }, [ + FetchUrlCall('GET', 200) + .result_json({ + 'failover': { + 'ip': '1.2.3.4', + 'netmask': '255.255.255.255', + 'server_ip': '2.3.4.5', + 'server_number': 2345, + 'active_server_ip': '4.3.2.1', + }, + }) + .expect_url('{0}/failover/1.2.3.4'.format(BASE_URL)), + FetchUrlCall('DELETE', 200) + .result_json({ + 'failover': { + 'ip': '1.2.3.4', + 'netmask': '255.255.255.255', + 'server_ip': '2.3.4.5', + 'server_number': 2345, + 'active_server_ip': None, + }, + }) + .expect_url('{0}/failover/1.2.3.4'.format(BASE_URL)), + ]) + assert result['changed'] is True + assert result['value'] is None + assert result['state'] == 'unrouted' + + # Tests for re-routing + + def test_rerouting(self, mocker): + result = self.run_module_success(mocker, hetzner_failover_ip, { + 'hetzner_user': '', + 'hetzner_password': '', + 'failover_ip': '1.2.3.4', + 'state': 'routed', + 'value': '4.3.2.1', + }, [ + FetchUrlCall('GET', 200) + .result_json({ + 'failover': { + 'ip': '1.2.3.4', + 'netmask': '255.255.255.255', + 'server_ip': '2.3.4.5', + 'server_number': 2345, + 'active_server_ip': '5.4.3.2', + }, + }) + .expect_url('{0}/failover/1.2.3.4'.format(BASE_URL)), + FetchUrlCall('POST', 200) + .result_json({ + 'failover': { + 'ip': '1.2.3.4', + 'netmask': '255.255.255.255', + 'server_ip': '2.3.4.5', + 'server_number': 2345, + 'active_server_ip': '4.3.2.1', + }, + }) + .expect_form_value('active_server_ip', '4.3.2.1') + .expect_url('{0}/failover/1.2.3.4'.format(BASE_URL)), + ]) + assert result['changed'] is True + assert result['value'] == '4.3.2.1' + assert result['state'] == 'routed' + + def test_rerouting_already_routed(self, mocker): + result = self.run_module_success(mocker, hetzner_failover_ip, { + 'hetzner_user': '', + 'hetzner_password': '', + 'failover_ip': '1.2.3.4', + 'state': 'routed', + 'value': '4.3.2.1', + }, [ + FetchUrlCall('GET', 200) + .result_json({ + 'failover': { + 'ip': '1.2.3.4', + 'netmask': '255.255.255.255', + 'server_ip': '2.3.4.5', + 'server_number': 2345, + 'active_server_ip': '5.4.3.2', + }, + }) + .expect_url('{0}/failover/1.2.3.4'.format(BASE_URL)), + FetchUrlCall('POST', 409) + .result_json({ + 'error': { + 'status': 409, + 'code': 'FAILOVER_ALREADY_ROUTED', + 'message': 'Failover already routed', + }, + 'failover': { + 'ip': '1.2.3.4', + 'netmask': '255.255.255.255', + 'server_ip': '2.3.4.5', + 'server_number': 2345, + 'active_server_ip': '4.3.2.1', + }, + }) + .expect_form_value('active_server_ip', '4.3.2.1') + .expect_url('{0}/failover/1.2.3.4'.format(BASE_URL)), + ]) + assert result['changed'] is False + assert result['value'] == '4.3.2.1' + assert result['state'] == 'routed' diff --git a/tests/unit/plugins/modules/net_tools/test_hetzner_failover_ip_info.py b/tests/unit/plugins/modules/net_tools/test_hetzner_failover_ip_info.py new file mode 100644 index 0000000000..cbb11e8b6a --- /dev/null +++ b/tests/unit/plugins/modules/net_tools/test_hetzner_failover_ip_info.py @@ -0,0 +1,71 @@ +# (c) 2020 Felix Fontein +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + + +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.modules.net_tools import hetzner_failover_ip_info + + +class TestHetznerFailoverIPInfo(BaseTestModule): + MOCK_ANSIBLE_MODULEUTILS_BASIC_ANSIBLEMODULE = 'ansible_collections.community.general.plugins.modules.net_tools.hetzner_failover_ip_info.AnsibleModule' + MOCK_ANSIBLE_MODULEUTILS_URLS_FETCH_URL = 'ansible_collections.community.general.plugins.module_utils.hetzner.fetch_url' + + # Tests for state (routed and unrouted) + + def test_unrouted(self, mocker): + result = self.run_module_success(mocker, hetzner_failover_ip_info, { + 'hetzner_user': '', + 'hetzner_password': '', + 'failover_ip': '1.2.3.4', + }, [ + FetchUrlCall('GET', 200) + .result_json({ + 'failover': { + 'ip': '1.2.3.4', + 'netmask': '255.255.255.255', + 'server_ip': '2.3.4.5', + 'server_number': 2345, + 'active_server_ip': None, + }, + }) + .expect_url('{0}/failover/1.2.3.4'.format(BASE_URL)), + ]) + assert result['changed'] is False + assert result['value'] is None + assert result['state'] == 'unrouted' + assert result['failover_ip'] == '1.2.3.4' + assert result['server_ip'] == '2.3.4.5' + assert result['server_number'] == 2345 + + def test_routed(self, mocker): + result = self.run_module_success(mocker, hetzner_failover_ip_info, { + 'hetzner_user': '', + 'hetzner_password': '', + 'failover_ip': '1.2.3.4', + }, [ + FetchUrlCall('GET', 200) + .result_json({ + 'failover': { + 'ip': '1.2.3.4', + 'netmask': '255.255.255.255', + 'server_ip': '2.3.4.5', + 'server_number': 2345, + 'active_server_ip': '4.3.2.1', + }, + }) + .expect_url('{0}/failover/1.2.3.4'.format(BASE_URL)), + ]) + assert result['changed'] is False + assert result['value'] == '4.3.2.1' + assert result['state'] == 'routed' + assert result['failover_ip'] == '1.2.3.4' + assert result['server_ip'] == '2.3.4.5' + assert result['server_number'] == 2345