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

Update unit tests for configmanager

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
Abhijeet Kasurde 2018-12-13 17:21:35 +05:30 committed by Toshio Kuratomi
parent efda3eaf1c
commit 44bd8fc0a8
2 changed files with 81 additions and 67 deletions

View file

@ -55,17 +55,25 @@ def ensure_type(value, value_type, origin=None):
:arg value: The value to ensure correct typing of :arg value: The value to ensure correct typing of
:kwarg value_type: The type of the value. This can be any of the following strings: :kwarg value_type: The type of the value. This can be any of the following strings:
:boolean: sets the value to a True or False value :boolean: sets the value to a True or False value
:bool: Same as 'boolean'
:integer: Sets the value to an integer or raises a ValueType error :integer: Sets the value to an integer or raises a ValueType error
:int: Same as 'integer'
:float: Sets the value to a float or raises a ValueType error :float: Sets the value to a float or raises a ValueType error
:list: Treats the value as a comma separated list. Split the value :list: Treats the value as a comma separated list. Split the value
and return it as a python list. and return it as a python list.
:none: Sets the value to None :none: Sets the value to None
:path: Expands any environment variables and tilde's in the value. :path: Expands any environment variables and tilde's in the value.
:tmp_path: Create a unique temporary directory inside of the directory :tmppath: Create a unique temporary directory inside of the directory
specified by value and return its path. specified by value and return its path.
:temppath: Same as 'tmppath'
:tmp: Same as 'tmppath'
:pathlist: Treat the value as a typical PATH string. (On POSIX, this :pathlist: Treat the value as a typical PATH string. (On POSIX, this
means colon separated strings.) Split the value and then expand means colon separated strings.) Split the value and then expand
each part for environment variables and tildes. each part for environment variables and tildes.
:pathspec: Treat the value as a PATH string. Expands any environment variables
tildes's in the value.
:str: Sets the value to string types.
:string: Same as 'str'
''' '''
basedir = None basedir = None
@ -125,7 +133,7 @@ def ensure_type(value, value_type, origin=None):
# FIXME: see if this can live in utils/path # FIXME: see if this can live in utils/path
def resolve_path(path, basedir=None): def resolve_path(path, basedir=None):
''' resolve relative or 'varaible' paths ''' ''' resolve relative or 'variable' paths '''
if '{{CWD}}' in path: # allow users to force CWD using 'magic' {{CWD}} if '{{CWD}}' in path: # allow users to force CWD using 'magic' {{CWD}}
path = path.replace('{{CWD}}', os.getcwd()) path = path.replace('{{CWD}}', os.getcwd())

View file

@ -1,13 +1,17 @@
# -*- coding: utf-8 -*-
# Copyright: (c) 2017, Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
# Make coding more python3-ish # Make coding more python3-ish
from __future__ import (absolute_import, division, print_function) from __future__ import (absolute_import, division, print_function)
__metaclass__ = type __metaclass__ = type
import os import os
import os.path import os.path
import pytest
from units.compat import unittest from ansible.config.manager import ConfigManager, Setting, ensure_type, resolve_path, get_config_type
from ansible.errors import AnsibleOptionsError, AnsibleError
from ansible.config.manager import ConfigManager, Setting, ensure_type, resolve_path, find_ini_config_file
curdir = os.path.dirname(__file__) curdir = os.path.dirname(__file__)
cfg_file = os.path.join(curdir, 'test.cfg') cfg_file = os.path.join(curdir, 'test.cfg')
@ -23,88 +27,90 @@ expected_ini = {'CONFIG_FILE': Setting(name='CONFIG_FILE', value=cfg_file, origi
'config_entry_multi_deprecated_source': Setting(name='config_entry_multi_deprecated_source', value=u'morefromini', 'config_entry_multi_deprecated_source': Setting(name='config_entry_multi_deprecated_source', value=u'morefromini',
origin=cfg_file, type='string')} origin=cfg_file, type='string')}
ensure_test_data = [
('a,b', 'list', list),
(['a', 'b'], 'list', list),
('y', 'bool', bool),
('yes', 'bool', bool),
('on', 'bool', bool),
('1', 'bool', bool),
('true', 'bool', bool),
('t', 'bool', bool),
(1, 'bool', bool),
(1.0, 'bool', bool),
(True, 'bool', bool),
('n', 'bool', bool),
('no', 'bool', bool),
('off', 'bool', bool),
('0', 'bool', bool),
('false', 'bool', bool),
('f', 'bool', bool),
(0, 'bool', bool),
(0.0, 'bool', bool),
(False, 'bool', bool),
('10', 'int', int),
(20, 'int', int),
('0.10', 'float', float),
(0.2, 'float', float),
('/tmp/test.yml', 'pathspec', list),
('/tmp/test.yml,/home/test2.yml', 'pathlist', list),
('a', 'str', str),
('a', 'string', str),
('None', 'none', type(None))
]
class TestConfigData(unittest.TestCase):
def setUp(self): class TestConfigManager:
self.manager = ConfigManager(cfg_file, os.path.join(curdir, 'test.yml')) @classmethod
def setup_class(cls):
cls.manager = ConfigManager(cfg_file, os.path.join(curdir, 'test.yml'))
def tearDown(self): @classmethod
self.manager = None def teardown_class(cls):
cls.manager = None
def test_initial_load(self): def test_initial_load(self):
self.assertEquals(self.manager.data._global_settings, expected_ini) assert self.manager.data._global_settings == expected_ini
def test_ensure_type_list(self): @pytest.mark.parametrize("value, expected_type, python_type", ensure_test_data)
self.assertIsInstance(ensure_type('a,b', 'list'), list) def test_ensure_type(self, value, expected_type, python_type):
self.assertIsInstance(ensure_type(['a', 'b'], 'list'), list) assert isinstance(ensure_type(value, expected_type), python_type)
def test_ensure_type_bool(self):
self.assertIsInstance(ensure_type('yes', 'bool'), bool)
self.assertIsInstance(ensure_type(True, 'bool'), bool)
def test_ensure_type_int(self):
self.assertIsInstance(ensure_type('10', 'int'), int)
self.assertIsInstance(ensure_type(20, 'int'), int)
def test_ensure_type_float(self):
self.assertIsInstance(ensure_type('0.10', 'float'), float)
self.assertIsInstance(ensure_type(0.2, 'float'), float)
def test_resolve_path(self): def test_resolve_path(self):
self.assertEquals(os.path.join(curdir, 'test.yml'), resolve_path('./test.yml', cfg_file)) assert os.path.join(curdir, 'test.yml') == resolve_path('./test.yml', cfg_file)
def test_resolve_path_cwd(self): def test_resolve_path_cwd(self):
self.assertEquals(os.path.join(os.getcwd(), 'test.yml'), resolve_path('{{CWD}}/test.yml')) assert os.path.join(os.getcwd(), 'test.yml') == resolve_path('{{CWD}}/test.yml')
self.assertEquals(os.path.join(os.getcwd(), 'test.yml'), resolve_path('./test.yml')) assert os.path.join(os.getcwd(), 'test.yml') == resolve_path('./test.yml')
def test_get_config_dest(self):
pass
def test_value_and_origin_from_ini(self): def test_value_and_origin_from_ini(self):
self.assertEquals(self.manager.get_config_value_and_origin('config_entry'), ('fromini', cfg_file)) assert self.manager.get_config_value_and_origin('config_entry') == ('fromini', cfg_file)
def test_value_from_ini(self): def test_value_from_ini(self):
self.assertEquals(self.manager.get_config_value('config_entry'), 'fromini') assert self.manager.get_config_value('config_entry') == 'fromini'
def test_value_and_origin_from_alt_ini(self): def test_value_and_origin_from_alt_ini(self):
self.assertEquals(self.manager.get_config_value_and_origin('config_entry', cfile=cfg_file2), ('fromini2', cfg_file2)) assert self.manager.get_config_value_and_origin('config_entry', cfile=cfg_file2) == ('fromini2', cfg_file2)
def test_value_from_alt_ini(self): def test_value_from_alt_ini(self):
self.assertEquals(self.manager.get_config_value('config_entry', cfile=cfg_file2), 'fromini2') assert self.manager.get_config_value('config_entry', cfile=cfg_file2) == 'fromini2'
def test_value_and_origin_from_yaml(self): def test_config_types(self):
pass assert get_config_type('/tmp/ansible.ini') == 'ini'
assert get_config_type('/tmp/ansible.cfg') == 'ini'
assert get_config_type('/tmp/ansible.yaml') == 'yaml'
assert get_config_type('/tmp/ansible.yml') == 'yaml'
def test_value_from_yaml(self): def test_config_types_negative(self):
pass with pytest.raises(AnsibleOptionsError) as exec_info:
get_config_type('/tmp/ansible.txt')
assert "Unsupported configuration file extension for" in str(exec_info.value)
def test_value_and_origin_from_alt_yaml(self): def test_read_config_yaml_file(self):
pass assert isinstance(self.manager._read_config_yaml_file(os.path.join(curdir, 'test.yml')), dict)
def test_value_from_alt_yaml(self): def test_read_config_yaml_file_negative(self):
pass with pytest.raises(AnsibleError) as exec_info:
self.manager._read_config_yaml_file(os.path.join(curdir, 'test_non_existent.yml'))
def test_config_type_bool(self): assert "Missing base YAML definition file (bad install?)" in str(exec_info.value)
pass
def test_config_type_list(self):
pass
def test_config_default(self):
pass
def test_deprecated_config(self):
pass
def test_deprecated_config_source(self):
pass
def test_multi_precedence(self):
pass
def test_initialize_plugin_config(self):
pass
def test_update_config_data(self):
pass