| 1 |
# Copyright (c) 2019 Felix Fontein <[email protected]> |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
from __future__ import (absolute_import, division, print_function) |
| 6 |
__metaclass__ = type |
| 7 |
|
| 8 |
|
| 9 |
from ansible_collections.community.internal_test_tools.tests.unit.utils.fetch_url_module_framework import ( |
| 10 |
FetchUrlCall, |
| 11 |
BaseTestModule, |
| 12 |
) |
| 13 |
|
| 14 |
from ansible_collections.community.hrobot.plugins.module_utils.robot import BASE_URL |
| 15 |
from ansible_collections.community.hrobot.plugins.modules import reverse_dns |
| 16 |
|
| 17 |
|
| 18 |
class TestHetznerReverseDNS(BaseTestModule): |
| 19 |
MOCK_ANSIBLE_MODULEUTILS_BASIC_ANSIBLEMODULE = 'ansible_collections.community.hrobot.plugins.modules.reverse_dns.AnsibleModule' |
| 20 |
MOCK_ANSIBLE_MODULEUTILS_URLS_FETCH_URL = 'ansible_collections.community.hrobot.plugins.module_utils.robot.fetch_url' |
| 21 |
|
| 22 |
def test_idempotent_present(self, mocker): |
| 23 |
result = self.run_module_success(mocker, reverse_dns, { |
| 24 |
'hetzner_user': 'test', |
| 25 |
'hetzner_password': 'hunter2', |
| 26 |
'ip': '1.2.3.4', |
| 27 |
'value': 'foo.example.com', |
| 28 |
}, [ |
| 29 |
FetchUrlCall('GET', 200) |
| 30 |
.expect_basic_auth('test', 'hunter2') |
| 31 |
.expect_force_basic_auth(True) |
| 32 |
.result_json({ |
| 33 |
'rdns': { |
| 34 |
'ip': '1.2.3.4', |
| 35 |
'ptr': 'foo.example.com', |
| 36 |
}, |
| 37 |
}) |
| 38 |
.expect_url('{0}/rdns/1.2.3.4'.format(BASE_URL)), |
| 39 |
]) |
| 40 |
assert result['changed'] is False |
| 41 |
|
| 42 |
def test_idempotent_absent(self, mocker): |
| 43 |
result = self.run_module_success(mocker, reverse_dns, { |
| 44 |
'hetzner_user': '', |
| 45 |
'hetzner_password': '', |
| 46 |
'ip': '1.2.3.4', |
| 47 |
'state': 'absent', |
| 48 |
}, [ |
| 49 |
FetchUrlCall('GET', 404) |
| 50 |
.result_json({ |
| 51 |
'error': { |
| 52 |
'status': 404, |
| 53 |
'code': 'RDNS_NOT_FOUND', |
| 54 |
'message': 'The IP address 1.2.3.4 has no reverse DNS entry yet', |
| 55 |
}, |
| 56 |
}) |
| 57 |
.expect_url('{0}/rdns/1.2.3.4'.format(BASE_URL)), |
| 58 |
]) |
| 59 |
assert result['changed'] is False |
| 60 |
|
| 61 |
def test_set_check_mode(self, mocker): |
| 62 |
result = self.run_module_success(mocker, reverse_dns, { |
| 63 |
'hetzner_user': '', |
| 64 |
'hetzner_password': '', |
| 65 |
'ip': '1.2.3.4', |
| 66 |
'value': 'foo.example.com', |
| 67 |
'_ansible_check_mode': True, |
| 68 |
}, [ |
| 69 |
FetchUrlCall('GET', 404) |
| 70 |
.result_json({ |
| 71 |
'error': { |
| 72 |
'status': 404, |
| 73 |
'code': 'RDNS_NOT_FOUND', |
| 74 |
'message': 'The IP address 1.2.3.4 has no reverse DNS entry yet', |
| 75 |
}, |
| 76 |
}) |
| 77 |
.expect_url('{0}/rdns/1.2.3.4'.format(BASE_URL)), |
| 78 |
]) |
| 79 |
assert result['changed'] is True |
| 80 |
|
| 81 |
def test_set(self, mocker): |
| 82 |
result = self.run_module_success(mocker, reverse_dns, { |
| 83 |
'hetzner_user': '', |
| 84 |
'hetzner_password': '', |
| 85 |
'ip': '1.2.3.4', |
| 86 |
'value': 'foo.example.com', |
| 87 |
}, [ |
| 88 |
FetchUrlCall('GET', 404) |
| 89 |
.result_json({ |
| 90 |
'error': { |
| 91 |
'status': 404, |
| 92 |
'code': 'RDNS_NOT_FOUND', |
| 93 |
'message': 'The IP address 1.2.3.4 has no reverse DNS entry yet', |
| 94 |
}, |
| 95 |
}) |
| 96 |
.expect_url('{0}/rdns/1.2.3.4'.format(BASE_URL)), |
| 97 |
FetchUrlCall('POST', 201) |
| 98 |
.expect_form_value('ptr', 'foo.example.com') |
| 99 |
.result_json({ |
| 100 |
'rdns': { |
| 101 |
'ip': '1.2.3.4', |
| 102 |
'ptr': 'foo.example.com', |
| 103 |
}, |
| 104 |
}) |
| 105 |
.expect_url('{0}/rdns/1.2.3.4'.format(BASE_URL)), |
| 106 |
]) |
| 107 |
assert result['changed'] is True |
| 108 |
|
| 109 |
def test_remove_check_mode(self, mocker): |
| 110 |
result = self.run_module_success(mocker, reverse_dns, { |
| 111 |
'hetzner_user': '', |
| 112 |
'hetzner_password': '', |
| 113 |
'ip': '1.2.3.4', |
| 114 |
'state': 'absent', |
| 115 |
'_ansible_check_mode': True, |
| 116 |
}, [ |
| 117 |
FetchUrlCall('GET', 200) |
| 118 |
.result_json({ |
| 119 |
'rdns': { |
| 120 |
'ip': '1.2.3.4', |
| 121 |
'ptr': 'foo.example.com', |
| 122 |
}, |
| 123 |
}) |
| 124 |
.expect_url('{0}/rdns/1.2.3.4'.format(BASE_URL)), |
| 125 |
]) |
| 126 |
assert result['changed'] is True |
| 127 |
|
| 128 |
def test_remove(self, mocker): |
| 129 |
result = self.run_module_success(mocker, reverse_dns, { |
| 130 |
'hetzner_user': '', |
| 131 |
'hetzner_password': '', |
| 132 |
'ip': '1.2.3.4', |
| 133 |
'state': 'absent', |
| 134 |
}, [ |
| 135 |
FetchUrlCall('GET', 200) |
| 136 |
.result_json({ |
| 137 |
'rdns': { |
| 138 |
'ip': '1.2.3.4', |
| 139 |
'ptr': 'foo.example.com', |
| 140 |
}, |
| 141 |
}) |
| 142 |
.expect_url('{0}/rdns/1.2.3.4'.format(BASE_URL)), |
| 143 |
FetchUrlCall('DELETE', 200) |
| 144 |
.expect_url('{0}/rdns/1.2.3.4'.format(BASE_URL)), |
| 145 |
]) |
| 146 |
assert result['changed'] is True |
| 147 |
|
| 148 |
def test_bad_ip(self, mocker): |
| 149 |
result = self.run_module_failed(mocker, reverse_dns, { |
| 150 |
'hetzner_user': '', |
| 151 |
'hetzner_password': '', |
| 152 |
'ip': '1.2.3.4', |
| 153 |
'value': 'foo.example.com', |
| 154 |
}, [ |
| 155 |
FetchUrlCall('GET', 404) |
| 156 |
.result_json({ |
| 157 |
'error': { |
| 158 |
'status': 404, |
| 159 |
'code': 'IP_NOT_FOUND', |
| 160 |
'message': 'The IP address 1.2.3.4 was not found', |
| 161 |
}, |
| 162 |
}) |
| 163 |
.expect_url('{0}/rdns/1.2.3.4'.format(BASE_URL)), |
| 164 |
]) |
| 165 |
assert result['msg'] == 'The IP address was not found' |