Merge lp:~lamont/maas/too-small into lp:~maas-committers/maas/trunk

Proposed by LaMont Jones
Status: Merged
Approved by: LaMont Jones
Approved revision: no longer in the source branch.
Merged at revision: 4446
Proposed branch: lp:~lamont/maas/too-small
Merge into: lp:~maas-committers/maas/trunk
Diff against target: 97 lines (+41/-7)
2 files modified
src/maasserver/forms.py (+15/-7)
src/maasserver/tests/test_forms_nodegroupinterface.py (+26/-0)
To merge this branch: bzr merge lp:~lamont/maas/too-small
Reviewer Review Type Date Requested Status
Mike Pontillo (community) Approve
Review via email: mp+276325@code.launchpad.net

Commit message

Correctly handle ValueError from netaddr functions.

Description of the change

netaddr can raise ValueError

To post a comment you must log in.
Revision history for this message
Mike Pontillo (mpontillo) wrote :

Looks pretty good. Just one minor point of feedback below.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'src/maasserver/forms.py'
--- src/maasserver/forms.py 2015-10-30 18:11:52 +0000
+++ src/maasserver/forms.py 2015-10-30 23:20:09 +0000
@@ -1526,7 +1526,7 @@
1526 ip_range = IPRange(ip_range_low, ip_range_high)1526 ip_range = IPRange(ip_range_low, ip_range_high)
1527 if ip_range.size <= 1:1527 if ip_range.size <= 1:
1528 raise ValidationError(ERROR_MESSAGE_INVALID_RANGE)1528 raise ValidationError(ERROR_MESSAGE_INVALID_RANGE)
1529 except AddrFormatError:1529 except (AddrFormatError, ValueError):
1530 raise ValidationError(ERROR_MESSAGE_INVALID_RANGE)1530 raise ValidationError(ERROR_MESSAGE_INVALID_RANGE)
15311531
1532 # Allow any size of dynamic range for v6 networks, but limit v41532 # Allow any size of dynamic range for v6 networks, but limit v4
@@ -1569,7 +1569,7 @@
1569 ip_range = IPRange(static_ip_range_low, static_ip_range_high)1569 ip_range = IPRange(static_ip_range_low, static_ip_range_high)
1570 if ip_range.size <= 1:1570 if ip_range.size <= 1:
1571 raise ValidationError(ERROR_MESSAGE_INVALID_RANGE)1571 raise ValidationError(ERROR_MESSAGE_INVALID_RANGE)
1572 except AddrFormatError:1572 except (AddrFormatError, ValueError):
1573 raise ValidationError(ERROR_MESSAGE_INVALID_RANGE)1573 raise ValidationError(ERROR_MESSAGE_INVALID_RANGE)
15741574
1575 # Find any allocated addresses within the old static range which do1575 # Find any allocated addresses within the old static range which do
@@ -1936,9 +1936,17 @@
1936 ]1936 ]
1937 for field in fields_in_network:1937 for field in fields_in_network:
1938 ip = cleaned_data.get(field)1938 ip = cleaned_data.get(field)
1939 if is_set(ip) and IPAddress(ip) not in network:1939 if is_set(ip):
1940 msg = "%s not in the %s network" % (ip, unicode(network.cidr))1940 try:
1941 set_form_error(self, field, msg)1941 ipaddr = IPAddress(ip)
1942 except (AddrFormatError, ValueError):
1943 msg = "%s (%s) is not a valid address" % (field, ip)
1944 set_form_error(self, field, msg)
1945 else:
1946 if ipaddr not in network:
1947 msg = "%s not in the %s network" % (
1948 ip, unicode(network.cidr))
1949 set_form_error(self, field, msg)
19421950
1943 def clean_dependent_ip_ranges(self, cleaned_data):1951 def clean_dependent_ip_ranges(self, cleaned_data):
1944 dynamic_range_low = cleaned_data.get('ip_range_low')1952 dynamic_range_low = cleaned_data.get('ip_range_low')
@@ -1997,7 +2005,7 @@
1997 if range.size <= 1:2005 if range.size <= 1:
1998 set_form_error(self, 'static_ip_range_low', message)2006 set_form_error(self, 'static_ip_range_low', message)
1999 set_form_error(self, 'static_ip_range_high', message)2007 set_form_error(self, 'static_ip_range_high', message)
2000 except AddrFormatError:2008 except (AddrFormatError, ValueError):
2001 set_form_error(self, 'static_ip_range_low', message)2009 set_form_error(self, 'static_ip_range_low', message)
2002 set_form_error(self, 'static_ip_range_high', message)2010 set_form_error(self, 'static_ip_range_high', message)
2003 try:2011 try:
@@ -2007,7 +2015,7 @@
2007 if range.size <= 1:2015 if range.size <= 1:
2008 set_form_error(self, 'ip_range_low', message)2016 set_form_error(self, 'ip_range_low', message)
2009 set_form_error(self, 'ip_range_high', message)2017 set_form_error(self, 'ip_range_high', message)
2010 except AddrFormatError:2018 except (AddrFormatError, ValueError):
2011 set_form_error(self, 'ip_range_low', message)2019 set_form_error(self, 'ip_range_low', message)
2012 set_form_error(self, 'ip_range_high', message)2020 set_form_error(self, 'ip_range_high', message)
20132021
20142022
=== modified file 'src/maasserver/tests/test_forms_nodegroupinterface.py'
--- src/maasserver/tests/test_forms_nodegroupinterface.py 2015-10-29 23:23:30 +0000
+++ src/maasserver/tests/test_forms_nodegroupinterface.py 2015-10-30 23:20:09 +0000
@@ -431,6 +431,32 @@
431 self.assertFalse(form.is_valid())431 self.assertFalse(form.is_valid())
432 self.assertThat(form.errors[field], Contains(message))432 self.assertThat(form.errors[field], Contains(message))
433433
434 def test_reports_invalid_ip(self):
435 network = IPNetwork('192.168.0.3/24')
436 invalid_ip = '192.168.0.9/24'
437 checked_fields = [
438 'router_ip',
439 'ip_range_low',
440 'ip_range_high',
441 'static_ip_range_low',
442 'static_ip_range_high',
443 ]
444 for field in checked_fields:
445 nodegroup = factory.make_NodeGroup(
446 network=network, management=NODEGROUPINTERFACE_MANAGEMENT.DHCP)
447 [interface] = nodegroup.get_managed_interfaces()
448 form = NodeGroupInterfaceForm(
449 data={field: invalid_ip}, instance=interface)
450 if field == 'router_ip':
451 message = "%s (%s) is not a valid address" % (
452 field,
453 invalid_ip,
454 )
455 else:
456 message = "Enter a valid IPv4 or IPv6 address."
457 self.assertFalse(form.is_valid())
458 self.assertThat(form.errors[field], Contains(message))
459
434 def test_identifies_duplicate_fqdns_in_nodegroup(self):460 def test_identifies_duplicate_fqdns_in_nodegroup(self):
435 # Don't allow DNS management to be enabled when it would461 # Don't allow DNS management to be enabled when it would
436 # cause more than one node on the nodegroup to have the462 # cause more than one node on the nodegroup to have the