| 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_info |
| 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_info_without_arguments(capfd): |
| 22 |
with set_module_args({}): |
| 23 |
with pytest.raises(SystemExit): |
| 24 |
redis_data_info.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_info_existing_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 |
"_ansible_check_mode": False, |
| 39 |
} |
| 40 |
): |
| 41 |
mocker.patch("redis.Redis.get", return_value="bar") |
| 42 |
with pytest.raises(SystemExit): |
| 43 |
redis_data_info.main() |
| 44 |
out, err = capfd.readouterr() |
| 45 |
print(out) |
| 46 |
assert not err |
| 47 |
assert json.loads(out)["exists"] |
| 48 |
assert json.loads(out)["value"] == "bar" |
| 49 |
|
| 50 |
|
| 51 |
@pytest.mark.skipif(not HAS_REDIS_USERNAME_OPTION, reason="Redis version < 3.4.0") |
| 52 |
def test_redis_data_info_absent_key(capfd, mocker): |
| 53 |
with set_module_args( |
| 54 |
{ |
| 55 |
"login_host": "localhost", |
| 56 |
"login_user": "root", |
| 57 |
"login_password": "secret", |
| 58 |
"key": "foo", |
| 59 |
"_ansible_check_mode": False, |
| 60 |
} |
| 61 |
): |
| 62 |
mocker.patch("redis.Redis.get", return_value=None) |
| 63 |
with pytest.raises(SystemExit): |
| 64 |
redis_data_info.main() |
| 65 |
out, err = capfd.readouterr() |
| 66 |
print(out) |
| 67 |
assert not err |
| 68 |
assert not json.loads(out)["exists"] |
| 69 |
assert "value" not in json.loads(out) |
| 70 |
|
| 71 |
|
| 72 |
@pytest.mark.skipif(HAS_REDIS_USERNAME_OPTION, reason="Redis version > 3.4.0") |
| 73 |
def test_redis_data_fail_username(capfd, mocker): |
| 74 |
with set_module_args( |
| 75 |
{ |
| 76 |
"login_host": "localhost", |
| 77 |
"login_user": "root", |
| 78 |
"login_password": "secret", |
| 79 |
"key": "foo", |
| 80 |
"_ansible_check_mode": False, |
| 81 |
} |
| 82 |
): |
| 83 |
with pytest.raises(SystemExit): |
| 84 |
redis_data_info.main() |
| 85 |
out, err = capfd.readouterr() |
| 86 |
print(out) |
| 87 |
assert not err |
| 88 |
assert json.loads(out)["failed"] |
| 89 |
assert json.loads(out)["msg"] == "The option `username` in only supported with redis >= 3.4.0." |
| 90 |
|
| 91 |
|
| 92 |
@pytest.mark.skipif(HAS_REDIS_USERNAME_OPTION, reason="Redis version > 3.4.0") |
| 93 |
def test_redis_data_info_absent_key_no_username(capfd, mocker): |
| 94 |
with set_module_args( |
| 95 |
{"login_host": "localhost", "login_password": "secret", "key": "foo", "_ansible_check_mode": False} |
| 96 |
): |
| 97 |
mocker.patch("redis.Redis.get", return_value=None) |
| 98 |
with pytest.raises(SystemExit): |
| 99 |
redis_data_info.main() |
| 100 |
out, err = capfd.readouterr() |
| 101 |
print(out) |
| 102 |
assert not err |
| 103 |
assert not json.loads(out)["exists"] |
| 104 |
assert "value" not in json.loads(out) |
| 105 |
|
| 106 |
|
| 107 |
@pytest.mark.skipif(HAS_REDIS_USERNAME_OPTION, reason="Redis version > 3.4.0") |
| 108 |
def test_redis_data_info_existing_key_no_username(capfd, mocker): |
| 109 |
with set_module_args( |
| 110 |
{"login_host": "localhost", "login_password": "secret", "key": "foo", "_ansible_check_mode": False} |
| 111 |
): |
| 112 |
mocker.patch("redis.Redis.get", return_value="bar") |
| 113 |
with pytest.raises(SystemExit): |
| 114 |
redis_data_info.main() |
| 115 |
out, err = capfd.readouterr() |
| 116 |
print(out) |
| 117 |
assert not err |
| 118 |
assert json.loads(out)["exists"] |
| 119 |
assert json.loads(out)["value"] == "bar" |