Merge lp:~jtv/maas/externalise-interfaces_by_mac 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: 3008
Proposed branch: lp:~jtv/maas/externalise-interfaces_by_mac
Merge into: lp:~maas-committers/maas/trunk
Diff against target: 126 lines (+29/-17)
2 files modified
etc/maas/templates/deployment-user-data/maas_configure_interfaces.py (+11/-3)
src/metadataserver/deployment/tests/test_maas_configure_interfaces.py (+18/-14)
To merge this branch: bzr merge lp:~jtv/maas/externalise-interfaces_by_mac
Reviewer Review Type Date Requested Status
Newell Jensen (community) Approve
Review via email: mp+234910@code.launchpad.net

Commit message

Preparatory refactoring in maas_configure_interfaces.py: move call to map_interfaces_by_mac out of configure_interfaces.

Description of the change

This lets me use the mapping for other top-level calls later. It also removes some patching from the tests.

Jeroen

To post a comment you must log in.
Revision history for this message
Newell Jensen (newell-jensen) wrote :

Looks good. Straightforward and nice that you were able to get rid of the patching in the tests.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'etc/maas/templates/deployment-user-data/maas_configure_interfaces.py'
2--- etc/maas/templates/deployment-user-data/maas_configure_interfaces.py 2014-09-16 10:02:55 +0000
3+++ etc/maas/templates/deployment-user-data/maas_configure_interfaces.py 2014-09-17 03:42:16 +0000
4@@ -201,10 +201,17 @@
5 rename(temp_file, path)
6
7
8-def configure_static_addresses(config_dir, ip_mac_pairs, gateway_mac_pairs):
9+def configure_static_addresses(config_dir, ip_mac_pairs, gateway_mac_pairs,
10+ interfaces_by_mac):
11 """Write interfaces config file for static addresses.
12
13 :param config_dir: Location of interfaces config directory.
14+ :param ip_mac_pairs: List of pairs, each of a static IP address and the
15+ MAC address for the network interface that should have that address.
16+ :param gateway_mac_pairs: List of pairs, each of a gateway address and a
17+ MAC address for the network interface that should use that gateway.
18+ :param interfaces_by_mac: Dict mapping each MAC address on the system to
19+ a list of network interfaces that have that MAC address.
20 :return: The list of affected network interfaces.
21 """
22 if not os.path.isfile(os.path.join(config_dir, 'interfaces')):
23@@ -212,7 +219,6 @@
24 "Not supported yet: This does not look like a "
25 "Debian/Ubuntu system. Not configuring networking.")
26 return []
27- interfaces_by_mac = map_interfaces_by_mac()
28 addresses_by_interface = map_addresses_by_interface(
29 interfaces_by_mac, ip_mac_pairs)
30 gateways_by_interface = map_addresses_by_interface(
31@@ -260,8 +266,10 @@
32
33 if __name__ == "__main__":
34 args = prepare_parser().parse_args()
35+ interfaces_by_mac = map_interfaces_by_mac()
36 interfaces = configure_static_addresses(
37- args.config_dir, args.static_ip, args.gateway)
38+ args.config_dir, ip_mac_pairs=args.static_ip,
39+ gateway_mac_pairs=args.gateway, interfaces_by_mac=interfaces_by_mac)
40 if len(interfaces) > 0:
41 if args.update_interfaces:
42 update_interfaces_file(args.config_dir)
43
44=== modified file 'src/metadataserver/deployment/tests/test_maas_configure_interfaces.py'
45--- src/metadataserver/deployment/tests/test_maas_configure_interfaces.py 2014-09-16 09:17:14 +0000
46+++ src/metadataserver/deployment/tests/test_maas_configure_interfaces.py 2014-09-17 03:42:16 +0000
47@@ -344,29 +344,30 @@
48 def patch_write_file(self):
49 return self.patch_autospec(script, 'write_file')
50
51- def patch_interfaces_by_mac(self, mapping):
52- patch = self.patch_autospec(script, 'map_interfaces_by_mac')
53- patch.return_value = mapping
54- return patch
55-
56 def test__skips_if_network_interfaces_does_not_exist(self):
57 config_dir = self.make_config_dir()
58 remove(os.path.join(config_dir, 'interfaces'))
59 write_file = self.patch_write_file()
60- result = script.configure_static_addresses(config_dir, [], [])
61+ result = script.configure_static_addresses(
62+ config_dir, ip_mac_pairs=[], gateway_mac_pairs=[],
63+ interfaces_by_mac={})
64 self.expectThat(result, Equals([]))
65 self.expectThat(write_file, MockNotCalled())
66
67 def test__skips_if_config_dir_does_not_exist(self):
68 config_dir = os.path.join(self.make_dir(), factory.make_name('nondir'))
69 write_file = self.patch_write_file()
70- result = script.configure_static_addresses(config_dir, [], [])
71+ result = script.configure_static_addresses(
72+ config_dir, ip_mac_pairs=[], gateway_mac_pairs=[],
73+ interfaces_by_mac={})
74 self.expectThat(result, Equals([]))
75 self.expectThat(write_file, MockNotCalled())
76
77 def test__writes_to_interfaces_d(self):
78 config_dir = self.make_config_dir()
79- script.configure_static_addresses(config_dir, [], [])
80+ script.configure_static_addresses(
81+ config_dir, ip_mac_pairs=[], gateway_mac_pairs=[],
82+ interfaces_by_mac={})
83 self.assertThat(
84 os.path.join(config_dir, 'interfaces.d', 'maas-config'),
85 FileExists())
86@@ -376,10 +377,11 @@
87 ip = factory.make_ipv6_address()
88 mac = factory.getRandomMACAddress()
89 interface = factory.make_name('eth')
90- self.patch_interfaces_by_mac({mac: [interface]})
91 config_dir = self.make_config_dir()
92
93- script.configure_static_addresses(config_dir, [(ip, mac)], [])
94+ script.configure_static_addresses(
95+ config_dir, ip_mac_pairs=[(ip, mac)], gateway_mac_pairs=[],
96+ interfaces_by_mac={mac: [interface]})
97
98 self.assertThat(write_file, MockCalledOnceWith(ANY, ANY))
99 [mock_call] = write_file.mock_calls
100@@ -393,20 +395,22 @@
101 interface = factory.make_name('eth')
102 config_dir = self.make_config_dir()
103 self.patch_write_file()
104- self.patch_interfaces_by_mac({mac: [interface]})
105 self.assertEqual(
106 [interface],
107- script.configure_static_addresses(config_dir, [(ip, mac)], []))
108+ script.configure_static_addresses(
109+ config_dir, ip_mac_pairs=[(ip, mac)], gateway_mac_pairs=[],
110+ interfaces_by_mac={mac: [interface]}))
111
112 def test__ignores_interfaces_without_addresses(self):
113 ip = factory.make_ipv6_address()
114 mac = factory.getRandomMACAddress()
115 config_dir = self.make_config_dir()
116 self.patch_write_file()
117- self.patch_interfaces_by_mac({})
118 self.assertEqual(
119 [],
120- script.configure_static_addresses(config_dir, [(ip, mac)], []))
121+ script.configure_static_addresses(
122+ config_dir, ip_mac_pairs=[(ip, mac)], gateway_mac_pairs=[],
123+ interfaces_by_mac={}))
124
125
126 class TestUpdateInterfacesFile(MAASTestCase):