Example #1
0
 def test_parse_empty_lease_file_errors(self):
     """parse_dhcp_lease_file errors when file content is empty."""
     empty_file = self.tmp_path('leases')
     ensure_file(empty_file)
     with self.assertRaises(InvalidDHCPLeaseFileError) as context_manager:
         parse_dhcp_lease_file(empty_file)
     error = context_manager.exception
     self.assertIn('Cannot parse empty dhcp lease file', str(error))
Example #2
0
 def test_parse_malformed_lease_file_content_errors(self):
     """parse_dhcp_lease_file errors when file content isn't dhcp leases."""
     non_lease_file = self.tmp_path('leases')
     write_file(non_lease_file, 'hi mom.')
     with self.assertRaises(InvalidDHCPLeaseFileError) as context_manager:
         parse_dhcp_lease_file(non_lease_file)
     error = context_manager.exception
     self.assertIn('Cannot parse dhcp lease file', str(error))
Example #3
0
 def test_parse_malformed_lease_file_content_errors(self):
     """parse_dhcp_lease_file errors when file content isn't dhcp leases."""
     non_lease_file = self.tmp_path('leases')
     write_file(non_lease_file, 'hi mom.')
     with self.assertRaises(InvalidDHCPLeaseFileError) as context_manager:
         parse_dhcp_lease_file(non_lease_file)
     error = context_manager.exception
     self.assertIn('Cannot parse dhcp lease file', str(error))
Example #4
0
 def test_parse_empty_lease_file_errors(self):
     """parse_dhcp_lease_file errors when file content is empty."""
     empty_file = self.tmp_path('leases')
     ensure_file(empty_file)
     with self.assertRaises(InvalidDHCPLeaseFileError) as context_manager:
         parse_dhcp_lease_file(empty_file)
     error = context_manager.exception
     self.assertIn('Cannot parse empty dhcp lease file', str(error))
Example #5
0
 def test_parse_lease_finds_classless_static_routes(self):
     """
     parse_dhcp_lease_file returns classless-static-routes
     for Centos lease format.
     """
     lease_file = self.tmp_path('leases')
     content = dedent("""
         lease {
           interface "wlp3s0";
           fixed-address 192.168.2.74;
           option subnet-mask 255.255.255.0;
           option routers 192.168.2.1;
           option classless-static-routes 0 130.56.240.1;
           renew 4 2017/07/27 18:02:30;
           expire 5 2017/07/28 07:08:15;
         }
     """)
     expected = [
         {'interface': 'wlp3s0', 'fixed-address': '192.168.2.74',
          'subnet-mask': '255.255.255.0', 'routers': '192.168.2.1',
          'classless-static-routes': '0 130.56.240.1',
          'renew': '4 2017/07/27 18:02:30',
          'expire': '5 2017/07/28 07:08:15'}]
     write_file(lease_file, content)
     self.assertCountEqual(expected, parse_dhcp_lease_file(lease_file))
Example #6
0
 def test_parse_multiple_leases(self):
     """parse_dhcp_lease_file returns a list of all leases within."""
     lease_file = self.tmp_path('leases')
     content = dedent("""
         lease {
           interface "wlp3s0";
           fixed-address 192.168.2.74;
           option subnet-mask 255.255.255.0;
           option routers 192.168.2.1;
           renew 4 2017/07/27 18:02:30;
           expire 5 2017/07/28 07:08:15;
         }
         lease {
           interface "wlp3s0";
           fixed-address 192.168.2.74;
           option subnet-mask 255.255.255.0;
           option routers 192.168.2.1;
         }
     """)
     expected = [
         {'interface': 'wlp3s0', 'fixed-address': '192.168.2.74',
          'subnet-mask': '255.255.255.0', 'routers': '192.168.2.1',
          'renew': '4 2017/07/27 18:02:30',
          'expire': '5 2017/07/28 07:08:15'},
         {'interface': 'wlp3s0', 'fixed-address': '192.168.2.74',
          'subnet-mask': '255.255.255.0', 'routers': '192.168.2.1'}]
     write_file(lease_file, content)
     self.assertCountEqual(expected, parse_dhcp_lease_file(lease_file))
Example #7
0
 def test_parse_lease_finds_rfc3442_classless_static_routes(self):
     """parse_dhcp_lease_file returns rfc3442-classless-static-routes."""
     lease_file = self.tmp_path("leases")
     content = dedent("""
         lease {
           interface "wlp3s0";
           fixed-address 192.168.2.74;
           option subnet-mask 255.255.255.0;
           option routers 192.168.2.1;
           option rfc3442-classless-static-routes 0,130,56,240,1;
           renew 4 2017/07/27 18:02:30;
           expire 5 2017/07/28 07:08:15;
         }
     """)
     expected = [{
         "interface": "wlp3s0",
         "fixed-address": "192.168.2.74",
         "subnet-mask": "255.255.255.0",
         "routers": "192.168.2.1",
         "rfc3442-classless-static-routes": "0,130,56,240,1",
         "renew": "4 2017/07/27 18:02:30",
         "expire": "5 2017/07/28 07:08:15",
     }]
     write_file(lease_file, content)
     self.assertCountEqual(expected, parse_dhcp_lease_file(lease_file))
Example #8
0
 def test_parse_multiple_leases(self):
     """parse_dhcp_lease_file returns a list of all leases within."""
     lease_file = self.tmp_path('leases')
     content = dedent("""
         lease {
           interface "wlp3s0";
           fixed-address 192.168.2.74;
           option subnet-mask 255.255.255.0;
           option routers 192.168.2.1;
           renew 4 2017/07/27 18:02:30;
           expire 5 2017/07/28 07:08:15;
         }
         lease {
           interface "wlp3s0";
           fixed-address 192.168.2.74;
           option subnet-mask 255.255.255.0;
           option routers 192.168.2.1;
         }
     """)
     expected = [
         {'interface': 'wlp3s0', 'fixed-address': '192.168.2.74',
          'subnet-mask': '255.255.255.0', 'routers': '192.168.2.1',
          'renew': '4 2017/07/27 18:02:30',
          'expire': '5 2017/07/28 07:08:15'},
         {'interface': 'wlp3s0', 'fixed-address': '192.168.2.74',
          'subnet-mask': '255.255.255.0', 'routers': '192.168.2.1'}]
     write_file(lease_file, content)
     self.assertItemsEqual(expected, parse_dhcp_lease_file(lease_file))
 def test_parse_multiple_leases(self):
     """parse_dhcp_lease_file returns a list of all leases within."""
     lease_file = self.tmp_path("leases")
     content = dedent(
         """
         lease {
           interface "wlp3s0";
           fixed-address 192.168.2.74;
           filename "http://192.168.2.50/boot.php?mac=${netX}";
           option subnet-mask 255.255.255.0;
           option routers 192.168.2.1;
           renew 4 2017/07/27 18:02:30;
           expire 5 2017/07/28 07:08:15;
         }
         lease {
           interface "wlp3s0";
           fixed-address 192.168.2.74;
           filename "http://192.168.2.50/boot.php?mac=${netX}";
           option subnet-mask 255.255.255.0;
           option routers 192.168.2.1;
         }
     """
     )
     expected = [
         {
             "interface": "wlp3s0",
             "fixed-address": "192.168.2.74",
             "subnet-mask": "255.255.255.0",
             "routers": "192.168.2.1",
             "renew": "4 2017/07/27 18:02:30",
             "expire": "5 2017/07/28 07:08:15",
             "filename": "http://192.168.2.50/boot.php?mac=${netX}",
         },
         {
             "interface": "wlp3s0",
             "fixed-address": "192.168.2.74",
             "filename": "http://192.168.2.50/boot.php?mac=${netX}",
             "subnet-mask": "255.255.255.0",
             "routers": "192.168.2.1",
         },
     ]
     write_file(lease_file, content)
     self.assertCountEqual(expected, parse_dhcp_lease_file(lease_file))