| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Copyright (c) 2022 Brian Scholer (@briantist) |
| 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 (absolute_import, division, print_function) |
| 7 |
__metaclass__ = type |
| 8 |
|
| 9 |
import pytest |
| 10 |
import re |
| 11 |
import json |
| 12 |
|
| 13 |
from .....plugins.modules import vault_read |
| 14 |
from .....plugins.module_utils._hashi_vault_common import HashiVaultValueError |
| 15 |
|
| 16 |
|
| 17 |
hvac = pytest.importorskip('hvac') |
| 18 |
|
| 19 |
|
| 20 |
pytestmark = pytest.mark.usefixtures( |
| 21 |
'patch_ansible_module', |
| 22 |
'patch_authenticator', |
| 23 |
'patch_get_vault_client', |
| 24 |
) |
| 25 |
|
| 26 |
|
| 27 |
def _connection_options(): |
| 28 |
return { |
| 29 |
'auth_method': 'token', |
| 30 |
'url': 'http://myvault', |
| 31 |
'token': 'beep-boop', |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
def _sample_options(): |
| 36 |
return { |
| 37 |
'path': 'endpoint', |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
def _combined_options(**kwargs): |
| 42 |
opt = _connection_options() |
| 43 |
opt.update(_sample_options()) |
| 44 |
opt.update(kwargs) |
| 45 |
return opt |
| 46 |
|
| 47 |
|
| 48 |
@pytest.fixture |
| 49 |
def kv1_get_response(fixture_loader): |
| 50 |
return fixture_loader('kv1_get_response.json') |
| 51 |
|
| 52 |
|
| 53 |
class TestModuleVaultRead(): |
| 54 |
|
| 55 |
@pytest.mark.parametrize('patch_ansible_module', [_combined_options()], indirect=True) |
| 56 |
@pytest.mark.parametrize('exc', [HashiVaultValueError('throwaway msg'), NotImplementedError('throwaway msg')]) |
| 57 |
def test_vault_read_authentication_error(self, authenticator, exc, capfd): |
| 58 |
authenticator.authenticate.side_effect = exc |
| 59 |
|
| 60 |
with pytest.raises(SystemExit) as e: |
| 61 |
vault_read.main() |
| 62 |
|
| 63 |
out, err = capfd.readouterr() |
| 64 |
result = json.loads(out) |
| 65 |
|
| 66 |
assert e.value.code != 0, "result: %r" % (result,) |
| 67 |
assert result['msg'] == 'throwaway msg', "result: %r" % result |
| 68 |
|
| 69 |
@pytest.mark.parametrize('patch_ansible_module', [_combined_options()], indirect=True) |
| 70 |
@pytest.mark.parametrize('exc', [HashiVaultValueError('throwaway msg'), NotImplementedError('throwaway msg')]) |
| 71 |
def test_vault_read_auth_validation_error(self, authenticator, exc, capfd): |
| 72 |
authenticator.validate.side_effect = exc |
| 73 |
|
| 74 |
with pytest.raises(SystemExit) as e: |
| 75 |
vault_read.main() |
| 76 |
|
| 77 |
out, err = capfd.readouterr() |
| 78 |
result = json.loads(out) |
| 79 |
|
| 80 |
assert e.value.code != 0, "result: %r" % (result,) |
| 81 |
assert result['msg'] == 'throwaway msg' |
| 82 |
|
| 83 |
@pytest.mark.parametrize('patch_ansible_module', [_combined_options()], indirect=True) |
| 84 |
def test_vault_read_return_data(self, patch_ansible_module, kv1_get_response, vault_client, capfd): |
| 85 |
client = vault_client |
| 86 |
client.read.return_value = kv1_get_response.copy() |
| 87 |
|
| 88 |
with pytest.raises(SystemExit) as e: |
| 89 |
vault_read.main() |
| 90 |
|
| 91 |
out, err = capfd.readouterr() |
| 92 |
result = json.loads(out) |
| 93 |
|
| 94 |
assert e.value.code == 0, "result: %r" % (result,) |
| 95 |
|
| 96 |
client.read.assert_called_once_with(patch_ansible_module['path']) |
| 97 |
|
| 98 |
assert result['data'] == kv1_get_response, "module result did not match expected result:\nexpected: %r\ngot: %r" % (kv1_get_response, result) |
| 99 |
|
| 100 |
@pytest.mark.parametrize('patch_ansible_module', [_combined_options()], indirect=True) |
| 101 |
def test_vault_read_no_data(self, patch_ansible_module, vault_client, capfd): |
| 102 |
client = vault_client |
| 103 |
client.read.return_value = None |
| 104 |
|
| 105 |
with pytest.raises(SystemExit) as e: |
| 106 |
vault_read.main() |
| 107 |
|
| 108 |
out, err = capfd.readouterr() |
| 109 |
result = json.loads(out) |
| 110 |
|
| 111 |
assert e.value.code != 0, "result: %r" % (result,) |
| 112 |
|
| 113 |
client.read.assert_called_once_with(patch_ansible_module['path']) |
| 114 |
|
| 115 |
match = re.search(r"The path '[^']+' doesn't seem to exist", result['msg']) |
| 116 |
|
| 117 |
assert match is not None, "Unexpected msg: %s" % result['msg'] |
| 118 |
|
| 119 |
@pytest.mark.parametrize( |
| 120 |
'exc', |
| 121 |
[ |
| 122 |
(hvac.exceptions.Forbidden, "", r"^Forbidden: Permission Denied to path '([^']+)'"), |
| 123 |
] |
| 124 |
) |
| 125 |
@pytest.mark.parametrize('patch_ansible_module', [[_combined_options(), 'path']], indirect=True) |
| 126 |
@pytest.mark.parametrize('opt_path', ['path/1', 'second/path']) |
| 127 |
def test_vault_read_vault_exception(self, vault_client, exc, opt_path, capfd): |
| 128 |
|
| 129 |
client = vault_client |
| 130 |
client.read.side_effect = exc[0](exc[1]) |
| 131 |
|
| 132 |
with pytest.raises(SystemExit) as e: |
| 133 |
vault_read.main() |
| 134 |
|
| 135 |
out, err = capfd.readouterr() |
| 136 |
result = json.loads(out) |
| 137 |
|
| 138 |
assert e.value.code != 0, "result: %r" % (result,) |
| 139 |
match = re.search(exc[2], result['msg']) |
| 140 |
assert match is not None, "result: %r\ndid not match: %s" % (result, exc[2]) |
| 141 |
|
| 142 |
assert opt_path == match.group(1) |