def test_redhat_format_with_a_space_too_much_after_comma(self): redhat_format = "24.191.168.128 192.168.128.1, 0 192.168.128.1" self.assertEqual( sorted([ ("191.168.128.0/24", "192.168.128.1"), ("0.0.0.0/0", "192.168.128.1"), ]), sorted(parse_static_routes(redhat_format)), )
def test_redhat_format(self): redhat_format = "24.191.168.128 192.168.128.1,0 192.168.128.1" self.assertEqual( sorted([ ("191.168.128.0/24", "192.168.128.1"), ("0.0.0.0/0", "192.168.128.1"), ]), sorted(parse_static_routes(redhat_format)), )
def test_parse_static_routes_class_c_b_a(self): class_c = "24,192,168,74,192,168,0,4" class_b = "16,172,16,172,16,0,4" class_a = "8,10,10,0,0,4" rfc3442 = ",".join([class_c, class_b, class_a]) self.assertEqual(sorted([ ("192.168.74.0/24", "192.168.0.4"), ("172.16.0.0/16", "172.16.0.4"), ("10.0.0.0/8", "10.0.0.4") ]), sorted(parse_static_routes(rfc3442)))
def test_parse_static_routes_returns_valid_routes_until_parse_err(self): class_c = "24,192,168,74,192,168,0,4" class_b = "16,172,16,172,16,0,4" class_a_error = "8,10,10,0,0" rfc3442 = ",".join([class_c, class_b, class_a_error]) self.assertEqual(sorted([ ("192.168.74.0/24", "192.168.0.4"), ("172.16.0.0/16", "172.16.0.4"), ]), sorted(parse_static_routes(rfc3442))) logs = self.logs.getvalue() self.assertIn(rfc3442, logs.splitlines()[0])
def test_parse_static_routes_logs_error_truncated(self): bad_rfc3442 = { "class_c": "24,169,254,169,10", "class_b": "16,172,16,10", "class_a": "8,10,10", "gateway": "0,0", "netlen": "33,0", } for rfc3442 in bad_rfc3442.values(): self.assertEqual([], parse_static_routes(rfc3442)) logs = self.logs.getvalue() self.assertEqual(len(bad_rfc3442.keys()), len(logs.splitlines()))
def obtain_lease(self): """Perform dhcp discovery in a sandboxed environment if possible. @return: A dict representing dhcp options on the most recent lease obtained from the dhclient discovery if run, otherwise an error is raised. @raises: NoDHCPLeaseError if no leases could be obtained. """ if self.lease: return self.lease leases = maybe_perform_dhcp_discovery(self.iface, self.dhcp_log_func) if not leases: raise NoDHCPLeaseError() self.lease = leases[-1] LOG.debug( "Received dhcp lease on %s for %s/%s", self.lease["interface"], self.lease["fixed-address"], self.lease["subnet-mask"], ) nmap = { "interface": "interface", "ip": "fixed-address", "prefix_or_mask": "subnet-mask", "broadcast": "broadcast-address", "static_routes": [ "rfc3442-classless-static-routes", "classless-static-routes", ], "router": "routers", } kwargs = self.extract_dhcp_options_mapping(nmap) if not kwargs["broadcast"]: kwargs["broadcast"] = net.mask_and_ipv4_to_bcast_addr( kwargs["prefix_or_mask"], kwargs["ip"]) if kwargs["static_routes"]: kwargs["static_routes"] = parse_static_routes( kwargs["static_routes"]) if self.connectivity_url_data: kwargs["connectivity_url_data"] = self.connectivity_url_data ephipv4 = EphemeralIPv4Network(**kwargs) ephipv4.__enter__() self._ephipv4 = ephipv4 return self.lease
def test_parse_static_routes_default_route(self): rfc3442 = "0,130,56,240,1" self.assertEqual([('0.0.0.0/0', '130.56.240.1')], parse_static_routes(rfc3442))
def test_parse_static_routes_single_ip_handles_trailing_semicolon(self): rfc3442 = "32,169,254,169,254,130,56,248,255;" self.assertEqual([('169.254.169.254/32', '130.56.248.255')], parse_static_routes(rfc3442))
def test_parse_static_routes_single_ip(self): rfc3442 = "32,169,254,169,254,130,56,248,255" self.assertEqual([('169.254.169.254/32', '130.56.248.255')], parse_static_routes(rfc3442))
def test_parse_static_routes_bogus_width_returns_empty_list(self): rfc3442 = "33,169,254,169,254,130,56,248" self.assertEqual([], parse_static_routes(rfc3442))
def test_parse_static_routes_invalid_input_returns_empty_list(self): rfc3442 = "32,169,254,169,254,130,56,248" self.assertEqual([], parse_static_routes(rfc3442))
def parse_static_routes_empty_string(self): self.assertEqual([], parse_static_routes(""))
def test_unspecified_gateway(self): rfc3442 = "32,169,254,169,254,0,0,0,0" self.assertEqual([('169.254.169.254/32', '0.0.0.0')], parse_static_routes(rfc3442))