mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
203747027e
* Move licenses to LICENSES/, run add-license.py, add LICENSES/MIT.txt.
* Replace 'Copyright:' with 'Copyright'
sed -i 's|Copyright:\(.*\)|Copyright\1|' $(rg -l 'Copyright:')
Co-authored-by: Maxwell G <gotmax@e.email>
(cherry picked from commit 123c7efe5e
)
Co-authored-by: Felix Fontein <felix@fontein.de>
58 lines
2 KiB
Python
58 lines
2 KiB
Python
# -*- coding: utf-8 -*-
|
|
#
|
|
# (c) 2020, SCC France, Eric Belhomme <ebelhomme@fr.scc.com>
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
__metaclass__ = type
|
|
|
|
|
|
import pytest
|
|
from ansible_collections.community.general.tests.unit.compat import unittest
|
|
from ansible_collections.community.general.tests.unit.compat.mock import patch, MagicMock
|
|
from ansible.errors import AnsibleError
|
|
from ansible_collections.community.general.plugins.lookup import etcd3
|
|
from ansible.plugins.loader import lookup_loader
|
|
|
|
|
|
class FakeKVMetadata:
|
|
|
|
def __init__(self, keyvalue, header):
|
|
self.key = keyvalue
|
|
self.create_revision = ''
|
|
self.mod_revision = ''
|
|
self.version = ''
|
|
self.lease_id = ''
|
|
self.response_header = header
|
|
|
|
|
|
class FakeEtcd3Client(MagicMock):
|
|
|
|
def get_prefix(self, key):
|
|
for i in range(1, 4):
|
|
yield self.get('{0}_{1}'.format(key, i))
|
|
|
|
def get(self, key):
|
|
return ("{0} value".format(key), FakeKVMetadata(key, None))
|
|
|
|
|
|
class TestLookupModule(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
etcd3.HAS_ETCD = True
|
|
self.lookup = lookup_loader.get('community.general.etcd3')
|
|
|
|
@patch('ansible_collections.community.general.plugins.lookup.etcd3.etcd3_client', FakeEtcd3Client())
|
|
def test_key(self):
|
|
expected_result = [{'key': 'a_key', 'value': 'a_key value'}]
|
|
self.assertListEqual(expected_result, self.lookup.run(['a_key'], []))
|
|
|
|
@patch('ansible_collections.community.general.plugins.lookup.etcd3.etcd3_client', FakeEtcd3Client())
|
|
def test_key_prefix(self):
|
|
expected_result = [
|
|
{'key': 'a_key_1', 'value': 'a_key_1 value'},
|
|
{'key': 'a_key_2', 'value': 'a_key_2 value'},
|
|
{'key': 'a_key_3', 'value': 'a_key_3 value'},
|
|
]
|
|
self.assertListEqual(expected_result, self.lookup.run(['a_key'], [], **{'prefix': True}))
|