Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/lookup/test_etcd3.py

raw
1 #
2 # Copyright (c) 2020, SCC France, Eric Belhomme <[email protected]>
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 unittest
9 from unittest.mock import MagicMock, patch
10
11 from ansible.plugins.loader import lookup_loader
12
13 from ansible_collections.community.general.plugins.lookup import etcd3
14
15
16 class FakeKVMetadata:
17 def __init__(self, keyvalue, header):
18 self.key = keyvalue
19 self.create_revision = ""
20 self.mod_revision = ""
21 self.version = ""
22 self.lease_id = ""
23 self.response_header = header
24
25
26 class FakeEtcd3Client(MagicMock):
27 def get_prefix(self, key):
28 for i in range(1, 4):
29 yield self.get(f"{key}_{i}")
30
31 def get(self, key):
32 return (f"{key} value", FakeKVMetadata(key, None))
33
34
35 class TestLookupModule(unittest.TestCase):
36 def setUp(self):
37 etcd3.HAS_ETCD = True
38 self.lookup = lookup_loader.get("community.general.etcd3")
39
40 @patch("ansible_collections.community.general.plugins.lookup.etcd3.etcd3_client", FakeEtcd3Client())
41 def test_key(self):
42 expected_result = [{"key": "a_key", "value": "a_key value"}]
43 self.assertListEqual(expected_result, self.lookup.run(["a_key"], []))
44
45 @patch("ansible_collections.community.general.plugins.lookup.etcd3.etcd3_client", FakeEtcd3Client())
46 def test_key_prefix(self):
47 expected_result = [
48 {"key": "a_key_1", "value": "a_key_1 value"},
49 {"key": "a_key_2", "value": "a_key_2 value"},
50 {"key": "a_key_3", "value": "a_key_3 value"},
51 ]
52 self.assertListEqual(expected_result, self.lookup.run(["a_key"], [], **{"prefix": True}))