| 1 |
# Copyright (c) 2020, Adam Migus <[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 |
# Make coding more python3-ish |
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
from unittest import TestCase |
| 9 |
from unittest.mock import ( |
| 10 |
DEFAULT, |
| 11 |
MagicMock, |
| 12 |
patch, |
| 13 |
) |
| 14 |
|
| 15 |
from ansible.plugins.loader import lookup_loader |
| 16 |
|
| 17 |
from ansible_collections.community.general.plugins.lookup import tss |
| 18 |
|
| 19 |
TSS_IMPORT_PATH = "ansible_collections.community.general.plugins.lookup.tss" |
| 20 |
|
| 21 |
|
| 22 |
def make_absolute(name): |
| 23 |
return f"{TSS_IMPORT_PATH}.{name}" |
| 24 |
|
| 25 |
|
| 26 |
class SecretServerError(Exception): |
| 27 |
def __init__(self): |
| 28 |
self.message = "" |
| 29 |
|
| 30 |
|
| 31 |
class MockSecretServer(MagicMock): |
| 32 |
RESPONSE = '{"foo": "bar"}' |
| 33 |
|
| 34 |
def get_secret_json(self, path): |
| 35 |
return self.RESPONSE |
| 36 |
|
| 37 |
|
| 38 |
class MockFaultySecretServer(MagicMock): |
| 39 |
def get_secret_json(self, path): |
| 40 |
raise SecretServerError |
| 41 |
|
| 42 |
|
| 43 |
@patch(make_absolute("SecretServer"), MockSecretServer()) |
| 44 |
class TestTSSClient(TestCase): |
| 45 |
def setUp(self): |
| 46 |
self.server_params = { |
| 47 |
"base_url": "", |
| 48 |
"username": "", |
| 49 |
"domain": "", |
| 50 |
"password": "", |
| 51 |
"api_path_uri": "", |
| 52 |
"token_path_uri": "", |
| 53 |
} |
| 54 |
|
| 55 |
def test_from_params(self): |
| 56 |
with patch(make_absolute("HAS_TSS_AUTHORIZER"), False): |
| 57 |
self.assert_client_version("v0") |
| 58 |
|
| 59 |
with patch.dict(self.server_params, {"domain": "foo"}): |
| 60 |
with self.assertRaises(tss.AnsibleError): |
| 61 |
self._get_client() |
| 62 |
|
| 63 |
with patch.multiple( |
| 64 |
TSS_IMPORT_PATH, |
| 65 |
HAS_TSS_AUTHORIZER=True, |
| 66 |
PasswordGrantAuthorizer=DEFAULT, |
| 67 |
DomainPasswordGrantAuthorizer=DEFAULT, |
| 68 |
): |
| 69 |
self.assert_client_version("v1") |
| 70 |
|
| 71 |
with patch.dict(self.server_params, {"domain": "foo"}): |
| 72 |
self.assert_client_version("v1") |
| 73 |
|
| 74 |
def assert_client_version(self, version): |
| 75 |
version_to_class = {"v0": tss.TSSClientV0, "v1": tss.TSSClientV1} |
| 76 |
|
| 77 |
client = self._get_client() |
| 78 |
self.assertIsInstance(client, version_to_class[version]) |
| 79 |
|
| 80 |
def _get_client(self): |
| 81 |
return tss.TSSClient.from_params(**self.server_params) |
| 82 |
|
| 83 |
|
| 84 |
class TestLookupModule(TestCase): |
| 85 |
VALID_TERMS = [1] |
| 86 |
INVALID_TERMS = ["foo"] |
| 87 |
|
| 88 |
def setUp(self): |
| 89 |
self.lookup = lookup_loader.get("community.general.tss") |
| 90 |
|
| 91 |
@patch.multiple(TSS_IMPORT_PATH, HAS_TSS_SDK=False, SecretServer=MockSecretServer) |
| 92 |
def test_missing_sdk(self): |
| 93 |
with self.assertRaises(tss.AnsibleError): |
| 94 |
self._run_lookup(self.VALID_TERMS) |
| 95 |
|
| 96 |
@patch.multiple(TSS_IMPORT_PATH, HAS_TSS_SDK=True, SecretServerError=SecretServerError) |
| 97 |
def test_get_secret_json(self): |
| 98 |
with patch(make_absolute("SecretServer"), MockSecretServer): |
| 99 |
self.assertListEqual([MockSecretServer.RESPONSE], self._run_lookup(self.VALID_TERMS)) |
| 100 |
|
| 101 |
with self.assertRaises(tss.AnsibleOptionsError): |
| 102 |
self._run_lookup(self.INVALID_TERMS) |
| 103 |
|
| 104 |
with patch(make_absolute("SecretServer"), MockFaultySecretServer): |
| 105 |
with self.assertRaises(tss.AnsibleError): |
| 106 |
self._run_lookup(self.VALID_TERMS) |
| 107 |
|
| 108 |
def _run_lookup(self, terms, variables=None, **kwargs): |
| 109 |
variables = variables or [] |
| 110 |
kwargs = kwargs or {"base_url": "dummy", "username": "dummy", "password": "dummy"} |
| 111 |
|
| 112 |
return self.lookup.run(terms, variables, **kwargs) |