Merge lp:~rvb/maas-test/bmc-params-3 into lp:maas-test

Proposed by Raphaël Badin
Status: Merged
Merged at revision: 49
Proposed branch: lp:~rvb/maas-test/bmc-params-3
Merge into: lp:maas-test
Prerequisite: lp:~rvb/maas-test/bmc-params-2
Diff against target: 145 lines (+76/-0)
2 files modified
maastest/kvmfixture.py (+22/-0)
maastest/tests/test_kvmfixture.py (+54/-0)
To merge this branch: bzr merge lp:~rvb/maas-test/bmc-params-3
Reviewer Review Type Date Requested Status
Julian Edwards (community) Disapprove
Graham Binns (community) Approve
Review via email: mp+196109@code.launchpad.net

Commit message

Add get_ip_from_network_scan utility.

Description of the change

This new utility is used to find the BMC's IP address. Yes, I reckon it's a bit cavalier to install 'nmap' every time the method is called but: a) once the package is installed, this really takes a second to return b) this method really should be used once (if that assumption becomes false, we might want to revisit this code).

To post a comment you must log in.
Revision history for this message
Graham Binns (gmb) wrote :

[0]
22 + self.install_packages(['nmap'])

I can't actually think of any reason not to do this when setting up the machine... although it's probably much of a muchness as long as this is only called once.

review: Approve
lp:~rvb/maas-test/bmc-params-3 updated
50. By Raphaël Badin

Install nmap when setting up the VM.

Revision history for this message
Raphaël Badin (rvb) wrote :

> [0]
> 22 + self.install_packages(['nmap'])
>
> I can't actually think of any reason not to do this when setting up the
> machine... although it's probably much of a muchness as long as this is only
> called once.

All right, I've fixed that… I might have been lazy :).
Thanks for the reviews!

lp:~rvb/maas-test/bmc-params-3 updated
51. By Raphaël Badin

Merge trunk, fix conflicts.

Revision history for this message
Julian Edwards (julian-edwards) wrote :

Again, nmap is verboten.

review: Disapprove

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'maastest/kvmfixture.py'
--- maastest/kvmfixture.py 2013-11-21 14:40:18 +0000
+++ maastest/kvmfixture.py 2013-11-21 14:40:19 +0000
@@ -27,6 +27,7 @@
27from lxml import etree27from lxml import etree
28from maastest.utils import (28from maastest.utils import (
29 binary_content,29 binary_content,
30 extract_mac_ip_mapping,
30 retries,31 retries,
31 run_command,32 run_command,
32 )33 )
@@ -133,6 +134,7 @@
133 self.wait_for_cloudinit()134 self.wait_for_cloudinit()
134 self.configure_network()135 self.configure_network()
135 self.configure_ppa()136 self.configure_ppa()
137 self.install_base_packages()
136 except:138 except:
137 self.destroy()139 self.destroy()
138 raise140 raise
@@ -310,6 +312,11 @@
310 self.run_command(312 self.run_command(
311 APT_GET_INSTALL + list(packages), check_call=True)313 APT_GET_INSTALL + list(packages), check_call=True)
312314
315 def install_base_packages(self):
316 """Install the packages needed for this instance to work."""
317 # Instal nmap so that 'get_ip_from_network_scan' can work.
318 self.install_packages(['nmap'])
319
313 def run_command(self, args, input=None, check_call=False):320 def run_command(self, args, input=None, check_call=False):
314 """Run the given command in the VM."""321 """Run the given command in the VM."""
315 args = self._make_ssh_command(args)322 args = self._make_ssh_command(args)
@@ -325,6 +332,21 @@
325 self.addDetail(cmd_prefix + ' stderr', binary_content(stderr))332 self.addDetail(cmd_prefix + ' stderr', binary_content(stderr))
326 return retcode, stdout, stderr333 return retcode, stdout, stderr
327334
335 def get_ip_from_network_scan(self, mac_address):
336 """Return the IP address associated with a MAC address.
337
338 The IP address is found by scanning the direct network using nmap.
339 """
340 network_repr = "%s" % self.direct_network
341 nmap_scan_cmd = ['sudo', 'nmap', '-sP', network_repr, '-oX', '-']
342 _, output, _ = self.run_command(
343 nmap_scan_cmd, check_call=True)
344 mapping = extract_mac_ip_mapping(output)
345 ip = mapping.get(mac_address.upper())
346 if ip is not None:
347 return ip
348 return None
349
328350
329KVM_TEMPLATE = """351KVM_TEMPLATE = """
330 <domain type='kvm'>352 <domain type='kvm'>
331353
=== modified file 'maastest/tests/test_kvmfixture.py'
--- maastest/tests/test_kvmfixture.py 2013-11-21 14:40:18 +0000
+++ maastest/tests/test_kvmfixture.py 2013-11-21 14:40:19 +0000
@@ -139,6 +139,10 @@
139 self.patch(kvmfixture.KVMFixture, 'import_image', mock_import_image)139 self.patch(kvmfixture.KVMFixture, 'import_image', mock_import_image)
140 mock_start = mock.MagicMock()140 mock_start = mock.MagicMock()
141 self.patch(kvmfixture.KVMFixture, 'start', mock_start)141 self.patch(kvmfixture.KVMFixture, 'start', mock_start)
142 mock_install_base_packages = mock.MagicMock()
143 self.patch(
144 kvmfixture.KVMFixture, 'install_base_packages',
145 mock_install_base_packages)
142 mock_configure_network = mock.MagicMock()146 mock_configure_network = mock.MagicMock()
143 self.patch(147 self.patch(
144 kvmfixture.KVMFixture, 'configure_network',148 kvmfixture.KVMFixture, 'configure_network',
@@ -156,12 +160,14 @@
156 mock.call(),160 mock.call(),
157 mock.call(),161 mock.call(),
158 mock.call(),162 mock.call(),
163 mock.call(),
159 ],164 ],
160 [165 [
161 mock_import_image.mock_calls,166 mock_import_image.mock_calls,
162 mock_start.mock_calls,167 mock_start.mock_calls,
163 mock_configure_network.mock_calls,168 mock_configure_network.mock_calls,
164 mock_configure_ppa.mock_calls,169 mock_configure_ppa.mock_calls,
170 mock_install_base_packages.mock_calls,
165 ])171 ])
166172
167 def test_setUp_awaits_boot_and_cloudinit_completion(self):173 def test_setUp_awaits_boot_and_cloudinit_completion(self):
@@ -502,6 +508,19 @@
502 kvmfixture.APT_GET_INSTALL + packages, check_call=True)],508 kvmfixture.APT_GET_INSTALL + packages, check_call=True)],
503 kvm_run_command.mock_calls)509 kvm_run_command.mock_calls)
504510
511 def test_install_base_packages(self):
512 self.patch_run_command()
513 fixture = self.make_KVMFixture('series', 'architecture')
514 kvm_run_command = mock.MagicMock(return_value=(0, '', ''))
515 self.patch(kvmfixture.KVMFixture, 'run_command', kvm_run_command)
516
517 fixture.install_base_packages()
518
519 self.assertEqual(
520 [mock.call(
521 kvmfixture.APT_GET_INSTALL + ['nmap'], check_call=True)],
522 kvm_run_command.mock_calls)
523
505 def test_run_command_runs_command_remotely(self):524 def test_run_command_runs_command_remotely(self):
506 fake_run_command = self.patch_run_command()525 fake_run_command = self.patch_run_command()
507 series = "test-series"526 series = "test-series"
@@ -552,6 +571,41 @@
552 details['cmd #0001 stderr'],571 details['cmd #0001 stderr'],
553 ))572 ))
554573
574 def test_get_ip_from_network_scan(self):
575 self.patch_run_command()
576 mac = self.getUniqueString()
577 ip = self.getUniqueString()
578 network_str = '192.168.12.0/24'
579 network = netaddr.IPNetwork(network_str)
580 fixture = self.make_KVMFixture(
581 'series', 'architecture', direct_network=network)
582 mapping = {mac.upper(): ip}
583 extract_mac_ip_mapping_mock = mock.MagicMock(return_value=mapping)
584 xml_output = self.getUniqueString()
585 self.patch(
586 kvmfixture, 'extract_mac_ip_mapping',
587 extract_mac_ip_mapping_mock)
588 kvm_run_command = mock.MagicMock(return_value=(0, xml_output, ''))
589 self.patch(kvmfixture.KVMFixture, 'run_command', kvm_run_command)
590
591 returned_ip = fixture.get_ip_from_network_scan(mac)
592
593 self.assertEqual(
594 (
595 ip,
596 [
597 mock.call(
598 ['sudo', 'nmap', '-sP', network_str, '-oX', '-'],
599 check_call=True)
600 ],
601 [mock.call(xml_output)],
602 ),
603 (
604 returned_ip,
605 kvm_run_command.mock_calls,
606 extract_mac_ip_mapping_mock.mock_calls,
607 ))
608
555609
556def xml_normalize(snippet):610def xml_normalize(snippet):
557 parser = etree.XMLParser(remove_blank_text=True)611 parser = etree.XMLParser(remove_blank_text=True)

Subscribers

People subscribed via source and target branches