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
1=== modified file 'maastest/kvmfixture.py'
2--- maastest/kvmfixture.py 2013-11-21 14:40:18 +0000
3+++ maastest/kvmfixture.py 2013-11-21 14:40:19 +0000
4@@ -27,6 +27,7 @@
5 from lxml import etree
6 from maastest.utils import (
7 binary_content,
8+ extract_mac_ip_mapping,
9 retries,
10 run_command,
11 )
12@@ -133,6 +134,7 @@
13 self.wait_for_cloudinit()
14 self.configure_network()
15 self.configure_ppa()
16+ self.install_base_packages()
17 except:
18 self.destroy()
19 raise
20@@ -310,6 +312,11 @@
21 self.run_command(
22 APT_GET_INSTALL + list(packages), check_call=True)
23
24+ def install_base_packages(self):
25+ """Install the packages needed for this instance to work."""
26+ # Instal nmap so that 'get_ip_from_network_scan' can work.
27+ self.install_packages(['nmap'])
28+
29 def run_command(self, args, input=None, check_call=False):
30 """Run the given command in the VM."""
31 args = self._make_ssh_command(args)
32@@ -325,6 +332,21 @@
33 self.addDetail(cmd_prefix + ' stderr', binary_content(stderr))
34 return retcode, stdout, stderr
35
36+ def get_ip_from_network_scan(self, mac_address):
37+ """Return the IP address associated with a MAC address.
38+
39+ The IP address is found by scanning the direct network using nmap.
40+ """
41+ network_repr = "%s" % self.direct_network
42+ nmap_scan_cmd = ['sudo', 'nmap', '-sP', network_repr, '-oX', '-']
43+ _, output, _ = self.run_command(
44+ nmap_scan_cmd, check_call=True)
45+ mapping = extract_mac_ip_mapping(output)
46+ ip = mapping.get(mac_address.upper())
47+ if ip is not None:
48+ return ip
49+ return None
50+
51
52 KVM_TEMPLATE = """
53 <domain type='kvm'>
54
55=== modified file 'maastest/tests/test_kvmfixture.py'
56--- maastest/tests/test_kvmfixture.py 2013-11-21 14:40:18 +0000
57+++ maastest/tests/test_kvmfixture.py 2013-11-21 14:40:19 +0000
58@@ -139,6 +139,10 @@
59 self.patch(kvmfixture.KVMFixture, 'import_image', mock_import_image)
60 mock_start = mock.MagicMock()
61 self.patch(kvmfixture.KVMFixture, 'start', mock_start)
62+ mock_install_base_packages = mock.MagicMock()
63+ self.patch(
64+ kvmfixture.KVMFixture, 'install_base_packages',
65+ mock_install_base_packages)
66 mock_configure_network = mock.MagicMock()
67 self.patch(
68 kvmfixture.KVMFixture, 'configure_network',
69@@ -156,12 +160,14 @@
70 mock.call(),
71 mock.call(),
72 mock.call(),
73+ mock.call(),
74 ],
75 [
76 mock_import_image.mock_calls,
77 mock_start.mock_calls,
78 mock_configure_network.mock_calls,
79 mock_configure_ppa.mock_calls,
80+ mock_install_base_packages.mock_calls,
81 ])
82
83 def test_setUp_awaits_boot_and_cloudinit_completion(self):
84@@ -502,6 +508,19 @@
85 kvmfixture.APT_GET_INSTALL + packages, check_call=True)],
86 kvm_run_command.mock_calls)
87
88+ def test_install_base_packages(self):
89+ self.patch_run_command()
90+ fixture = self.make_KVMFixture('series', 'architecture')
91+ kvm_run_command = mock.MagicMock(return_value=(0, '', ''))
92+ self.patch(kvmfixture.KVMFixture, 'run_command', kvm_run_command)
93+
94+ fixture.install_base_packages()
95+
96+ self.assertEqual(
97+ [mock.call(
98+ kvmfixture.APT_GET_INSTALL + ['nmap'], check_call=True)],
99+ kvm_run_command.mock_calls)
100+
101 def test_run_command_runs_command_remotely(self):
102 fake_run_command = self.patch_run_command()
103 series = "test-series"
104@@ -552,6 +571,41 @@
105 details['cmd #0001 stderr'],
106 ))
107
108+ def test_get_ip_from_network_scan(self):
109+ self.patch_run_command()
110+ mac = self.getUniqueString()
111+ ip = self.getUniqueString()
112+ network_str = '192.168.12.0/24'
113+ network = netaddr.IPNetwork(network_str)
114+ fixture = self.make_KVMFixture(
115+ 'series', 'architecture', direct_network=network)
116+ mapping = {mac.upper(): ip}
117+ extract_mac_ip_mapping_mock = mock.MagicMock(return_value=mapping)
118+ xml_output = self.getUniqueString()
119+ self.patch(
120+ kvmfixture, 'extract_mac_ip_mapping',
121+ extract_mac_ip_mapping_mock)
122+ kvm_run_command = mock.MagicMock(return_value=(0, xml_output, ''))
123+ self.patch(kvmfixture.KVMFixture, 'run_command', kvm_run_command)
124+
125+ returned_ip = fixture.get_ip_from_network_scan(mac)
126+
127+ self.assertEqual(
128+ (
129+ ip,
130+ [
131+ mock.call(
132+ ['sudo', 'nmap', '-sP', network_str, '-oX', '-'],
133+ check_call=True)
134+ ],
135+ [mock.call(xml_output)],
136+ ),
137+ (
138+ returned_ip,
139+ kvm_run_command.mock_calls,
140+ extract_mac_ip_mapping_mock.mock_calls,
141+ ))
142+
143
144 def xml_normalize(snippet):
145 parser = etree.XMLParser(remove_blank_text=True)

Subscribers

People subscribed via source and target branches