Debian Codegraph @

ansible_collections/community/hashi_vault/tests/unit/plugins/lookup/test_hashi_vault.py

raw
1 # -*- coding: utf-8 -*-
2 # Copyright (c) 2021 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 os
10 import pytest
11
12 from ansible.plugins.loader import lookup_loader
13
14 from urllib.parse import urlparse
15
16 from ansible_collections.community.hashi_vault.tests.unit.compat import mock
17
18 from ansible_collections.community.hashi_vault.plugins.plugin_utils._hashi_vault_lookup_base import HashiVaultLookupBase
19
20 from requests.exceptions import ConnectionError
21
22
23 @pytest.fixture
24 def hashi_vault_lookup_module():
25 return lookup_loader.get('community.hashi_vault.hashi_vault')
26
27
28 class TestHashiVaultLookup(object):
29
30 def test_is_hashi_vault_lookup_base(self, hashi_vault_lookup_module):
31 assert issubclass(type(hashi_vault_lookup_module), HashiVaultLookupBase)
32
33 # TODO: this test should be decoupled from the hashi_vault lookup and moved to the connection options tests
34 @pytest.mark.parametrize(
35 'envpatch,expected',
36 [
37 ({'VAULT_ADDR': 'http://vault:0'}, 'http://vault:0'),
38 ({'ANSIBLE_HASHI_VAULT_ADDR': 'https://vaultalt'}, 'https://vaultalt'),
39 ({'VAULT_ADDR': 'https://vaultlow:8443', 'ANSIBLE_HASHI_VAULT_ADDR': 'http://vaulthigh:8200'}, 'http://vaulthigh:8200'),
40 ],
41 )
42 def test_vault_addr_low_pref(self, hashi_vault_lookup_module, envpatch, expected):
43 url = urlparse(expected)
44 host = url.hostname
45 port = url.port if url.port is not None else {'http': 80, 'https': 443}[url.scheme]
46
47 with mock.patch.dict(os.environ, envpatch):
48 with pytest.raises(ConnectionError) as e:
49 hashi_vault_lookup_module.run(['secret/fake'], token='fake')
50
51 s_err = str(e.value)
52
53 assert str(host) in s_err, "host '%s' not found in exception: %r" % (host, str(e.value))
54 assert str(port) in s_err, "port '%i' not found in exception: %r" % (port, str(e.value))