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