2008-10-03 15:27:15 -05:00
|
|
|
# Authors:
|
|
|
|
# Martin Nagy <mnagy@redhat.com>
|
2008-12-30 16:02:15 -06:00
|
|
|
# Jason Gerard DeRose <jderose@redhat.com>
|
2008-10-03 15:27:15 -05:00
|
|
|
#
|
|
|
|
# Copyright (C) 2008 Red Hat
|
|
|
|
# see file 'COPYING' for use and warranty information
|
|
|
|
#
|
2010-12-09 06:59:11 -06:00
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
2008-10-03 15:27:15 -05:00
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2010-12-09 06:59:11 -06:00
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2008-10-03 15:27:15 -05:00
|
|
|
|
|
|
|
"""
|
2008-10-07 22:25:23 -05:00
|
|
|
Test the `ipalib.config` module.
|
2008-10-03 15:27:15 -05:00
|
|
|
"""
|
|
|
|
|
2008-10-24 16:07:07 -05:00
|
|
|
from os import path
|
2020-05-04 12:41:38 -05:00
|
|
|
import site
|
2008-10-24 16:07:07 -05:00
|
|
|
import sys
|
2020-05-04 12:41:38 -05:00
|
|
|
|
2015-12-16 09:06:03 -06:00
|
|
|
from ipatests.util import raises, delitem, ClassChecker
|
|
|
|
from ipatests.util import getitem
|
2013-05-21 06:40:27 -05:00
|
|
|
from ipatests.util import TempDir, TempHome
|
2015-12-16 09:06:03 -06:00
|
|
|
from ipalib.constants import OVERRIDE_ERROR, SET_ERROR, DEL_ERROR
|
2008-12-30 01:45:48 -06:00
|
|
|
from ipalib.constants import NAME_REGEX, NAME_ERROR
|
2009-01-06 19:21:46 -06:00
|
|
|
from ipalib import config, constants, base
|
2008-10-03 15:27:15 -05:00
|
|
|
|
2015-04-24 07:39:48 -05:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
pytestmark = pytest.mark.tier0
|
2008-10-03 15:27:15 -05:00
|
|
|
|
2008-12-22 18:29:11 -06:00
|
|
|
# Valid environment variables in (key, raw, value) tuples:
|
|
|
|
# key: the name of the environment variable
|
|
|
|
# raw: the value being set (possibly a string repr)
|
|
|
|
# value: the expected value after the lightweight conversion
|
|
|
|
good_vars = (
|
2010-02-22 13:29:26 -06:00
|
|
|
('a_string', u'Hello world!', u'Hello world!'),
|
|
|
|
('trailing_whitespace', u' value ', u'value'),
|
2008-12-22 18:29:11 -06:00
|
|
|
('an_int', 42, 42),
|
|
|
|
('int_repr', ' 42 ', 42),
|
2014-06-13 05:47:48 -05:00
|
|
|
('not_a_float', '3.14', u'3.14'),
|
2008-12-22 18:29:11 -06:00
|
|
|
('true', True, True),
|
|
|
|
('true_repr', ' True ', True),
|
|
|
|
('false', False, False),
|
|
|
|
('false_repr', ' False ', False),
|
|
|
|
('none', None, None),
|
|
|
|
('none_repr', ' None ', None),
|
2008-12-23 02:11:03 -06:00
|
|
|
('empty', '', None),
|
2008-12-22 18:29:11 -06:00
|
|
|
|
|
|
|
# These verify that the implied conversion is case-sensitive:
|
2010-02-22 13:29:26 -06:00
|
|
|
('not_true', u' true ', u'true'),
|
|
|
|
('not_false', u' false ', u'false'),
|
|
|
|
('not_none', u' none ', u'none'),
|
2008-12-22 18:29:11 -06:00
|
|
|
)
|
|
|
|
|
2008-10-17 15:55:03 -05:00
|
|
|
|
2008-12-30 01:45:48 -06:00
|
|
|
bad_names = (
|
2010-02-22 13:29:26 -06:00
|
|
|
('CamelCase', u'value'),
|
|
|
|
('_leading_underscore', u'value'),
|
|
|
|
('trailing_underscore_', u'value'),
|
2008-12-30 01:45:48 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2008-10-24 16:07:07 -05:00
|
|
|
# Random base64-encoded data to simulate a misbehaving config file.
|
|
|
|
config_bad = """
|
|
|
|
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
|
|
|
|
"""
|
|
|
|
|
2008-12-23 02:11:03 -06:00
|
|
|
|
2008-10-24 16:07:07 -05:00
|
|
|
# A config file that tries to override some standard vars:
|
|
|
|
config_override = """
|
|
|
|
[global]
|
2008-10-24 21:02:14 -05:00
|
|
|
|
2008-10-24 16:07:07 -05:00
|
|
|
key0 = var0
|
|
|
|
home = /home/sweet/home
|
|
|
|
key1 = var1
|
|
|
|
site_packages = planet
|
|
|
|
key2 = var2
|
|
|
|
key3 = var3
|
|
|
|
"""
|
|
|
|
|
2008-12-23 02:11:03 -06:00
|
|
|
|
2008-10-24 21:02:14 -05:00
|
|
|
# A config file that tests the automatic type conversion
|
2008-10-24 16:07:07 -05:00
|
|
|
config_good = """
|
|
|
|
[global]
|
2008-10-24 21:02:14 -05:00
|
|
|
|
2008-12-30 16:02:15 -06:00
|
|
|
string = Hello world!
|
|
|
|
null = None
|
2008-12-22 17:16:57 -06:00
|
|
|
yes = True
|
|
|
|
no = False
|
2008-10-24 16:07:07 -05:00
|
|
|
number = 42
|
2008-12-30 16:02:15 -06:00
|
|
|
floating = 3.14
|
2008-10-24 16:07:07 -05:00
|
|
|
"""
|
|
|
|
|
2008-12-23 02:11:03 -06:00
|
|
|
|
2008-10-27 02:09:53 -05:00
|
|
|
# A default config file to make sure it does not overwrite the explicit one
|
|
|
|
config_default = """
|
|
|
|
[global]
|
|
|
|
|
|
|
|
yes = Hello
|
|
|
|
not_in_other = foo_bar
|
|
|
|
"""
|
|
|
|
|
2008-10-24 16:07:07 -05:00
|
|
|
|
2008-10-24 02:51:36 -05:00
|
|
|
class test_Env(ClassChecker):
|
|
|
|
"""
|
|
|
|
Test the `ipalib.config.Env` class.
|
|
|
|
"""
|
|
|
|
|
|
|
|
_cls = config.Env
|
|
|
|
|
|
|
|
def test_init(self):
|
|
|
|
"""
|
|
|
|
Test the `ipalib.config.Env.__init__` method.
|
|
|
|
"""
|
2008-12-23 02:11:03 -06:00
|
|
|
o = self.cls()
|
2008-12-23 00:09:35 -06:00
|
|
|
assert list(o) == []
|
|
|
|
assert len(o) == 0
|
|
|
|
assert o.__islocked__() is False
|
2008-10-24 16:07:07 -05:00
|
|
|
|
2008-12-23 02:11:03 -06:00
|
|
|
def test_lock(self):
|
|
|
|
"""
|
|
|
|
Test the `ipalib.config.Env.__lock__` method.
|
|
|
|
"""
|
|
|
|
o = self.cls()
|
2009-01-06 19:21:46 -06:00
|
|
|
assert o.__islocked__() is False
|
2008-12-23 02:11:03 -06:00
|
|
|
o.__lock__()
|
2009-01-06 19:21:46 -06:00
|
|
|
assert o.__islocked__() is True
|
2015-08-24 05:40:33 -05:00
|
|
|
e = raises(Exception, o.__lock__)
|
2008-12-23 02:11:03 -06:00
|
|
|
assert str(e) == 'Env.__lock__() already called'
|
|
|
|
|
2009-01-06 19:21:46 -06:00
|
|
|
# Also test with base.lock() function:
|
|
|
|
o = self.cls()
|
|
|
|
assert o.__islocked__() is False
|
|
|
|
assert base.lock(o) is o
|
|
|
|
assert o.__islocked__() is True
|
|
|
|
e = raises(AssertionError, base.lock, o)
|
|
|
|
assert str(e) == 'already locked: %r' % o
|
|
|
|
|
|
|
|
def test_islocked(self):
|
|
|
|
"""
|
|
|
|
Test the `ipalib.config.Env.__islocked__` method.
|
|
|
|
"""
|
|
|
|
o = self.cls()
|
|
|
|
assert o.__islocked__() is False
|
|
|
|
assert base.islocked(o) is False
|
|
|
|
o.__lock__()
|
|
|
|
assert o.__islocked__() is True
|
|
|
|
assert base.islocked(o) is True
|
|
|
|
|
2008-12-22 18:29:11 -06:00
|
|
|
def test_setattr(self):
|
|
|
|
"""
|
|
|
|
Test the `ipalib.config.Env.__setattr__` method.
|
|
|
|
"""
|
|
|
|
o = self.cls()
|
|
|
|
for (name, raw, value) in good_vars:
|
|
|
|
# Test setting the value:
|
|
|
|
setattr(o, name, raw)
|
|
|
|
result = getattr(o, name)
|
|
|
|
assert type(result) is type(value)
|
|
|
|
assert result == value
|
|
|
|
assert result is o[name]
|
|
|
|
|
|
|
|
# Test that value cannot be overridden once set:
|
|
|
|
e = raises(AttributeError, setattr, o, name, raw)
|
|
|
|
assert str(e) == OVERRIDE_ERROR % ('Env', name, value, raw)
|
|
|
|
|
|
|
|
# Test that values cannot be set once locked:
|
|
|
|
o = self.cls()
|
|
|
|
o.__lock__()
|
|
|
|
for (name, raw, value) in good_vars:
|
|
|
|
e = raises(AttributeError, setattr, o, name, raw)
|
2008-12-22 22:02:43 -06:00
|
|
|
assert str(e) == SET_ERROR % ('Env', name, raw)
|
2008-12-22 18:29:11 -06:00
|
|
|
|
2008-12-30 01:45:48 -06:00
|
|
|
# Test that name is tested with check_name():
|
|
|
|
o = self.cls()
|
|
|
|
for (name, value) in bad_names:
|
|
|
|
e = raises(ValueError, setattr, o, name, value)
|
|
|
|
assert str(e) == NAME_ERROR % (NAME_REGEX, name)
|
|
|
|
|
2008-12-22 18:29:11 -06:00
|
|
|
def test_setitem(self):
|
|
|
|
"""
|
|
|
|
Test the `ipalib.config.Env.__setitem__` method.
|
|
|
|
"""
|
|
|
|
o = self.cls()
|
|
|
|
for (key, raw, value) in good_vars:
|
|
|
|
# Test setting the value:
|
|
|
|
o[key] = raw
|
|
|
|
result = o[key]
|
|
|
|
assert type(result) is type(value)
|
|
|
|
assert result == value
|
|
|
|
assert result is getattr(o, key)
|
|
|
|
|
|
|
|
# Test that value cannot be overridden once set:
|
|
|
|
e = raises(AttributeError, o.__setitem__, key, raw)
|
|
|
|
assert str(e) == OVERRIDE_ERROR % ('Env', key, value, raw)
|
|
|
|
|
|
|
|
# Test that values cannot be set once locked:
|
|
|
|
o = self.cls()
|
|
|
|
o.__lock__()
|
|
|
|
for (key, raw, value) in good_vars:
|
|
|
|
e = raises(AttributeError, o.__setitem__, key, raw)
|
2008-12-22 22:02:43 -06:00
|
|
|
assert str(e) == SET_ERROR % ('Env', key, raw)
|
|
|
|
|
2008-12-30 01:45:48 -06:00
|
|
|
# Test that name is tested with check_name():
|
|
|
|
o = self.cls()
|
|
|
|
for (key, value) in bad_names:
|
|
|
|
e = raises(ValueError, o.__setitem__, key, value)
|
|
|
|
assert str(e) == NAME_ERROR % (NAME_REGEX, key)
|
|
|
|
|
2008-12-22 22:02:43 -06:00
|
|
|
def test_getitem(self):
|
|
|
|
"""
|
|
|
|
Test the `ipalib.config.Env.__getitem__` method.
|
|
|
|
"""
|
|
|
|
o = self.cls()
|
2010-02-22 13:29:26 -06:00
|
|
|
value = u'some value'
|
2008-12-22 22:02:43 -06:00
|
|
|
o.key = value
|
|
|
|
assert o.key is value
|
|
|
|
assert o['key'] is value
|
|
|
|
for name in ('one', 'two'):
|
|
|
|
e = raises(KeyError, getitem, o, name)
|
|
|
|
assert str(e) == repr(name)
|
|
|
|
|
|
|
|
def test_delattr(self):
|
|
|
|
"""
|
|
|
|
Test the `ipalib.config.Env.__delattr__` method.
|
|
|
|
|
|
|
|
This also tests that ``__delitem__`` is not implemented.
|
|
|
|
"""
|
|
|
|
o = self.cls()
|
|
|
|
o.one = 1
|
|
|
|
assert o.one == 1
|
|
|
|
for key in ('one', 'two'):
|
|
|
|
e = raises(AttributeError, delattr, o, key)
|
|
|
|
assert str(e) == DEL_ERROR % ('Env', key)
|
|
|
|
e = raises(AttributeError, delitem, o, key)
|
|
|
|
assert str(e) == '__delitem__'
|
2008-12-22 18:29:11 -06:00
|
|
|
|
2008-12-23 02:11:03 -06:00
|
|
|
def test_contains(self):
|
|
|
|
"""
|
|
|
|
Test the `ipalib.config.Env.__contains__` method.
|
|
|
|
"""
|
|
|
|
o = self.cls()
|
|
|
|
items = [
|
|
|
|
('one', 1),
|
|
|
|
('two', 2),
|
|
|
|
('three', 3),
|
|
|
|
('four', 4),
|
|
|
|
]
|
|
|
|
for (key, value) in items:
|
|
|
|
assert key not in o
|
|
|
|
o[key] = value
|
|
|
|
assert key in o
|
|
|
|
|
|
|
|
def test_len(self):
|
|
|
|
"""
|
|
|
|
Test the `ipalib.config.Env.__len__` method.
|
|
|
|
"""
|
|
|
|
o = self.cls()
|
|
|
|
assert len(o) == 0
|
2015-08-12 08:23:56 -05:00
|
|
|
for i in range(1, 11):
|
2008-12-23 02:11:03 -06:00
|
|
|
key = 'key%d' % i
|
2010-02-22 13:29:26 -06:00
|
|
|
value = u'value %d' % i
|
2008-12-23 02:11:03 -06:00
|
|
|
o[key] = value
|
|
|
|
assert o[key] is value
|
|
|
|
assert len(o) == i
|
|
|
|
|
|
|
|
def test_iter(self):
|
|
|
|
"""
|
|
|
|
Test the `ipalib.config.Env.__iter__` method.
|
|
|
|
"""
|
|
|
|
o = self.cls()
|
|
|
|
default_keys = tuple(o)
|
|
|
|
keys = ('one', 'two', 'three', 'four', 'five')
|
|
|
|
for key in keys:
|
|
|
|
o[key] = 'the value'
|
|
|
|
assert list(o) == sorted(keys + default_keys)
|
|
|
|
|
2008-12-30 15:38:05 -06:00
|
|
|
def test_merge(self):
|
|
|
|
"""
|
|
|
|
Test the `ipalib.config.Env._merge` method.
|
|
|
|
"""
|
|
|
|
group1 = (
|
2010-02-22 13:29:26 -06:00
|
|
|
('key1', u'value 1'),
|
|
|
|
('key2', u'value 2'),
|
|
|
|
('key3', u'value 3'),
|
|
|
|
('key4', u'value 4'),
|
2008-12-30 15:38:05 -06:00
|
|
|
)
|
|
|
|
group2 = (
|
2010-02-22 13:29:26 -06:00
|
|
|
('key0', u'Value 0'),
|
|
|
|
('key2', u'Value 2'),
|
|
|
|
('key4', u'Value 4'),
|
|
|
|
('key5', u'Value 5'),
|
2008-12-30 15:38:05 -06:00
|
|
|
)
|
|
|
|
o = self.cls()
|
|
|
|
assert o._merge(**dict(group1)) == (4, 4)
|
|
|
|
assert len(o) == 4
|
|
|
|
assert list(o) == list(key for (key, value) in group1)
|
|
|
|
for (key, value) in group1:
|
|
|
|
assert getattr(o, key) is value
|
|
|
|
assert o[key] is value
|
|
|
|
assert o._merge(**dict(group2)) == (2, 4)
|
|
|
|
assert len(o) == 6
|
|
|
|
expected = dict(group2)
|
|
|
|
expected.update(dict(group1))
|
|
|
|
assert list(o) == sorted(expected)
|
|
|
|
assert expected['key2'] == 'value 2' # And not 'Value 2'
|
Use Python3-compatible dict method names
Python 2 has keys()/values()/items(), which return lists,
iterkeys()/itervalues()/iteritems(), which return iterators,
and viewkeys()/viewvalues()/viewitems() which return views.
Python 3 has only keys()/values()/items(), which return views.
To get iterators, one can use iter() or a for loop/comprehension;
for lists there's the list() constructor.
When iterating through the entire dict, without modifying the dict,
the difference between Python 2's items() and iteritems() is
negligible, especially on small dicts (the main overhead is
extra memory, not CPU time). In the interest of simpler code,
this patch changes many instances of iteritems() to items(),
iterkeys() to keys() etc.
In other cases, helpers like six.itervalues are used.
Reviewed-By: Christian Heimes <cheimes@redhat.com>
Reviewed-By: Jan Cholasta <jcholast@redhat.com>
2015-08-11 06:51:14 -05:00
|
|
|
for (key, value) in expected.items():
|
2008-12-30 15:38:05 -06:00
|
|
|
assert getattr(o, key) is value
|
|
|
|
assert o[key] is value
|
|
|
|
assert o._merge(**expected) == (0, 6)
|
|
|
|
assert len(o) == 6
|
|
|
|
assert list(o) == sorted(expected)
|
|
|
|
|
|
|
|
def test_merge_from_file(self):
|
|
|
|
"""
|
|
|
|
Test the `ipalib.config.Env._merge_from_file` method.
|
|
|
|
"""
|
|
|
|
tmp = TempDir()
|
|
|
|
assert callable(tmp.join)
|
|
|
|
|
|
|
|
# Test a config file that doesn't exist
|
|
|
|
no_exist = tmp.join('no_exist.conf')
|
|
|
|
assert not path.exists(no_exist)
|
|
|
|
o = self.cls()
|
|
|
|
o._bootstrap()
|
|
|
|
keys = tuple(o)
|
|
|
|
orig = dict((k, o[k]) for k in o)
|
|
|
|
assert o._merge_from_file(no_exist) is None
|
|
|
|
assert tuple(o) == keys
|
|
|
|
|
|
|
|
# Test an empty config file
|
|
|
|
empty = tmp.touch('empty.conf')
|
|
|
|
assert path.isfile(empty)
|
|
|
|
assert o._merge_from_file(empty) == (0, 0)
|
|
|
|
assert tuple(o) == keys
|
|
|
|
|
|
|
|
# Test a mal-formed config file:
|
|
|
|
bad = tmp.join('bad.conf')
|
|
|
|
open(bad, 'w').write(config_bad)
|
|
|
|
assert path.isfile(bad)
|
|
|
|
assert o._merge_from_file(bad) is None
|
|
|
|
assert tuple(o) == keys
|
|
|
|
|
|
|
|
# Test a valid config file that tries to override
|
|
|
|
override = tmp.join('override.conf')
|
|
|
|
open(override, 'w').write(config_override)
|
|
|
|
assert path.isfile(override)
|
|
|
|
assert o._merge_from_file(override) == (4, 6)
|
|
|
|
for (k, v) in orig.items():
|
|
|
|
assert o[k] is v
|
2010-10-18 13:52:46 -05:00
|
|
|
assert list(o) == sorted(keys + ('key0', 'key1', 'key2', 'key3', 'config_loaded'))
|
2015-08-12 08:23:56 -05:00
|
|
|
for i in range(4):
|
2008-12-30 15:38:05 -06:00
|
|
|
assert o['key%d' % i] == ('var%d' % i)
|
|
|
|
keys = tuple(o)
|
|
|
|
|
|
|
|
# Test a valid config file with type conversion
|
|
|
|
good = tmp.join('good.conf')
|
|
|
|
open(good, 'w').write(config_good)
|
|
|
|
assert path.isfile(good)
|
2008-12-30 16:02:15 -06:00
|
|
|
assert o._merge_from_file(good) == (6, 6)
|
|
|
|
added = ('string', 'null', 'yes', 'no', 'number', 'floating')
|
|
|
|
assert list(o) == sorted(keys + added)
|
2021-07-29 05:51:55 -05:00
|
|
|
|
|
|
|
# pylint: disable=no-member
|
2008-12-30 16:02:15 -06:00
|
|
|
assert o.string == 'Hello world!'
|
|
|
|
assert o.null is None
|
2008-12-30 15:38:05 -06:00
|
|
|
assert o.yes is True
|
|
|
|
assert o.no is False
|
|
|
|
assert o.number == 42
|
2014-06-13 05:47:48 -05:00
|
|
|
assert o.floating == '3.14'
|
2021-07-29 05:51:55 -05:00
|
|
|
# pylint: enable=no-member
|
2008-12-30 15:38:05 -06:00
|
|
|
|
2009-05-11 03:34:52 -05:00
|
|
|
def new(self, in_tree=False):
|
2008-12-23 02:11:03 -06:00
|
|
|
"""
|
|
|
|
Set os.environ['HOME'] to a tempdir.
|
|
|
|
|
|
|
|
Returns tuple with new Env instance and the TempHome instance. This
|
|
|
|
helper method is used in testing the bootstrap related methods below.
|
|
|
|
"""
|
|
|
|
home = TempHome()
|
2009-05-11 03:34:52 -05:00
|
|
|
o = self.cls()
|
|
|
|
if in_tree:
|
|
|
|
o.in_tree = True
|
|
|
|
return (o, home)
|
2008-12-23 02:11:03 -06:00
|
|
|
|
2008-10-24 16:07:07 -05:00
|
|
|
def bootstrap(self, **overrides):
|
2008-12-23 02:11:03 -06:00
|
|
|
"""
|
|
|
|
Helper method used in testing bootstrap related methods below.
|
|
|
|
"""
|
2008-10-24 17:02:26 -05:00
|
|
|
(o, home) = self.new()
|
2008-10-24 21:02:14 -05:00
|
|
|
assert o._isdone('_bootstrap') is False
|
2008-10-24 16:07:07 -05:00
|
|
|
o._bootstrap(**overrides)
|
2008-10-24 21:02:14 -05:00
|
|
|
assert o._isdone('_bootstrap') is True
|
2015-08-24 05:40:33 -05:00
|
|
|
e = raises(Exception, o._bootstrap)
|
2008-10-24 16:35:58 -05:00
|
|
|
assert str(e) == 'Env._bootstrap() already called'
|
2008-10-24 17:02:26 -05:00
|
|
|
return (o, home)
|
2008-10-24 16:07:07 -05:00
|
|
|
|
|
|
|
def test_bootstrap(self):
|
|
|
|
"""
|
|
|
|
Test the `ipalib.config.Env._bootstrap` method.
|
|
|
|
"""
|
|
|
|
# Test defaults created by _bootstrap():
|
2008-10-24 17:02:26 -05:00
|
|
|
(o, home) = self.new()
|
2008-10-24 16:07:07 -05:00
|
|
|
o._bootstrap()
|
2008-12-23 00:09:35 -06:00
|
|
|
ipalib = path.dirname(path.abspath(config.__file__))
|
|
|
|
assert o.ipalib == ipalib
|
|
|
|
assert o.site_packages == path.dirname(ipalib)
|
|
|
|
assert o.script == path.abspath(sys.argv[0])
|
|
|
|
assert o.bin == path.dirname(path.abspath(sys.argv[0]))
|
|
|
|
assert o.home == home.path
|
|
|
|
assert o.dot_ipa == home.join('.ipa')
|
2008-10-24 16:07:07 -05:00
|
|
|
assert o.context == 'default'
|
2020-05-04 12:41:38 -05:00
|
|
|
if (
|
|
|
|
# venv site module doesn't have getsitepackages()
|
|
|
|
not hasattr(site, "getsitepackages")
|
|
|
|
or o.site_packages in site.getsitepackages()
|
|
|
|
):
|
|
|
|
assert o.in_tree is False
|
|
|
|
assert o.confdir == '/etc/ipa'
|
|
|
|
assert o.conf == '/etc/ipa/default.conf'
|
|
|
|
assert o.conf_default == o.conf
|
|
|
|
else:
|
|
|
|
assert o.in_tree is True
|
|
|
|
assert o.confdir == o.dot_ipa
|
|
|
|
assert o.conf == home.join('.ipa/default.conf')
|
|
|
|
assert o.conf_default == o.conf
|
2008-10-24 16:07:07 -05:00
|
|
|
|
|
|
|
# Test overriding values created by _bootstrap()
|
2008-12-22 17:16:57 -06:00
|
|
|
(o, home) = self.bootstrap(in_tree='True', context='server')
|
2008-10-24 16:07:07 -05:00
|
|
|
assert o.in_tree is True
|
|
|
|
assert o.context == 'server'
|
2008-10-24 17:02:26 -05:00
|
|
|
assert o.conf == home.join('.ipa', 'server.conf')
|
2020-05-04 12:41:38 -05:00
|
|
|
|
|
|
|
o, home = self.bootstrap(
|
|
|
|
conf='/my/wacky/whatever.conf', in_tree=False
|
|
|
|
)
|
2008-10-24 16:07:07 -05:00
|
|
|
assert o.in_tree is False
|
|
|
|
assert o.context == 'default'
|
|
|
|
assert o.conf == '/my/wacky/whatever.conf'
|
2016-12-01 07:52:39 -06:00
|
|
|
assert o.conf_default == '/etc/ipa/default.conf'
|
2020-05-04 12:41:38 -05:00
|
|
|
|
|
|
|
o, home = self.bootstrap(
|
|
|
|
conf_default='/my/wacky/default.conf', in_tree=False
|
|
|
|
)
|
2008-10-27 01:58:25 -05:00
|
|
|
assert o.in_tree is False
|
|
|
|
assert o.context == 'default'
|
2016-12-01 07:52:39 -06:00
|
|
|
assert o.conf == '/etc/ipa/default.conf'
|
2008-10-27 01:58:25 -05:00
|
|
|
assert o.conf_default == '/my/wacky/default.conf'
|
2008-10-24 16:07:07 -05:00
|
|
|
|
|
|
|
# Test various overrides and types conversion
|
|
|
|
kw = dict(
|
|
|
|
yes=True,
|
|
|
|
no=False,
|
|
|
|
num=42,
|
|
|
|
msg='Hello, world!',
|
|
|
|
)
|
|
|
|
override = dict(
|
|
|
|
(k, u' %s ' % v) for (k, v) in kw.items()
|
|
|
|
)
|
2008-10-24 17:02:26 -05:00
|
|
|
(o, home) = self.new()
|
2008-10-24 16:07:07 -05:00
|
|
|
for key in kw:
|
|
|
|
assert key not in o
|
|
|
|
o._bootstrap(**override)
|
|
|
|
for (key, value) in kw.items():
|
|
|
|
assert getattr(o, key) == value
|
|
|
|
assert o[key] == value
|
|
|
|
|
2009-05-11 03:34:52 -05:00
|
|
|
def finalize_core(self, ctx, **defaults):
|
2008-12-23 02:11:03 -06:00
|
|
|
"""
|
|
|
|
Helper method used in testing `Env._finalize_core`.
|
|
|
|
"""
|
2009-05-11 03:34:52 -05:00
|
|
|
# We must force in_tree=True so we don't load possible config files in
|
|
|
|
# /etc/ipa/, whose contents could break this test:
|
|
|
|
(o, home) = self.new(in_tree=True)
|
|
|
|
if ctx:
|
|
|
|
o.context = ctx
|
2008-10-24 21:02:14 -05:00
|
|
|
|
2009-05-11 03:34:52 -05:00
|
|
|
# Check that calls cascade down the chain:
|
|
|
|
set_here = ('in_server', 'logdir', 'log')
|
2008-10-24 21:02:14 -05:00
|
|
|
assert o._isdone('_bootstrap') is False
|
|
|
|
assert o._isdone('_finalize_core') is False
|
|
|
|
assert o._isdone('_finalize') is False
|
2009-05-11 03:34:52 -05:00
|
|
|
for key in set_here:
|
|
|
|
assert key not in o
|
|
|
|
o._finalize_core(**defaults)
|
2008-10-24 21:02:14 -05:00
|
|
|
assert o._isdone('_bootstrap') is True
|
|
|
|
assert o._isdone('_finalize_core') is True
|
2009-05-11 03:34:52 -05:00
|
|
|
assert o._isdone('_finalize') is False # Should not cascade
|
|
|
|
for key in set_here:
|
|
|
|
assert key in o
|
2008-10-24 21:02:14 -05:00
|
|
|
|
|
|
|
# Check that it can't be called twice:
|
2015-08-24 05:40:33 -05:00
|
|
|
e = raises(Exception, o._finalize_core)
|
2008-10-24 21:02:14 -05:00
|
|
|
assert str(e) == 'Env._finalize_core() already called'
|
|
|
|
|
2009-05-11 03:34:52 -05:00
|
|
|
return (o, home)
|
2008-10-24 21:02:14 -05:00
|
|
|
|
2009-05-11 03:34:52 -05:00
|
|
|
def test_finalize_core(self):
|
|
|
|
"""
|
|
|
|
Test the `ipalib.config.Env._finalize_core` method.
|
|
|
|
"""
|
|
|
|
# Test that correct defaults are generated:
|
|
|
|
(o, home) = self.finalize_core(None)
|
2008-10-24 21:02:14 -05:00
|
|
|
assert o.in_server is False
|
2009-05-11 03:34:52 -05:00
|
|
|
assert o.logdir == home.join('.ipa', 'log')
|
|
|
|
assert o.log == home.join('.ipa', 'log', 'default.log')
|
2008-10-24 21:02:14 -05:00
|
|
|
|
2009-05-11 03:34:52 -05:00
|
|
|
# Test with context='server'
|
|
|
|
(o, home) = self.finalize_core('server')
|
2008-10-24 21:02:14 -05:00
|
|
|
assert o.in_server is True
|
2009-05-11 03:34:52 -05:00
|
|
|
assert o.logdir == home.join('.ipa', 'log')
|
|
|
|
assert o.log == home.join('.ipa', 'log', 'server.log')
|
|
|
|
|
|
|
|
# Test that **defaults can't set in_server, logdir, nor log:
|
|
|
|
(o, home) = self.finalize_core(None,
|
|
|
|
in_server='IN_SERVER',
|
|
|
|
logdir='LOGDIR',
|
|
|
|
log='LOG',
|
|
|
|
)
|
|
|
|
assert o.in_server is False
|
|
|
|
assert o.logdir == home.join('.ipa', 'log')
|
|
|
|
assert o.log == home.join('.ipa', 'log', 'default.log')
|
2008-10-24 21:02:14 -05:00
|
|
|
|
|
|
|
# Test loading config file, plus test some in-tree stuff
|
|
|
|
(o, home) = self.bootstrap(in_tree=True, context='server')
|
|
|
|
for key in ('yes', 'no', 'number'):
|
|
|
|
assert key not in o
|
|
|
|
home.write(config_good, '.ipa', 'server.conf')
|
2008-10-27 02:09:53 -05:00
|
|
|
home.write(config_default, '.ipa', 'default.conf')
|
2008-10-24 21:02:14 -05:00
|
|
|
o._finalize_core()
|
|
|
|
assert o.in_tree is True
|
|
|
|
assert o.context == 'server'
|
|
|
|
assert o.in_server is True
|
2009-05-11 03:34:52 -05:00
|
|
|
assert o.logdir == home.join('.ipa', 'log')
|
2008-10-24 21:02:14 -05:00
|
|
|
assert o.log == home.join('.ipa', 'log', 'server.log')
|
2021-07-29 05:51:55 -05:00
|
|
|
# pylint: disable=no-member
|
2008-10-24 21:02:14 -05:00
|
|
|
assert o.yes is True
|
|
|
|
assert o.no is False
|
|
|
|
assert o.number == 42
|
2008-10-27 02:09:53 -05:00
|
|
|
assert o.not_in_other == 'foo_bar'
|
2021-07-29 05:51:55 -05:00
|
|
|
# pylint: enable=no-member
|
2008-10-24 21:02:14 -05:00
|
|
|
|
|
|
|
# Test using DEFAULT_CONFIG:
|
|
|
|
defaults = dict(constants.DEFAULT_CONFIG)
|
2009-05-11 03:34:52 -05:00
|
|
|
(o, home) = self.finalize_core(None, **defaults)
|
2017-02-27 06:17:17 -06:00
|
|
|
list_o = [key for key in o if key != 'fips_mode']
|
|
|
|
assert list_o == sorted(defaults)
|
2008-10-24 21:02:14 -05:00
|
|
|
for (key, value) in defaults.items():
|
2008-12-22 17:16:57 -06:00
|
|
|
if value is object:
|
2008-10-24 21:02:14 -05:00
|
|
|
continue
|
2011-02-09 16:02:10 -06:00
|
|
|
if key == 'mode':
|
|
|
|
continue
|
2009-05-11 03:34:52 -05:00
|
|
|
assert o[key] == value, '%r is %r; should be %r' % (key, o[key], value)
|
2008-10-24 21:02:14 -05:00
|
|
|
|
|
|
|
def test_finalize(self):
|
|
|
|
"""
|
|
|
|
Test the `ipalib.config.Env._finalize` method.
|
|
|
|
"""
|
|
|
|
# Check that calls cascade up the chain:
|
2016-09-26 11:22:22 -05:00
|
|
|
o, _home = self.new(in_tree=True)
|
2008-10-24 21:02:14 -05:00
|
|
|
assert o._isdone('_bootstrap') is False
|
|
|
|
assert o._isdone('_finalize_core') is False
|
|
|
|
assert o._isdone('_finalize') is False
|
|
|
|
o._finalize()
|
|
|
|
assert o._isdone('_bootstrap') is True
|
|
|
|
assert o._isdone('_finalize_core') is True
|
|
|
|
assert o._isdone('_finalize') is True
|
|
|
|
|
|
|
|
# Check that it can't be called twice:
|
2015-08-24 05:40:33 -05:00
|
|
|
e = raises(Exception, o._finalize)
|
2008-10-24 21:02:14 -05:00
|
|
|
assert str(e) == 'Env._finalize() already called'
|
|
|
|
|
2008-10-24 21:21:27 -05:00
|
|
|
# Check that _finalize() calls __lock__()
|
2016-09-26 11:22:22 -05:00
|
|
|
o, _home = self.new(in_tree=True)
|
2008-10-24 21:21:27 -05:00
|
|
|
assert o.__islocked__() is False
|
|
|
|
o._finalize()
|
|
|
|
assert o.__islocked__() is True
|
2015-08-24 05:40:33 -05:00
|
|
|
e = raises(Exception, o.__lock__)
|
2008-10-24 21:21:27 -05:00
|
|
|
assert str(e) == 'Env.__lock__() already called'
|
|
|
|
|
|
|
|
# Check that **lastchance works
|
2016-09-26 11:22:22 -05:00
|
|
|
o, _home = self.finalize_core(None)
|
2008-10-24 21:21:27 -05:00
|
|
|
key = 'just_one_more_key'
|
2010-02-22 13:29:26 -06:00
|
|
|
value = u'with one more value'
|
2008-10-24 21:21:27 -05:00
|
|
|
lastchance = {key: value}
|
|
|
|
assert key not in o
|
|
|
|
assert o._isdone('_finalize') is False
|
|
|
|
o._finalize(**lastchance)
|
|
|
|
assert key in o
|
|
|
|
assert o[key] is value
|