author | Tero Marttila <terom@fixme.fi> |
Thu, 02 Apr 2009 21:11:01 +0300 | |
changeset 3 | ff98fa9b84ce |
parent 2 | e66102ab7048 |
child 4 | 8b633782f02d |
permissions | -rwxr-xr-x |
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
1 |
#!/usr/bin/env python2.5 |
0 | 2 |
""" |
3 |
Test conf_dhcp |
|
4 |
""" |
|
5 |
||
2
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
6 |
import dhcp_conf as dhcpc, dhcp, addr |
0 | 7 |
|
8 |
import unittest |
|
9 |
||
2
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
10 |
class _TestConfObj (unittest.TestCase) : |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
11 |
def assert_obj (self, obj, lines) : |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
12 |
""" |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
13 |
Formats the given conf.Object and compares the output against the given lines |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
14 |
""" |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
15 |
|
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
16 |
for obj_line, line in zip(obj.fmt_lines(), lines) : |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
17 |
self.assertEqual(obj_line, line) |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
18 |
|
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
19 |
class TestDHCPConf (_TestConfObj) : |
0 | 20 |
def assert_stmt (self, stmt, line) : |
21 |
""" |
|
2
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
22 |
Formats the given Statement, and compares the output against the given line. |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
23 |
|
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
24 |
Note that the dhcpc.Statement doesn't have a working fmt_lines implementation. |
0 | 25 |
""" |
26 |
||
27 |
self.assertEqual(stmt._fmt_data(), line) |
|
28 |
||
2
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
29 |
def test_comment (self) : |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
30 |
self.assert_obj(dhcpc.Comment("foo bar"), [ "# foo bar" ]) |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
31 |
|
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
32 |
def test_section (self) : |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
33 |
self.assert_obj(dhcpc.Section(comment="test"), [ "# test" ]) |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
34 |
|
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
35 |
self.assert_obj(dhcpc.Section(params=[ |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
36 |
dhcpc.Parameter("param0"), None |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
37 |
], comment="foo"), [ |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
38 |
"# foo", |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
39 |
"param0;", |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
40 |
]) |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
41 |
|
0 | 42 |
def test_statement (self) : |
43 |
self.assert_stmt(dhcpc.Statement("stmt0"), "stmt0") |
|
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
44 |
self.assert_stmt(dhcpc.Statement("stmt1", [ "this", "that" ]), "stmt1 this, that") |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
45 |
self.assert_stmt(dhcpc.Statement("stmt2", dhcpc.Literal("...")), "stmt2 ...") |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
46 |
self.assert_stmt(dhcpc.Statement("stmt3", u"quux"), "stmt3 quux") |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
47 |
self.assert_stmt(dhcpc.Statement("stmt4", "bar"), "stmt4 bar") |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
48 |
self.assert_stmt(dhcpc.Statement("stmt5", 1), "stmt5 1") |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
49 |
self.assert_stmt(dhcpc.Statement("stmt6", 1, None, 2), "stmt6 1 2") |
2
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
50 |
|
0 | 51 |
def test_literal (self) : |
52 |
self.assert_obj(dhcpc.Literal("///"), [ "///" ]) |
|
53 |
||
54 |
def test_parameter (self) : |
|
55 |
self.assert_obj(dhcpc.Parameter("param0", "this", 13, "that"), [ "param0 this 13 that;" ]) |
|
3 | 56 |
self.assert_obj(dhcpc.Parameter("param1", comment="testing"), [ "# testing", "param1;" ]) |
0 | 57 |
|
58 |
def test_declaration (self) : |
|
59 |
self.assert_obj(dhcpc.Declaration("decl0", ["arg0", "arg1"], [ |
|
1
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
60 |
dhcpc.Parameter("param0"), |
2223ade4f259
continue the overengineering effort, we are now able to generate dhcpd.conf files
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
61 |
None |
0 | 62 |
], [ |
63 |
dhcpc.Declaration("decl0.0", params=[ |
|
64 |
dhcpc.Parameter("param0.0.1", "value") |
|
65 |
]) |
|
3 | 66 |
], comment="foo"), [ |
67 |
"# foo", |
|
0 | 68 |
"decl0 arg0 arg1 {", |
69 |
"\tparam0;", |
|
70 |
"\tdecl0.0 {", |
|
71 |
"\t\tparam0.0.1 value;", |
|
72 |
"\t}", |
|
73 |
"}", |
|
74 |
]) |
|
75 |
||
76 |
def test_shared_network (self) : |
|
77 |
self.assert_obj(dhcpc.SharedNetwork("net0", params=[ |
|
78 |
dhcpc.Parameter("param0") |
|
79 |
]), [ |
|
80 |
"shared-network net0 {", |
|
81 |
"\tparam0;", |
|
82 |
"}" |
|
83 |
]) |
|
84 |
||
85 |
def test_subnet (self) : |
|
86 |
self.assert_obj(dhcpc.Subnet(addr.Network("194.197.235.0/24"), params=[ |
|
87 |
dhcpc.Parameter("param0") |
|
88 |
]), [ |
|
89 |
"subnet 194.197.235.0 netmask 255.255.255.0 {", |
|
90 |
"\tparam0;", |
|
91 |
"}" |
|
92 |
]) |
|
93 |
||
94 |
def test_group (self) : |
|
95 |
self.assert_obj(dhcpc.Group(decls=[ |
|
96 |
dhcpc.Declaration("decl0.0", params=[ |
|
97 |
dhcpc.Parameter("param0.0.1", "value") |
|
98 |
]) |
|
99 |
]), [ |
|
100 |
"group {", |
|
101 |
"\tdecl0.0 {", |
|
102 |
"\t\tparam0.0.1 value;", |
|
103 |
"\t}", |
|
104 |
"}" |
|
105 |
]) |
|
106 |
||
107 |
def test_host (self) : |
|
108 |
self.assert_obj(dhcpc.Host("test-hostname", params=[ |
|
109 |
dhcpc.Parameter("param0") |
|
110 |
]), [ |
|
111 |
"host test-hostname {", |
|
112 |
"\tparam0;", |
|
113 |
"}" |
|
114 |
]) |
|
115 |
||
116 |
def test_option (self) : |
|
117 |
self.assert_obj(dhcpc.Option("foo", "example.com"), [ |
|
118 |
"option foo example.com;", |
|
119 |
]) |
|
120 |
||
2
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
121 |
class TestDHCP (_TestConfObj) : |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
122 |
def test_host (self) : |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
123 |
self.assert_obj(dhcp.Host("testhost", addr.MAC("12:34:56:78:90:ab"), addr.IP("1.2.3.4")), [ |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
124 |
"host testhost {", |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
125 |
"\thardware ethernet 12:34:56:78:90:ab;", |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
126 |
"\tfixed-address 1.2.3.4;", |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
127 |
"}" |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
128 |
]) |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
129 |
|
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
130 |
def test_subnet (self) : |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
131 |
self.assert_obj(dhcp.Subnet(addr.Network("1.2.3.0/24")), [ |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
132 |
"subnet 1.2.3.0 netmask 255.255.255.0 {", |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
133 |
"\toption routers 1.2.3.1;", |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
134 |
"}" |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
135 |
]) |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
136 |
|
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
137 |
self.assert_obj(dhcp.Subnet(addr.Network("1.2.3.0/24"), router_idx=10, range=(20, 30), unknown_clients='allow'), [ |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
138 |
"subnet 1.2.3.0 netmask 255.255.255.0 {", |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
139 |
"\toption routers 1.2.3.10;", |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
140 |
"\trange 1.2.3.20 1.2.3.30;", |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
141 |
"\tallow unknown-clients;", |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
142 |
"}" |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
143 |
]) |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
144 |
|
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
145 |
def test_config (self) : |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
146 |
self.assert_obj(dhcp.Config( |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
147 |
settings = { 'foo-setting': 'someval' }, |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
148 |
options = { 'bar-opt': ['one', 'two'] }, |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
149 |
shared_network = "FOO-NET", |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
150 |
subnets = [ |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
151 |
dhcp.Subnet(addr.Network("1.2.3.0/24")) |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
152 |
], |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
153 |
hosts = [ |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
154 |
dhcp.Host("testhost", addr.MAC("12:34:56:78:90:ab"), addr.IP("1.2.3.4")) |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
155 |
], |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
156 |
), [ |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
157 |
"foo-setting someval;", |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
158 |
"option bar-opt one, two;", |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
159 |
"shared-network FOO-NET {", |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
160 |
"\tsubnet 1.2.3.0 netmask 255.255.255.0 {", |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
161 |
"\t\toption routers 1.2.3.1;", |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
162 |
"\t}", |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
163 |
"}", |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
164 |
"host testhost {", |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
165 |
"\thardware ethernet 12:34:56:78:90:ab;", |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
166 |
"\tfixed-address 1.2.3.4;", |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
167 |
"}" |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
168 |
]) |
e66102ab7048
fix up data.load_py, and make conf.File be a ConfObject itself - implement this for dhcp_conf
Tero Marttila <terom@fixme.fi>
parents:
1
diff
changeset
|
169 |
|
0 | 170 |
if __name__ == '__main__' : |
171 |
unittest.main() |
|
172 |