def test_node_ttl_overrides_domain(self): global_ttl = random.randint(100, 199) Config.objects.set_config("default_dns_ttl", global_ttl) subnet = factory.make_Subnet(cidr="10.0.0.0/23") domain = factory.make_Domain(ttl=random.randint(200, 299)) node = factory.make_Node_with_Interface_on_Subnet( status=NODE_STATUS.READY, subnet=subnet, domain=domain, address_ttl=random.randint(300, 399), ) boot_iface = node.get_boot_interface() [boot_ip] = boot_iface.claim_auto_ips() expected_forward = { node.hostname: HostnameIPMapping( node.system_id, node.address_ttl, {boot_ip.ip}, node.node_type ) } expected_reverse = { node.fqdn: HostnameIPMapping( node.system_id, node.address_ttl, {boot_ip.ip}, node.node_type ) } zones = ZoneGenerator( domain, subnet, default_ttl=global_ttl, serial=random.randint(0, 65535), ).as_list() self.assertEqual(expected_forward, zones[0]._mapping) self.assertEqual(expected_reverse, zones[1]._mapping)
def test_get_hostname_ip_mapping_containts_both_static_and_dynamic(self): node1 = factory.make_Node(interface=True) node1_interface = node1.get_boot_interface() subnet = factory.make_Subnet() static_ip = factory.make_StaticIPAddress( alloc_type=IPADDRESS_TYPE.AUTO, ip=factory.pick_ip_in_Subnet(subnet), subnet=subnet, interface=node1_interface, ) node2 = factory.make_Node(interface=True) node2_interface = node2.get_boot_interface() subnet = factory.make_ipv4_Subnet_with_IPRanges() dynamic_ip = factory.make_StaticIPAddress( alloc_type=IPADDRESS_TYPE.DISCOVERED, ip=factory.pick_ip_in_IPRange(subnet.get_dynamic_ranges()[0]), subnet=subnet, interface=node2_interface, ) ttl = random.randint(10, 300) Config.objects.set_config("default_dns_ttl", ttl) expected_mapping = { "%s.maas" % node1.hostname: HostnameIPMapping( node1.system_id, ttl, {static_ip.ip}, node1.node_type ), "%s.maas" % node2.hostname: HostnameIPMapping( node2.system_id, ttl, {dynamic_ip.ip}, node2.node_type ), } actual = get_hostname_ip_mapping(Domain.objects.get_default_domain()) self.assertItemsEqual(expected_mapping.items(), actual.items())
def test_dnsresource_address_overrides_domain(self): # DNSResource.address_ttl _does_, however, override Domain.ttl for # addresses that do not have nodes associated with them. global_ttl = random.randint(100, 199) Config.objects.set_config('default_dns_ttl', global_ttl) subnet = factory.make_Subnet(cidr="10.0.0.0/23") domain = factory.make_Domain(ttl=random.randint(200, 299)) node = factory.make_Node_with_Interface_on_Subnet( status=NODE_STATUS.READY, subnet=subnet, domain=domain, address_ttl=random.randint(300, 399)) boot_iface = node.get_boot_interface() [boot_ip] = boot_iface.claim_auto_ips() dnsrr = factory.make_DNSResource( domain=domain, address_ttl=random.randint(400, 499)) node_ips = {boot_ip.ip} dnsrr_ips = { ip.ip for ip in dnsrr.ip_addresses.all() if ip is not None} expected_forward = { node.hostname: HostnameIPMapping( node.system_id, node.address_ttl, node_ips, node.node_type), dnsrr.name: HostnameIPMapping( None, dnsrr.address_ttl, dnsrr_ips, None, dnsrr.id), } expected_reverse = { node.fqdn: HostnameIPMapping( node.system_id, node.address_ttl, node_ips, node.node_type, None), dnsrr.fqdn: HostnameIPMapping( None, dnsrr.address_ttl, dnsrr_ips, None, dnsrr.id)} zones = ZoneGenerator( domain, subnet, default_ttl=global_ttl, serial=random.randint(0, 65535)).as_list() self.assertEqual(expected_forward, zones[0]._mapping) self.assertEqual(expected_reverse, zones[1]._mapping)
def test_dnsresource_address_does_not_affect_addresses_when_node_set(self): # If a node has the same FQDN as a DNSResource, then we use whatever # address_ttl there is on the Node (whether None, or not) rather than # that on any DNSResource addresses with the same FQDN. global_ttl = random.randint(100, 199) Config.objects.set_config('default_dns_ttl', global_ttl) subnet = factory.make_Subnet(cidr="10.0.0.0/23") domain = factory.make_Domain(ttl=random.randint(200, 299)) node = factory.make_Node_with_Interface_on_Subnet( status=NODE_STATUS.READY, subnet=subnet, domain=domain, address_ttl=random.randint(300, 399)) boot_iface = node.get_boot_interface() [boot_ip] = boot_iface.claim_auto_ips() dnsrr = factory.make_DNSResource( name=node.hostname, domain=domain, address_ttl=random.randint(400, 499)) ips = { ip.ip for ip in dnsrr.ip_addresses.all() if ip is not None} ips.add(boot_ip.ip) expected_forward = {node.hostname: HostnameIPMapping( node.system_id, node.address_ttl, ips, node.node_type, dnsrr.id)} expected_reverse = { node.fqdn: HostnameIPMapping( node.system_id, node.address_ttl, ips, node.node_type, dnsrr.id)} zones = ZoneGenerator( domain, subnet, default_ttl=global_ttl, serial=random.randint(0, 65535)).as_list() self.assertEqual(expected_forward, zones[0]._mapping) self.assertEqual(expected_reverse, zones[1]._mapping)
def test_returns_interface_ips_but_no_nulls(self): default_domain = Domain.objects.get_default_domain().name domain = factory.make_Domain(name='henry') subnet = factory.make_Subnet(cidr=str(IPNetwork("10/29").cidr)) subnet.gateway_ip = str(IPAddress(IPNetwork(subnet.cidr).ip + 1)) subnet.save() # Create a node with two interfaces, with NULL ips node = factory.make_Node_with_Interface_on_Subnet( subnet=subnet, vlan=subnet.vlan, fabric=subnet.vlan.fabric, domain=domain, interface_count=3) dnsdata = factory.make_DNSData(domain=domain) boot_iface = node.boot_interface interfaces = list(node.interface_set.all().exclude(id=boot_iface.id)) # Now go add IP addresses to the boot interface, and one other boot_ip = factory.make_StaticIPAddress(interface=boot_iface, subnet=subnet) sip = factory.make_StaticIPAddress(interface=interfaces[0], subnet=subnet) default_ttl = random.randint(10, 300) Config.objects.set_config('default_dns_ttl', default_ttl) zones = ZoneGenerator(domain, subnet, default_ttl=default_ttl, serial=random.randint(0, 65535)).as_list() self.assertThat( zones, MatchesSetwise(forward_zone("henry"), reverse_zone(default_domain, "10/29"), reverse_zone(default_domain, "10/24"))) self.assertEqual( { node.hostname: HostnameIPMapping(node.system_id, default_ttl, {'%s' % boot_ip.ip}, node.node_type), "%s.%s" % (interfaces[0].name, node.hostname): HostnameIPMapping(node.system_id, default_ttl, {'%s' % sip.ip}, node.node_type) }, zones[0]._mapping) self.assertEqual({ dnsdata.dnsresource.name: HostnameRRsetMapping( None, {(default_ttl, dnsdata.rrtype, dnsdata.rrdata)}) }.items(), zones[0]._other_mapping.items()) self.assertEqual( { node.fqdn: HostnameIPMapping(node.system_id, default_ttl, {'%s' % boot_ip.ip}, node.node_type), '%s.%s' % (interfaces[0].name, node.fqdn): HostnameIPMapping(node.system_id, default_ttl, {'%s' % sip.ip}, node.node_type) }, zones[1]._mapping) self.assertEqual({}, zones[2]._mapping)