1 #!/usr/bin/env python2.5 |
1 #!/usr/bin/env python2.5 |
2 """ |
2 """ |
3 Test conf_dhcp |
3 Test conf_dhcp |
4 """ |
4 """ |
5 |
5 |
6 import dhcp_conf as dhcpc, conf, addr |
6 import dhcp_conf as dhcpc, dhcp, addr |
7 |
7 |
8 import unittest |
8 import unittest |
9 from cStringIO import StringIO |
|
10 |
9 |
11 class TestConfDHCP (unittest.TestCase) : |
10 class _TestConfObj (unittest.TestCase) : |
|
11 def assert_obj (self, obj, lines) : |
|
12 """ |
|
13 Formats the given conf.Object and compares the output against the given lines |
|
14 """ |
|
15 |
|
16 for obj_line, line in zip(obj.fmt_lines(), lines) : |
|
17 self.assertEqual(obj_line, line) |
|
18 |
|
19 class TestDHCPConf (_TestConfObj) : |
12 def assert_stmt (self, stmt, line) : |
20 def assert_stmt (self, stmt, line) : |
13 """ |
21 """ |
14 Formats the given Statement, and compares the output against the given line |
22 Formats the given Statement, and compares the output against the given line. |
|
23 |
|
24 Note that the dhcpc.Statement doesn't have a working fmt_lines implementation. |
15 """ |
25 """ |
16 |
26 |
17 self.assertEqual(stmt._fmt_data(), line) |
27 self.assertEqual(stmt._fmt_data(), line) |
18 |
28 |
|
29 def test_comment (self) : |
|
30 self.assert_obj(dhcpc.Comment("foo bar"), [ "# foo bar" ]) |
|
31 |
|
32 def test_section (self) : |
|
33 self.assert_obj(dhcpc.Section(comment="test"), [ "# test" ]) |
|
34 |
|
35 self.assert_obj(dhcpc.Section(params=[ |
|
36 dhcpc.Parameter("param0"), None |
|
37 ], comment="foo"), [ |
|
38 "# foo", |
|
39 "param0;", |
|
40 ]) |
|
41 |
19 def test_statement (self) : |
42 def test_statement (self) : |
20 self.assert_stmt(dhcpc.Statement("stmt0"), "stmt0") |
43 self.assert_stmt(dhcpc.Statement("stmt0"), "stmt0") |
21 self.assert_stmt(dhcpc.Statement("stmt1", [ "this", "that" ]), "stmt1 this, that") |
44 self.assert_stmt(dhcpc.Statement("stmt1", [ "this", "that" ]), "stmt1 this, that") |
22 self.assert_stmt(dhcpc.Statement("stmt2", dhcpc.Literal("...")), "stmt2 ...") |
45 self.assert_stmt(dhcpc.Statement("stmt2", dhcpc.Literal("...")), "stmt2 ...") |
23 self.assert_stmt(dhcpc.Statement("stmt3", u"quux"), "stmt3 quux") |
46 self.assert_stmt(dhcpc.Statement("stmt3", u"quux"), "stmt3 quux") |
24 self.assert_stmt(dhcpc.Statement("stmt4", "bar"), "stmt4 bar") |
47 self.assert_stmt(dhcpc.Statement("stmt4", "bar"), "stmt4 bar") |
25 self.assert_stmt(dhcpc.Statement("stmt5", 1), "stmt5 1") |
48 self.assert_stmt(dhcpc.Statement("stmt5", 1), "stmt5 1") |
26 self.assert_stmt(dhcpc.Statement("stmt6", 1, None, 2), "stmt6 1 2") |
49 self.assert_stmt(dhcpc.Statement("stmt6", 1, None, 2), "stmt6 1 2") |
27 |
50 |
28 def assert_obj (self, obj, lines) : |
|
29 """ |
|
30 Formats the given conf.Object and compares the output against the given lines |
|
31 """ |
|
32 |
|
33 self.assertEqual(list(obj.fmt_lines()), lines) |
|
34 |
|
35 def test_literal (self) : |
51 def test_literal (self) : |
36 self.assert_obj(dhcpc.Literal("///"), [ "///" ]) |
52 self.assert_obj(dhcpc.Literal("///"), [ "///" ]) |
37 |
53 |
38 def test_parameter (self) : |
54 def test_parameter (self) : |
39 self.assert_obj(dhcpc.Parameter("param0", "this", 13, "that"), [ "param0 this 13 that;" ]) |
55 self.assert_obj(dhcpc.Parameter("param0", "this", 13, "that"), [ "param0 this 13 that;" ]) |
99 def test_option (self) : |
115 def test_option (self) : |
100 self.assert_obj(dhcpc.Option("foo", "example.com"), [ |
116 self.assert_obj(dhcpc.Option("foo", "example.com"), [ |
101 "option foo example.com;", |
117 "option foo example.com;", |
102 ]) |
118 ]) |
103 |
119 |
|
120 class TestDHCP (_TestConfObj) : |
|
121 def test_host (self) : |
|
122 self.assert_obj(dhcp.Host("testhost", addr.MAC("12:34:56:78:90:ab"), addr.IP("1.2.3.4")), [ |
|
123 "host testhost {", |
|
124 "\thardware ethernet 12:34:56:78:90:ab;", |
|
125 "\tfixed-address 1.2.3.4;", |
|
126 "}" |
|
127 ]) |
|
128 |
|
129 def test_subnet (self) : |
|
130 self.assert_obj(dhcp.Subnet(addr.Network("1.2.3.0/24")), [ |
|
131 "subnet 1.2.3.0 netmask 255.255.255.0 {", |
|
132 "\toption routers 1.2.3.1;", |
|
133 "}" |
|
134 ]) |
|
135 |
|
136 self.assert_obj(dhcp.Subnet(addr.Network("1.2.3.0/24"), router_idx=10, range=(20, 30), unknown_clients='allow'), [ |
|
137 "subnet 1.2.3.0 netmask 255.255.255.0 {", |
|
138 "\toption routers 1.2.3.10;", |
|
139 "\trange 1.2.3.20 1.2.3.30;", |
|
140 "\tallow unknown-clients;", |
|
141 "}" |
|
142 ]) |
|
143 |
|
144 def test_config (self) : |
|
145 self.assert_obj(dhcp.Config( |
|
146 settings = { 'foo-setting': 'someval' }, |
|
147 options = { 'bar-opt': ['one', 'two'] }, |
|
148 shared_network = "FOO-NET", |
|
149 subnets = [ |
|
150 dhcp.Subnet(addr.Network("1.2.3.0/24")) |
|
151 ], |
|
152 hosts = [ |
|
153 dhcp.Host("testhost", addr.MAC("12:34:56:78:90:ab"), addr.IP("1.2.3.4")) |
|
154 ], |
|
155 ), [ |
|
156 "foo-setting someval;", |
|
157 "option bar-opt one, two;", |
|
158 "shared-network FOO-NET {", |
|
159 "\tsubnet 1.2.3.0 netmask 255.255.255.0 {", |
|
160 "\t\toption routers 1.2.3.1;", |
|
161 "\t}", |
|
162 "}", |
|
163 "host testhost {", |
|
164 "\thardware ethernet 12:34:56:78:90:ab;", |
|
165 "\tfixed-address 1.2.3.4;", |
|
166 "}" |
|
167 ]) |
|
168 |
104 if __name__ == '__main__' : |
169 if __name__ == '__main__' : |
105 unittest.main() |
170 unittest.main() |
106 |
171 |