Merge lp:~jtv/maas/no-sets-across-rpc into lp:~maas-committers/maas/trunk

Proposed by Jeroen T. Vermeulen
Status: Merged
Approved by: Jeroen T. Vermeulen
Approved revision: no longer in the source branch.
Merged at revision: 3170
Proposed branch: lp:~jtv/maas/no-sets-across-rpc
Merge into: lp:~maas-committers/maas/trunk
Diff against target: 138 lines (+31/-22)
2 files modified
src/maasserver/networking_preseed.py (+4/-1)
src/maasserver/tests/test_networking_preseed.py (+27/-21)
To merge this branch: bzr merge lp:~jtv/maas/no-sets-across-rpc
Reviewer Review Type Date Requested Status
Raphaël Badin (community) Approve
Review via email: mp+236812@code.launchpad.net

Commit message

When generating Curtin network preseeds, map MACs to lists, not sets, of IPs.

These mappings will be sent across RPC, serialised as JSON. Sets won't serialise.

Description of the change

I figured this might come, but I was hoping that it might not. Anyway, the difference isn't so great, and it does simplify my data structures a bit further.

Jeroen

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

Thanks! There was a reason for the two assertions, see below.

Revision history for this message
Gavin Panella (allenap) wrote :

You could also do it by declaring a Set argument type:

class Set(amp.ListOf):
    def fromString(self, inString):
        objects = super(Set, amp.ListOf).fromString(inString)
        return set(objects)

Revision history for this message
Gavin Panella (allenap) wrote :

Ah, okay, I missed the bit about JSON.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/maasserver/networking_preseed.py'
2--- src/maasserver/networking_preseed.py 2014-10-01 15:34:52 +0000
3+++ src/maasserver/networking_preseed.py 2014-10-02 04:18:37 +0000
4@@ -266,8 +266,11 @@
5 be added to the mapping.
6 """
7 if ip not in (None, ''):
8+ ip = normalise_ip(ip)
9 mac = extract_mac_string(macaddress)
10- mapping.setdefault(mac, set()).add(normalise_ip(ip))
11+ ips = mapping.setdefault(mac, [])
12+ if ip not in ips:
13+ ips.append(ip)
14
15
16 def map_static_ips(node):
17
18=== modified file 'src/maasserver/tests/test_networking_preseed.py'
19--- src/maasserver/tests/test_networking_preseed.py 2014-10-01 15:34:52 +0000
20+++ src/maasserver/tests/test_networking_preseed.py 2014-10-02 04:18:37 +0000
21@@ -546,7 +546,7 @@
22 ip = factory.make_ipv4_address()
23 mapping = {}
24 add_ip_to_mapping(mapping, mac, ip)
25- self.assertEqual({mac.mac_address: {ip}}, mapping)
26+ self.assertEqual({mac.mac_address: [ip]}, mapping)
27
28 def test__adds_to_nonempty_entry(self):
29 mapping = {}
30@@ -555,7 +555,15 @@
31 add_ip_to_mapping(mapping, mac, ip1)
32 ip2 = factory.make_ipv4_address()
33 add_ip_to_mapping(mapping, mac, ip2)
34- self.assertEqual({mac.mac_address: {ip1, ip2}}, mapping)
35+ self.assertItemsEqual([ip1, ip2], mapping[mac.mac_address])
36+
37+ def test__will_not_add_duplicate(self):
38+ mac = factory.make_MACAddress()
39+ ip = factory.make_ipv4_address()
40+ mapping = {mac.mac_address: [ip]}
41+ original_mapping = mapping.copy()
42+ add_ip_to_mapping(mapping, mac, ip)
43+ self.assertEqual(original_mapping, mapping)
44
45 def test__does_not_add_None(self):
46 mac = factory.make_MACAddress()
47@@ -581,7 +589,7 @@
48 ip = factory.make_ipv4_address()
49 factory.make_StaticIPAddress(ip=ip, mac=mac)
50 self.assertEqual(
51- {mac.mac_address: {ip}},
52+ {mac.mac_address: [ip]},
53 map_static_ips(node))
54
55 def test__finds_IPv6_address(self):
56@@ -590,7 +598,7 @@
57 ip = factory.make_ipv6_address()
58 factory.make_StaticIPAddress(ip=ip, mac=mac)
59 self.assertEqual(
60- {mac.mac_address: {ip}},
61+ {mac.mac_address: [ip]},
62 map_static_ips(node))
63
64 def test__finds_addresses_on_multiple_MACs(self):
65@@ -603,8 +611,8 @@
66 factory.make_StaticIPAddress(ip=ip2, mac=mac2)
67 self.assertEqual(
68 {
69- mac1.mac_address: {ip1},
70- mac2.mac_address: {ip2},
71+ mac1.mac_address: [ip1],
72+ mac2.mac_address: [ip2],
73 },
74 map_static_ips(node))
75
76@@ -615,9 +623,9 @@
77 ipv6 = factory.make_ipv6_address()
78 factory.make_StaticIPAddress(ip=ipv4, mac=mac)
79 factory.make_StaticIPAddress(ip=ipv6, mac=mac)
80- self.assertEqual(
81- {mac.mac_address: {ipv4, ipv6}},
82- map_static_ips(node))
83+ mapping = map_static_ips(node)
84+ self.assertItemsEqual([mac.mac_address], mapping.keys())
85+ self.assertItemsEqual([ipv4, ipv6], mapping[mac.mac_address])
86
87
88 class TestMapGateways(MAASServerTestCase):
89@@ -637,7 +645,7 @@
90 node=node, cluster_interface=cluster_interface)
91
92 self.assertEqual(
93- {mac.mac_address: {gateway}},
94+ {mac.mac_address: [gateway]},
95 map_gateways(node))
96
97 def test__finds_IPv6_gateway(self):
98@@ -657,7 +665,7 @@
99 node=node, cluster_interface=ipv4_interface)
100
101 self.assertEqual(
102- {mac.mac_address: {gateway}},
103+ {mac.mac_address: [gateway]},
104 map_gateways(node))
105
106 def test__finds_gateways_on_multiple_MACs(self):
107@@ -680,8 +688,8 @@
108
109 self.assertEqual(
110 {
111- mac1.mac_address: {gateway1},
112- mac2.mac_address: {gateway2},
113+ mac1.mac_address: [gateway1],
114+ mac2.mac_address: [gateway2],
115 },
116 map_gateways(node))
117
118@@ -704,14 +712,12 @@
119 mac = factory.make_MACAddress(
120 node=node, cluster_interface=ipv4_interface)
121
122- self.assertEqual(
123- {
124- mac.mac_address: {
125- ipv4_gateway,
126- ipv6_gateway,
127- },
128- },
129- map_gateways(node))
130+ mapping = map_gateways(node)
131+
132+ self.assertItemsEqual([mac.mac_address], mapping.keys())
133+ self.assertItemsEqual(
134+ [ipv4_gateway, ipv6_gateway],
135+ mapping[mac.mac_address])
136
137
138 class TestFindMACsForAutomaticInterfaces(MAASServerTestCase):