Merge lp:~julian-edwards/maas/dup-static-ip-bug-1338452 into lp:~maas-committers/maas/trunk

Proposed by Julian Edwards
Status: Merged
Approved by: Julian Edwards
Approved revision: no longer in the source branch.
Merged at revision: 2517
Proposed branch: lp:~julian-edwards/maas/dup-static-ip-bug-1338452
Merge into: lp:~maas-committers/maas/trunk
Diff against target: 41 lines (+19/-1)
2 files modified
src/maasserver/models/staticipaddress.py (+9/-1)
src/maasserver/models/tests/test_staticipaddress.py (+10/-0)
To merge this branch: bzr merge lp:~julian-edwards/maas/dup-static-ip-bug-1338452
Reviewer Review Type Date Requested Status
Raphaël Badin (community) Approve
Review via email: mp+225779@code.launchpad.net

Commit message

Ensure that the same static IP address cannot be re-issued. A bug in Django that forces a HOST cast around IP addresses in SQL queries was causing alphabetical comparison instead of IP comparison.

To post a comment you must log in.
Revision history for this message
Raphaël Badin (rvb) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/maasserver/models/staticipaddress.py'
2--- src/maasserver/models/staticipaddress.py 2014-07-04 07:27:05 +0000
3+++ src/maasserver/models/staticipaddress.py 2014-07-07 07:53:06 +0000
4@@ -81,7 +81,15 @@
5 static_range = IPRange(range_low, range_high)
6 # When we do ipv6, this needs changing.
7 range_list = [ip.ipv4().format() for ip in static_range]
8- existing = self.filter(ip__gte=range_low, ip__lte=range_high)
9+
10+ # We're using a raw query to bypass a bug in Django that inserts
11+ # a HOST() cast on the IP, causing the wrong comparison on the
12+ # IP field.
13+ # https://code.djangoproject.com/ticket/11442
14+ existing = StaticIPAddress.objects.raw("""
15+ SELECT * FROM maasserver_staticipaddress
16+ WHERE IP >= %s AND IP <= %s""",
17+ [range_low, range_high])
18
19 # Calculate the set of available IPs. This will be inefficient
20 # with large sets, but it will do for now.
21
22=== modified file 'src/maasserver/models/tests/test_staticipaddress.py'
23--- src/maasserver/models/tests/test_staticipaddress.py 2014-07-02 10:18:34 +0000
24+++ src/maasserver/models/tests/test_staticipaddress.py 2014-07-07 07:53:06 +0000
25@@ -66,6 +66,16 @@
26 AssertionError, StaticIPAddress.objects.allocate_new, low, high,
27 alloc_type=IPADDRESS_TYPE.USER_RESERVED)
28
29+ def test_allocate_new_compares_by_IP_not_alphabetically(self):
30+ # Django has a bug that casts IP addresses with HOST(), which
31+ # results in alphabetical comparisons of strings instead of IP
32+ # addresses. See https://bugs.launchpad.net/maas/+bug/1338452
33+ low = "10.0.0.98"
34+ high = "10.0.0.100"
35+ factory.make_staticipaddress("10.0.0.99")
36+ ipaddress = StaticIPAddress.objects.allocate_new(low, high)
37+ self.assertEqual(ipaddress.ip, "10.0.0.98")
38+
39 def test_deallocate_by_node_removes_addresses(self):
40 node = factory.make_node()
41 [mac1, mac2] = [