From 7721443a625b2efd0744ad347c62795e5ba6bb91 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Tue, 7 Oct 2008 20:41:15 -0600 Subject: Moved ipalib/tests/ into tests/test_ipalib/ --- tests/test_ipalib/test_config.py | 101 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 tests/test_ipalib/test_config.py (limited to 'tests/test_ipalib/test_config.py') diff --git a/tests/test_ipalib/test_config.py b/tests/test_ipalib/test_config.py new file mode 100644 index 00000000..de7d4c22 --- /dev/null +++ b/tests/test_ipalib/test_config.py @@ -0,0 +1,101 @@ +# Authors: +# Martin Nagy +# +# Copyright (C) 2008 Red Hat +# see file 'COPYING' for use and warranty information +# +# 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; version 2 only +# +# 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 +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +""" +Unit tests for `ipalib.config` module. +""" + +import types + +from tstutil import raises +from ipalib import config + + +def test_generate_env(): + """ + Test the `config.generate_env` function + """ + + # Make sure we don't overwrite any properties + env = dict( + query_dns = False, + server = ('first', 'second'), + realm = 'myrealm', + ) + d = config.generate_env(env) + assert d['query_dns'] == False + + # Make sure the servers is overwrote properly (that it is still LazyProp) + iter = d['server'].get_value() + assert iter.next() == 'first' + assert iter.next() == 'second' + + +def test_LazyProp(): + """ + Test the `config.LazyProp` class + """ + + def dummy(): + return 1 + + # Basic sanity testing with no initial value + prop = config.LazyProp(dummy) + assert prop.get_value() == 1 + prop.set_value(2) + assert prop.get_value() == 2 + + # Basic sanity testing with initial value + prop = config.LazyProp(dummy, 3) + assert prop.get_value() == 3 + prop.set_value(4) + assert prop.get_value() == 4 + + +def test_LazyIter(): + """ + Test the `config.LazyIter` class + """ + + def dummy(): + yield 1 + yield 2 + + # Basic sanity testing with no initial value + prop = config.LazyIter(dummy) + iter = prop.get_value() + assert iter.next() == 1 + assert iter.next() == 2 + raises(StopIteration, iter.next) + + # Basic sanity testing with initial value + prop = config.LazyIter(dummy, 0) + iter = prop.get_value() + assert iter.next() == 0 + assert iter.next() == 1 + assert iter.next() == 2 + raises(StopIteration, iter.next) + + +def test_read_config(): + """ + Test the `config.read_config` class + """ + + raises(AssertionError, config.read_config, 1) -- cgit From af56c71d506c2573f99a1ca8334cfa90dc7f74d7 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Tue, 7 Oct 2008 21:25:23 -0600 Subject: Cleaned up package and module level docstrings for everything in tests/ --- tests/test_ipalib/test_config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/test_ipalib/test_config.py') diff --git a/tests/test_ipalib/test_config.py b/tests/test_ipalib/test_config.py index de7d4c22..242ce16b 100644 --- a/tests/test_ipalib/test_config.py +++ b/tests/test_ipalib/test_config.py @@ -18,7 +18,7 @@ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA """ -Unit tests for `ipalib.config` module. +Test the `ipalib.config` module. """ import types -- cgit From f6ac2df6bd7ceddd0f3eb968198cc3ebd1388087 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Tue, 7 Oct 2008 21:59:47 -0600 Subject: Moved tstutil.py into base of tests so it can be used by all test subpackages more easily --- tests/test_ipalib/test_config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/test_ipalib/test_config.py') diff --git a/tests/test_ipalib/test_config.py b/tests/test_ipalib/test_config.py index 242ce16b..a1329fa4 100644 --- a/tests/test_ipalib/test_config.py +++ b/tests/test_ipalib/test_config.py @@ -23,7 +23,7 @@ Test the `ipalib.config` module. import types -from tstutil import raises +from tests.tstutil import raises from ipalib import config -- cgit From deb8e3dfc899cfc7ee31f47c1ba7c4301c58fc51 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Tue, 7 Oct 2008 22:30:53 -0600 Subject: Renamed tests/tstutil.py to tests/util.py --- tests/test_ipalib/test_config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/test_ipalib/test_config.py') diff --git a/tests/test_ipalib/test_config.py b/tests/test_ipalib/test_config.py index a1329fa4..7b12a53e 100644 --- a/tests/test_ipalib/test_config.py +++ b/tests/test_ipalib/test_config.py @@ -23,7 +23,7 @@ Test the `ipalib.config` module. import types -from tests.tstutil import raises +from tests.util import raises from ipalib import config -- cgit From 0d2b5a889237165a9870668d9f97787606524e32 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Tue, 7 Oct 2008 23:31:40 -0600 Subject: PEP 257: cleaned up docstrings in test_config.py --- tests/test_ipalib/test_config.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'tests/test_ipalib/test_config.py') diff --git a/tests/test_ipalib/test_config.py b/tests/test_ipalib/test_config.py index 7b12a53e..2ec75048 100644 --- a/tests/test_ipalib/test_config.py +++ b/tests/test_ipalib/test_config.py @@ -29,7 +29,7 @@ from ipalib import config def test_generate_env(): """ - Test the `config.generate_env` function + Test the `ipalib.config.generate_env` function. """ # Make sure we don't overwrite any properties @@ -49,7 +49,7 @@ def test_generate_env(): def test_LazyProp(): """ - Test the `config.LazyProp` class + Test the `ipalib.config.LazyProp` class. """ def dummy(): @@ -70,7 +70,7 @@ def test_LazyProp(): def test_LazyIter(): """ - Test the `config.LazyIter` class + Test the `ipalib.config.LazyIter` class. """ def dummy(): @@ -95,7 +95,7 @@ def test_LazyIter(): def test_read_config(): """ - Test the `config.read_config` class + Test the `ipalib.config.read_config` class. """ raises(AssertionError, config.read_config, 1) -- cgit From ff88652a405c7fd9236a9b1d80dd8955a9ca056d Mon Sep 17 00:00:00 2001 From: Martin Nagy Date: Tue, 14 Oct 2008 21:22:44 +0200 Subject: Convert string values to boolean when generating environment --- tests/test_ipalib/test_config.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'tests/test_ipalib/test_config.py') diff --git a/tests/test_ipalib/test_config.py b/tests/test_ipalib/test_config.py index 2ec75048..3fe44136 100644 --- a/tests/test_ipalib/test_config.py +++ b/tests/test_ipalib/test_config.py @@ -37,8 +37,11 @@ def test_generate_env(): query_dns = False, server = ('first', 'second'), realm = 'myrealm', + # test right conversions + server_context = 'off', ) d = config.generate_env(env) + assert d['server_context'] == False assert d['query_dns'] == False # Make sure the servers is overwrote properly (that it is still LazyProp) -- cgit From 3a80297b04d6fbfd2367ec76c5651d20293adccc Mon Sep 17 00:00:00 2001 From: Martin Nagy Date: Fri, 17 Oct 2008 22:55:03 +0200 Subject: Reworking Environment, moved it to config.py --- tests/test_ipalib/test_config.py | 106 ++++++++++++++++++++++++++++++++++----- 1 file changed, 94 insertions(+), 12 deletions(-) (limited to 'tests/test_ipalib/test_config.py') diff --git a/tests/test_ipalib/test_config.py b/tests/test_ipalib/test_config.py index 3fe44136..df830232 100644 --- a/tests/test_ipalib/test_config.py +++ b/tests/test_ipalib/test_config.py @@ -23,29 +23,111 @@ Test the `ipalib.config` module. import types -from tests.util import raises +from tests.util import raises, setitem, delitem +#from tests.util import getitem, setitem, delitem from ipalib import config -def test_generate_env(): +def test_Environment(): """ - Test the `ipalib.config.generate_env` function. + Test the `ipalib.config.Environment` class. + """ + # This has to be the same as iter_cnt + control_cnt = 0 + class prop_class: + def __init__(self, val): + self._val = val + def get_value(self): + return self._val + + class iter_class(prop_class): + # Increment this for each time iter_class yields + iter_cnt = 0 + def get_value(self): + for item in self._val: + self.__class__.iter_cnt += 1 + yield item + + # Tests for basic functionality + basic_tests = ( + ('a', 1), + ('b', 'basic_foo'), + ('c', ('basic_bar', 'basic_baz')), + ) + # Tests with prop classes + prop_tests = ( + ('d', prop_class(2), 2), + ('e', prop_class('prop_foo'), 'prop_foo'), + ('f', prop_class(('prop_bar', 'prop_baz')), ('prop_bar', 'prop_baz')), + ) + # Tests with iter classes + iter_tests = ( + ('g', iter_class((3, 4, 5)), (3, 4, 5)), + ('h', iter_class(('iter_foo', 'iter_bar', 'iter_baz')), + ('iter_foo', 'iter_bar', 'iter_baz') + ), + ) + + # Set all the values + env = config.Environment() + for name, val in basic_tests: + env[name] = val + for name, val, dummy in prop_tests: + env[name] = val + for name, val, dummy in iter_tests: + env[name] = val + + # Test if the values are correct + for name, val in basic_tests: + assert env[name] == val + for name, dummy, val in prop_tests: + assert env[name] == val + # Test if the get_value() function is called only when needed + for name, dummy, correct_values in iter_tests: + values_in_env = [] + for val in env[name]: + control_cnt += 1 + assert iter_class.iter_cnt == control_cnt + values_in_env.append(val) + assert tuple(values_in_env) == correct_values + + # Test __setattr__() + env.spam = 'ham' + assert env.spam == 'ham' + + # Test if we throw AttributeError exception when trying to overwrite + # existing value, or delete it + raises(AttributeError, setitem, env, 'a', 1) + raises(AttributeError, setattr, env, 'a', 1) + raises(AttributeError, delitem, env, 'a') + raises(AttributeError, delattr, env, 'a') + raises(AttributeError, config.Environment.update, env, dict(a=1000)) + # This should be silently ignored + env.update(dict(a=1000), True) + assert env.a != 1000 + + +def test_set_default_env(): + """ + Test the `ipalib.config.set_default_env` function. """ # Make sure we don't overwrite any properties - env = dict( + d = dict( query_dns = False, server = ('first', 'second'), realm = 'myrealm', # test right conversions server_context = 'off', ) - d = config.generate_env(env) - assert d['server_context'] == False - assert d['query_dns'] == False + env = config.Environment() + config.set_default_env(env) + env.update(d) + assert env['server_context'] == False + assert env['query_dns'] == False # Make sure the servers is overwrote properly (that it is still LazyProp) - iter = d['server'].get_value() + iter = env['server'] assert iter.next() == 'first' assert iter.next() == 'second' @@ -59,13 +141,13 @@ def test_LazyProp(): return 1 # Basic sanity testing with no initial value - prop = config.LazyProp(dummy) + prop = config.LazyProp(int, dummy) assert prop.get_value() == 1 prop.set_value(2) assert prop.get_value() == 2 # Basic sanity testing with initial value - prop = config.LazyProp(dummy, 3) + prop = config.LazyProp(int, dummy, 3) assert prop.get_value() == 3 prop.set_value(4) assert prop.get_value() == 4 @@ -81,14 +163,14 @@ def test_LazyIter(): yield 2 # Basic sanity testing with no initial value - prop = config.LazyIter(dummy) + prop = config.LazyIter(int, dummy) iter = prop.get_value() assert iter.next() == 1 assert iter.next() == 2 raises(StopIteration, iter.next) # Basic sanity testing with initial value - prop = config.LazyIter(dummy, 0) + prop = config.LazyIter(int, dummy, 0) iter = prop.get_value() assert iter.next() == 0 assert iter.next() == 1 -- cgit From 18e74643a6979ca4e490d34975a38c83a1722417 Mon Sep 17 00:00:00 2001 From: Martin Nagy Date: Mon, 20 Oct 2008 19:53:07 +0200 Subject: Add comments in config.py and fix Environment.get() --- tests/test_ipalib/test_config.py | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'tests/test_ipalib/test_config.py') diff --git a/tests/test_ipalib/test_config.py b/tests/test_ipalib/test_config.py index df830232..ed982a73 100644 --- a/tests/test_ipalib/test_config.py +++ b/tests/test_ipalib/test_config.py @@ -93,7 +93,12 @@ def test_Environment(): # Test __setattr__() env.spam = 'ham' + assert env.spam == 'ham' + assert env['spam'] == 'ham' + assert env.get('spam') == 'ham' + assert env.get('nonexistent') == None + assert env.get('nonexistent', 42) == 42 # Test if we throw AttributeError exception when trying to overwrite # existing value, or delete it -- cgit From 2ec0312eb6d4131fe22fab6f4409b71cac83f98f Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Fri, 24 Oct 2008 01:51:36 -0600 Subject: Finished doodle with stricter version of Environment --- tests/test_ipalib/test_config.py | 122 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 120 insertions(+), 2 deletions(-) (limited to 'tests/test_ipalib/test_config.py') diff --git a/tests/test_ipalib/test_config.py b/tests/test_ipalib/test_config.py index ed982a73..fa497206 100644 --- a/tests/test_ipalib/test_config.py +++ b/tests/test_ipalib/test_config.py @@ -23,8 +23,8 @@ Test the `ipalib.config` module. import types -from tests.util import raises, setitem, delitem -#from tests.util import getitem, setitem, delitem +from tests.util import raises, setitem, delitem, ClassChecker +from tests.util import getitem, setitem, delitem from ipalib import config @@ -112,6 +112,124 @@ def test_Environment(): assert env.a != 1000 +class test_Env(ClassChecker): + """ + Test the `ipalib.config.Env` class. + """ + + _cls = config.Env + + def test_init(self): + """ + Test the `ipalib.config.Env.__init__` method. + """ + o = self.cls() + + def test_lock(self): + """ + Test the `ipalib.config.Env.__lock__` method. + """ + o = self.cls() + assert o._Env__locked is False + o.__lock__() + assert o._Env__locked is True + e = raises(StandardError, o.__lock__) + assert str(e) == 'Env.__lock__() already called' + + def test_getattr(self): + """ + Test the `ipalib.config.Env.__getattr__` method. + + Also tests the `ipalib.config.Env.__getitem__` method. + """ + o = self.cls() + value = 'some value' + o.key = value + assert o.key is value + assert o['key'] is value + o.call = lambda: 'whatever' + assert o.call == 'whatever' + assert o['call'] == 'whatever' + for name in ('one', 'two'): + e = raises(AttributeError, getattr, o, name) + assert str(e) == 'Env.%s' % name + e = raises(KeyError, getitem, o, name) + assert str(e) == repr(name) + + def test_setattr(self): + """ + Test the `ipalib.config.Env.__setattr__` method. + + Also tests the `ipalib.config.Env.__setitem__` method. + """ + items = [ + ('one', 1), + ('two', lambda: 2), + ('three', 3), + ('four', lambda: 4), + ] + for setvar in (setattr, setitem): + o = self.cls() + for (i, (name, value)) in enumerate(items): + setvar(o, name, value) + assert getattr(o, name) == i + 1 + assert o[name] == i + 1 + if callable(value): + assert name not in dir(o) + else: + assert name in dir(o) + e = raises(AttributeError, setvar, o, name, 42) + assert str(e) == 'cannot overwrite Env.%s with 42' % name + o = self.cls() + o.__lock__() + for (name, value) in items: + e = raises(AttributeError, setvar, o, name, value) + assert str(e) == \ + 'locked: cannot set Env.%s to %r' % (name, value) + + 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) == 'cannot del Env.%s' % key + e = raises(AttributeError, delitem, o, key) + assert str(e) == '__delitem__' + + def test_contains(self): + """ + Test the `ipalib.config.Env.__contains__` method. + """ + o = self.cls() + items = [ + ('one', 1), + ('two', lambda: 2), + ('three', 3), + ('four', lambda: 4), + ] + for (key, value) in items: + assert key not in o + o[key] = value + assert key in o + + def test_iter(self): + """ + Test the `ipalib.config.Env.__iter__` method. + """ + o = self.cls() + assert list(o) == [] + keys = ('one', 'two', 'three', 'four', 'five') + for key in keys: + o[key] = 'the value' + assert list(o) == sorted(keys) + + def test_set_default_env(): """ Test the `ipalib.config.set_default_env` function. -- cgit From f80beb948bb8914df922e85ef20d9152ca47b527 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Fri, 24 Oct 2008 15:07:07 -0600 Subject: Added ipalib/constants.py; added Env._load_config() method along with comprehensive unit tests for same --- tests/test_ipalib/test_config.py | 185 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 182 insertions(+), 3 deletions(-) (limited to 'tests/test_ipalib/test_config.py') diff --git a/tests/test_ipalib/test_config.py b/tests/test_ipalib/test_config.py index fa497206..39dc7906 100644 --- a/tests/test_ipalib/test_config.py +++ b/tests/test_ipalib/test_config.py @@ -22,9 +22,12 @@ Test the `ipalib.config` module. """ import types - +import os +from os import path +import sys from tests.util import raises, setitem, delitem, ClassChecker from tests.util import getitem, setitem, delitem +from tests.util import TempDir from ipalib import config @@ -112,6 +115,65 @@ def test_Environment(): assert env.a != 1000 +# Random base64-encoded data to simulate a misbehaving config file. +config_bad = """ +/9j/4AAQSkZJRgABAQEAlgCWAAD//gAIT2xpdmVy/9sAQwAQCwwODAoQDg0OEhEQExgoGhgWFhgx +IyUdKDozPTw5Mzg3QEhcTkBEV0U3OFBtUVdfYmdoZz5NcXlwZHhcZWdj/8AACwgAlgB0AQERAP/E +ABsAAAEFAQEAAAAAAAAAAAAAAAQAAQIDBQYH/8QAMhAAAgICAAUDBAIABAcAAAAAAQIAAwQRBRIh +MUEGE1EiMmFxFIEVI0LBFjNSYnKRof/aAAgBAQAAPwDCtzmNRr1o/MEP1D6f7kdkRakgBsAtoQhk +xls/y3Z113I11mhiUc1ewCf1Oq4anJgINdhLhQoextfedmYrenfcvdzaFQnYAE08XhONTWEK8+js +Fpo1oqAKoAA8CWjoJJTHM8kJ5jsiOiszAKD1+IV/hmW76rosbfnlh1Pp3Mah2srCnXQE9YXiel/c +p5r7uVj2CwxPTuFjjmdLbteNwmrLwsYe3TjsD8cmjKV43ycy+3o76D4llFuXmuCoZEPczXVOSsLv +f5lgGpNZLxJL2jnvMar0/wAOp6jHDH/uO4RViY9f/KpRdfC6k3R9fRyj+pRZVkWKqF10e+hCKaFq +XlH/ALlmhK7Met/uUGZ5ow8XL57lU8/Yt4lx4jUOJphLobTe/wDaHeZLxHXtJEya9o5lFzCqpmPY +CUYoPtDfc9TLj0G5jZvHaMFirAs++oEHq9U4rbNiMp8a6wO/1Zbzn2alC+Nx8P1JfdeBboA+AILx +rin8pfbA1ynvKuFUXZOXXkLbzOp2R56andL2G45MmO0RPWWLEe8GzaffoKb/ADI44Pt9ZXxAuuFa +axtgp0BOSPCcviNX8n3Aw8KTNHB4FiY9StkobLWHVSeghq8M4bkAhKKyV6Hl8RV8MwMZG1Uuz3Jn +IcUQJlMFGlJ6D4hfpymy7iChHKqvVtefxO7Ai1txLBIn7pcojN3jGVhQO0ZgCNfM5ZHycTLycSkr +yhtqD4Bmrfw5cuqsm6xHXyp1seRLcHCp4dQy1bOzslj1MzeJ5dVFnuMVdgOiHxOWzrmyMg2Nrbde +k3vR2OTddcd6A5R8GdZqOo67k4wXrLAQPMRKnzImMZEzm+P1nFz6cxQeVujagWR6jsYiqivlH/Ux +1M+7jWY30i7QHx1gF11tjGyxiSfmVc+503pPidVROHYNNY21b/adVZZySo3uOo1qIZQYd9RCzfYm +TUk/qW71LjGkTA+IYiZmM1T9N9j8Gee5+McXJem0/Wp8GUK6KOi7b5MgzFjsxpJHZGDKSCOxE3cD +OvsxbbLc9lsT7Vc73KX4ln3q1ZyVrPx2J/uAjLyan37z7B+Zp4vqPJqKi0K4EvzvUt1qBMdfb+T5 +gycfzkXXuc35InfE6nO8Y9SjFc1Yqh2Hdj2mH/xFxR26XgD/AMRJf45mWMqW5bBD3KqAZlZtb++7 +kEqTsHe//sG1CcTBvy7OWpD+Sewhz8CyKCTYAQPiGV0LVWPdxqQNADQ6zL4nWq2gopU6+ofmA8x3 +1MlvfeIGbnBeCHitRt94IFbRGus2U9H08v13sT+BNHjeX/D4bY4OmP0rPPbHLMWJ2Yy2EDQjVsos +BdeYDx8wo5L5KpSdLWPAE1+G8NrFtBKgOAXPTf6mzViql5ZBoE87eJZkKbOQ8m+Yjf5EBzcO621y +GCqD0H41Obzq7U6vzM577HTXgzPPeOIvM1eB59nD8xXVj7bHTr8iej1MtlauvUMNgzi/V2ctliYy +HYTq37nMExpZXRZYpZVJUdzNjg+FXYwZgdhv6nVVUJU/uH7iNf1CARrtF0IB113M7jTNVjFl2xJA +5ROey88OrVOugOy67TDs+89NRKdSYILdRC8ZQVJ+PHyJs4fqe3EoFPLzBexPxOdusa2xndiWY7JM +qMUNrzOTAfHC9XO9/E3vT9blVJB0o2Zu3MAoYrsL13Ii0Muw3XvJG9KkDOeqjf6gWcw5A33XN9nX +tOeyMRFWy3Jch+bX7mXmCsW/5RBXUoHaOIRi2asAJ0IRbjqzll3o/EAaRiltDojgv2E1aePmhEWq +rsNHZ7wir1K/8Y1vUCSCAd+IXiZ9b1gLYvN07trXTUD4rxN2TkUgEts8p2NDtD0t5MVGchr2Xe99 +hMPNvD1LX5J2TuZhGyYwBijjfiHU5bJXrnYfqBRtRtSbIBWG3+xI6HiLUWz8xA9RuaVNrMAPfB5x +r6v9MLr4S1il7LaxyjY69Jl5eG+Kyhiv1jYIMGYMO8etGscKoJJ8Cbp4bVg4ivaq22t3G/tmRYo5 +zyjQ+JRFFET01GB0Yid9YiYh1l9KgEHqT8Tco/hewA/NzgdQdwTNGNTY3uU2crL9HN00ZlovNzfV +oCanBrBRk1rpCHPUkQjjYoW4GtwAw30MDpuxvbAvpJceR5mXFFEY0W4o4mpg0XNXutQxPUHxLb8q +7mRDyszLr6esz8u++9wL2LcvQb8RXCkhBV3A6mR5rEVSrdFPT8SBLMdsdmWe6P8AUAx+TB4oooxi +i1Jmt0+5dfuOLbANB2H6MjzNzc2zv5ji1g2+5/MYnbb+Yh+T0kubUY940UUbUWtRpJN8w1CfebkK +WfUu+/mDOAGOjsRo0UkIo+pPl6Rckl7ehuR1INGAj9u0kW2nXvK45YlQp1odukaICSAjgSQWf//Z +""" + +# A config file that tries to override some standard vars: +config_override = """ +[global] +key0 = var0 +home = /home/sweet/home +key1 = var1 +site_packages = planet +key2 = var2 +key3 = var3 +""" + +# A config file that test the automatic type conversion +config_good = """ +[global] +yes = TRUE +no = False +number = 42 +""" + + class test_Env(ClassChecker): """ Test the `ipalib.config.Env` class. @@ -124,6 +186,113 @@ class test_Env(ClassChecker): Test the `ipalib.config.Env.__init__` method. """ o = self.cls() + 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 == os.environ['HOME'] + assert o.dot_ipa == path.join(os.environ['HOME'], '.ipa') + + def bootstrap(self, **overrides): + o = self.cls() + o._bootstrap(**overrides) + return o + + def test_bootstrap(self): + """ + Test the `ipalib.config.Env._bootstrap` method. + """ + dot_ipa = path.join(os.environ['HOME'], '.ipa') + + # Test defaults created by _bootstrap(): + o = self.cls() + assert 'in_tree' not in o + assert 'context' not in o + assert 'conf' not in o + o._bootstrap() + assert o.in_tree is False + assert o.context == 'default' + assert o.conf == '/etc/ipa/default.conf' + + # Test overriding values created by _bootstrap() + o = self.bootstrap(in_tree='true', context='server') + assert o.in_tree is True + assert o.context == 'server' + assert o.conf == path.join(dot_ipa, 'server.conf') + o = self.bootstrap(conf='/my/wacky/whatever.conf') + assert o.in_tree is False + assert o.context == 'default' + assert o.conf == '/my/wacky/whatever.conf' + + # 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() + ) + o = self.cls() + 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 + + def test_load_config(self): + """ + Test the `ipalib.config.Env._load_config` 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() + keys = tuple(o) + orig = dict((k, o[k]) for k in o) + assert o._load_config(no_exist) is None + assert tuple(o) == keys + + # Test an empty config file + empty = tmp.touch('empty.conf') + assert path.isfile(empty) + assert o._load_config(empty) is None + 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._load_config(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._load_config(override) == (4, 6) + for (k, v) in orig.items(): + assert o[k] is v + assert list(o) == sorted(keys + ('key0', 'key1', 'key2', 'key3')) + for i in xrange(4): + 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) + assert o._load_config(good) == (3, 3) + assert list(o) == sorted(keys + ('yes', 'no', 'number')) + assert o.yes is True + assert o.no is False + assert o.number == 42 def test_lock(self): """ @@ -186,6 +355,16 @@ class test_Env(ClassChecker): e = raises(AttributeError, setvar, o, name, value) assert str(e) == \ 'locked: cannot set Env.%s to %r' % (name, value) + o = self.cls() + setvar(o, 'yes', ' true ') + assert o.yes is True + setvar(o, 'no', ' false ') + assert o.no is False + setvar(o, 'msg', u' Hello, world! ') + assert o.msg == 'Hello, world!' + assert type(o.msg) is str + setvar(o, 'num', ' 42 ') + assert o.num == 42 def test_delattr(self): """ @@ -223,11 +402,11 @@ class test_Env(ClassChecker): Test the `ipalib.config.Env.__iter__` method. """ o = self.cls() - assert list(o) == [] + default_keys = tuple(o) keys = ('one', 'two', 'three', 'four', 'five') for key in keys: o[key] = 'the value' - assert list(o) == sorted(keys) + assert list(o) == sorted(keys + default_keys) def test_set_default_env(): -- cgit From 2a41db33c6d9f6efa826e16d91eba76defe899d2 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Fri, 24 Oct 2008 15:35:58 -0600 Subject: Env._bootstrap() now raises StandardError if called more than once --- tests/test_ipalib/test_config.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'tests/test_ipalib/test_config.py') diff --git a/tests/test_ipalib/test_config.py b/tests/test_ipalib/test_config.py index 39dc7906..67eedd14 100644 --- a/tests/test_ipalib/test_config.py +++ b/tests/test_ipalib/test_config.py @@ -197,6 +197,8 @@ class test_Env(ClassChecker): def bootstrap(self, **overrides): o = self.cls() o._bootstrap(**overrides) + e = raises(StandardError, o._bootstrap) + assert str(e) == 'Env._bootstrap() already called' return o def test_bootstrap(self): -- cgit From 8ca44bcbfa2aec0c7c84205dc08c81f711a22c5d Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Fri, 24 Oct 2008 16:02:26 -0600 Subject: Added tests.util.TempHome class for created a tempdir and setting os.environ['HOME'] to it; updated various unit tests for Env so they are run using a tempdir for home --- tests/test_ipalib/test_config.py | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) (limited to 'tests/test_ipalib/test_config.py') diff --git a/tests/test_ipalib/test_config.py b/tests/test_ipalib/test_config.py index 67eedd14..01ff2f01 100644 --- a/tests/test_ipalib/test_config.py +++ b/tests/test_ipalib/test_config.py @@ -27,7 +27,7 @@ from os import path import sys from tests.util import raises, setitem, delitem, ClassChecker from tests.util import getitem, setitem, delitem -from tests.util import TempDir +from tests.util import TempDir, TempHome from ipalib import config @@ -181,34 +181,42 @@ class test_Env(ClassChecker): _cls = config.Env + def new(self): + """ + Set os.environ['HOME'] to a tempdir. + + Returns tuple with new Env instance and the TempHome instance. + """ + home = TempHome() + return (self.cls(), home) + def test_init(self): """ Test the `ipalib.config.Env.__init__` method. """ - o = self.cls() + (o, home) = self.new() 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 == os.environ['HOME'] - assert o.dot_ipa == path.join(os.environ['HOME'], '.ipa') + assert o.home == home.path + assert o.dot_ipa == home.join('.ipa') def bootstrap(self, **overrides): - o = self.cls() + (o, home) = self.new() o._bootstrap(**overrides) e = raises(StandardError, o._bootstrap) assert str(e) == 'Env._bootstrap() already called' - return o + return (o, home) def test_bootstrap(self): """ Test the `ipalib.config.Env._bootstrap` method. """ - dot_ipa = path.join(os.environ['HOME'], '.ipa') # Test defaults created by _bootstrap(): - o = self.cls() + (o, home) = self.new() assert 'in_tree' not in o assert 'context' not in o assert 'conf' not in o @@ -218,11 +226,11 @@ class test_Env(ClassChecker): assert o.conf == '/etc/ipa/default.conf' # Test overriding values created by _bootstrap() - o = self.bootstrap(in_tree='true', context='server') + (o, home) = self.bootstrap(in_tree='true', context='server') assert o.in_tree is True assert o.context == 'server' - assert o.conf == path.join(dot_ipa, 'server.conf') - o = self.bootstrap(conf='/my/wacky/whatever.conf') + assert o.conf == home.join('.ipa', 'server.conf') + (o, home) = self.bootstrap(conf='/my/wacky/whatever.conf') assert o.in_tree is False assert o.context == 'default' assert o.conf == '/my/wacky/whatever.conf' @@ -237,7 +245,7 @@ class test_Env(ClassChecker): override = dict( (k, u' %s ' % v) for (k, v) in kw.items() ) - o = self.cls() + (o, home) = self.new() for key in kw: assert key not in o o._bootstrap(**override) -- cgit From ac4efac3944d180cffd0ad9d63f631dc928e1d28 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Fri, 24 Oct 2008 20:02:14 -0600 Subject: Finished Env._finalize_core() and corresponding unit tests --- tests/test_ipalib/test_config.py | 124 +++++++++++++++++++++++++++++++++++---- 1 file changed, 114 insertions(+), 10 deletions(-) (limited to 'tests/test_ipalib/test_config.py') diff --git a/tests/test_ipalib/test_config.py b/tests/test_ipalib/test_config.py index 01ff2f01..b4c86319 100644 --- a/tests/test_ipalib/test_config.py +++ b/tests/test_ipalib/test_config.py @@ -28,7 +28,7 @@ import sys from tests.util import raises, setitem, delitem, ClassChecker from tests.util import getitem, setitem, delitem from tests.util import TempDir, TempHome -from ipalib import config +from ipalib import config, constants def test_Environment(): @@ -157,6 +157,7 @@ WfUu+/mDOAGOjsRo0UkIo+pPl6Rckl7ehuR1INGAj9u0kW2nXvK45YlQp1odukaICSAjgSQWf//Z # A config file that tries to override some standard vars: config_override = """ [global] + key0 = var0 home = /home/sweet/home key1 = var1 @@ -165,9 +166,10 @@ key2 = var2 key3 = var3 """ -# A config file that test the automatic type conversion +# A config file that tests the automatic type conversion config_good = """ [global] + yes = TRUE no = False number = 42 @@ -205,7 +207,9 @@ class test_Env(ClassChecker): def bootstrap(self, **overrides): (o, home) = self.new() + assert o._isdone('_bootstrap') is False o._bootstrap(**overrides) + assert o._isdone('_bootstrap') is True e = raises(StandardError, o._bootstrap) assert str(e) == 'Env._bootstrap() already called' return (o, home) @@ -214,7 +218,6 @@ class test_Env(ClassChecker): """ Test the `ipalib.config.Env._bootstrap` method. """ - # Test defaults created by _bootstrap(): (o, home) = self.new() assert 'in_tree' not in o @@ -253,9 +256,110 @@ class test_Env(ClassChecker): assert getattr(o, key) == value assert o[key] == value - def test_load_config(self): + def finalize_core(self, **defaults): + (o, home) = self.new() + assert o._isdone('_finalize_core') is False + o._finalize_core(**defaults) + assert o._isdone('_finalize_core') is True + e = raises(StandardError, o._finalize_core) + assert str(e) == 'Env._finalize_core() already called' + return (o, home) + + def test_finalize_core(self): + """ + Test the `ipalib.config.Env._finalize_core` method. + """ + # Check that calls cascade up the chain: + (o, home) = self.new() + assert o._isdone('_bootstrap') is False + assert o._isdone('_finalize_core') is False + assert o._isdone('_finalize') is False + o._finalize_core() + assert o._isdone('_bootstrap') is True + assert o._isdone('_finalize_core') is True + assert o._isdone('_finalize') is False + + # Check that it can't be called twice: + e = raises(StandardError, o._finalize_core) + assert str(e) == 'Env._finalize_core() already called' + + # Check that _bootstrap() did its job: + (o, home) = self.bootstrap() + assert 'in_tree' in o + assert 'conf' in o + assert 'context' in o + + # Check that keys _finalize_core() will set are not set yet: + assert 'log' not in o + assert 'in_server' not in o + + # Check that _finalize_core() did its job: + o._finalize_core() + assert 'in_server' in o + assert 'log' in o + assert o.in_tree is False + assert o.context == 'default' + assert o.in_server is False + assert o.log == '/var/log/ipa/default.log' + + # Check log is in ~/.ipa/log when context='cli' + (o, home) = self.bootstrap(context='cli') + o._finalize_core() + assert o.in_tree is False + assert o.log == home.join('.ipa', 'log', 'cli.log') + + # Check **defaults can't set in_server nor log: + (o, home) = self.bootstrap(in_server='tRUE') + o._finalize_core(in_server=False) + assert o.in_server is True + (o, home) = self.bootstrap(log='/some/silly/log') + o._finalize_core(log='/a/different/log') + assert o.log == '/some/silly/log' + + # 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') + o._finalize_core() + assert o.in_tree is True + assert o.context == 'server' + assert o.in_server is True + assert o.log == home.join('.ipa', 'log', 'server.log') + assert o.yes is True + assert o.no is False + assert o.number == 42 + + # Test using DEFAULT_CONFIG: + defaults = dict(constants.DEFAULT_CONFIG) + (o, home) = self.finalize_core(**defaults) + assert list(o) == sorted(defaults) + for (key, value) in defaults.items(): + if value is None: + continue + assert o[key] is value + + def test_finalize(self): + """ + Test the `ipalib.config.Env._finalize` method. + """ + # Check that calls cascade up the chain: + (o, home) = self.new() + 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: + e = raises(StandardError, o._finalize) + assert str(e) == 'Env._finalize() already called' + + def test_merge_config(self): """ - Test the `ipalib.config.Env._load_config` method. + Test the `ipalib.config.Env._merge_config` method. """ tmp = TempDir() assert callable(tmp.join) @@ -266,27 +370,27 @@ class test_Env(ClassChecker): o = self.cls() keys = tuple(o) orig = dict((k, o[k]) for k in o) - assert o._load_config(no_exist) is None + assert o._merge_config(no_exist) is None assert tuple(o) == keys # Test an empty config file empty = tmp.touch('empty.conf') assert path.isfile(empty) - assert o._load_config(empty) is None + assert o._merge_config(empty) is None 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._load_config(bad) is None + assert o._merge_config(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._load_config(override) == (4, 6) + assert o._merge_config(override) == (4, 6) for (k, v) in orig.items(): assert o[k] is v assert list(o) == sorted(keys + ('key0', 'key1', 'key2', 'key3')) @@ -298,7 +402,7 @@ class test_Env(ClassChecker): good = tmp.join('good.conf') open(good, 'w').write(config_good) assert path.isfile(good) - assert o._load_config(good) == (3, 3) + assert o._merge_config(good) == (3, 3) assert list(o) == sorted(keys + ('yes', 'no', 'number')) assert o.yes is True assert o.no is False -- cgit From 759734864e72c209d62c970d2e325e96ae02fcb7 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Fri, 24 Oct 2008 20:21:27 -0600 Subject: Finished Env._finalize() and corresponding unit tests --- tests/test_ipalib/test_config.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'tests/test_ipalib/test_config.py') diff --git a/tests/test_ipalib/test_config.py b/tests/test_ipalib/test_config.py index b4c86319..6136bdf0 100644 --- a/tests/test_ipalib/test_config.py +++ b/tests/test_ipalib/test_config.py @@ -170,8 +170,8 @@ key3 = var3 config_good = """ [global] -yes = TRUE -no = False +yes = tRUE +no = fALse number = 42 """ @@ -357,6 +357,25 @@ class test_Env(ClassChecker): e = raises(StandardError, o._finalize) assert str(e) == 'Env._finalize() already called' + # Check that _finalize() calls __lock__() + (o, home) = self.new() + assert o.__islocked__() is False + o._finalize() + assert o.__islocked__() is True + e = raises(StandardError, o.__lock__) + assert str(e) == 'Env.__lock__() already called' + + # Check that **lastchance works + (o, home) = self.finalize_core() + key = 'just_one_more_key' + value = 'with one more value' + 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 + def test_merge_config(self): """ Test the `ipalib.config.Env._merge_config` method. -- cgit From 28dd8e74bdefd62307881f6e086af59db97a21a0 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Mon, 27 Oct 2008 00:58:25 -0600 Subject: Env._bootstrap() now also sets Env.conf_default --- tests/test_ipalib/test_config.py | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'tests/test_ipalib/test_config.py') diff --git a/tests/test_ipalib/test_config.py b/tests/test_ipalib/test_config.py index 6136bdf0..0608bf8e 100644 --- a/tests/test_ipalib/test_config.py +++ b/tests/test_ipalib/test_config.py @@ -227,6 +227,7 @@ class test_Env(ClassChecker): assert o.in_tree is False assert o.context == 'default' assert o.conf == '/etc/ipa/default.conf' + assert o.conf_default == o.conf # Test overriding values created by _bootstrap() (o, home) = self.bootstrap(in_tree='true', context='server') @@ -237,6 +238,12 @@ class test_Env(ClassChecker): assert o.in_tree is False assert o.context == 'default' assert o.conf == '/my/wacky/whatever.conf' + assert o.conf_default == '/etc/ipa/default.conf' + (o, home) = self.bootstrap(conf_default='/my/wacky/default.conf') + assert o.in_tree is False + assert o.context == 'default' + assert o.conf == '/etc/ipa/default.conf' + assert o.conf_default == '/my/wacky/default.conf' # Test various overrides and types conversion kw = dict( -- cgit From 25a7df9615058372b81a41df6baa2c4692df0063 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Mon, 27 Oct 2008 01:09:53 -0600 Subject: Env._finalize_core() now also loads config from Env.conf_default --- tests/test_ipalib/test_config.py | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'tests/test_ipalib/test_config.py') diff --git a/tests/test_ipalib/test_config.py b/tests/test_ipalib/test_config.py index 0608bf8e..ddfbb708 100644 --- a/tests/test_ipalib/test_config.py +++ b/tests/test_ipalib/test_config.py @@ -175,6 +175,14 @@ no = fALse number = 42 """ +# A default config file to make sure it does not overwrite the explicit one +config_default = """ +[global] + +yes = Hello +not_in_other = foo_bar +""" + class test_Env(ClassChecker): """ @@ -328,6 +336,7 @@ class test_Env(ClassChecker): for key in ('yes', 'no', 'number'): assert key not in o home.write(config_good, '.ipa', 'server.conf') + home.write(config_default, '.ipa', 'default.conf') o._finalize_core() assert o.in_tree is True assert o.context == 'server' @@ -336,6 +345,7 @@ class test_Env(ClassChecker): assert o.yes is True assert o.no is False assert o.number == 42 + assert o.not_in_other == 'foo_bar' # Test using DEFAULT_CONFIG: defaults = dict(constants.DEFAULT_CONFIG) -- cgit From 5b637f6a18a647a0ff084b2932faa1a4a887a5c2 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Mon, 22 Dec 2008 15:41:24 -0700 Subject: Removed depreciated code from config.py; removed corresponding unit tests --- tests/test_ipalib/test_config.py | 162 +-------------------------------------- 1 file changed, 1 insertion(+), 161 deletions(-) (limited to 'tests/test_ipalib/test_config.py') diff --git a/tests/test_ipalib/test_config.py b/tests/test_ipalib/test_config.py index ddfbb708..e17c4799 100644 --- a/tests/test_ipalib/test_config.py +++ b/tests/test_ipalib/test_config.py @@ -31,88 +31,7 @@ from tests.util import TempDir, TempHome from ipalib import config, constants -def test_Environment(): - """ - Test the `ipalib.config.Environment` class. - """ - # This has to be the same as iter_cnt - control_cnt = 0 - class prop_class: - def __init__(self, val): - self._val = val - def get_value(self): - return self._val - - class iter_class(prop_class): - # Increment this for each time iter_class yields - iter_cnt = 0 - def get_value(self): - for item in self._val: - self.__class__.iter_cnt += 1 - yield item - - # Tests for basic functionality - basic_tests = ( - ('a', 1), - ('b', 'basic_foo'), - ('c', ('basic_bar', 'basic_baz')), - ) - # Tests with prop classes - prop_tests = ( - ('d', prop_class(2), 2), - ('e', prop_class('prop_foo'), 'prop_foo'), - ('f', prop_class(('prop_bar', 'prop_baz')), ('prop_bar', 'prop_baz')), - ) - # Tests with iter classes - iter_tests = ( - ('g', iter_class((3, 4, 5)), (3, 4, 5)), - ('h', iter_class(('iter_foo', 'iter_bar', 'iter_baz')), - ('iter_foo', 'iter_bar', 'iter_baz') - ), - ) - - # Set all the values - env = config.Environment() - for name, val in basic_tests: - env[name] = val - for name, val, dummy in prop_tests: - env[name] = val - for name, val, dummy in iter_tests: - env[name] = val - - # Test if the values are correct - for name, val in basic_tests: - assert env[name] == val - for name, dummy, val in prop_tests: - assert env[name] == val - # Test if the get_value() function is called only when needed - for name, dummy, correct_values in iter_tests: - values_in_env = [] - for val in env[name]: - control_cnt += 1 - assert iter_class.iter_cnt == control_cnt - values_in_env.append(val) - assert tuple(values_in_env) == correct_values - - # Test __setattr__() - env.spam = 'ham' - - assert env.spam == 'ham' - assert env['spam'] == 'ham' - assert env.get('spam') == 'ham' - assert env.get('nonexistent') == None - assert env.get('nonexistent', 42) == 42 - - # Test if we throw AttributeError exception when trying to overwrite - # existing value, or delete it - raises(AttributeError, setitem, env, 'a', 1) - raises(AttributeError, setattr, env, 'a', 1) - raises(AttributeError, delitem, env, 'a') - raises(AttributeError, delattr, env, 'a') - raises(AttributeError, config.Environment.update, env, dict(a=1000)) - # This should be silently ignored - env.update(dict(a=1000), True) - assert env.a != 1000 + # Random base64-encoded data to simulate a misbehaving config file. @@ -557,82 +476,3 @@ class test_Env(ClassChecker): for key in keys: o[key] = 'the value' assert list(o) == sorted(keys + default_keys) - - -def test_set_default_env(): - """ - Test the `ipalib.config.set_default_env` function. - """ - - # Make sure we don't overwrite any properties - d = dict( - query_dns = False, - server = ('first', 'second'), - realm = 'myrealm', - # test right conversions - server_context = 'off', - ) - env = config.Environment() - config.set_default_env(env) - env.update(d) - assert env['server_context'] == False - assert env['query_dns'] == False - - # Make sure the servers is overwrote properly (that it is still LazyProp) - iter = env['server'] - assert iter.next() == 'first' - assert iter.next() == 'second' - - -def test_LazyProp(): - """ - Test the `ipalib.config.LazyProp` class. - """ - - def dummy(): - return 1 - - # Basic sanity testing with no initial value - prop = config.LazyProp(int, dummy) - assert prop.get_value() == 1 - prop.set_value(2) - assert prop.get_value() == 2 - - # Basic sanity testing with initial value - prop = config.LazyProp(int, dummy, 3) - assert prop.get_value() == 3 - prop.set_value(4) - assert prop.get_value() == 4 - - -def test_LazyIter(): - """ - Test the `ipalib.config.LazyIter` class. - """ - - def dummy(): - yield 1 - yield 2 - - # Basic sanity testing with no initial value - prop = config.LazyIter(int, dummy) - iter = prop.get_value() - assert iter.next() == 1 - assert iter.next() == 2 - raises(StopIteration, iter.next) - - # Basic sanity testing with initial value - prop = config.LazyIter(int, dummy, 0) - iter = prop.get_value() - assert iter.next() == 0 - assert iter.next() == 1 - assert iter.next() == 2 - raises(StopIteration, iter.next) - - -def test_read_config(): - """ - Test the `ipalib.config.read_config` class. - """ - - raises(AssertionError, config.read_config, 1) -- cgit From c070d390e92df0c9cc6b6070e6c94bd3a130ff65 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Mon, 22 Dec 2008 15:51:54 -0700 Subject: Removed Env.__getattr__(); Env no longer accepts callables for values (no more dynamic/lazy values) --- tests/test_ipalib/test_config.py | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) (limited to 'tests/test_ipalib/test_config.py') diff --git a/tests/test_ipalib/test_config.py b/tests/test_ipalib/test_config.py index e17c4799..6c70aeb9 100644 --- a/tests/test_ipalib/test_config.py +++ b/tests/test_ipalib/test_config.py @@ -374,23 +374,16 @@ class test_Env(ClassChecker): e = raises(StandardError, o.__lock__) assert str(e) == 'Env.__lock__() already called' - def test_getattr(self): + def test_getitem(self): """ - Test the `ipalib.config.Env.__getattr__` method. - - Also tests the `ipalib.config.Env.__getitem__` method. + Test the `ipalib.config.Env.__getitem__` method. """ o = self.cls() value = 'some value' o.key = value assert o.key is value assert o['key'] is value - o.call = lambda: 'whatever' - assert o.call == 'whatever' - assert o['call'] == 'whatever' for name in ('one', 'two'): - e = raises(AttributeError, getattr, o, name) - assert str(e) == 'Env.%s' % name e = raises(KeyError, getitem, o, name) assert str(e) == repr(name) @@ -402,9 +395,9 @@ class test_Env(ClassChecker): """ items = [ ('one', 1), - ('two', lambda: 2), + ('two', 2), ('three', 3), - ('four', lambda: 4), + ('four', 4), ] for setvar in (setattr, setitem): o = self.cls() @@ -457,9 +450,9 @@ class test_Env(ClassChecker): o = self.cls() items = [ ('one', 1), - ('two', lambda: 2), + ('two', 2), ('three', 3), - ('four', lambda: 4), + ('four', 4), ] for (key, value) in items: assert key not in o -- cgit From 014cca57ad31f0ff9230923c8b7fdb1b59157dae Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Mon, 22 Dec 2008 16:16:57 -0700 Subject: The Env.__setitem__() implied conversion is now case sensitive; Env.__setitem__() now also accepts None as a value --- tests/test_ipalib/test_config.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'tests/test_ipalib/test_config.py') diff --git a/tests/test_ipalib/test_config.py b/tests/test_ipalib/test_config.py index 6c70aeb9..f92445f8 100644 --- a/tests/test_ipalib/test_config.py +++ b/tests/test_ipalib/test_config.py @@ -89,8 +89,8 @@ key3 = var3 config_good = """ [global] -yes = tRUE -no = fALse +yes = True +no = False number = 42 """ @@ -157,7 +157,7 @@ class test_Env(ClassChecker): assert o.conf_default == o.conf # Test overriding values created by _bootstrap() - (o, home) = self.bootstrap(in_tree='true', context='server') + (o, home) = self.bootstrap(in_tree='True', context='server') assert o.in_tree is True assert o.context == 'server' assert o.conf == home.join('.ipa', 'server.conf') @@ -243,7 +243,7 @@ class test_Env(ClassChecker): assert o.log == home.join('.ipa', 'log', 'cli.log') # Check **defaults can't set in_server nor log: - (o, home) = self.bootstrap(in_server='tRUE') + (o, home) = self.bootstrap(in_server='True') o._finalize_core(in_server=False) assert o.in_server is True (o, home) = self.bootstrap(log='/some/silly/log') @@ -271,9 +271,9 @@ class test_Env(ClassChecker): (o, home) = self.finalize_core(**defaults) assert list(o) == sorted(defaults) for (key, value) in defaults.items(): - if value is None: + if value is object: continue - assert o[key] is value + assert o[key] is value, value def test_finalize(self): """ @@ -418,9 +418,9 @@ class test_Env(ClassChecker): assert str(e) == \ 'locked: cannot set Env.%s to %r' % (name, value) o = self.cls() - setvar(o, 'yes', ' true ') + setvar(o, 'yes', ' True ') assert o.yes is True - setvar(o, 'no', ' false ') + setvar(o, 'no', ' False ') assert o.no is False setvar(o, 'msg', u' Hello, world! ') assert o.msg == 'Hello, world!' -- cgit From 6b055b435f93bf9b63ee9b3b2fdd6f082dacc07b Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Mon, 22 Dec 2008 17:29:11 -0700 Subject: Cleaned up Env.__setattr__() and Env.__setitem__() a bit updated their unit tests --- tests/test_ipalib/test_config.py | 110 +++++++++++++++++++++++++-------------- 1 file changed, 71 insertions(+), 39 deletions(-) (limited to 'tests/test_ipalib/test_config.py') diff --git a/tests/test_ipalib/test_config.py b/tests/test_ipalib/test_config.py index f92445f8..c1a5faaa 100644 --- a/tests/test_ipalib/test_config.py +++ b/tests/test_ipalib/test_config.py @@ -28,10 +28,33 @@ import sys from tests.util import raises, setitem, delitem, ClassChecker from tests.util import getitem, setitem, delitem from tests.util import TempDir, TempHome +from ipalib.constants import TYPE_ERROR, OVERRIDE_ERROR, LOCK_ERROR from ipalib import config, constants +# 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 = ( + ('a_string', 'Hello world!', 'Hello world!'), + ('trailing_whitespace', ' value ', 'value'), + ('an_int', 42, 42), + ('int_repr', ' 42 ', 42), + ('true', True, True), + ('true_repr', ' True ', True), + ('false', False, False), + ('false_repr', ' False ', False), + ('none', None, None), + ('none_repr', ' None ', None), + + # These verify that the implied conversion is case-sensitive: + ('not_true', ' true ', 'true'), + ('not_false', ' false ', 'false'), + ('not_none', ' none ', 'none'), +) + # Random base64-encoded data to simulate a misbehaving config file. @@ -132,6 +155,54 @@ class test_Env(ClassChecker): assert o.home == home.path assert o.dot_ipa == home.join('.ipa') + 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) + assert str(e) == LOCK_ERROR % ('Env', name, raw) + + 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) + assert str(e) == LOCK_ERROR % ('Env', key, raw) + def bootstrap(self, **overrides): (o, home) = self.new() assert o._isdone('_bootstrap') is False @@ -387,46 +458,7 @@ class test_Env(ClassChecker): e = raises(KeyError, getitem, o, name) assert str(e) == repr(name) - def test_setattr(self): - """ - Test the `ipalib.config.Env.__setattr__` method. - Also tests the `ipalib.config.Env.__setitem__` method. - """ - items = [ - ('one', 1), - ('two', 2), - ('three', 3), - ('four', 4), - ] - for setvar in (setattr, setitem): - o = self.cls() - for (i, (name, value)) in enumerate(items): - setvar(o, name, value) - assert getattr(o, name) == i + 1 - assert o[name] == i + 1 - if callable(value): - assert name not in dir(o) - else: - assert name in dir(o) - e = raises(AttributeError, setvar, o, name, 42) - assert str(e) == 'cannot overwrite Env.%s with 42' % name - o = self.cls() - o.__lock__() - for (name, value) in items: - e = raises(AttributeError, setvar, o, name, value) - assert str(e) == \ - 'locked: cannot set Env.%s to %r' % (name, value) - o = self.cls() - setvar(o, 'yes', ' True ') - assert o.yes is True - setvar(o, 'no', ' False ') - assert o.no is False - setvar(o, 'msg', u' Hello, world! ') - assert o.msg == 'Hello, world!' - assert type(o.msg) is str - setvar(o, 'num', ' 42 ') - assert o.num == 42 def test_delattr(self): """ -- cgit From 01cae56e0a19876cf6a614469c0c5e6fb73170e6 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Mon, 22 Dec 2008 21:02:43 -0700 Subject: Some more reorganization in Env and added class docstring to Env with lots of examples --- tests/test_ipalib/test_config.py | 64 +++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 33 deletions(-) (limited to 'tests/test_ipalib/test_config.py') diff --git a/tests/test_ipalib/test_config.py b/tests/test_ipalib/test_config.py index c1a5faaa..740ef6cf 100644 --- a/tests/test_ipalib/test_config.py +++ b/tests/test_ipalib/test_config.py @@ -28,7 +28,7 @@ import sys from tests.util import raises, setitem, delitem, ClassChecker from tests.util import getitem, setitem, delitem from tests.util import TempDir, TempHome -from ipalib.constants import TYPE_ERROR, OVERRIDE_ERROR, LOCK_ERROR +from ipalib.constants import TYPE_ERROR, OVERRIDE_ERROR, SET_ERROR, DEL_ERROR from ipalib import config, constants @@ -177,7 +177,7 @@ class test_Env(ClassChecker): o.__lock__() for (name, raw, value) in good_vars: e = raises(AttributeError, setattr, o, name, raw) - assert str(e) == LOCK_ERROR % ('Env', name, raw) + assert str(e) == SET_ERROR % ('Env', name, raw) def test_setitem(self): """ @@ -201,7 +201,35 @@ class test_Env(ClassChecker): o.__lock__() for (key, raw, value) in good_vars: e = raises(AttributeError, o.__setitem__, key, raw) - assert str(e) == LOCK_ERROR % ('Env', key, raw) + assert str(e) == SET_ERROR % ('Env', key, raw) + + def test_getitem(self): + """ + Test the `ipalib.config.Env.__getitem__` method. + """ + o = self.cls() + value = 'some value' + 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__' def bootstrap(self, **overrides): (o, home) = self.new() @@ -445,36 +473,6 @@ class test_Env(ClassChecker): e = raises(StandardError, o.__lock__) assert str(e) == 'Env.__lock__() already called' - def test_getitem(self): - """ - Test the `ipalib.config.Env.__getitem__` method. - """ - o = self.cls() - value = 'some value' - 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) == 'cannot del Env.%s' % key - e = raises(AttributeError, delitem, o, key) - assert str(e) == '__delitem__' - def test_contains(self): """ Test the `ipalib.config.Env.__contains__` method. -- cgit From fd43b39145382b96cd2e0d0da3d5dcbe0d3a4a2a Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Mon, 22 Dec 2008 23:09:35 -0700 Subject: Moved setting of run-time variables from Env.__init__() to Env._bootstrap() --- tests/test_ipalib/test_config.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'tests/test_ipalib/test_config.py') diff --git a/tests/test_ipalib/test_config.py b/tests/test_ipalib/test_config.py index 740ef6cf..8884697e 100644 --- a/tests/test_ipalib/test_config.py +++ b/tests/test_ipalib/test_config.py @@ -147,13 +147,9 @@ class test_Env(ClassChecker): Test the `ipalib.config.Env.__init__` method. """ (o, home) = self.new() - 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') + assert list(o) == [] + assert len(o) == 0 + assert o.__islocked__() is False def test_setattr(self): """ @@ -246,10 +242,14 @@ class test_Env(ClassChecker): """ # Test defaults created by _bootstrap(): (o, home) = self.new() - assert 'in_tree' not in o - assert 'context' not in o - assert 'conf' not in o o._bootstrap() + 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') assert o.in_tree is False assert o.context == 'default' assert o.conf == '/etc/ipa/default.conf' @@ -422,6 +422,7 @@ class test_Env(ClassChecker): 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_config(no_exist) is None -- cgit From 16526142f36e81f4d8a767f339c559188485f756 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Tue, 23 Dec 2008 01:11:03 -0700 Subject: Finished Env class docstring; more organizational cleanup in Env and its unit tests --- tests/test_ipalib/test_config.py | 122 +++++++++++++++++++++++---------------- 1 file changed, 72 insertions(+), 50 deletions(-) (limited to 'tests/test_ipalib/test_config.py') diff --git a/tests/test_ipalib/test_config.py b/tests/test_ipalib/test_config.py index 8884697e..acdcfaae 100644 --- a/tests/test_ipalib/test_config.py +++ b/tests/test_ipalib/test_config.py @@ -32,7 +32,6 @@ from ipalib.constants import TYPE_ERROR, OVERRIDE_ERROR, SET_ERROR, DEL_ERROR from ipalib import config, constants - # Valid environment variables in (key, raw, value) tuples: # key: the name of the environment variable # raw: the value being set (possibly a string repr) @@ -48,6 +47,7 @@ good_vars = ( ('false_repr', ' False ', False), ('none', None, None), ('none_repr', ' None ', None), + ('empty', '', None), # These verify that the implied conversion is case-sensitive: ('not_true', ' true ', 'true'), @@ -56,7 +56,6 @@ good_vars = ( ) - # Random base64-encoded data to simulate a misbehaving config file. config_bad = """ /9j/4AAQSkZJRgABAQEAlgCWAAD//gAIT2xpdmVy/9sAQwAQCwwODAoQDg0OEhEQExgoGhgWFhgx @@ -96,6 +95,7 @@ i1Jmt0+5dfuOLbANB2H6MjzNzc2zv5ji1g2+5/MYnbb+Yh+T0kubUY940UUbUWtRpJN8w1CfebkK WfUu+/mDOAGOjsRo0UkIo+pPl6Rckl7ehuR1INGAj9u0kW2nXvK45YlQp1odukaICSAjgSQWf//Z """ + # A config file that tries to override some standard vars: config_override = """ [global] @@ -108,6 +108,7 @@ key2 = var2 key3 = var3 """ + # A config file that tests the automatic type conversion config_good = """ [global] @@ -117,6 +118,7 @@ no = False number = 42 """ + # A default config file to make sure it does not overwrite the explicit one config_default = """ [global] @@ -133,24 +135,26 @@ class test_Env(ClassChecker): _cls = config.Env - def new(self): - """ - Set os.environ['HOME'] to a tempdir. - - Returns tuple with new Env instance and the TempHome instance. - """ - home = TempHome() - return (self.cls(), home) - def test_init(self): """ Test the `ipalib.config.Env.__init__` method. """ - (o, home) = self.new() + o = self.cls() assert list(o) == [] assert len(o) == 0 assert o.__islocked__() is False + def test_lock(self): + """ + Test the `ipalib.config.Env.__lock__` method. + """ + o = self.cls() + assert o._Env__locked is False + o.__lock__() + assert o._Env__locked is True + e = raises(StandardError, o.__lock__) + assert str(e) == 'Env.__lock__() already called' + def test_setattr(self): """ Test the `ipalib.config.Env.__setattr__` method. @@ -227,7 +231,60 @@ class test_Env(ClassChecker): e = raises(AttributeError, delitem, o, key) assert str(e) == '__delitem__' + 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 + for i in xrange(1, 11): + key = 'key%d' % i + value = 'value %d' % i + 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) + + def new(self): + """ + 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() + return (self.cls(), home) + def bootstrap(self, **overrides): + """ + Helper method used in testing bootstrap related methods below. + """ (o, home) = self.new() assert o._isdone('_bootstrap') is False o._bootstrap(**overrides) @@ -290,6 +347,9 @@ class test_Env(ClassChecker): assert o[key] == value def finalize_core(self, **defaults): + """ + Helper method used in testing `Env._finalize_core`. + """ (o, home) = self.new() assert o._isdone('_finalize_core') is False o._finalize_core(**defaults) @@ -462,41 +522,3 @@ class test_Env(ClassChecker): assert o.yes is True assert o.no is False assert o.number == 42 - - def test_lock(self): - """ - Test the `ipalib.config.Env.__lock__` method. - """ - o = self.cls() - assert o._Env__locked is False - o.__lock__() - assert o._Env__locked is True - e = raises(StandardError, o.__lock__) - assert str(e) == 'Env.__lock__() already called' - - 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_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) -- cgit From e14fc84dfccbb06f775bbd5d3de864c7b879453f Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Mon, 29 Dec 2008 21:23:34 -0700 Subject: Renamed Env._merge_config() to Env._merge_from_file() --- tests/test_ipalib/test_config.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'tests/test_ipalib/test_config.py') diff --git a/tests/test_ipalib/test_config.py b/tests/test_ipalib/test_config.py index acdcfaae..388994f4 100644 --- a/tests/test_ipalib/test_config.py +++ b/tests/test_ipalib/test_config.py @@ -471,9 +471,9 @@ class test_Env(ClassChecker): assert key in o assert o[key] is value - def test_merge_config(self): + def test_merge_from_file(self): """ - Test the `ipalib.config.Env._merge_config` method. + Test the `ipalib.config.Env._merge_from_file` method. """ tmp = TempDir() assert callable(tmp.join) @@ -485,27 +485,27 @@ class test_Env(ClassChecker): o._bootstrap() keys = tuple(o) orig = dict((k, o[k]) for k in o) - assert o._merge_config(no_exist) is None + 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_config(empty) is None + assert o._merge_from_file(empty) is None 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_config(bad) is None + 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_config(override) == (4, 6) + assert o._merge_from_file(override) == (4, 6) for (k, v) in orig.items(): assert o[k] is v assert list(o) == sorted(keys + ('key0', 'key1', 'key2', 'key3')) @@ -517,7 +517,7 @@ class test_Env(ClassChecker): good = tmp.join('good.conf') open(good, 'w').write(config_good) assert path.isfile(good) - assert o._merge_config(good) == (3, 3) + assert o._merge_from_file(good) == (3, 3) assert list(o) == sorted(keys + ('yes', 'no', 'number')) assert o.yes is True assert o.no is False -- cgit From 447c88a2bb9dd364f9c67a73bfce5000ac81d375 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Tue, 30 Dec 2008 00:45:48 -0700 Subject: Started moving some core classes and functions from plugable.py to new base.py module --- tests/test_ipalib/test_config.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'tests/test_ipalib/test_config.py') diff --git a/tests/test_ipalib/test_config.py b/tests/test_ipalib/test_config.py index 388994f4..7c53efe2 100644 --- a/tests/test_ipalib/test_config.py +++ b/tests/test_ipalib/test_config.py @@ -21,7 +21,6 @@ Test the `ipalib.config` module. """ -import types import os from os import path import sys @@ -29,6 +28,7 @@ from tests.util import raises, setitem, delitem, ClassChecker from tests.util import getitem, setitem, delitem from tests.util import TempDir, TempHome from ipalib.constants import TYPE_ERROR, OVERRIDE_ERROR, SET_ERROR, DEL_ERROR +from ipalib.constants import NAME_REGEX, NAME_ERROR from ipalib import config, constants @@ -56,6 +56,13 @@ good_vars = ( ) +bad_names = ( + ('CamelCase', 'value'), + ('_leading_underscore', 'value'), + ('trailing_underscore_', 'value'), +) + + # Random base64-encoded data to simulate a misbehaving config file. config_bad = """ /9j/4AAQSkZJRgABAQEAlgCWAAD//gAIT2xpdmVy/9sAQwAQCwwODAoQDg0OEhEQExgoGhgWFhgx @@ -179,6 +186,12 @@ class test_Env(ClassChecker): e = raises(AttributeError, setattr, o, name, raw) assert str(e) == SET_ERROR % ('Env', name, raw) + # 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) + def test_setitem(self): """ Test the `ipalib.config.Env.__setitem__` method. @@ -203,6 +216,12 @@ class test_Env(ClassChecker): e = raises(AttributeError, o.__setitem__, key, raw) assert str(e) == SET_ERROR % ('Env', key, raw) + # 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) + def test_getitem(self): """ Test the `ipalib.config.Env.__getitem__` method. -- cgit From 11e165073eb3bd21d764fa1c4d71f1e6a52eae1b Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Tue, 30 Dec 2008 03:11:45 -0700 Subject: Docstring cleanup in the Env bootstraping methods --- tests/test_ipalib/test_config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/test_ipalib/test_config.py') diff --git a/tests/test_ipalib/test_config.py b/tests/test_ipalib/test_config.py index 7c53efe2..6737b086 100644 --- a/tests/test_ipalib/test_config.py +++ b/tests/test_ipalib/test_config.py @@ -510,7 +510,7 @@ class test_Env(ClassChecker): # Test an empty config file empty = tmp.touch('empty.conf') assert path.isfile(empty) - assert o._merge_from_file(empty) is None + assert o._merge_from_file(empty) == (0, 0) assert tuple(o) == keys # Test a mal-formed config file: -- cgit From bc2395724708abc80c7c13c5474cefcfb6f3579c Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Tue, 30 Dec 2008 14:38:05 -0700 Subject: Added unit test for Env._merge() --- tests/test_ipalib/test_config.py | 140 ++++++++++++++++++++++++--------------- 1 file changed, 88 insertions(+), 52 deletions(-) (limited to 'tests/test_ipalib/test_config.py') diff --git a/tests/test_ipalib/test_config.py b/tests/test_ipalib/test_config.py index 6737b086..5daad037 100644 --- a/tests/test_ipalib/test_config.py +++ b/tests/test_ipalib/test_config.py @@ -290,6 +290,94 @@ class test_Env(ClassChecker): o[key] = 'the value' assert list(o) == sorted(keys + default_keys) + def test_merge(self): + """ + Test the `ipalib.config.Env._merge` method. + """ + group1 = ( + ('key1', 'value 1'), + ('key2', 'value 2'), + ('key3', 'value 3'), + ('key4', 'value 4'), + ) + group2 = ( + ('key0', 'Value 0'), + ('key2', 'Value 2'), + ('key4', 'Value 4'), + ('key5', 'Value 5'), + ) + 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' + for (key, value) in expected.iteritems(): + 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 + assert list(o) == sorted(keys + ('key0', 'key1', 'key2', 'key3')) + for i in xrange(4): + 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) + assert o._merge_from_file(good) == (3, 3) + assert list(o) == sorted(keys + ('yes', 'no', 'number')) + assert o.yes is True + assert o.no is False + assert o.number == 42 + def new(self): """ Set os.environ['HOME'] to a tempdir. @@ -489,55 +577,3 @@ class test_Env(ClassChecker): o._finalize(**lastchance) assert key in o assert o[key] is value - - 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 - assert list(o) == sorted(keys + ('key0', 'key1', 'key2', 'key3')) - for i in xrange(4): - 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) - assert o._merge_from_file(good) == (3, 3) - assert list(o) == sorted(keys + ('yes', 'no', 'number')) - assert o.yes is True - assert o.no is False - assert o.number == 42 -- cgit From 379c549fc16fbb2eed6685f5e189da26f021abe9 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Tue, 30 Dec 2008 15:02:15 -0700 Subject: Env now supports float values --- tests/test_ipalib/test_config.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'tests/test_ipalib/test_config.py') diff --git a/tests/test_ipalib/test_config.py b/tests/test_ipalib/test_config.py index 5daad037..ab8d9006 100644 --- a/tests/test_ipalib/test_config.py +++ b/tests/test_ipalib/test_config.py @@ -1,5 +1,6 @@ # Authors: # Martin Nagy +# Jason Gerard DeRose # # Copyright (C) 2008 Red Hat # see file 'COPYING' for use and warranty information @@ -41,6 +42,8 @@ good_vars = ( ('trailing_whitespace', ' value ', 'value'), ('an_int', 42, 42), ('int_repr', ' 42 ', 42), + ('a_float', 3.14, 3.14), + ('float_repr', ' 3.14 ', 3.14), ('true', True, True), ('true_repr', ' True ', True), ('false', False, False), @@ -120,9 +123,12 @@ key3 = var3 config_good = """ [global] +string = Hello world! +null = None yes = True no = False number = 42 +floating = 3.14 """ @@ -372,11 +378,15 @@ class test_Env(ClassChecker): good = tmp.join('good.conf') open(good, 'w').write(config_good) assert path.isfile(good) - assert o._merge_from_file(good) == (3, 3) - assert list(o) == sorted(keys + ('yes', 'no', 'number')) + assert o._merge_from_file(good) == (6, 6) + added = ('string', 'null', 'yes', 'no', 'number', 'floating') + assert list(o) == sorted(keys + added) + assert o.string == 'Hello world!' + assert o.null is None assert o.yes is True assert o.no is False assert o.number == 42 + assert o.floating == 3.14 def new(self): """ -- cgit From 166b3ca80c2ed651eb2a5c20bcda5b51ed9e5e2e Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Tue, 6 Jan 2009 18:21:46 -0700 Subject: Added unit test for Env.__islocked__(); unit test for Env.__lock__() now also tests with base.lock() function --- tests/test_ipalib/test_config.py | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) (limited to 'tests/test_ipalib/test_config.py') diff --git a/tests/test_ipalib/test_config.py b/tests/test_ipalib/test_config.py index ab8d9006..d3109f7b 100644 --- a/tests/test_ipalib/test_config.py +++ b/tests/test_ipalib/test_config.py @@ -30,7 +30,7 @@ from tests.util import getitem, setitem, delitem from tests.util import TempDir, TempHome from ipalib.constants import TYPE_ERROR, OVERRIDE_ERROR, SET_ERROR, DEL_ERROR from ipalib.constants import NAME_REGEX, NAME_ERROR -from ipalib import config, constants +from ipalib import config, constants, base # Valid environment variables in (key, raw, value) tuples: @@ -162,12 +162,31 @@ class test_Env(ClassChecker): Test the `ipalib.config.Env.__lock__` method. """ o = self.cls() - assert o._Env__locked is False + assert o.__islocked__() is False o.__lock__() - assert o._Env__locked is True + assert o.__islocked__() is True e = raises(StandardError, o.__lock__) assert str(e) == 'Env.__lock__() already called' + # 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 + def test_setattr(self): """ Test the `ipalib.config.Env.__setattr__` method. -- cgit