1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00
community.general/tests/unit/plugins/lookup/test_etcd3.py

58 lines
1.9 KiB
Python
Raw Normal View History

add etcd3 lookup plugin (#127) * add etcd3 lookup plugin * retire version_added tag Co-Authored-By: Felix Fontein <felix@fontein.de> * typo fixes Co-Authored-By: Felix Fontein <felix@fontein.de> * fix YAML syntax in example Co-Authored-By: Felix Fontein <felix@fontein.de> * typo fixes Co-Authored-By: Felix Fontein <felix@fontein.de> * remove python shebang as it is useless in Ansible lookup module Co-Authored-By: Felix Fontein <felix@fontein.de> * Update plugins/lookup/etcd3.py typo Co-Authored-By: Felix Fontein <felix@fontein.de> * fixes: - replaced LookupBase._display by ansible.utils.display.Display - add regex to retrieve host and port from ETCDCTL_ENDPOINTS env - add env support for user, password, timeout * fixes: - use short form for types - update doc section with envs - catch exceptions between etcd3 api calls * etcd3 lookup pass ansible sanity checks introduce ansible integration tests for etcd3 lookup * extract etcd3 setup from existing etcd3 module integration test * fix etcd3 module/lookup integration tests * fixes: - fix port option in docstring - raise connecttion error - fix display format issues - fix ETCDCTL_ENDPOINTS regex adds: - basic unit tests * fix sanity issues * add etcd3 lookup plugin * retire version_added tag Co-Authored-By: Felix Fontein <felix@fontein.de> * typo fixes Co-Authored-By: Felix Fontein <felix@fontein.de> * fix YAML syntax in example Co-Authored-By: Felix Fontein <felix@fontein.de> * typo fixes Co-Authored-By: Felix Fontein <felix@fontein.de> * remove python shebang as it is useless in Ansible lookup module Co-Authored-By: Felix Fontein <felix@fontein.de> * Update plugins/lookup/etcd3.py typo Co-Authored-By: Felix Fontein <felix@fontein.de> * fixes: - replaced LookupBase._display by ansible.utils.display.Display - add regex to retrieve host and port from ETCDCTL_ENDPOINTS env - add env support for user, password, timeout * fixes: - use short form for types - update doc section with envs - catch exceptions between etcd3 api calls * etcd3 lookup pass ansible sanity checks introduce ansible integration tests for etcd3 lookup * extract etcd3 setup from existing etcd3 module integration test * fix etcd3 module/lookup integration tests * fixes: - fix port option in docstring - raise connecttion error - fix display format issues - fix ETCDCTL_ENDPOINTS regex adds: - basic unit tests * fix sanity issues * changes: - replace kwargs lookups with get_option() - add 'entpoint' option for correct handling of ETCDCTL_ENDPOINTS env - code simplification * fix etcd3 lookup unit test: replace LookupModule instanciation with lookup_loader * fix sanity checks * etcd3 changes: - docstring documentation fixes/updates - create etcd3 cnx object with a get_option() loop instead of copying 'private' class object - set 'endpoints' option mutually exclusive with 'host' and 'port' (raises an AnsibleError exception) * etcd3 changes: - added ANSIBLE_METADATA, - added default value for 'endpoints' option, - removed defaults for options 'host' and 'port', - fixed docstring links, - added 'notes' and 'seealso' sections in doctring - updated options code handling to reflect docstring's updates * etcd3 changes: - fix descriptions for endpoints, host, and port options - update notes sections - fix reference to etcd lookup plugin in seealso section - fix return docstring - remove useless logging - obfuscates password in connection logging * more pythonic lookup on dict keys Co-Authored-By: Felix Fontein <felix@fontein.de> * Update password obfuscation Co-Authored-By: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
2020-04-25 10:58:06 +02:00
# -*- coding: utf-8 -*-
#
# (c) 2020, SCC France, Eric Belhomme <ebelhomme@fr.scc.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
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}))