|
1 """ |
|
2 Test conf_dhcp |
|
3 """ |
|
4 |
|
5 import conf_dhcp as dhcpc, conf, addr |
|
6 |
|
7 import unittest |
|
8 from cStringIO import StringIO |
|
9 |
|
10 class TestConfDHCP (unittest.TestCase) : |
|
11 def assert_stmt (self, stmt, line) : |
|
12 """ |
|
13 Formats the given Statement, and compares the output against the given line |
|
14 """ |
|
15 |
|
16 self.assertEqual(stmt._fmt_data(), line) |
|
17 |
|
18 def test_statement (self) : |
|
19 self.assert_stmt(dhcpc.Statement("stmt0"), "stmt0") |
|
20 self.assert_stmt(dhcpc.Statement("stmt3", [ "this", "that" ]), "stmt3 this, that") |
|
21 self.assert_stmt(dhcpc.Statement("stmt4", dhcpc.Literal("...")), "stmt4 ...") |
|
22 self.assert_stmt(dhcpc.Statement("stmt1", u"quux"), "stmt1 quux") |
|
23 self.assert_stmt(dhcpc.Statement("stmt1", "bar"), "stmt1 bar") |
|
24 self.assert_stmt(dhcpc.Statement("stmt2", 1), "stmt2 1") |
|
25 |
|
26 def assert_obj (self, obj, lines) : |
|
27 """ |
|
28 Formats the given conf.Object and compares the output against the given lines |
|
29 """ |
|
30 |
|
31 self.assertEqual(list(obj.fmt_lines()), lines) |
|
32 |
|
33 def test_literal (self) : |
|
34 self.assert_obj(dhcpc.Literal("///"), [ "///" ]) |
|
35 |
|
36 def test_parameter (self) : |
|
37 self.assert_obj(dhcpc.Parameter("param0", "this", 13, "that"), [ "param0 this 13 that;" ]) |
|
38 |
|
39 def test_declaration (self) : |
|
40 self.assert_obj(dhcpc.Declaration("decl0", ["arg0", "arg1"], [ |
|
41 dhcpc.Parameter("param0") |
|
42 ], [ |
|
43 dhcpc.Declaration("decl0.0", params=[ |
|
44 dhcpc.Parameter("param0.0.1", "value") |
|
45 ]) |
|
46 ]), [ |
|
47 |
|
48 "decl0 arg0 arg1 {", |
|
49 "\tparam0;", |
|
50 "\tdecl0.0 {", |
|
51 "\t\tparam0.0.1 value;", |
|
52 "\t}", |
|
53 "}", |
|
54 ]) |
|
55 |
|
56 def test_shared_network (self) : |
|
57 self.assert_obj(dhcpc.SharedNetwork("net0", params=[ |
|
58 dhcpc.Parameter("param0") |
|
59 ]), [ |
|
60 "shared-network net0 {", |
|
61 "\tparam0;", |
|
62 "}" |
|
63 ]) |
|
64 |
|
65 def test_subnet (self) : |
|
66 self.assert_obj(dhcpc.Subnet(addr.Network("194.197.235.0/24"), params=[ |
|
67 dhcpc.Parameter("param0") |
|
68 ]), [ |
|
69 "subnet 194.197.235.0 netmask 255.255.255.0 {", |
|
70 "\tparam0;", |
|
71 "}" |
|
72 ]) |
|
73 |
|
74 def test_group (self) : |
|
75 self.assert_obj(dhcpc.Group(decls=[ |
|
76 dhcpc.Declaration("decl0.0", params=[ |
|
77 dhcpc.Parameter("param0.0.1", "value") |
|
78 ]) |
|
79 ]), [ |
|
80 "group {", |
|
81 "\tdecl0.0 {", |
|
82 "\t\tparam0.0.1 value;", |
|
83 "\t}", |
|
84 "}" |
|
85 ]) |
|
86 |
|
87 def test_host (self) : |
|
88 self.assert_obj(dhcpc.Host("test-hostname", params=[ |
|
89 dhcpc.Parameter("param0") |
|
90 ]), [ |
|
91 "host test-hostname {", |
|
92 "\tparam0;", |
|
93 "}" |
|
94 ]) |
|
95 |
|
96 def test_option (self) : |
|
97 self.assert_obj(dhcpc.Option("foo", "example.com"), [ |
|
98 "option foo example.com;", |
|
99 ]) |
|
100 |
|
101 if __name__ == '__main__' : |
|
102 unittest.main() |
|
103 |