1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# This file is part of cloud-init. See LICENSE file for license information.
"""Tests for cloudinit.dhclient_hook."""
import argparse
import json
import os
from unittest import mock
from cloudinit import dhclient_hook as dhc
from tests.unittests.helpers import CiTestCase, dir2dict, populate_dir
class TestDhclientHook(CiTestCase):
ex_env = {
"interface": "eth0",
"new_dhcp_lease_time": "3600",
"new_host_name": "x1",
"new_ip_address": "10.145.210.163",
"new_subnet_mask": "255.255.255.0",
"old_host_name": "x1",
"PATH": "/usr/sbin:/usr/bin:/sbin:/bin",
"pid": "614",
"reason": "BOUND",
}
# some older versions of dhclient put the same content,
# but in upper case with DHCP4_ instead of new_
ex_env_dhcp4 = {
"REASON": "BOUND",
"DHCP4_dhcp_lease_time": "3600",
"DHCP4_host_name": "x1",
"DHCP4_ip_address": "10.145.210.163",
"DHCP4_subnet_mask": "255.255.255.0",
"INTERFACE": "eth0",
"PATH": "/usr/sbin:/usr/bin:/sbin:/bin",
"pid": "614",
}
expected = {
"dhcp_lease_time": "3600",
"host_name": "x1",
"ip_address": "10.145.210.163",
"subnet_mask": "255.255.255.0",
}
def setUp(self):
super(TestDhclientHook, self).setUp()
self.tmp = self.tmp_dir()
def test_handle_args(self):
"""quick test of call to handle_args."""
nic = "eth0"
args = argparse.Namespace(event=dhc.UP, interface=nic)
with mock.patch.dict("os.environ", clear=True, values=self.ex_env):
dhc.handle_args(dhc.NAME, args, data_d=self.tmp)
found = dir2dict(self.tmp + os.path.sep)
self.assertEqual([nic + ".json"], list(found.keys()))
self.assertEqual(self.expected, json.loads(found[nic + ".json"]))
def test_run_hook_up_creates_dir(self):
"""If dir does not exist, run_hook should create it."""
subd = self.tmp_path("subdir", self.tmp)
nic = "eth1"
dhc.run_hook(nic, "up", data_d=subd, env=self.ex_env)
self.assertEqual(
set([nic + ".json"]), set(dir2dict(subd + os.path.sep))
)
def test_run_hook_up(self):
"""Test expected use of run_hook_up."""
nic = "eth0"
dhc.run_hook(nic, "up", data_d=self.tmp, env=self.ex_env)
found = dir2dict(self.tmp + os.path.sep)
self.assertEqual([nic + ".json"], list(found.keys()))
self.assertEqual(self.expected, json.loads(found[nic + ".json"]))
def test_run_hook_up_dhcp4_prefix(self):
"""Test run_hook filters correctly with older DHCP4_ data."""
nic = "eth0"
dhc.run_hook(nic, "up", data_d=self.tmp, env=self.ex_env_dhcp4)
found = dir2dict(self.tmp + os.path.sep)
self.assertEqual([nic + ".json"], list(found.keys()))
self.assertEqual(self.expected, json.loads(found[nic + ".json"]))
def test_run_hook_down_deletes(self):
"""down should delete the created json file."""
nic = "eth1"
populate_dir(
self.tmp, {nic + ".json": "{'abcd'}", "myfile.txt": "text"}
)
dhc.run_hook(nic, "down", data_d=self.tmp, env={"old_host_name": "x1"})
self.assertEqual(
set(["myfile.txt"]), set(dir2dict(self.tmp + os.path.sep))
)
def test_get_parser(self):
"""Smoke test creation of get_parser."""
# cloud-init main uses 'action'.
event, interface = (dhc.UP, "mynic0")
self.assertEqual(
argparse.Namespace(
event=event,
interface=interface,
action=(dhc.NAME, dhc.handle_args),
),
dhc.get_parser().parse_args([event, interface]),
)
# vi: ts=4 expandtab
|