terom@1: #!/usr/bin/env python2.5 terom@0: """ terom@4: Test dhcp_conf terom@0: """ terom@0: terom@4: import dhcp_conf as dhcpc terom@4: import test_conf, dhcp, addr terom@0: terom@0: import unittest terom@0: terom@4: class TestDHCPConf (test_conf._TestConfBase) : terom@0: def assert_stmt (self, stmt, line) : terom@0: """ terom@2: Formats the given Statement, and compares the output against the given line. terom@2: terom@2: Note that the dhcpc.Statement doesn't have a working fmt_lines implementation. terom@0: """ terom@0: terom@0: self.assertEqual(stmt._fmt_data(), line) terom@0: terom@2: def test_comment (self) : terom@2: self.assert_obj(dhcpc.Comment("foo bar"), [ "# foo bar" ]) terom@2: terom@2: def test_section (self) : terom@2: self.assert_obj(dhcpc.Section(comment="test"), [ "# test" ]) terom@2: terom@2: self.assert_obj(dhcpc.Section(params=[ terom@2: dhcpc.Parameter("param0"), None terom@2: ], comment="foo"), [ terom@2: "# foo", terom@2: "param0;", terom@2: ]) terom@2: terom@0: def test_statement (self) : terom@0: self.assert_stmt(dhcpc.Statement("stmt0"), "stmt0") terom@1: self.assert_stmt(dhcpc.Statement("stmt1", [ "this", "that" ]), "stmt1 this, that") terom@1: self.assert_stmt(dhcpc.Statement("stmt2", dhcpc.Literal("...")), "stmt2 ...") terom@1: self.assert_stmt(dhcpc.Statement("stmt3", u"quux"), "stmt3 quux") terom@1: self.assert_stmt(dhcpc.Statement("stmt4", "bar"), "stmt4 bar") terom@1: self.assert_stmt(dhcpc.Statement("stmt5", 1), "stmt5 1") terom@1: self.assert_stmt(dhcpc.Statement("stmt6", 1, None, 2), "stmt6 1 2") terom@2: terom@0: def test_literal (self) : terom@0: self.assert_obj(dhcpc.Literal("///"), [ "///" ]) terom@0: terom@0: def test_parameter (self) : terom@0: self.assert_obj(dhcpc.Parameter("param0", "this", 13, "that"), [ "param0 this 13 that;" ]) terom@3: self.assert_obj(dhcpc.Parameter("param1", comment="testing"), [ "# testing", "param1;" ]) terom@0: terom@0: def test_declaration (self) : terom@0: self.assert_obj(dhcpc.Declaration("decl0", ["arg0", "arg1"], [ terom@1: dhcpc.Parameter("param0"), terom@1: None terom@0: ], [ terom@0: dhcpc.Declaration("decl0.0", params=[ terom@0: dhcpc.Parameter("param0.0.1", "value") terom@0: ]) terom@3: ], comment="foo"), [ terom@3: "# foo", terom@0: "decl0 arg0 arg1 {", terom@0: "\tparam0;", terom@0: "\tdecl0.0 {", terom@0: "\t\tparam0.0.1 value;", terom@0: "\t}", terom@0: "}", terom@0: ]) terom@0: terom@0: def test_shared_network (self) : terom@0: self.assert_obj(dhcpc.SharedNetwork("net0", params=[ terom@0: dhcpc.Parameter("param0") terom@0: ]), [ terom@0: "shared-network net0 {", terom@0: "\tparam0;", terom@0: "}" terom@0: ]) terom@0: terom@0: def test_subnet (self) : terom@0: self.assert_obj(dhcpc.Subnet(addr.Network("194.197.235.0/24"), params=[ terom@0: dhcpc.Parameter("param0") terom@0: ]), [ terom@0: "subnet 194.197.235.0 netmask 255.255.255.0 {", terom@0: "\tparam0;", terom@0: "}" terom@0: ]) terom@0: terom@0: def test_group (self) : terom@0: self.assert_obj(dhcpc.Group(decls=[ terom@0: dhcpc.Declaration("decl0.0", params=[ terom@0: dhcpc.Parameter("param0.0.1", "value") terom@0: ]) terom@0: ]), [ terom@0: "group {", terom@0: "\tdecl0.0 {", terom@0: "\t\tparam0.0.1 value;", terom@0: "\t}", terom@0: "}" terom@0: ]) terom@0: terom@0: def test_host (self) : terom@0: self.assert_obj(dhcpc.Host("test-hostname", params=[ terom@0: dhcpc.Parameter("param0") terom@0: ]), [ terom@0: "host test-hostname {", terom@0: "\tparam0;", terom@0: "}" terom@0: ]) terom@0: terom@0: def test_option (self) : terom@0: self.assert_obj(dhcpc.Option("foo", "example.com"), [ terom@0: "option foo example.com;", terom@0: ]) terom@0: terom@2: class TestDHCP (_TestConfObj) : terom@2: def test_host (self) : terom@4: self.assert_obj(dhcp.Host("testhost", addr.MAC("12:34:56:78:90:ab"), addr.IP("1.2.3.4"), comment="foo"), [ terom@4: "# foo", terom@2: "host testhost {", terom@2: "\thardware ethernet 12:34:56:78:90:ab;", terom@2: "\tfixed-address 1.2.3.4;", terom@2: "}" terom@2: ]) terom@2: terom@2: def test_subnet (self) : terom@4: self.assert_obj(dhcp.Subnet(addr.Network("1.2.3.0/24"), comment="bar"), [ terom@4: "# bar", terom@2: "subnet 1.2.3.0 netmask 255.255.255.0 {", terom@2: "\toption routers 1.2.3.1;", terom@2: "}" terom@2: ]) terom@2: terom@2: self.assert_obj(dhcp.Subnet(addr.Network("1.2.3.0/24"), router_idx=10, range=(20, 30), unknown_clients='allow'), [ terom@2: "subnet 1.2.3.0 netmask 255.255.255.0 {", terom@2: "\toption routers 1.2.3.10;", terom@2: "\trange 1.2.3.20 1.2.3.30;", terom@2: "\tallow unknown-clients;", terom@2: "}" terom@2: ]) terom@2: terom@2: def test_config (self) : terom@2: self.assert_obj(dhcp.Config( terom@2: settings = { 'foo-setting': 'someval' }, terom@2: options = { 'bar-opt': ['one', 'two'] }, terom@2: shared_network = "FOO-NET", terom@2: subnets = [ terom@2: dhcp.Subnet(addr.Network("1.2.3.0/24")) terom@2: ], terom@2: hosts = [ terom@2: dhcp.Host("testhost", addr.MAC("12:34:56:78:90:ab"), addr.IP("1.2.3.4")) terom@2: ], terom@2: ), [ terom@2: "foo-setting someval;", terom@2: "option bar-opt one, two;", terom@2: "shared-network FOO-NET {", terom@2: "\tsubnet 1.2.3.0 netmask 255.255.255.0 {", terom@2: "\t\toption routers 1.2.3.1;", terom@2: "\t}", terom@2: "}", terom@2: "host testhost {", terom@2: "\thardware ethernet 12:34:56:78:90:ab;", terom@2: "\tfixed-address 1.2.3.4;", terom@2: "}" terom@2: ]) terom@2: terom@0: if __name__ == '__main__' : terom@0: unittest.main() terom@0: