| 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 |
import redis |
| 12 |
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import set_module_args |
| 13 |
from redis import __version__ |
| 14 |
|
| 15 |
from ansible_collections.community.general.plugins.modules import redis_data_incr |
| 16 |
|
| 17 |
HAS_REDIS_USERNAME_OPTION = True |
| 18 |
if tuple(map(int, __version__.split("."))) < (3, 4, 0): |
| 19 |
HAS_REDIS_USERNAME_OPTION = False |
| 20 |
if HAS_REDIS_USERNAME_OPTION: |
| 21 |
from redis.exceptions import NoPermissionError |
| 22 |
|
| 23 |
|
| 24 |
def test_redis_data_incr_without_arguments(capfd): |
| 25 |
with set_module_args({}): |
| 26 |
with pytest.raises(SystemExit): |
| 27 |
redis_data_incr.main() |
| 28 |
out, err = capfd.readouterr() |
| 29 |
assert not err |
| 30 |
assert json.loads(out)["failed"] |
| 31 |
|
| 32 |
|
| 33 |
@pytest.mark.skipif(not HAS_REDIS_USERNAME_OPTION, reason="Redis version < 3.4.0") |
| 34 |
def test_redis_data_incr(capfd, mocker): |
| 35 |
with set_module_args( |
| 36 |
{ |
| 37 |
"login_host": "localhost", |
| 38 |
"login_user": "root", |
| 39 |
"login_password": "secret", |
| 40 |
"key": "foo", |
| 41 |
} |
| 42 |
): |
| 43 |
mocker.patch("redis.Redis.incr", return_value=57) |
| 44 |
with pytest.raises(SystemExit): |
| 45 |
redis_data_incr.main() |
| 46 |
out, err = capfd.readouterr() |
| 47 |
print(out) |
| 48 |
assert not err |
| 49 |
assert json.loads(out)["value"] == 57.0 |
| 50 |
assert json.loads(out)["msg"] == "Incremented key: foo to 57" |
| 51 |
assert json.loads(out)["changed"] |
| 52 |
|
| 53 |
|
| 54 |
@pytest.mark.skipif(not HAS_REDIS_USERNAME_OPTION, reason="Redis version < 3.4.0") |
| 55 |
def test_redis_data_incr_int(capfd, mocker): |
| 56 |
with set_module_args( |
| 57 |
{"login_host": "localhost", "login_user": "root", "login_password": "secret", "key": "foo", "increment_int": 10} |
| 58 |
): |
| 59 |
mocker.patch("redis.Redis.incrby", return_value=57) |
| 60 |
with pytest.raises(SystemExit): |
| 61 |
redis_data_incr.main() |
| 62 |
out, err = capfd.readouterr() |
| 63 |
print(out) |
| 64 |
assert not err |
| 65 |
assert json.loads(out)["value"] == 57.0 |
| 66 |
assert json.loads(out)["msg"] == "Incremented key: foo by 10 to 57" |
| 67 |
assert json.loads(out)["changed"] |
| 68 |
|
| 69 |
|
| 70 |
@pytest.mark.skipif(not HAS_REDIS_USERNAME_OPTION, reason="Redis version < 3.4.0") |
| 71 |
def test_redis_data_inc_float(capfd, mocker): |
| 72 |
with set_module_args( |
| 73 |
{ |
| 74 |
"login_host": "localhost", |
| 75 |
"login_user": "root", |
| 76 |
"login_password": "secret", |
| 77 |
"key": "foo", |
| 78 |
"increment_float": "5.5", |
| 79 |
} |
| 80 |
): |
| 81 |
mocker.patch("redis.Redis.incrbyfloat", return_value=57.45) |
| 82 |
with pytest.raises(SystemExit): |
| 83 |
redis_data_incr.main() |
| 84 |
out, err = capfd.readouterr() |
| 85 |
print(out) |
| 86 |
assert not err |
| 87 |
assert json.loads(out)["value"] == 57.45 |
| 88 |
assert json.loads(out)["msg"] == "Incremented key: foo by 5.5 to 57.45" |
| 89 |
assert json.loads(out)["changed"] |
| 90 |
|
| 91 |
|
| 92 |
@pytest.mark.skipif(not HAS_REDIS_USERNAME_OPTION, reason="Redis version < 3.4.0") |
| 93 |
def test_redis_data_incr_float_wrong_value(capfd): |
| 94 |
with set_module_args( |
| 95 |
{ |
| 96 |
"login_host": "localhost", |
| 97 |
"login_user": "root", |
| 98 |
"login_password": "secret", |
| 99 |
"key": "foo", |
| 100 |
"increment_float": "not_a_number", |
| 101 |
} |
| 102 |
): |
| 103 |
with pytest.raises(SystemExit): |
| 104 |
redis_data_incr.main() |
| 105 |
out, err = capfd.readouterr() |
| 106 |
print(out) |
| 107 |
assert not err |
| 108 |
assert json.loads(out)["failed"] |
| 109 |
|
| 110 |
|
| 111 |
@pytest.mark.skipif(HAS_REDIS_USERNAME_OPTION, reason="Redis version > 3.4.0") |
| 112 |
def test_redis_data_incr_fail_username(capfd, mocker): |
| 113 |
with set_module_args( |
| 114 |
{ |
| 115 |
"login_host": "localhost", |
| 116 |
"login_user": "root", |
| 117 |
"login_password": "secret", |
| 118 |
"key": "foo", |
| 119 |
"_ansible_check_mode": False, |
| 120 |
} |
| 121 |
): |
| 122 |
with pytest.raises(SystemExit): |
| 123 |
redis_data_incr.main() |
| 124 |
out, err = capfd.readouterr() |
| 125 |
print(out) |
| 126 |
assert not err |
| 127 |
assert json.loads(out)["failed"] |
| 128 |
assert json.loads(out)["msg"] == "The option `username` in only supported with redis >= 3.4.0." |
| 129 |
|
| 130 |
|
| 131 |
def test_redis_data_incr_no_username(capfd, mocker): |
| 132 |
with set_module_args( |
| 133 |
{ |
| 134 |
"login_host": "localhost", |
| 135 |
"login_password": "secret", |
| 136 |
"key": "foo", |
| 137 |
} |
| 138 |
): |
| 139 |
mocker.patch("redis.Redis.incr", return_value=57) |
| 140 |
with pytest.raises(SystemExit): |
| 141 |
redis_data_incr.main() |
| 142 |
out, err = capfd.readouterr() |
| 143 |
print(out) |
| 144 |
assert not err |
| 145 |
assert json.loads(out)["value"] == 57.0 |
| 146 |
assert json.loads(out)["msg"] == "Incremented key: foo to 57" |
| 147 |
assert json.loads(out)["changed"] |
| 148 |
|
| 149 |
|
| 150 |
def test_redis_data_incr_float_no_username(capfd, mocker): |
| 151 |
with set_module_args( |
| 152 |
{"login_host": "localhost", "login_password": "secret", "key": "foo", "increment_float": "5.5"} |
| 153 |
): |
| 154 |
mocker.patch("redis.Redis.incrbyfloat", return_value=57.45) |
| 155 |
with pytest.raises(SystemExit): |
| 156 |
redis_data_incr.main() |
| 157 |
out, err = capfd.readouterr() |
| 158 |
print(out) |
| 159 |
assert not err |
| 160 |
assert json.loads(out)["value"] == 57.45 |
| 161 |
assert json.loads(out)["msg"] == "Incremented key: foo by 5.5 to 57.45" |
| 162 |
assert json.loads(out)["changed"] |
| 163 |
|
| 164 |
|
| 165 |
def test_redis_data_incr_check_mode(capfd, mocker): |
| 166 |
with set_module_args( |
| 167 |
{"login_host": "localhost", "login_password": "secret", "key": "foo", "_ansible_check_mode": True} |
| 168 |
): |
| 169 |
mocker.patch("redis.Redis.get", return_value=10) |
| 170 |
with pytest.raises(SystemExit): |
| 171 |
redis_data_incr.main() |
| 172 |
out, err = capfd.readouterr() |
| 173 |
print(out) |
| 174 |
assert not err |
| 175 |
assert json.loads(out)["value"] == 11.0 |
| 176 |
assert json.loads(out)["msg"] == "Incremented key: foo by 1 to 11.0" |
| 177 |
assert not json.loads(out)["changed"] |
| 178 |
|
| 179 |
|
| 180 |
def test_redis_data_incr_check_mode_not_incrementable(capfd, mocker): |
| 181 |
with set_module_args( |
| 182 |
{"login_host": "localhost", "login_password": "secret", "key": "foo", "_ansible_check_mode": True} |
| 183 |
): |
| 184 |
mocker.patch("redis.Redis.get", return_value="bar") |
| 185 |
with pytest.raises(SystemExit): |
| 186 |
redis_data_incr.main() |
| 187 |
out, err = capfd.readouterr() |
| 188 |
print(out) |
| 189 |
assert not err |
| 190 |
assert json.loads(out)["failed"] |
| 191 |
assert json.loads(out)["msg"] == "Value: bar of key: foo is not incrementable(int or float)" |
| 192 |
assert "value" not in json.loads(out) |
| 193 |
assert not json.loads(out)["changed"] |
| 194 |
|
| 195 |
|
| 196 |
@pytest.mark.skipif(not HAS_REDIS_USERNAME_OPTION, reason="Redis version < 3.4.0") |
| 197 |
def test_redis_data_incr_check_mode_permissions(capfd, mocker): |
| 198 |
with set_module_args( |
| 199 |
{"login_host": "localhost", "login_password": "secret", "key": "foo", "_ansible_check_mode": True} |
| 200 |
): |
| 201 |
redis.Redis.get = mocker.Mock( |
| 202 |
side_effect=NoPermissionError("this user has no permissions to run the 'get' command or its subcommand") |
| 203 |
) |
| 204 |
with pytest.raises(SystemExit): |
| 205 |
redis_data_incr.main() |
| 206 |
out, err = capfd.readouterr() |
| 207 |
print(out) |
| 208 |
assert not err |
| 209 |
assert json.loads(out)["failed"] |
| 210 |
assert json.loads(out)["msg"].startswith("Failed to get value of key: foo with exception:") |
| 211 |
assert "value" not in json.loads(out) |
| 212 |
assert not json.loads(out)["changed"] |