Merge lp:~jtv/maas/bug-1373261 into lp:~maas-committers/maas/trunk
- bug-1373261
- Merge into trunk
Status: | Merged |
---|---|
Approved by: | Jeroen T. Vermeulen |
Approved revision: | no longer in the source branch. |
Merged at revision: | 3352 |
Proposed branch: | lp:~jtv/maas/bug-1373261 |
Merge into: | lp:~maas-committers/maas/trunk |
Diff against target: |
503 lines (+476/-0) 3 files modified
src/provisioningserver/__main__.py (+2/-0) src/provisioningserver/configure_maas_url.py (+123/-0) src/provisioningserver/tests/test_configure_maas_url.py (+351/-0) |
To merge this branch: | bzr merge lp:~jtv/maas/bug-1373261 |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Gavin Panella (community) | Approve | ||
Review via email: mp+241123@code.launchpad.net |
Commit message
Python replacement for maas-cluster-
This is lots and lots more work than the old shell code, but it has two advantages: it's covered by the test suite, and it supports IPv6 addresses in URLs. These have a special syntax, and caused our shell code to corrupt pserv.yaml.
Description of the change
The problem with the shell code was that it mistook any colon (":") in the generator URL for the beginning of a port specifier. Thus, once you had an IPv6-based URL configured, reconfiguring for a different host would produce garbage in pserv.yaml.
While I was at it, I opted to handle both config files that this shell code rewrites. The one that needed fixing was the hard one anyway, so replacing a bit more shell code was a cheap bonus.
An accompanying packaging change will replace the postinst shell code with an invocation of the new maas-provision sub-command.
Jeroen
Jeroen T. Vermeulen (jtv) wrote : | # |
Thanks!
Preview Diff
1 | === modified file 'src/provisioningserver/__main__.py' | |||
2 | --- src/provisioningserver/__main__.py 2014-10-09 19:48:37 +0000 | |||
3 | +++ src/provisioningserver/__main__.py 2014-11-07 18:13:30 +0000 | |||
4 | @@ -17,6 +17,7 @@ | |||
5 | 17 | from provisioningserver import security | 17 | from provisioningserver import security |
6 | 18 | import provisioningserver.boot.install_bootloader | 18 | import provisioningserver.boot.install_bootloader |
7 | 19 | import provisioningserver.boot.install_grub | 19 | import provisioningserver.boot.install_grub |
8 | 20 | import provisioningserver.configure_maas_url | ||
9 | 20 | import provisioningserver.customize_config | 21 | import provisioningserver.customize_config |
10 | 21 | import provisioningserver.dhcp.writer | 22 | import provisioningserver.dhcp.writer |
11 | 22 | import provisioningserver.upgrade_cluster | 23 | import provisioningserver.upgrade_cluster |
12 | @@ -29,6 +30,7 @@ | |||
13 | 29 | script_commands = { | 30 | script_commands = { |
14 | 30 | 'atomic-write': AtomicWriteScript, | 31 | 'atomic-write': AtomicWriteScript, |
15 | 31 | 'check-for-shared-secret': security.CheckForSharedSecretScript, | 32 | 'check-for-shared-secret': security.CheckForSharedSecretScript, |
16 | 33 | 'configure-maas-url': provisioningserver.configure_maas_url, | ||
17 | 32 | 'customize-config': provisioningserver.customize_config, | 34 | 'customize-config': provisioningserver.customize_config, |
18 | 33 | 'generate-dhcp-config': provisioningserver.dhcp.writer, | 35 | 'generate-dhcp-config': provisioningserver.dhcp.writer, |
19 | 34 | 'install-shared-secret': security.InstallSharedSecretScript, | 36 | 'install-shared-secret': security.InstallSharedSecretScript, |
20 | 35 | 37 | ||
21 | === added file 'src/provisioningserver/configure_maas_url.py' | |||
22 | --- src/provisioningserver/configure_maas_url.py 1970-01-01 00:00:00 +0000 | |||
23 | +++ src/provisioningserver/configure_maas_url.py 2014-11-07 18:13:30 +0000 | |||
24 | @@ -0,0 +1,123 @@ | |||
25 | 1 | # Copyright 2014 Canonical Ltd. This software is licensed under the | ||
26 | 2 | # GNU Affero General Public License version 3 (see the file LICENSE). | ||
27 | 3 | |||
28 | 4 | """Management command: update `MAAS_URL`. | ||
29 | 5 | |||
30 | 6 | The MAAS cluster controller packaging calls this in order to set a new | ||
31 | 7 | "MAAS URL" (the URL where nodes and cluster controllers can reach the | ||
32 | 8 | region controller) in the cluster controller's configuration files. | ||
33 | 9 | """ | ||
34 | 10 | |||
35 | 11 | from __future__ import ( | ||
36 | 12 | absolute_import, | ||
37 | 13 | print_function, | ||
38 | 14 | unicode_literals, | ||
39 | 15 | ) | ||
40 | 16 | |||
41 | 17 | str = None | ||
42 | 18 | |||
43 | 19 | __metaclass__ = type | ||
44 | 20 | __all__ = [ | ||
45 | 21 | 'add_arguments', | ||
46 | 22 | 'run', | ||
47 | 23 | ] | ||
48 | 24 | |||
49 | 25 | from functools import partial | ||
50 | 26 | import re | ||
51 | 27 | from urlparse import urlparse | ||
52 | 28 | |||
53 | 29 | from provisioningserver.utils.fs import ( | ||
54 | 30 | atomic_write, | ||
55 | 31 | read_text_file, | ||
56 | 32 | ) | ||
57 | 33 | from provisioningserver.utils.url import compose_URL | ||
58 | 34 | |||
59 | 35 | |||
60 | 36 | MAAS_CLUSTER_CONF = '/etc/maas/maas_cluster.conf' | ||
61 | 37 | |||
62 | 38 | PSERV_YAML = '/etc/maas/pserv.yaml' | ||
63 | 39 | |||
64 | 40 | |||
65 | 41 | def rewrite_config_file(path, line_filter, mode=0600): | ||
66 | 42 | """Rewrite config file at `path` on a line-by-line basis. | ||
67 | 43 | |||
68 | 44 | Reads the file at `path`, runs its lines through `line_filter`, and | ||
69 | 45 | writes the result back to `path`. | ||
70 | 46 | |||
71 | 47 | Newlines may not be exactly as they were. A trailing newline is ensured. | ||
72 | 48 | |||
73 | 49 | :param path: Path to the config file to be rewritten. | ||
74 | 50 | :param line_filter: A callable which accepts a line of input text (without | ||
75 | 51 | trailing newline), and returns the corresponding line of output text | ||
76 | 52 | (also without trailing newline). | ||
77 | 53 | :param mode: File access permissions for the newly written file. | ||
78 | 54 | """ | ||
79 | 55 | input_lines = read_text_file(path).splitlines() | ||
80 | 56 | output_lines = [line_filter(line) for line in input_lines] | ||
81 | 57 | result = '%s\n' % '\n'.join(output_lines) | ||
82 | 58 | atomic_write(result, path, mode=mode) | ||
83 | 59 | |||
84 | 60 | |||
85 | 61 | def update_maas_cluster_conf(url): | ||
86 | 62 | """Update `MAAS_URL` in `/etc/maas/maas_cluster.conf`. | ||
87 | 63 | |||
88 | 64 | This file contains a shell-style assignment of the `MAAS_URL` | ||
89 | 65 | variable. Its assigned value will be changed to `url`. | ||
90 | 66 | """ | ||
91 | 67 | substitute_line = lambda line: ( | ||
92 | 68 | 'MAAS_URL="%s"' % url | ||
93 | 69 | if re.match('\s*MAAS_URL=', line) | ||
94 | 70 | else line) | ||
95 | 71 | rewrite_config_file(MAAS_CLUSTER_CONF, substitute_line, mode=0640) | ||
96 | 72 | |||
97 | 73 | |||
98 | 74 | def extract_host(url): | ||
99 | 75 | """Return just the host part of `url`.""" | ||
100 | 76 | return urlparse(url).hostname | ||
101 | 77 | |||
102 | 78 | |||
103 | 79 | def substitute_pserv_yaml_line(new_host, line): | ||
104 | 80 | match = re.match('(\s*generator:)\s+(\S*)(.*)$', line) | ||
105 | 81 | if match is None: | ||
106 | 82 | # Not the generator line. Keep as-is. | ||
107 | 83 | return line | ||
108 | 84 | [head, input_url, tail] = match.groups() | ||
109 | 85 | return "%s %s%s" % (head, compose_URL(input_url, new_host), tail) | ||
110 | 86 | |||
111 | 87 | |||
112 | 88 | def update_pserv_yaml(host): | ||
113 | 89 | """Update `generator` in `/etc/maas/pserv.yaml`. | ||
114 | 90 | |||
115 | 91 | This file contains a YAML line defining a `generator` URL. The line must | ||
116 | 92 | look something like:: | ||
117 | 93 | |||
118 | 94 | generator: http://10.9.8.7/MAAS/api/1.0/pxeconfig/ | ||
119 | 95 | |||
120 | 96 | The host part of the URL (in this example, `10.9.8.7`) will be replaced | ||
121 | 97 | with the new `host`. If `host` is an IPv6 address, this function will | ||
122 | 98 | ensure that it is surrounded by square brackets. | ||
123 | 99 | """ | ||
124 | 100 | substitute_line = partial(substitute_pserv_yaml_line, host) | ||
125 | 101 | rewrite_config_file(PSERV_YAML, substitute_line, mode=0644) | ||
126 | 102 | |||
127 | 103 | |||
128 | 104 | def add_arguments(parser): | ||
129 | 105 | """Add this command's options to the `ArgumentParser`. | ||
130 | 106 | |||
131 | 107 | Specified by the `ActionScript` interface. | ||
132 | 108 | """ | ||
133 | 109 | parser.add_argument( | ||
134 | 110 | 'maas_url', metavar='URL', | ||
135 | 111 | help=( | ||
136 | 112 | "URL where nodes and cluster controllers can reach the MAAS " | ||
137 | 113 | "region controller.")) | ||
138 | 114 | |||
139 | 115 | |||
140 | 116 | def run(args): | ||
141 | 117 | """Update MAAS_URL setting in configuration files. | ||
142 | 118 | |||
143 | 119 | For use by the MAAS packaging scripts. Updates configuration files | ||
144 | 120 | to reflect a new MAAS_URL setting. | ||
145 | 121 | """ | ||
146 | 122 | update_maas_cluster_conf(args.maas_url) | ||
147 | 123 | update_pserv_yaml(extract_host(args.maas_url)) | ||
148 | 0 | 124 | ||
149 | === added file 'src/provisioningserver/tests/test_configure_maas_url.py' | |||
150 | --- src/provisioningserver/tests/test_configure_maas_url.py 1970-01-01 00:00:00 +0000 | |||
151 | +++ src/provisioningserver/tests/test_configure_maas_url.py 2014-11-07 18:13:30 +0000 | |||
152 | @@ -0,0 +1,351 @@ | |||
153 | 1 | # Copyright 2014 Canonical Ltd. This software is licensed under the | ||
154 | 2 | # GNU Affero General Public License version 3 (see the file LICENSE). | ||
155 | 3 | |||
156 | 4 | """Tests for `MAAS_URL` configuration update code.""" | ||
157 | 5 | |||
158 | 6 | from __future__ import ( | ||
159 | 7 | absolute_import, | ||
160 | 8 | print_function, | ||
161 | 9 | unicode_literals, | ||
162 | 10 | ) | ||
163 | 11 | |||
164 | 12 | str = None | ||
165 | 13 | |||
166 | 14 | __metaclass__ = type | ||
167 | 15 | __all__ = [] | ||
168 | 16 | |||
169 | 17 | from argparse import ArgumentParser | ||
170 | 18 | from random import randint | ||
171 | 19 | from textwrap import dedent | ||
172 | 20 | |||
173 | 21 | from maastesting.factory import factory | ||
174 | 22 | from maastesting.matchers import ( | ||
175 | 23 | MockAnyCall, | ||
176 | 24 | MockCalledOnceWith, | ||
177 | 25 | ) | ||
178 | 26 | from maastesting.testcase import MAASTestCase | ||
179 | 27 | from mock import ( | ||
180 | 28 | ANY, | ||
181 | 29 | Mock, | ||
182 | 30 | ) | ||
183 | 31 | from provisioningserver import configure_maas_url | ||
184 | 32 | from provisioningserver.configure_maas_url import substitute_pserv_yaml_line | ||
185 | 33 | from testtools.matchers import ( | ||
186 | 34 | FileContains, | ||
187 | 35 | StartsWith, | ||
188 | 36 | ) | ||
189 | 37 | |||
190 | 38 | |||
191 | 39 | class TestRewriteConfigFile(MAASTestCase): | ||
192 | 40 | |||
193 | 41 | def test__rewrites_file(self): | ||
194 | 42 | path = self.make_file(contents='foo\n') | ||
195 | 43 | configure_maas_url.rewrite_config_file(path, lambda line: 'bar') | ||
196 | 44 | self.assertThat(path, FileContains('bar\n')) | ||
197 | 45 | |||
198 | 46 | def test__sets_access_permissions(self): | ||
199 | 47 | writer = self.patch(configure_maas_url, 'atomic_write') | ||
200 | 48 | mode = 0215 | ||
201 | 49 | path = self.make_file() | ||
202 | 50 | configure_maas_url.rewrite_config_file( | ||
203 | 51 | path, lambda line: line, mode=mode) | ||
204 | 52 | self.assertThat(writer, MockCalledOnceWith(ANY, path, mode=mode)) | ||
205 | 53 | |||
206 | 54 | def test__preserves_trailing_newline(self): | ||
207 | 55 | path = self.make_file(contents='x\n') | ||
208 | 56 | configure_maas_url.rewrite_config_file(path, lambda line: line) | ||
209 | 57 | self.assertThat(path, FileContains('x\n')) | ||
210 | 58 | |||
211 | 59 | def test__ensures_trailing_newline(self): | ||
212 | 60 | path = self.make_file(contents='x') | ||
213 | 61 | configure_maas_url.rewrite_config_file(path, lambda line: line) | ||
214 | 62 | self.assertThat(path, FileContains('x\n')) | ||
215 | 63 | |||
216 | 64 | |||
217 | 65 | class TestUpdateMAASClusterConf(MAASTestCase): | ||
218 | 66 | |||
219 | 67 | def patch_file(self, content): | ||
220 | 68 | """Inject a fake `/etc/maas/maas_cluster.conf`.""" | ||
221 | 69 | path = self.make_file(name='maas_cluster.conf', contents=content) | ||
222 | 70 | self.patch(configure_maas_url, 'MAAS_CLUSTER_CONF', path) | ||
223 | 71 | return path | ||
224 | 72 | |||
225 | 73 | def test__updates_realistic_file(self): | ||
226 | 74 | config_file = self.patch_file(dedent("""\ | ||
227 | 75 | # Leading comments. | ||
228 | 76 | MAAS_URL="http://10.9.8.7/MAAS" | ||
229 | 77 | CLUSTER_UUID="5d02950e-6318-8195-ac3e-e6ccb12673c5" | ||
230 | 78 | """)) | ||
231 | 79 | configure_maas_url.update_maas_cluster_conf('http://1.2.3.4/MAAS') | ||
232 | 80 | self.assertThat( | ||
233 | 81 | config_file, | ||
234 | 82 | FileContains(dedent("""\ | ||
235 | 83 | # Leading comments. | ||
236 | 84 | MAAS_URL="http://1.2.3.4/MAAS" | ||
237 | 85 | CLUSTER_UUID="5d02950e-6318-8195-ac3e-e6ccb12673c5" | ||
238 | 86 | """))) | ||
239 | 87 | |||
240 | 88 | def test__updates_quoted_value(self): | ||
241 | 89 | old_url = factory.make_url() | ||
242 | 90 | new_url = factory.make_url() | ||
243 | 91 | config_file = self.patch_file('MAAS_URL="%s"\n' % old_url) | ||
244 | 92 | configure_maas_url.update_maas_cluster_conf(new_url) | ||
245 | 93 | self.assertThat( | ||
246 | 94 | config_file, | ||
247 | 95 | FileContains('MAAS_URL="%s"\n' % new_url)) | ||
248 | 96 | |||
249 | 97 | def test__updates_unquoted_value(self): | ||
250 | 98 | old_url = factory.make_url() | ||
251 | 99 | new_url = factory.make_url() | ||
252 | 100 | config_file = self.patch_file('MAAS_URL=%s\n' % old_url) | ||
253 | 101 | configure_maas_url.update_maas_cluster_conf(new_url) | ||
254 | 102 | self.assertThat( | ||
255 | 103 | config_file, | ||
256 | 104 | FileContains('MAAS_URL="%s"\n' % new_url)) | ||
257 | 105 | |||
258 | 106 | def test__leaves_other_lines_unchanged(self): | ||
259 | 107 | old_content = '#MAAS_URL="%s"\n' % factory.make_url() | ||
260 | 108 | config_file = self.patch_file(old_content) | ||
261 | 109 | configure_maas_url.update_maas_cluster_conf(factory.make_url()) | ||
262 | 110 | self.assertThat(config_file, FileContains(old_content)) | ||
263 | 111 | |||
264 | 112 | |||
265 | 113 | class TestExtractHost(MAASTestCase): | ||
266 | 114 | |||
267 | 115 | def test__extracts_hostname(self): | ||
268 | 116 | host = factory.make_name('host').lower() | ||
269 | 117 | port = factory.pick_port() | ||
270 | 118 | self.assertEqual( | ||
271 | 119 | host, | ||
272 | 120 | configure_maas_url.extract_host('http://%s/path' % host)) | ||
273 | 121 | self.assertEqual( | ||
274 | 122 | host, | ||
275 | 123 | configure_maas_url.extract_host('http://%s:%d' % (host, port))) | ||
276 | 124 | |||
277 | 125 | def test__extracts_IPv4_address(self): | ||
278 | 126 | host = factory.make_ipv4_address() | ||
279 | 127 | port = factory.pick_port() | ||
280 | 128 | self.assertEqual( | ||
281 | 129 | host, | ||
282 | 130 | configure_maas_url.extract_host('http://%s' % host)) | ||
283 | 131 | self.assertEqual( | ||
284 | 132 | host, | ||
285 | 133 | configure_maas_url.extract_host('http://%s:%d' % (host, port))) | ||
286 | 134 | |||
287 | 135 | def test__extracts_IPv6_address(self): | ||
288 | 136 | host = factory.make_ipv6_address() | ||
289 | 137 | port = factory.pick_port() | ||
290 | 138 | self.assertEqual( | ||
291 | 139 | host, | ||
292 | 140 | configure_maas_url.extract_host('http://[%s]' % host)) | ||
293 | 141 | self.assertEqual( | ||
294 | 142 | host, | ||
295 | 143 | configure_maas_url.extract_host('http://[%s]:%d' % (host, port))) | ||
296 | 144 | |||
297 | 145 | def test__extracts_IPv6_address_with_zone_index(self): | ||
298 | 146 | host = ( | ||
299 | 147 | factory.make_ipv6_address() + | ||
300 | 148 | '%25' + | ||
301 | 149 | factory.make_name('zone').lower()) | ||
302 | 150 | port = factory.pick_port() | ||
303 | 151 | self.assertEqual( | ||
304 | 152 | host, | ||
305 | 153 | configure_maas_url.extract_host('http://[%s]' % host)) | ||
306 | 154 | self.assertEqual( | ||
307 | 155 | host, | ||
308 | 156 | configure_maas_url.extract_host('http://[%s]:%d' % (host, port))) | ||
309 | 157 | |||
310 | 158 | |||
311 | 159 | class TestSubstitutePservYamlLine(MAASTestCase): | ||
312 | 160 | |||
313 | 161 | def make_generator_line(self, url): | ||
314 | 162 | return " generator: %s" % url | ||
315 | 163 | |||
316 | 164 | def test__replaces_hostname_generator_URL(self): | ||
317 | 165 | old_host = factory.make_name('old-host') | ||
318 | 166 | new_host = factory.make_name('new-host') | ||
319 | 167 | input_line = self.make_generator_line('http://%s' % old_host) | ||
320 | 168 | self.assertEqual( | ||
321 | 169 | self.make_generator_line('http://%s' % new_host), | ||
322 | 170 | substitute_pserv_yaml_line(new_host, input_line)) | ||
323 | 171 | |||
324 | 172 | def test__replaces_IPv4_generator_URL(self): | ||
325 | 173 | old_host = factory.make_ipv4_address() | ||
326 | 174 | new_host = factory.make_name('new-host') | ||
327 | 175 | input_line = self.make_generator_line('http://%s' % old_host) | ||
328 | 176 | self.assertEqual( | ||
329 | 177 | self.make_generator_line('http://%s' % new_host), | ||
330 | 178 | substitute_pserv_yaml_line(new_host, input_line)) | ||
331 | 179 | |||
332 | 180 | def test__replaces_IPv6_generator_URL(self): | ||
333 | 181 | old_host = factory.make_ipv6_address() | ||
334 | 182 | new_host = factory.make_name('new-host') | ||
335 | 183 | input_line = self.make_generator_line('http://[%s]' % old_host) | ||
336 | 184 | self.assertEqual( | ||
337 | 185 | self.make_generator_line('http://%s' % new_host), | ||
338 | 186 | substitute_pserv_yaml_line(new_host, input_line)) | ||
339 | 187 | |||
340 | 188 | def test__replaces_IPv6_generator_URL_with_zone_index(self): | ||
341 | 189 | old_host = ( | ||
342 | 190 | factory.make_ipv6_address() + | ||
343 | 191 | '%25' + | ||
344 | 192 | factory.make_name('zone') | ||
345 | 193 | ) | ||
346 | 194 | new_host = factory.make_name('new-host') | ||
347 | 195 | input_line = self.make_generator_line('http://[%s]' % old_host) | ||
348 | 196 | self.assertEqual( | ||
349 | 197 | self.make_generator_line('http://%s' % new_host), | ||
350 | 198 | substitute_pserv_yaml_line(new_host, input_line)) | ||
351 | 199 | |||
352 | 200 | def test__inserts_IPv6_with_brackets(self): | ||
353 | 201 | old_host = factory.make_name('old-host') | ||
354 | 202 | new_host = '[%s]' % factory.make_ipv6_address() | ||
355 | 203 | input_line = self.make_generator_line('http://%s' % old_host) | ||
356 | 204 | self.assertEqual( | ||
357 | 205 | self.make_generator_line('http://%s' % new_host), | ||
358 | 206 | substitute_pserv_yaml_line(new_host, input_line)) | ||
359 | 207 | |||
360 | 208 | def test__inserts_IPv6_without_brackets(self): | ||
361 | 209 | old_host = factory.make_name('old-host') | ||
362 | 210 | new_host = factory.make_ipv6_address() | ||
363 | 211 | input_line = self.make_generator_line('http://%s' % old_host) | ||
364 | 212 | self.assertEqual( | ||
365 | 213 | self.make_generator_line('http://[%s]' % new_host), | ||
366 | 214 | substitute_pserv_yaml_line(new_host, input_line)) | ||
367 | 215 | |||
368 | 216 | def test__preserves_port_after_simple_host(self): | ||
369 | 217 | port = factory.pick_port() | ||
370 | 218 | old_host = factory.make_name('old-host') | ||
371 | 219 | new_host = factory.make_name('new-host') | ||
372 | 220 | input_line = self.make_generator_line( | ||
373 | 221 | 'http://%s:%d' % (old_host, port)) | ||
374 | 222 | self.assertEqual( | ||
375 | 223 | self.make_generator_line('http://%s:%d' % (new_host, port)), | ||
376 | 224 | substitute_pserv_yaml_line(new_host, input_line)) | ||
377 | 225 | |||
378 | 226 | def test__preserves_port_with_IPv6(self): | ||
379 | 227 | port = factory.pick_port() | ||
380 | 228 | old_host = factory.make_ipv6_address() | ||
381 | 229 | new_host = factory.make_name('new-host') | ||
382 | 230 | input_line = self.make_generator_line( | ||
383 | 231 | 'http://[%s]:%d' % (old_host, port)) | ||
384 | 232 | self.assertEqual( | ||
385 | 233 | self.make_generator_line('http://%s:%d' % (new_host, port)), | ||
386 | 234 | substitute_pserv_yaml_line(new_host, input_line)) | ||
387 | 235 | |||
388 | 236 | def test__preserves_port_with_IPv6_and_zone_index(self): | ||
389 | 237 | port = factory.pick_port() | ||
390 | 238 | old_host = ( | ||
391 | 239 | factory.make_ipv6_address() + | ||
392 | 240 | '%25' + | ||
393 | 241 | factory.make_name('zone') | ||
394 | 242 | ) | ||
395 | 243 | new_host = factory.make_name('new-host') | ||
396 | 244 | input_line = self.make_generator_line( | ||
397 | 245 | 'http://[%s]:%d' % (old_host, port)) | ||
398 | 246 | self.assertEqual( | ||
399 | 247 | self.make_generator_line('http://%s:%d' % (new_host, port)), | ||
400 | 248 | substitute_pserv_yaml_line(new_host, input_line)) | ||
401 | 249 | |||
402 | 250 | def test__preserves_other_line(self): | ||
403 | 251 | line = '#' + self.make_generator_line(factory.make_url()) | ||
404 | 252 | self.assertEqual( | ||
405 | 253 | line, | ||
406 | 254 | substitute_pserv_yaml_line(factory.make_name('host'), line)) | ||
407 | 255 | |||
408 | 256 | def test__preserves_indentation(self): | ||
409 | 257 | spaces = ' ' * randint(0, 10) | ||
410 | 258 | input_line = spaces + 'generator: %s' % factory.make_url() | ||
411 | 259 | output_line = substitute_pserv_yaml_line( | ||
412 | 260 | factory.make_name('host'), input_line) | ||
413 | 261 | self.assertThat(output_line, StartsWith(spaces + 'generator:')) | ||
414 | 262 | |||
415 | 263 | def test__preserves_trailing_comments(self): | ||
416 | 264 | comment = " # Trailing comment." | ||
417 | 265 | old_host = factory.make_name('old-host') | ||
418 | 266 | new_host = factory.make_name('new-host') | ||
419 | 267 | input_line = self.make_generator_line('http://%s' % old_host) + comment | ||
420 | 268 | self.assertEqual( | ||
421 | 269 | self.make_generator_line('http://%s' % new_host) + comment, | ||
422 | 270 | substitute_pserv_yaml_line(new_host, input_line)) | ||
423 | 271 | |||
424 | 272 | |||
425 | 273 | class TestUpdatePservYaml(MAASTestCase): | ||
426 | 274 | |||
427 | 275 | def patch_file(self, content): | ||
428 | 276 | """Inject a fake `/etc/maas/pserv.yaml`.""" | ||
429 | 277 | path = self.make_file(name='pserv.yaml', contents=content) | ||
430 | 278 | self.patch(configure_maas_url, 'PSERV_YAML', path) | ||
431 | 279 | return path | ||
432 | 280 | |||
433 | 281 | def test__updates_realistic_file(self): | ||
434 | 282 | old_host = factory.make_name('old-host') | ||
435 | 283 | new_host = factory.make_name('new-host') | ||
436 | 284 | config_file = self.patch_file(dedent("""\ | ||
437 | 285 | ## TFTP configuration. | ||
438 | 286 | tftp: | ||
439 | 287 | ## The URL to be contacted to generate PXE configurations. | ||
440 | 288 | generator: http://%s/MAAS/api/1.0/pxeconfig/ | ||
441 | 289 | """) % old_host) | ||
442 | 290 | configure_maas_url.update_pserv_yaml(new_host) | ||
443 | 291 | self.assertThat( | ||
444 | 292 | config_file, | ||
445 | 293 | FileContains(dedent("""\ | ||
446 | 294 | ## TFTP configuration. | ||
447 | 295 | tftp: | ||
448 | 296 | ## The URL to be contacted to generate PXE configurations. | ||
449 | 297 | generator: http://%s/MAAS/api/1.0/pxeconfig/ | ||
450 | 298 | """) % new_host)) | ||
451 | 299 | |||
452 | 300 | |||
453 | 301 | class TestAddArguments(MAASTestCase): | ||
454 | 302 | |||
455 | 303 | def test__accepts_maas_url(self): | ||
456 | 304 | url = factory.make_url() | ||
457 | 305 | parser = ArgumentParser() | ||
458 | 306 | configure_maas_url.add_arguments(parser) | ||
459 | 307 | args = parser.parse_args([url]) | ||
460 | 308 | self.assertEqual(url, args.maas_url) | ||
461 | 309 | |||
462 | 310 | |||
463 | 311 | class TestRun(MAASTestCase): | ||
464 | 312 | |||
465 | 313 | def make_args(self, maas_url): | ||
466 | 314 | args = Mock() | ||
467 | 315 | args.maas_url = maas_url | ||
468 | 316 | return args | ||
469 | 317 | |||
470 | 318 | def patch_read_file(self): | ||
471 | 319 | return self.patch(configure_maas_url, 'read_text_file') | ||
472 | 320 | |||
473 | 321 | def patch_write_file(self): | ||
474 | 322 | return self.patch(configure_maas_url, 'atomic_write') | ||
475 | 323 | |||
476 | 324 | def test__updates_maas_cluster_conf(self): | ||
477 | 325 | reader = self.patch_read_file() | ||
478 | 326 | writer = self.patch_write_file() | ||
479 | 327 | url = factory.make_url() | ||
480 | 328 | configure_maas_url.run(self.make_args(url)) | ||
481 | 329 | self.assertThat(reader, MockAnyCall('/etc/maas/maas_cluster.conf')) | ||
482 | 330 | self.assertThat( | ||
483 | 331 | writer, | ||
484 | 332 | MockAnyCall(ANY, '/etc/maas/maas_cluster.conf', mode=0640)) | ||
485 | 333 | |||
486 | 334 | def test__updates_pserv_yaml(self): | ||
487 | 335 | reader = self.patch_read_file() | ||
488 | 336 | writer = self.patch_write_file() | ||
489 | 337 | url = factory.make_url() | ||
490 | 338 | configure_maas_url.run(self.make_args(url)) | ||
491 | 339 | self.assertThat(reader, MockAnyCall('/etc/maas/pserv.yaml')) | ||
492 | 340 | self.assertThat( | ||
493 | 341 | writer, | ||
494 | 342 | MockAnyCall(ANY, '/etc/maas/pserv.yaml', mode=0644)) | ||
495 | 343 | |||
496 | 344 | def test__passes_host_to_update_pserv_yaml(self): | ||
497 | 345 | self.patch_read_file() | ||
498 | 346 | self.patch_write_file() | ||
499 | 347 | update_pserv_yaml = self.patch(configure_maas_url, 'update_pserv_yaml') | ||
500 | 348 | host = factory.make_name('host').lower() | ||
501 | 349 | url = factory.make_url(netloc=host) | ||
502 | 350 | configure_maas_url.run(self.make_args(url)) | ||
503 | 351 | self.assertThat(update_pserv_yaml, MockCalledOnceWith(host)) |
Nice :)