2012-07-13 09:42:14 +02:00
|
|
|
import autopath
|
|
|
|
from py.test import raises
|
|
|
|
|
2013-05-10 22:32:42 +02:00
|
|
|
from tiramisu.setting import owners, groups
|
2013-04-22 09:19:05 +02:00
|
|
|
from tiramisu.config import Config
|
2013-05-08 18:14:42 +02:00
|
|
|
from tiramisu.option import IPOption, NetworkOption, NetmaskOption, IntOption,\
|
2013-09-27 23:26:10 +02:00
|
|
|
BroadcastOption, SymLinkOption, OptionDescription
|
2014-03-12 21:56:53 +01:00
|
|
|
from tiramisu.error import ConfigError, ValueWarning
|
|
|
|
import warnings
|
2013-05-08 18:14:42 +02:00
|
|
|
|
|
|
|
|
2013-12-09 18:56:29 +01:00
|
|
|
def test_consistency():
|
|
|
|
a = IntOption('a', '')
|
|
|
|
b = IntOption('b', '')
|
|
|
|
od = OptionDescription('od', '', [a, b])
|
|
|
|
a.impl_add_consistency('not_equal', b)
|
|
|
|
#consistency to itself
|
|
|
|
raises(ConfigError, "a.impl_add_consistency('not_equal', a)")
|
|
|
|
#consistency with string
|
|
|
|
raises(ConfigError, "a.impl_add_consistency('not_equal', 'a')")
|
|
|
|
|
|
|
|
|
2014-03-12 21:56:53 +01:00
|
|
|
def test_consistency_warnings_only():
|
|
|
|
a = IntOption('a', '')
|
|
|
|
b = IntOption('b', '')
|
|
|
|
od = OptionDescription('od', '', [a, b])
|
|
|
|
a.impl_add_consistency('not_equal', b, warnings_only=True)
|
|
|
|
c = Config(od)
|
|
|
|
c.a = 1
|
|
|
|
warnings.simplefilter("always", ValueWarning)
|
|
|
|
with warnings.catch_warnings(record=True) as w:
|
|
|
|
c.b = 1
|
|
|
|
assert w != []
|
|
|
|
|
|
|
|
|
2013-05-08 18:14:42 +02:00
|
|
|
def test_consistency_not_equal():
|
|
|
|
a = IntOption('a', '')
|
|
|
|
b = IntOption('b', '')
|
|
|
|
od = OptionDescription('od', '', [a, b])
|
2013-05-10 15:10:06 +02:00
|
|
|
a.impl_add_consistency('not_equal', b)
|
2013-05-08 18:14:42 +02:00
|
|
|
c = Config(od)
|
|
|
|
assert c.a is None
|
|
|
|
assert c.b is None
|
|
|
|
c.a = 1
|
|
|
|
del(c.a)
|
|
|
|
c.a = 1
|
|
|
|
raises(ValueError, "c.b = 1")
|
|
|
|
c.b = 2
|
|
|
|
|
|
|
|
|
2013-09-28 17:05:01 +02:00
|
|
|
def test_consistency_not_equal_many_opts():
|
|
|
|
a = IntOption('a', '')
|
|
|
|
b = IntOption('b', '')
|
|
|
|
c = IntOption('c', '')
|
|
|
|
d = IntOption('d', '')
|
|
|
|
e = IntOption('e', '')
|
|
|
|
f = IntOption('f', '')
|
|
|
|
od = OptionDescription('od', '', [a, b, c, d, e, f])
|
|
|
|
a.impl_add_consistency('not_equal', b, c, d, e, f)
|
|
|
|
c = Config(od)
|
|
|
|
assert c.a is None
|
|
|
|
assert c.b is None
|
|
|
|
#
|
|
|
|
c.a = 1
|
|
|
|
del(c.a)
|
|
|
|
#
|
|
|
|
c.a = 1
|
|
|
|
raises(ValueError, "c.b = 1")
|
|
|
|
#
|
|
|
|
c.b = 2
|
|
|
|
raises(ValueError, "c.f = 2")
|
|
|
|
raises(ValueError, "c.f = 1")
|
|
|
|
#
|
|
|
|
c.d = 3
|
|
|
|
raises(ValueError, "c.f = 3")
|
|
|
|
raises(ValueError, "c.a = 3")
|
|
|
|
raises(ValueError, "c.c = 3")
|
|
|
|
raises(ValueError, "c.e = 3")
|
|
|
|
|
|
|
|
|
2013-11-23 23:34:17 +01:00
|
|
|
def test_consistency_not_in_config_1():
|
2013-09-28 17:05:01 +02:00
|
|
|
a = IntOption('a', '')
|
|
|
|
b = IntOption('b', '')
|
|
|
|
a.impl_add_consistency('not_equal', b)
|
|
|
|
od1 = OptionDescription('od1', '', [a])
|
|
|
|
od2 = OptionDescription('od2', '', [b])
|
|
|
|
od = OptionDescription('root', '', [od1])
|
|
|
|
raises(ConfigError, "Config(od)")
|
2013-11-23 23:34:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_consistency_not_in_config_2():
|
|
|
|
a = IntOption('a', '')
|
|
|
|
b = IntOption('b', '')
|
|
|
|
a.impl_add_consistency('not_equal', b)
|
|
|
|
od1 = OptionDescription('od1', '', [a])
|
|
|
|
od2 = OptionDescription('od2', '', [b])
|
2013-09-28 17:05:01 +02:00
|
|
|
od = OptionDescription('root', '', [od1, od2])
|
|
|
|
Config(od)
|
2013-11-23 23:34:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_consistency_not_in_config_3():
|
|
|
|
a = IntOption('a', '')
|
|
|
|
b = IntOption('b', '')
|
|
|
|
a.impl_add_consistency('not_equal', b)
|
|
|
|
od1 = OptionDescription('od1', '', [a])
|
|
|
|
od2 = OptionDescription('od2', '', [b])
|
|
|
|
od = OptionDescription('root', '', [od1, od2])
|
2013-09-28 17:05:01 +02:00
|
|
|
#with subconfig
|
|
|
|
raises(ConfigError, "Config(od.od1)")
|
|
|
|
|
|
|
|
|
2013-11-23 23:34:17 +01:00
|
|
|
def test_consistency_after_config():
|
2013-09-28 17:05:01 +02:00
|
|
|
a = IntOption('a', '')
|
|
|
|
b = IntOption('b', '')
|
|
|
|
od1 = OptionDescription('od1', '', [a])
|
|
|
|
od2 = OptionDescription('od2', '', [b])
|
|
|
|
od = OptionDescription('root', '', [od1, od2])
|
|
|
|
Config(od)
|
2014-01-30 22:55:15 +01:00
|
|
|
raises(AttributeError, "a.impl_add_consistency('not_equal', b)")
|
2013-09-28 17:05:01 +02:00
|
|
|
|
|
|
|
|
2013-09-03 22:41:18 +02:00
|
|
|
def test_consistency_not_equal_symlink():
|
|
|
|
a = IntOption('a', '')
|
|
|
|
b = IntOption('b', '')
|
|
|
|
c = SymLinkOption('c', a)
|
|
|
|
od = OptionDescription('od', '', [a, b, c])
|
|
|
|
a.impl_add_consistency('not_equal', b)
|
|
|
|
c = Config(od)
|
2013-09-28 17:05:01 +02:00
|
|
|
assert set(od._cache_consistencies.keys()) == set([a, b])
|
2013-09-03 22:41:18 +02:00
|
|
|
|
|
|
|
|
2013-05-10 15:10:06 +02:00
|
|
|
def test_consistency_not_equal_multi():
|
|
|
|
a = IntOption('a', '', multi=True)
|
|
|
|
b = IntOption('b', '', multi=True)
|
|
|
|
od = OptionDescription('od', '', [a, b])
|
|
|
|
a.impl_add_consistency('not_equal', b)
|
|
|
|
c = Config(od)
|
|
|
|
assert c.a == []
|
|
|
|
assert c.b == []
|
|
|
|
c.a = [1]
|
|
|
|
del(c.a)
|
|
|
|
c.a = [1]
|
|
|
|
raises(ValueError, "c.b = [1]")
|
|
|
|
c.b = [2]
|
|
|
|
|
|
|
|
|
2013-05-08 18:14:42 +02:00
|
|
|
def test_consistency_default():
|
|
|
|
a = IntOption('a', '', 1)
|
|
|
|
b = IntOption('b', '', 1)
|
2013-05-10 15:10:06 +02:00
|
|
|
raises(ValueError, "a.impl_add_consistency('not_equal', b)")
|
2013-05-08 18:14:42 +02:00
|
|
|
|
|
|
|
|
2013-09-28 17:05:01 +02:00
|
|
|
def test_consistency_default_multi():
|
|
|
|
a = IntOption('a', '', [2, 1], multi=True)
|
|
|
|
b = IntOption('b', '', [1, 1], multi=True)
|
|
|
|
c = IntOption('c', '', [1, 2], multi=True)
|
|
|
|
raises(ValueError, "a.impl_add_consistency('not_equal', b)")
|
|
|
|
a.impl_add_consistency('not_equal', c)
|
|
|
|
|
|
|
|
|
2013-05-08 18:14:42 +02:00
|
|
|
def test_consistency_default_diff():
|
|
|
|
a = IntOption('a', '', 3)
|
|
|
|
b = IntOption('b', '', 1)
|
|
|
|
od = OptionDescription('od', '', [a, b])
|
2013-05-10 15:10:06 +02:00
|
|
|
a.impl_add_consistency('not_equal', b)
|
2013-05-08 18:14:42 +02:00
|
|
|
c = Config(od)
|
|
|
|
raises(ValueError, "c.a = 1")
|
|
|
|
c.a = 2
|
|
|
|
c.b = 3
|
2013-08-24 22:32:54 +02:00
|
|
|
assert c.getowner(a) is owners.user
|
2013-05-08 18:14:42 +02:00
|
|
|
raises(ValueError, "del(c.a)")
|
2013-08-24 22:32:54 +02:00
|
|
|
assert c.getowner(a) is owners.user
|
2013-05-08 18:14:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_consistency_ip_netmask():
|
|
|
|
a = IPOption('a', '')
|
|
|
|
b = NetmaskOption('b', '')
|
|
|
|
od = OptionDescription('od', '', [a, b])
|
2013-05-10 15:10:06 +02:00
|
|
|
b.impl_add_consistency('ip_netmask', a)
|
2013-05-08 18:14:42 +02:00
|
|
|
c = Config(od)
|
|
|
|
c.a = '192.168.1.1'
|
|
|
|
c.b = '255.255.255.0'
|
|
|
|
c.a = '192.168.1.2'
|
|
|
|
c.b = '255.255.255.255'
|
|
|
|
c.b = '255.255.255.0'
|
|
|
|
raises(ValueError, "c.a = '192.168.1.0'")
|
|
|
|
|
|
|
|
|
|
|
|
def test_consistency_network_netmask():
|
|
|
|
a = NetworkOption('a', '')
|
|
|
|
b = NetmaskOption('b', '')
|
|
|
|
od = OptionDescription('od', '', [a, b])
|
2013-05-10 15:10:06 +02:00
|
|
|
b.impl_add_consistency('network_netmask', a)
|
2013-05-08 18:14:42 +02:00
|
|
|
c = Config(od)
|
|
|
|
c.a = '192.168.1.1'
|
|
|
|
c.b = '255.255.255.255'
|
|
|
|
del(c.b)
|
|
|
|
c.a = '192.168.1.0'
|
|
|
|
c.b = '255.255.255.0'
|
|
|
|
raises(ValueError, "c.a = '192.168.1.1'")
|
2013-05-10 15:10:06 +02:00
|
|
|
|
|
|
|
|
2014-03-11 18:57:19 +01:00
|
|
|
def test_consistency_ip_in_network():
|
|
|
|
a = NetworkOption('a', '')
|
|
|
|
b = NetmaskOption('b', '')
|
|
|
|
c = IPOption('c', '')
|
|
|
|
od = OptionDescription('od', '', [a, b, c])
|
|
|
|
c.impl_add_consistency('in_network', a, b)
|
|
|
|
cfg = Config(od)
|
|
|
|
cfg.a = '192.168.1.0'
|
|
|
|
cfg.b = '255.255.255.0'
|
|
|
|
cfg.c = '192.168.1.1'
|
|
|
|
raises(ValueError, "cfg.c = '192.168.2.1'")
|
|
|
|
|
|
|
|
|
|
|
|
def test_consistency_ip_in_network_len_error():
|
|
|
|
a = NetworkOption('a', '')
|
|
|
|
b = NetmaskOption('b', '')
|
|
|
|
c = IPOption('c', '')
|
|
|
|
od = OptionDescription('od', '', [a, b, c])
|
|
|
|
c.impl_add_consistency('in_network', a)
|
|
|
|
cfg = Config(od)
|
|
|
|
raises(ConfigError, "cfg.a = '192.168.2.0'")
|
|
|
|
|
|
|
|
|
2014-02-06 22:17:20 +01:00
|
|
|
def test_consistency_ip_netmask_network_error():
|
|
|
|
a = IPOption('a', '')
|
|
|
|
b = NetworkOption('b', '')
|
|
|
|
c = NetmaskOption('c', '')
|
|
|
|
od = OptionDescription('od', '', [a, b, c])
|
|
|
|
c.impl_add_consistency('ip_netmask', a, b)
|
|
|
|
c = Config(od)
|
|
|
|
c.a = '192.168.1.1'
|
|
|
|
c.b = '192.168.1.0'
|
|
|
|
raises(ConfigError, "c.c = '255.255.255.0'")
|
|
|
|
|
|
|
|
|
2013-05-10 22:32:42 +02:00
|
|
|
def test_consistency_ip_netmask_error_multi():
|
|
|
|
a = IPOption('a', '', multi=True)
|
|
|
|
b = NetmaskOption('b', '')
|
|
|
|
od = OptionDescription('od', '', [a, b])
|
2013-09-28 17:05:01 +02:00
|
|
|
raises(ConfigError, "b.impl_add_consistency('ip_netmask', a)")
|
2013-05-10 22:32:42 +02:00
|
|
|
|
|
|
|
|
2013-05-10 15:10:06 +02:00
|
|
|
def test_consistency_ip_netmask_multi():
|
|
|
|
a = IPOption('a', '', multi=True)
|
|
|
|
b = NetmaskOption('b', '', multi=True)
|
|
|
|
od = OptionDescription('od', '', [a, b])
|
|
|
|
b.impl_add_consistency('ip_netmask', a)
|
|
|
|
c = Config(od)
|
|
|
|
c.a = ['192.168.1.1']
|
|
|
|
c.b = ['255.255.255.0']
|
|
|
|
c.a = ['192.168.1.2']
|
|
|
|
c.b = ['255.255.255.255']
|
|
|
|
c.b = ['255.255.255.0']
|
|
|
|
raises(ValueError, "c.a = ['192.168.1.0']")
|
|
|
|
|
|
|
|
|
|
|
|
def test_consistency_network_netmask_multi():
|
|
|
|
a = NetworkOption('a', '', multi=True)
|
|
|
|
b = NetmaskOption('b', '', multi=True)
|
|
|
|
od = OptionDescription('od', '', [a, b])
|
|
|
|
b.impl_add_consistency('network_netmask', a)
|
|
|
|
c = Config(od)
|
|
|
|
c.a = ['192.168.1.1']
|
|
|
|
c.b = ['255.255.255.255']
|
|
|
|
del(c.b)
|
|
|
|
c.a = ['192.168.1.0']
|
|
|
|
c.b = ['255.255.255.0']
|
|
|
|
raises(ValueError, "c.a = ['192.168.1.1']")
|
2013-05-10 22:32:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_consistency_ip_netmask_multi_master():
|
|
|
|
a = IPOption('a', '', multi=True)
|
|
|
|
b = NetmaskOption('b', '', multi=True)
|
|
|
|
od = OptionDescription('a', '', [a, b])
|
|
|
|
od.impl_set_group_type(groups.master)
|
|
|
|
b.impl_add_consistency('ip_netmask', a)
|
|
|
|
c = Config(od)
|
|
|
|
c.a = ['192.168.1.1']
|
|
|
|
c.b = ['255.255.255.0']
|
|
|
|
c.a = ['192.168.1.2']
|
|
|
|
c.b = ['255.255.255.255']
|
|
|
|
c.b = ['255.255.255.0']
|
|
|
|
raises(ValueError, "c.a = ['192.168.1.0']")
|
2014-02-06 22:17:20 +01:00
|
|
|
c.a = ['192.168.1.128']
|
|
|
|
raises(ValueError, "c.b = ['255.255.255.128']")
|
2013-08-25 18:06:07 +02:00
|
|
|
c.a = ['192.168.1.2', '192.168.1.3']
|
2013-05-10 22:32:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_consistency_network_netmask_multi_master():
|
|
|
|
a = NetworkOption('a', '', multi=True)
|
|
|
|
b = NetmaskOption('b', '', multi=True)
|
|
|
|
od = OptionDescription('a', '', [a, b])
|
|
|
|
od.impl_set_group_type(groups.master)
|
|
|
|
b.impl_add_consistency('network_netmask', a)
|
|
|
|
c = Config(od)
|
|
|
|
c.a = ['192.168.1.1']
|
|
|
|
c.b = ['255.255.255.255']
|
|
|
|
del(c.b)
|
|
|
|
c.a = ['192.168.1.0']
|
|
|
|
c.b = ['255.255.255.0']
|
|
|
|
raises(ValueError, "c.a = ['192.168.1.1']")
|
2013-09-27 23:26:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_consistency_broadcast():
|
|
|
|
a = NetworkOption('a', '', multi=True)
|
|
|
|
b = NetmaskOption('b', '', multi=True)
|
|
|
|
c = BroadcastOption('c', '', multi=True)
|
|
|
|
od = OptionDescription('a', '', [a, b, c])
|
|
|
|
od.impl_set_group_type(groups.master)
|
|
|
|
b.impl_add_consistency('network_netmask', a)
|
|
|
|
c.impl_add_consistency('broadcast', a, b)
|
|
|
|
c = Config(od)
|
2013-09-28 17:05:01 +02:00
|
|
|
#first, test network_netmask
|
|
|
|
c.a = ['192.168.1.128']
|
|
|
|
raises(ValueError, "c.b = ['255.255.255.0']")
|
|
|
|
#
|
2013-09-27 23:26:10 +02:00
|
|
|
c.a = ['192.168.1.0']
|
|
|
|
c.b = ['255.255.255.0']
|
|
|
|
c.c = ['192.168.1.255']
|
|
|
|
raises(ValueError, "c.a = ['192.168.1.1']")
|
2013-09-28 17:05:01 +02:00
|
|
|
#
|
2013-09-27 23:26:10 +02:00
|
|
|
c.a = ['192.168.1.0', '192.168.2.128']
|
|
|
|
c.b = ['255.255.255.0', '255.255.255.128']
|
|
|
|
c.c = ['192.168.1.255', '192.168.2.255']
|
|
|
|
raises(ValueError, "c.c[1] = '192.168.2.128'")
|
2013-09-28 17:05:01 +02:00
|
|
|
c.c[1] = '192.168.2.255'
|
|
|
|
|
|
|
|
|
2014-02-06 22:17:20 +01:00
|
|
|
def test_consistency_broadcast_error():
|
|
|
|
a = NetworkOption('a', '', multi=True)
|
|
|
|
b = NetmaskOption('b', '', multi=True)
|
|
|
|
c = BroadcastOption('c', '', multi=True)
|
|
|
|
od = OptionDescription('a', '', [a, b, c])
|
|
|
|
od.impl_set_group_type(groups.master)
|
|
|
|
b.impl_add_consistency('network_netmask', a)
|
|
|
|
c.impl_add_consistency('broadcast', a)
|
|
|
|
c = Config(od)
|
|
|
|
c.a = ['192.168.1.0']
|
2014-04-12 11:53:58 +02:00
|
|
|
raises(ConfigError, "c.b = ['255.255.255.0']")
|
2014-02-06 22:17:20 +01:00
|
|
|
raises(ConfigError, "c.c = ['192.168.1.255']")
|
|
|
|
|
|
|
|
|
2013-11-23 23:34:17 +01:00
|
|
|
def test_consistency_broadcast_default_1():
|
2013-09-28 17:05:01 +02:00
|
|
|
a = NetworkOption('a', '', '192.168.1.0')
|
|
|
|
b = NetmaskOption('b', '', '255.255.255.128')
|
|
|
|
c = BroadcastOption('c', '', '192.168.2.127')
|
|
|
|
d = BroadcastOption('d', '', '192.168.1.127')
|
|
|
|
od = OptionDescription('a', '', [a, b, c])
|
|
|
|
raises(ValueError, "c.impl_add_consistency('broadcast', a, b)")
|
2013-11-23 23:34:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_consistency_broadcast_default_2():
|
|
|
|
a = NetworkOption('a', '', '192.168.1.0')
|
|
|
|
b = NetmaskOption('b', '', '255.255.255.128')
|
|
|
|
c = BroadcastOption('c', '', '192.168.2.127')
|
|
|
|
d = BroadcastOption('d', '', '192.168.1.127')
|
2013-09-28 17:05:01 +02:00
|
|
|
od2 = OptionDescription('a', '', [a, b, d])
|
|
|
|
d.impl_add_consistency('broadcast', a, b)
|
|
|
|
|
|
|
|
|
|
|
|
def test_consistency_not_all():
|
|
|
|
#_cache_consistencies is not None by not options has consistencies
|
|
|
|
a = NetworkOption('a', '', multi=True)
|
|
|
|
b = NetmaskOption('b', '', multi=True)
|
|
|
|
c = BroadcastOption('c', '', multi=True)
|
|
|
|
od = OptionDescription('a', '', [a, b, c])
|
|
|
|
od.impl_set_group_type(groups.master)
|
|
|
|
b.impl_add_consistency('network_netmask', a)
|
|
|
|
c = Config(od)
|
|
|
|
c.a = ['192.168.1.0']
|
|
|
|
c.b = ['255.255.255.0']
|
|
|
|
c.c = ['192.168.1.255']
|
2014-02-06 19:19:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_consistency_permissive():
|
|
|
|
a = IntOption('a', '', 1)
|
|
|
|
b = IntOption('b', '', 2, properties=('hidden',))
|
|
|
|
od = OptionDescription('od', '', [a, b])
|
|
|
|
a.impl_add_consistency('not_equal', b)
|
|
|
|
c = Config(od)
|
|
|
|
c.cfgimpl_get_settings().setpermissive(('hidden',))
|
|
|
|
c.read_write()
|
|
|
|
c.a = 1
|
2014-03-12 16:44:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
def return_val(*args, **kwargs):
|
|
|
|
return '192.168.1.1'
|
|
|
|
|
|
|
|
|
|
|
|
def test_consistency_with_callback():
|
|
|
|
a = NetworkOption('a', '', default='192.168.1.0')
|
|
|
|
b = NetmaskOption('b', '', default='255.255.255.0')
|
|
|
|
c = IPOption('c', '', callback=return_val, callback_params={'': ((a, False),)})
|
|
|
|
od = OptionDescription('od', '', [a, b, c])
|
|
|
|
c.impl_add_consistency('in_network', a, b)
|
|
|
|
cfg = Config(od)
|
|
|
|
cfg.c
|