summaryrefslogtreecommitdiff
path: root/cloudinit/net
diff options
context:
space:
mode:
authorChad Smith <chad.smith@canonical.com>2017-11-21 11:43:26 -0700
committerChad Smith <chad.smith@canonical.com>2017-11-21 11:43:26 -0700
commit5b974bbab161e6cd73751bf27b7741f6b0d19051 (patch)
treef634411a9b12b2e36ff8beefbec39dad21cd45ea /cloudinit/net
parentc9c7ff70f55ee024dd54336f07ba52acec1f6929 (diff)
parent7624348712b4502f0085d30c05b34dce3f2ceeae (diff)
downloadvyos-cloud-init-5b974bbab161e6cd73751bf27b7741f6b0d19051.tar.gz
vyos-cloud-init-5b974bbab161e6cd73751bf27b7741f6b0d19051.zip
merge from 7624348712b4502f0085d30c05b34dce3f2ceeae at 17.1-41-g76243487
Diffstat (limited to 'cloudinit/net')
-rw-r--r--cloudinit/net/dhcp.py12
-rw-r--r--cloudinit/net/tests/test_dhcp.py9
2 files changed, 17 insertions, 4 deletions
diff --git a/cloudinit/net/dhcp.py b/cloudinit/net/dhcp.py
index 0cba7032..d8624d82 100644
--- a/cloudinit/net/dhcp.py
+++ b/cloudinit/net/dhcp.py
@@ -8,6 +8,7 @@ import configobj
import logging
import os
import re
+import signal
from cloudinit.net import find_fallback_nic, get_devicelist
from cloudinit import temp_utils
@@ -41,8 +42,7 @@ def maybe_perform_dhcp_discovery(nic=None):
if nic is None:
nic = find_fallback_nic()
if nic is None:
- LOG.debug(
- 'Skip dhcp_discovery: Unable to find fallback nic.')
+ LOG.debug('Skip dhcp_discovery: Unable to find fallback nic.')
return {}
elif nic not in get_devicelist():
LOG.debug(
@@ -119,7 +119,13 @@ def dhcp_discovery(dhclient_cmd_path, interface, cleandir):
cmd = [sandbox_dhclient_cmd, '-1', '-v', '-lf', lease_file,
'-pf', pid_file, interface, '-sf', '/bin/true']
util.subp(cmd, capture=True)
- return parse_dhcp_lease_file(lease_file)
+ pid = None
+ try:
+ pid = int(util.load_file(pid_file).strip())
+ return parse_dhcp_lease_file(lease_file)
+ finally:
+ if pid:
+ os.kill(pid, signal.SIGKILL)
def networkd_parse_lease(content):
diff --git a/cloudinit/net/tests/test_dhcp.py b/cloudinit/net/tests/test_dhcp.py
index 1c1f504a..3d8e15c0 100644
--- a/cloudinit/net/tests/test_dhcp.py
+++ b/cloudinit/net/tests/test_dhcp.py
@@ -2,6 +2,7 @@
import mock
import os
+import signal
from textwrap import dedent
from cloudinit.net.dhcp import (
@@ -114,8 +115,9 @@ class TestDHCPDiscoveryClean(CiTestCase):
self.assertEqual('eth9', call[0][1])
self.assertIn('/var/tmp/cloud-init/cloud-init-dhcp-', call[0][2])
+ @mock.patch('cloudinit.net.dhcp.os.kill')
@mock.patch('cloudinit.net.dhcp.util.subp')
- def test_dhcp_discovery_run_in_sandbox(self, m_subp):
+ def test_dhcp_discovery_run_in_sandbox(self, m_subp, m_kill):
"""dhcp_discovery brings up the interface and runs dhclient.
It also returns the parsed dhcp.leases file generated in the sandbox.
@@ -134,6 +136,10 @@ class TestDHCPDiscoveryClean(CiTestCase):
""")
lease_file = os.path.join(tmpdir, 'dhcp.leases')
write_file(lease_file, lease_content)
+ pid_file = os.path.join(tmpdir, 'dhclient.pid')
+ my_pid = 1
+ write_file(pid_file, "%d\n" % my_pid)
+
self.assertItemsEqual(
[{'interface': 'eth9', 'fixed-address': '192.168.2.74',
'subnet-mask': '255.255.255.0', 'routers': '192.168.2.1'}],
@@ -149,6 +155,7 @@ class TestDHCPDiscoveryClean(CiTestCase):
[os.path.join(tmpdir, 'dhclient'), '-1', '-v', '-lf',
lease_file, '-pf', os.path.join(tmpdir, 'dhclient.pid'),
'eth9', '-sf', '/bin/true'], capture=True)])
+ m_kill.assert_has_calls([mock.call(my_pid, signal.SIGKILL)])
class TestSystemdParseLeases(CiTestCase):