Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/modules/test_redis_data.py

raw
1 #
2 # Copyright (c) 2021, Andreas Botzner <andreas at botzner dot com>
3 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4 # SPDX-License-Identifier: GPL-3.0-or-later
5
6 from __future__ import annotations
7
8 import json
9
10 import pytest
11 from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import set_module_args
12 from redis import __version__
13
14 from ansible_collections.community.general.plugins.modules import redis_data
15
16 HAS_REDIS_USERNAME_OPTION = True
17 if tuple(map(int, __version__.split("."))) < (3, 4, 0):
18 HAS_REDIS_USERNAME_OPTION = False
19
20
21 def test_redis_data_without_arguments(capfd):
22 with set_module_args({}):
23 with pytest.raises(SystemExit):
24 redis_data.main()
25 out, err = capfd.readouterr()
26 assert not err
27 assert json.loads(out)["failed"]
28
29
30 @pytest.mark.skipif(not HAS_REDIS_USERNAME_OPTION, reason="Redis version < 3.4.0")
31 def test_redis_data_key(capfd, mocker):
32 with set_module_args(
33 {
34 "login_host": "localhost",
35 "login_user": "root",
36 "login_password": "secret",
37 "key": "foo",
38 "value": "baz",
39 "_ansible_check_mode": False,
40 }
41 ):
42 mocker.patch("redis.Redis.get", return_value="bar")
43 mocker.patch("redis.Redis.set", return_value=True)
44 with pytest.raises(SystemExit):
45 redis_data.main()
46 out, err = capfd.readouterr()
47 print(out)
48 assert not err
49 assert json.loads(out)["old_value"] == "bar"
50 assert json.loads(out)["value"] == "baz"
51 assert json.loads(out)["msg"] == "Set key: foo"
52 assert json.loads(out)["changed"] is True
53
54
55 @pytest.mark.skipif(not HAS_REDIS_USERNAME_OPTION, reason="Redis version < 3.4.0")
56 def test_redis_data_existing_key_nx(capfd, mocker):
57 with set_module_args(
58 {
59 "login_host": "localhost",
60 "login_user": "root",
61 "login_password": "secret",
62 "key": "foo",
63 "value": "baz",
64 "non_existing": True,
65 "_ansible_check_mode": False,
66 }
67 ):
68 mocker.patch("redis.Redis.get", return_value="bar")
69 mocker.patch("redis.Redis.set", return_value=None)
70 with pytest.raises(SystemExit):
71 redis_data.main()
72 out, err = capfd.readouterr()
73 print(out)
74 assert not err
75 assert json.loads(out)["old_value"] == "bar"
76 assert "value" not in json.loads(out)
77 assert json.loads(out)["msg"] == "Could not set key: foo. Key already present."
78 assert json.loads(out)["changed"] is False
79 assert json.loads(out)["failed"] is True
80
81
82 @pytest.mark.skipif(not HAS_REDIS_USERNAME_OPTION, reason="Redis version < 3.4.0")
83 def test_redis_data_non_existing_key_xx(capfd, mocker):
84 with set_module_args(
85 {
86 "login_host": "localhost",
87 "login_user": "root",
88 "login_password": "secret",
89 "key": "foo",
90 "value": "baz",
91 "existing": True,
92 "_ansible_check_mode": False,
93 }
94 ):
95 mocker.patch("redis.Redis.get", return_value=None)
96 mocker.patch("redis.Redis.set", return_value=None)
97 with pytest.raises(SystemExit):
98 redis_data.main()
99 out, err = capfd.readouterr()
100 print(out)
101 assert not err
102 assert json.loads(out)["old_value"] is None
103 assert "value" not in json.loads(out)
104 assert json.loads(out)["msg"] == "Could not set key: foo. Key not present."
105 assert json.loads(out)["changed"] is False
106 assert json.loads(out)["failed"] is True
107
108
109 @pytest.mark.skipif(not HAS_REDIS_USERNAME_OPTION, reason="Redis version < 3.4.0")
110 def test_redis_data_delete_present_key(capfd, mocker):
111 with set_module_args(
112 {"login_host": "localhost", "login_user": "root", "login_password": "secret", "key": "foo", "state": "absent"}
113 ):
114 mocker.patch("redis.Redis.get", return_value="bar")
115 mocker.patch("redis.Redis.delete", return_value=1)
116 with pytest.raises(SystemExit):
117 redis_data.main()
118 out, err = capfd.readouterr()
119 print(out)
120 assert not err
121 assert json.loads(out)["msg"] == "Deleted key: foo"
122 assert json.loads(out)["changed"] is True
123
124
125 @pytest.mark.skipif(not HAS_REDIS_USERNAME_OPTION, reason="Redis version < 3.4.0")
126 def test_redis_data_delete_absent_key(capfd, mocker):
127 with set_module_args(
128 {"login_host": "localhost", "login_user": "root", "login_password": "secret", "key": "foo", "state": "absent"}
129 ):
130 mocker.patch("redis.Redis.delete", return_value=0)
131 mocker.patch("redis.Redis.get", return_value=None)
132 with pytest.raises(SystemExit):
133 redis_data.main()
134 out, err = capfd.readouterr()
135 print(out)
136 assert not err
137 assert json.loads(out)["msg"] == "Key: foo not present"
138 assert json.loads(out)["changed"] is False
139
140
141 @pytest.mark.skipif(HAS_REDIS_USERNAME_OPTION, reason="Redis version > 3.4.0")
142 def test_redis_data_fail_username(capfd, mocker):
143 with set_module_args(
144 {
145 "login_host": "localhost",
146 "login_user": "root",
147 "login_password": "secret",
148 "key": "foo",
149 "value": "baz",
150 "_ansible_check_mode": False,
151 }
152 ):
153 with pytest.raises(SystemExit):
154 redis_data.main()
155 out, err = capfd.readouterr()
156 print(out)
157 assert not err
158 assert json.loads(out)["failed"]
159 assert json.loads(out)["msg"] == "The option `username` in only supported with redis >= 3.4.0."
160
161
162 def test_redis_data_key_no_username(capfd, mocker):
163 with set_module_args(
164 {
165 "login_host": "localhost",
166 "login_password": "secret",
167 "key": "foo",
168 "value": "baz",
169 "_ansible_check_mode": False,
170 }
171 ):
172 mocker.patch("redis.Redis.get", return_value="bar")
173 mocker.patch("redis.Redis.set", return_value=True)
174 with pytest.raises(SystemExit):
175 redis_data.main()
176 out, err = capfd.readouterr()
177 print(out)
178 assert not err
179 assert json.loads(out)["old_value"] == "bar"
180 assert json.loads(out)["value"] == "baz"
181 assert json.loads(out)["msg"] == "Set key: foo"
182 assert json.loads(out)["changed"] is True
183
184
185 def test_redis_delete_key_no_username(capfd, mocker):
186 with set_module_args(
187 {
188 "login_host": "localhost",
189 "login_password": "secret",
190 "key": "foo",
191 "state": "absent",
192 "_ansible_check_mode": False,
193 }
194 ):
195 mocker.patch("redis.Redis.get", return_value="bar")
196 mocker.patch("redis.Redis.delete", return_value=1)
197 with pytest.raises(SystemExit):
198 redis_data.main()
199 out, err = capfd.readouterr()
200 print(out)
201 assert not err
202 assert json.loads(out)["msg"] == "Deleted key: foo"
203 assert json.loads(out)["changed"] is True
204
205
206 def test_redis_delete_key_non_existent_key(capfd, mocker):
207 with set_module_args(
208 {
209 "login_host": "localhost",
210 "login_password": "secret",
211 "key": "foo",
212 "state": "absent",
213 "_ansible_check_mode": False,
214 }
215 ):
216 mocker.patch("redis.Redis.get", return_value=None)
217 mocker.patch("redis.Redis.delete", return_value=0)
218 with pytest.raises(SystemExit):
219 redis_data.main()
220 out, err = capfd.readouterr()
221 print(out)
222 assert not err
223 assert json.loads(out)["msg"] == "Key: foo not present"
224 assert json.loads(out)["changed"] is False
225
226
227 def test_redis_set_key_check_mode_nochange(capfd, mocker):
228 with set_module_args(
229 {
230 "login_host": "localhost",
231 "login_password": "secret",
232 "key": "foo",
233 "state": "present",
234 "value": "bar",
235 "_ansible_check_mode": True,
236 }
237 ):
238 mocker.patch("redis.Redis.get", return_value="bar")
239 with pytest.raises(SystemExit):
240 redis_data.main()
241 out, err = capfd.readouterr()
242 print(out)
243 assert not err
244 assert json.loads(out)["msg"] == "Key foo already has desired value"
245 assert json.loads(out)["value"] == "bar"
246 assert not json.loads(out)["changed"]
247 assert json.loads(out)["old_value"] == "bar"
248
249
250 def test_redis_set_key_check_mode_delete_nx(capfd, mocker):
251 with set_module_args(
252 {
253 "login_host": "localhost",
254 "login_password": "secret",
255 "key": "foo",
256 "state": "present",
257 "value": "baz",
258 "_ansible_check_mode": True,
259 }
260 ):
261 mocker.patch("redis.Redis.get", return_value=None)
262 with pytest.raises(SystemExit):
263 redis_data.main()
264 out, err = capfd.readouterr()
265 print(out)
266 assert not err
267 assert json.loads(out)["msg"] == "Set key: foo"
268 assert json.loads(out)["value"] == "baz"
269 assert json.loads(out)["old_value"] is None
270
271
272 def test_redis_set_key_check_mode_delete(capfd, mocker):
273 with set_module_args(
274 {
275 "login_host": "localhost",
276 "login_password": "secret",
277 "key": "foo",
278 "state": "present",
279 "value": "baz",
280 "_ansible_check_mode": True,
281 }
282 ):
283 mocker.patch("redis.Redis.get", return_value="bar")
284 with pytest.raises(SystemExit):
285 redis_data.main()
286 out, err = capfd.readouterr()
287 print(out)
288 assert not err
289 assert json.loads(out)["msg"] == "Set key: foo"
290 assert json.loads(out)["value"] == "baz"
291 assert json.loads(out)["old_value"] == "bar"
292
293
294 def test_redis_set_key_check_mode(capfd, mocker):
295 with set_module_args(
296 {
297 "login_host": "localhost",
298 "login_password": "secret",
299 "key": "foo",
300 "state": "present",
301 "value": "baz",
302 "_ansible_check_mode": True,
303 }
304 ):
305 mocker.patch("redis.Redis.get", return_value="bar")
306 with pytest.raises(SystemExit):
307 redis_data.main()
308 out, err = capfd.readouterr()
309 print(out)
310 assert not err
311 assert json.loads(out)["msg"] == "Set key: foo"
312 assert json.loads(out)["value"] == "baz"
313 assert json.loads(out)["old_value"] == "bar"