Debian Codegraph @

ansible_collections/community/hrobot/tests/unit/plugins/module_utils/test_failover.py

raw
1 # Copyright (c) 2017 Ansible Project
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 import copy
9 import json
10 import pytest
11
12 from mock import MagicMock
13 from ansible_collections.community.hrobot.plugins.module_utils import robot
14 from ansible_collections.community.hrobot.plugins.module_utils import failover
15
16
17 class ModuleFailException(Exception):
18 def __init__(self, msg, **kwargs):
19 super(ModuleFailException, self).__init__(msg)
20 self.fail_msg = msg
21 self.fail_kwargs = kwargs
22
23
24 def get_module_mock():
25 def f(msg, **kwargs):
26 raise ModuleFailException(msg, **kwargs)
27
28 module = MagicMock()
29 module.fail_json = f
30 module.from_json = json.loads
31 return module
32
33
34 # ########################################################################################
35
36 GET_FAILOVER_SUCCESS = [
37 (
38 '1.2.3.4',
39 (None, dict(
40 body=json.dumps(dict(
41 failover=dict(
42 active_server_ip='1.1.1.1',
43 ip='1.2.3.4',
44 netmask='255.255.255.255',
45 )
46 )).encode('utf-8'),
47 )),
48 '1.1.1.1',
49 dict(
50 active_server_ip='1.1.1.1',
51 ip='1.2.3.4',
52 netmask='255.255.255.255',
53 )
54 ),
55 ]
56
57
58 GET_FAILOVER_FAIL = [
59 (
60 '1.2.3.4',
61 (None, dict(
62 body=json.dumps(dict(
63 error=dict(
64 code="foo",
65 status=400,
66 message="bar",
67 ),
68 )).encode('utf-8'),
69 )),
70 'Request failed: 400 foo (bar)',
71 {
72 'error': {
73 'code': 'foo',
74 'status': 400,
75 'message': 'bar',
76 },
77 },
78 ),
79 (
80 '1.2.3.4',
81 (None, dict(
82 body='{"foo": "bar"}'.encode('utf-8'),
83 )),
84 'Cannot interpret result: {"foo": "bar"}',
85 {},
86 ),
87 ]
88
89
90 @pytest.mark.parametrize("ip, return_value, result, record", GET_FAILOVER_SUCCESS)
91 def test_get_failover_record(monkeypatch, ip, return_value, result, record):
92 module = get_module_mock()
93 robot.fetch_url = MagicMock(return_value=copy.deepcopy(return_value))
94
95 assert failover.get_failover_record(module, ip) == record
96
97
98 @pytest.mark.parametrize("ip, return_value, fail_msg, fail_kwargs", GET_FAILOVER_FAIL)
99 def test_get_failover_record_fail(monkeypatch, ip, return_value, fail_msg, fail_kwargs):
100 module = get_module_mock()
101 robot.fetch_url = MagicMock(return_value=copy.deepcopy(return_value))
102
103 with pytest.raises(ModuleFailException) as exc:
104 failover.get_failover_record(module, ip)
105
106 assert exc.value.fail_msg == fail_msg
107 assert exc.value.fail_kwargs == fail_kwargs
108
109
110 @pytest.mark.parametrize("ip, return_value, result, record", GET_FAILOVER_SUCCESS)
111 def test_get_failover(monkeypatch, ip, return_value, result, record):
112 module = get_module_mock()
113 robot.fetch_url = MagicMock(return_value=copy.deepcopy(return_value))
114
115 assert failover.get_failover(module, ip) == result
116
117
118 @pytest.mark.parametrize("ip, return_value, fail_msg, fail_kwargs", GET_FAILOVER_FAIL)
119 def test_get_failover_fail(monkeypatch, ip, return_value, fail_msg, fail_kwargs):
120 module = get_module_mock()
121 robot.fetch_url = MagicMock(return_value=copy.deepcopy(return_value))
122
123 with pytest.raises(ModuleFailException) as exc:
124 failover.get_failover(module, ip)
125
126 assert exc.value.fail_msg == fail_msg
127 assert exc.value.fail_kwargs == fail_kwargs
128
129
130 # ########################################################################################
131
132 SET_FAILOVER_SUCCESS = [
133 (
134 '1.2.3.4',
135 '1.1.1.1',
136 (None, dict(
137 body=json.dumps(dict(
138 failover=dict(
139 active_server_ip='1.1.1.2',
140 )
141 )).encode('utf-8'),
142 )),
143 ('1.1.1.2', True)
144 ),
145 (
146 '1.2.3.4',
147 '1.1.1.1',
148 (None, dict(
149 body=json.dumps(dict(
150 error=dict(
151 code="FAILOVER_ALREADY_ROUTED",
152 status=400,
153 message="Failover already routed",
154 ),
155 )).encode('utf-8'),
156 )),
157 ('1.1.1.1', False)
158 ),
159 ]
160
161
162 SET_FAILOVER_FAIL = [
163 (
164 '1.2.3.4',
165 '1.1.1.1',
166 (None, dict(
167 body=json.dumps(dict(
168 error=dict(
169 code="foo",
170 status=400,
171 message="bar",
172 ),
173 )).encode('utf-8'),
174 )),
175 'Request failed: 400 foo (bar)',
176 {
177 'error': {
178 'code': 'foo',
179 'status': 400,
180 'message': 'bar',
181 },
182 },
183 ),
184 ]
185
186
187 @pytest.mark.parametrize("ip, value, return_value, result", SET_FAILOVER_SUCCESS)
188 def test_set_failover(monkeypatch, ip, value, return_value, result):
189 module = get_module_mock()
190 robot.fetch_url = MagicMock(return_value=copy.deepcopy(return_value))
191
192 assert failover.set_failover(module, ip, value) == result
193
194
195 @pytest.mark.parametrize("ip, value, return_value, fail_msg, fail_kwargs", SET_FAILOVER_FAIL)
196 def test_set_failover_fail(monkeypatch, ip, value, return_value, fail_msg, fail_kwargs):
197 module = get_module_mock()
198 robot.fetch_url = MagicMock(return_value=copy.deepcopy(return_value))
199
200 with pytest.raises(ModuleFailException) as exc:
201 failover.set_failover(module, ip, value)
202
203 assert exc.value.fail_msg == fail_msg
204 assert exc.value.fail_kwargs == fail_kwargs