coverage for tiramisu/option/option.py

This commit is contained in:
Emmanuel Garette 2017-02-04 10:21:44 +01:00
parent 722779ebf7
commit 56e180dc31
5 changed files with 113 additions and 44 deletions

View File

@ -22,6 +22,10 @@ def return_calc_list(val):
return [val] return [val]
def return_error():
raise Exception('test')
def test_choiceoption_function(): def test_choiceoption_function():
ch = ChoiceOption('ch', '', values=return_list) ch = ChoiceOption('ch', '', values=return_list)
od = OptionDescription('od', '', [ch]) od = OptionDescription('od', '', [ch])
@ -39,6 +43,14 @@ def test_choiceoption_function():
assert ch.impl_get_values(cfg) == ['val1', 'val2'] assert ch.impl_get_values(cfg) == ['val1', 'val2']
def test_choiceoption_function_error():
ch = ChoiceOption('ch', '', values=return_error)
od = OptionDescription('od', '', [ch])
cfg = Config(od)
cfg.read_write()
raises(Exception, "cfg.ch = 'no'")
def test_choiceoption_calc_function(): def test_choiceoption_calc_function():
ch = ChoiceOption('ch', "", values=return_calc_list, values_params={'': ('val1',)}) ch = ChoiceOption('ch', "", values=return_calc_list, values_params={'': ('val1',)})
od = OptionDescription('od', '', [ch]) od = OptionDescription('od', '', [ch])
@ -70,6 +82,15 @@ def test_choiceoption_calc_opt_function():
assert cfg.getowner(ch) == owners.default assert cfg.getowner(ch) == owners.default
def test_choiceoption_calc_opt_function_propertyerror():
st = StrOption('st', '', 'val1', properties=('disabled',))
ch = ChoiceOption('ch', "", values=return_calc_list, values_params={'': ((st, False),)})
od = OptionDescription('od', '', [st, ch])
cfg = Config(od)
cfg.read_write()
raises(ValueError, "cfg.ch='no'")
def test_choiceoption_calc_opt_multi_function(): def test_choiceoption_calc_opt_multi_function():
st = StrOption('st', '', ['val1'], multi=True) st = StrOption('st', '', ['val1'], multi=True)
ch = ChoiceOption('ch', "", default_multi='val2', values=return_val, values_params={'': ((st, False),)}, multi=True) ch = ChoiceOption('ch', "", default_multi='val2', values=return_val, values_params={'': ((st, False),)}, multi=True)

View File

@ -146,6 +146,7 @@ def test_email():
c.e = u'foo-bar.baz@example.com' c.e = u'foo-bar.baz@example.com'
c.e = u'root@foo.com' c.e = u'root@foo.com'
c.e = u'root@domain' c.e = u'root@domain'
raises(ValueError, "c.e = 1")
raises(ValueError, "c.e = u'root'") raises(ValueError, "c.e = u'root'")
raises(ValueError, "c.e = u'root[]@domain'") raises(ValueError, "c.e = u'root[]@domain'")
@ -158,6 +159,7 @@ def test_url():
c.u = 'http://foo.com' c.u = 'http://foo.com'
c.u = 'https://foo.com' c.u = 'https://foo.com'
c.u = 'https://foo.com/' c.u = 'https://foo.com/'
raises(ValueError, "c.u = 1")
raises(ValueError, "c.u = 'ftp://foo.com'") raises(ValueError, "c.u = 'ftp://foo.com'")
raises(ValueError, "c.u = 'foo.com'") raises(ValueError, "c.u = 'foo.com'")
raises(ValueError, "c.u = ':/foo.com'") raises(ValueError, "c.u = ':/foo.com'")

View File

@ -5,7 +5,7 @@ import warnings
from py.test import raises from py.test import raises
from tiramisu.config import Config from tiramisu.config import Config
from tiramisu.option import IPOption, NetworkOption, NetmaskOption, \ from tiramisu.option import IPOption, NetworkOption, NetmaskOption, \
PortOption, OptionDescription PortOption, BroadcastOption, OptionDescription
from tiramisu.error import ValueWarning from tiramisu.error import ValueWarning
@ -26,6 +26,7 @@ def test_ip():
raises(ValueError, "c.b = '88.88.88.88'") raises(ValueError, "c.b = '88.88.88.88'")
c.b = '0.0.0.0' c.b = '0.0.0.0'
raises(ValueError, "c.b = '255.255.255.0'") raises(ValueError, "c.b = '255.255.255.0'")
raises(ValueError, "c.a = '333.0.1.20'")
raises(ValueError, "IPOption('a', 'ip', default='192.000.023.01')") raises(ValueError, "IPOption('a', 'ip', default='192.000.023.01')")
with warnings.catch_warnings(record=True) as w: with warnings.catch_warnings(record=True) as w:
@ -64,7 +65,11 @@ def test_network():
c.a = '192.168.1.0' c.a = '192.168.1.0'
c.a = '88.88.88.88' c.a = '88.88.88.88'
c.a = '0.0.0.0' c.a = '0.0.0.0'
raises(ValueError, "c.a = 1")
raises(ValueError, "c.a = '1.1.1.1.1'")
raises(ValueError, "c.a = '255.255.255.0'") raises(ValueError, "c.a = '255.255.255.0'")
raises(ValueError, "c.a = '192.168.001.0'")
raises(ValueError, "c.a = '333.168.1.1'")
with warnings.catch_warnings(record=True) as w: with warnings.catch_warnings(record=True) as w:
c.b = '255.255.255.0' c.b = '255.255.255.0'
assert len(w) == 1 assert len(w) == 1
@ -78,9 +83,25 @@ def test_netmask():
a = NetmaskOption('a', '') a = NetmaskOption('a', '')
od = OptionDescription('od', '', [a]) od = OptionDescription('od', '', [a])
c = Config(od) c = Config(od)
raises(ValueError, "c.a = '192.168.1.1.1'")
raises(ValueError, "c.a = '192.168.1.1'") raises(ValueError, "c.a = '192.168.1.1'")
raises(ValueError, "c.a = '192.168.1.0'") raises(ValueError, "c.a = '192.168.1.0'")
raises(ValueError, "c.a = '88.88.88.88'") raises(ValueError, "c.a = '88.88.88.88'")
raises(ValueError, "c.a = '255.255.255.000'")
raises(ValueError, "c.a = 2")
c.a = '0.0.0.0'
c.a = '255.255.255.0'
def test_broadcast():
a = BroadcastOption('a', '')
od = OptionDescription('od', '', [a])
c = Config(od)
raises(ValueError, "c.a = '192.168.1.255.1'")
raises(ValueError, "c.a = '192.168.001.255'")
raises(ValueError, "c.a = '192.168.0.300'")
raises(ValueError, "c.a = 1")
raises(ValueError, "c.a = 2")
c.a = '0.0.0.0' c.a = '0.0.0.0'
c.a = '255.255.255.0' c.a = '255.255.255.0'

View File

@ -7,7 +7,7 @@ from py.test import raises
from tiramisu.config import Config from tiramisu.config import Config
from tiramisu.option import ChoiceOption, BoolOption, IntOption, FloatOption, \ from tiramisu.option import ChoiceOption, BoolOption, IntOption, FloatOption, \
StrOption, OptionDescription PasswordOption, StrOption, DateOption, OptionDescription
from tiramisu.error import PropertiesOptionError from tiramisu.error import PropertiesOptionError
@ -133,3 +133,28 @@ def test_with_many_subgroups():
assert name == "booltwo" assert name == "booltwo"
getattr(homeconfig.cfgimpl_get_description(), name) getattr(homeconfig.cfgimpl_get_description(), name)
assert 'hidden' in setting[booltwo] assert 'hidden' in setting[booltwo]
def test_password_option():
o = PasswordOption('o', '')
d = OptionDescription('d', '', [o])
c = Config(d)
c.o = u'a_valid_password'
raises(ValueError, "c.o = 1")
def test_date_option():
o = DateOption('o', '')
d = OptionDescription('d', '', [o])
c = Config(d)
c.o = u'2017-02-04'
c.o = u'2017-2-4'
raises(ValueError, "c.o = 1")
raises(ValueError, "c.o = u'2017-13-20'")
raises(ValueError, "c.o = u'2017-11-31'")
raises(ValueError, "c.o = u'2017-12-32'")
raises(ValueError, "c.o = u'2017-2-29'")
raises(ValueError, "c.o = u'2-2-2017'")
raises(ValueError, "c.o = u'2017/2/2'")

View File

@ -53,7 +53,7 @@ class ChoiceOption(Option):
else: else:
if values_params is not None: if values_params is not None:
raise ValueError(_('values is not a function, so values_params must be None')) raise ValueError(_('values is not a function, so values_params must be None'))
if not isinstance(values, tuple): # pragma: optional cover if not isinstance(values, tuple):
raise TypeError(_('values must be a tuple or a function for {0}' raise TypeError(_('values must be a tuple or a function for {0}'
).format(name)) ).format(name))
session = self.getsession() session = self.getsession()
@ -74,23 +74,18 @@ class ChoiceOption(Option):
def impl_get_values(self, context, current_opt=undefined): def impl_get_values(self, context, current_opt=undefined):
if current_opt is undefined: if current_opt is undefined:
current_opt = self current_opt = self
params = undefined
#FIXME cache? but in context... #FIXME cache? but in context...
values = self._choice_values values = self._choice_values
if isinstance(values, FunctionType): if isinstance(values, FunctionType):
if context is None: if context is None:
values = [] values = []
else: else:
if params is not undefined:
values_params = params
else:
values_params = self.impl_get_choice_values_params()
values = carry_out_calculation(current_opt, context=context, values = carry_out_calculation(current_opt, context=context,
callback=values, callback=values,
callback_params=values_params) callback_params=self.impl_get_choice_values_params())
if isinstance(values, Exception): if isinstance(values, Exception):
return values return values
if values is not undefined and not isinstance(values, list): # pragma: optional cover if values is not undefined and not isinstance(values, list):
raise ConfigError(_('calculated values for {0} is not a list' raise ConfigError(_('calculated values for {0} is not a list'
'').format(self.impl_getname())) '').format(self.impl_getname()))
return values return values
@ -100,7 +95,7 @@ class ChoiceOption(Option):
values = self.impl_get_values(context, current_opt=current_opt) values = self.impl_get_values(context, current_opt=current_opt)
if isinstance(values, Exception): if isinstance(values, Exception):
return values return values
if values is not undefined and not value in values: # pragma: optional cover if values is not undefined and not value in values:
if len(values) == 1: if len(values) == 1:
return ValueError(_('only {0} is allowed' return ValueError(_('only {0} is allowed'
'').format(values[0])) '').format(values[0]))
@ -116,7 +111,7 @@ class BoolOption(Option):
def _validate(self, value, context=undefined, current_opt=undefined): def _validate(self, value, context=undefined, current_opt=undefined):
if not isinstance(value, bool): if not isinstance(value, bool):
return ValueError() # pragma: optional cover return ValueError()
class IntOption(Option): class IntOption(Option):
@ -126,7 +121,7 @@ class IntOption(Option):
def _validate(self, value, context=undefined, current_opt=undefined): def _validate(self, value, context=undefined, current_opt=undefined):
if not isinstance(value, int): if not isinstance(value, int):
return ValueError() # pragma: optional cover return ValueError()
class FloatOption(Option): class FloatOption(Option):
@ -136,7 +131,7 @@ class FloatOption(Option):
def _validate(self, value, context=undefined, current_opt=undefined): def _validate(self, value, context=undefined, current_opt=undefined):
if not isinstance(value, float): if not isinstance(value, float):
return ValueError() # pragma: optional cover return ValueError()
class StrOption(Option): class StrOption(Option):
@ -146,10 +141,10 @@ class StrOption(Option):
def _validate(self, value, context=undefined, current_opt=undefined): def _validate(self, value, context=undefined, current_opt=undefined):
if not isinstance(value, str): if not isinstance(value, str):
return ValueError() # pragma: optional cover return ValueError()
if sys.version_info[0] >= 3: # pragma: optional cover if sys.version_info[0] >= 3: # pragma: no cover
#UnicodeOption is same as StrOption in python 3+ #UnicodeOption is same as StrOption in python 3+
class UnicodeOption(StrOption): class UnicodeOption(StrOption):
__slots__ = tuple() __slots__ = tuple()
@ -163,7 +158,7 @@ else:
def _validate(self, value, context=undefined, current_opt=undefined): def _validate(self, value, context=undefined, current_opt=undefined):
if not isinstance(value, unicode): if not isinstance(value, unicode):
return ValueError() # pragma: optional cover return ValueError()
class PasswordOption(Option): class PasswordOption(Option):
@ -211,22 +206,22 @@ class IPOption(Option):
return ValueError() return ValueError()
for val in value.split('.'): for val in value.split('.'):
if val.startswith("0") and len(val) > 1: if val.startswith("0") and len(val) > 1:
return ValueError() # pragma: optional cover return ValueError()
# 'standard' validation # 'standard' validation
try: try:
IP('{0}/32'.format(value)) IP('{0}/32'.format(value))
except ValueError: # pragma: optional cover except ValueError:
return ValueError() return ValueError()
def _second_level_validation(self, value, warnings_only): def _second_level_validation(self, value, warnings_only):
ip = IP('{0}/32'.format(value)) ip = IP('{0}/32'.format(value))
if not self._get_extra('_allow_reserved') and ip.iptype() == 'RESERVED': # pragma: optional cover if not self._get_extra('_allow_reserved') and ip.iptype() == 'RESERVED':
if warnings_only: if warnings_only:
msg = _("shouldn't in reserved class") msg = _("shouldn't in reserved class")
else: else:
msg = _("mustn't be in reserved class") msg = _("mustn't be in reserved class")
return ValueError(msg) return ValueError(msg)
if self._get_extra('_private_only') and not ip.iptype() == 'PRIVATE': # pragma: optional cover if self._get_extra('_private_only') and not ip.iptype() == 'PRIVATE':
if warnings_only: if warnings_only:
msg = _("should be in private class") msg = _("should be in private class")
else: else:
@ -235,11 +230,11 @@ class IPOption(Option):
def _cons_in_network(self, current_opt, opts, vals, warnings_only): def _cons_in_network(self, current_opt, opts, vals, warnings_only):
if len(vals) != 3: if len(vals) != 3:
raise ConfigError(_('invalid len for vals')) # pragma: optional cover raise ConfigError(_('invalid len for vals'))
if None in vals: if None in vals:
return return
ip, network, netmask = vals ip, network, netmask = vals
if IP(ip) not in IP('{0}/{1}'.format(network, netmask)): # pragma: optional cover if IP(ip) not in IP('{0}/{1}'.format(network, netmask)):
if warnings_only: if warnings_only:
msg = _('should be in network {0}/{1} ({2}/{3})') msg = _('should be in network {0}/{1} ({2}/{3})')
else: else:
@ -286,12 +281,12 @@ class PortOption(Option):
elif not allowed: elif not allowed:
is_finally = True is_finally = True
elif allowed and is_finally: elif allowed and is_finally:
raise ValueError(_('inconsistency in allowed range')) # pragma: optional cover raise ValueError(_('inconsistency in allowed range'))
if allowed: if allowed:
extra['_max_value'] = ports_max[index] extra['_max_value'] = ports_max[index]
if extra['_max_value'] is None: if extra['_max_value'] is None:
raise ValueError(_('max value is empty')) # pragma: optional cover raise ValueError(_('max value is empty'))
super(PortOption, self).__init__(name, doc, default=default, super(PortOption, self).__init__(name, doc, default=default,
default_multi=default_multi, default_multi=default_multi,
@ -307,14 +302,14 @@ class PortOption(Option):
def _validate(self, value, context=undefined, current_opt=undefined): def _validate(self, value, context=undefined, current_opt=undefined):
if isinstance(value, int): if isinstance(value, int):
if sys.version_info[0] >= 3: # pragma: optional cover if sys.version_info[0] >= 3: # pragma: no cover
value = str(value) value = str(value)
else: else:
value = unicode(value) value = unicode(value)
err = self._impl_valid_unicode(value) err = self._impl_valid_unicode(value)
if err: if err:
return err return err
if self._get_extra('_allow_range') and ":" in str(value): # pragma: optional cover if self._get_extra('_allow_range') and ":" in str(value):
value = str(value).split(':') value = str(value).split(':')
if len(value) != 2: if len(value) != 2:
return ValueError(_('range must have two values only')) return ValueError(_('range must have two values only'))
@ -328,7 +323,7 @@ class PortOption(Option):
if not self.port_re.search(val): if not self.port_re.search(val):
return ValueError() return ValueError()
val = int(val) val = int(val)
if not self._get_extra('_min_value') <= val <= self._get_extra('_max_value'): # pragma: optional cover if not self._get_extra('_min_value') <= val <= self._get_extra('_max_value'):
return ValueError(_('must be an integer between {0} ' return ValueError(_('must be an integer between {0} '
'and {1}').format(self._get_extra('_min_value'), 'and {1}').format(self._get_extra('_min_value'),
self._get_extra('_max_value'))) self._get_extra('_max_value')))
@ -350,12 +345,12 @@ class NetworkOption(Option):
return ValueError() return ValueError()
try: try:
IP(value) IP(value)
except ValueError: # pragma: optional cover except ValueError:
return ValueError() return ValueError()
def _second_level_validation(self, value, warnings_only): def _second_level_validation(self, value, warnings_only):
ip = IP(value) ip = IP(value)
if ip.iptype() == 'RESERVED': # pragma: optional cover if ip.iptype() == 'RESERVED':
if warnings_only: if warnings_only:
msg = _("shouldn't be in reserved class") msg = _("shouldn't be in reserved class")
else: else:
@ -379,7 +374,7 @@ class NetmaskOption(Option):
return ValueError() return ValueError()
try: try:
IP('0.0.0.0/{0}'.format(value)) IP('0.0.0.0/{0}'.format(value))
except ValueError: # pragma: optional cover except ValueError:
return ValueError() return ValueError()
def _cons_network_netmask(self, current_opt, opts, vals, warnings_only): def _cons_network_netmask(self, current_opt, opts, vals, warnings_only):
@ -397,7 +392,7 @@ class NetmaskOption(Option):
def __cons_netmask(self, opts, val_netmask, val_ipnetwork, make_net, def __cons_netmask(self, opts, val_netmask, val_ipnetwork, make_net,
warnings_only): warnings_only):
if len(opts) != 2: if len(opts) != 2:
return ConfigError(_('invalid len for opts')) # pragma: optional cover return ConfigError(_('invalid len for opts'))
msg = None msg = None
try: try:
ip = IP('{0}/{1}'.format(val_ipnetwork, val_netmask), ip = IP('{0}/{1}'.format(val_ipnetwork, val_netmask),
@ -410,10 +405,10 @@ class NetmaskOption(Option):
if ip.broadcast() == val_ip: if ip.broadcast() == val_ip:
msg = _("this is a broadcast with netmask {0} ({1})") msg = _("this is a broadcast with netmask {0} ({1})")
except ValueError: # pragma: optional cover except ValueError:
if not make_net: if not make_net:
msg = _('with netmask {0} ({1})') msg = _('with netmask {0} ({1})')
if msg is not None: # pragma: optional cover if msg is not None:
return ValueError(msg.format(val_netmask, opts[1].impl_getname())) return ValueError(msg.format(val_netmask, opts[1].impl_getname()))
@ -425,20 +420,25 @@ class BroadcastOption(Option):
err = self._impl_valid_unicode(value) err = self._impl_valid_unicode(value)
if err: if err:
return err return err
if value.count('.') != 3:
return ValueError()
for val in value.split('.'):
if val.startswith("0") and len(val) > 1:
return ValueError()
try: try:
IP('{0}/32'.format(value)) IP('{0}/32'.format(value))
except ValueError: # pragma: optional cover except ValueError:
return ValueError() return ValueError()
def _cons_broadcast(self, current_opt, opts, vals, warnings_only): def _cons_broadcast(self, current_opt, opts, vals, warnings_only):
if len(vals) != 3: if len(vals) != 3:
raise ConfigError(_('invalid len for vals')) # pragma: optional cover raise ConfigError(_('invalid len for vals'))
if None in vals: if None in vals:
return return
broadcast, network, netmask = vals broadcast, network, netmask = vals
if IP('{0}/{1}'.format(network, netmask)).broadcast() != IP(broadcast): if IP('{0}/{1}'.format(network, netmask)).broadcast() != IP(broadcast):
return ValueError(_('with network {0}/{1} ({2}/{3})').format( return ValueError(_('with network {0}/{1} ({2}/{3})').format(
network, netmask, opts[1].impl_getname(), opts[2].impl_getname())) # pragma: optional cover network, netmask, opts[1].impl_getname(), opts[2].impl_getname()))
class DomainnameOption(Option): class DomainnameOption(Option):
@ -457,12 +457,12 @@ class DomainnameOption(Option):
properties=None, allow_ip=False, type_='domainname', properties=None, allow_ip=False, type_='domainname',
warnings_only=False, allow_without_dot=False): warnings_only=False, allow_without_dot=False):
if type_ not in ['netbios', 'hostname', 'domainname']: if type_ not in ['netbios', 'hostname', 'domainname']:
raise ValueError(_('unknown type_ {0} for hostname').format(type_)) # pragma: optional cover raise ValueError(_('unknown type_ {0} for hostname').format(type_))
extra = {'_dom_type': type_} extra = {'_dom_type': type_}
if allow_ip not in [True, False]: if allow_ip not in [True, False]:
raise ValueError(_('allow_ip must be a boolean')) # pragma: optional cover raise ValueError(_('allow_ip must be a boolean'))
if allow_without_dot not in [True, False]: if allow_without_dot not in [True, False]:
raise ValueError(_('allow_without_dot must be a boolean')) # pragma: optional cover raise ValueError(_('allow_without_dot must be a boolean'))
extra['_allow_ip'] = allow_ip extra['_allow_ip'] = allow_ip
extra['_allow_without_dot'] = allow_without_dot extra['_allow_without_dot'] = allow_without_dot
extra['_domain_re'] = re.compile(r'^[a-z\d][a-z\d\-]*$') extra['_domain_re'] = re.compile(r'^[a-z\d][a-z\d\-]*$')
@ -492,7 +492,7 @@ class DomainnameOption(Option):
return ValueError(_("invalid length (max {0})" return ValueError(_("invalid length (max {0})"
"").format(part_name_length)) "").format(part_name_length))
if self._get_extra('_allow_ip') is True: # pragma: optional cover if self._get_extra('_allow_ip') is True:
try: try:
IP('{0}/32'.format(value)) IP('{0}/32'.format(value))
return return
@ -555,7 +555,7 @@ class URLOption(DomainnameOption):
if err: if err:
return err return err
match = self.proto_re.search(value) match = self.proto_re.search(value)
if not match: # pragma: optional cover if not match:
return ValueError(_('must start with http:// or ' return ValueError(_('must start with http:// or '
'https://')) 'https://'))
value = value[len(match.group(0)):] value = value[len(match.group(0)):]
@ -575,7 +575,7 @@ class URLOption(DomainnameOption):
domain, port = splitted domain, port = splitted
if not 0 <= int(port) <= 65535: if not 0 <= int(port) <= 65535:
return ValueError(_('port must be an between 0 and ' return ValueError(_('port must be an between 0 and '
'65536')) # pragma: optional cover '65536'))
# validate domainname # validate domainname
err = super(URLOption, self)._validate(domain) err = super(URLOption, self)._validate(domain)
if err: if err:
@ -585,7 +585,7 @@ class URLOption(DomainnameOption):
return err return err
# validate file # validate file
if files is not None and files != '' and not self.path_re.search(files): if files is not None and files != '' and not self.path_re.search(files):
return ValueError(_('must ends with a valid resource name')) # pragma: optional cover return ValueError(_('must ends with a valid resource name'))
def _second_level_validation(self, value, warnings_only): def _second_level_validation(self, value, warnings_only):
pass pass