예제 #1
0
 def test_ignores_networks_that_span_slash_16s(self):
     # If the upper and lower bounds of a range span two /16 networks
     # (but contain between them no more than 65536 addresses),
     # get_GENERATE_directives() will return early
     ip_range = IPRange("10.0.0.55", "10.1.0.54")
     directives = DNSForwardZoneConfig.get_GENERATE_directives(ip_range)
     self.assertEqual([], directives)
예제 #2
0
    def test_returns_single_entry_for_tiny_network(self):
        network = IPNetwork("%s/31" % factory.make_ipv4_address())

        expected_directives = self.get_expected_generate_directives(network)
        directives = DNSForwardZoneConfig.get_GENERATE_directives(network)
        self.assertEqual(1, len(expected_directives))
        self.assertItemsEqual(expected_directives, directives)
예제 #3
0
    def test_returns_two_entries_for_slash_23_network(self):
        network = IPNetwork("%s/23" % factory.make_ipv4_address())

        expected_directives = self.get_expected_generate_directives(network)
        directives = DNSForwardZoneConfig.get_GENERATE_directives(network)
        self.assertEqual(2, len(expected_directives))
        self.assertItemsEqual(expected_directives, directives)
예제 #4
0
 def test_dtrt_for_larger_networks(self):
     # For every other network size that we're not explicitly
     # testing here,
     # DNSForwardZoneConfig.get_GENERATE_directives() will return
     # one GENERATE directive for every 255 addresses in the network.
     for prefixlen in range(23, 16):
         network = IPNetwork("%s/%s" %
                             (factory.make_ipv4_address(), prefixlen))
         directives = DNSForwardZoneConfig.get_GENERATE_directives(network)
         self.assertIsEqual(network.size / 256, len(directives))
예제 #5
0
 def test_excplicitly(self):
     # The other tests in this TestCase rely on
     # get_expected_generate_directives(), which is quite dense. Here
     # we test get_GENERATE_directives() explicitly.
     ip_range = IPRange('192.168.0.55', '192.168.2.128')
     expected_directives = [
         ("55-255", "192-168-0-$", "192.168.0.$"),
         ("0-255", "192-168-1-$", "192.168.1.$"),
         ("0-128", "192-168-2-$", "192.168.2.$"),
     ]
     self.assertItemsEqual(
         expected_directives,
         DNSForwardZoneConfig.get_GENERATE_directives(ip_range))
예제 #6
0
    def test_sorts_output(self):
        network = IPNetwork("10.0.0.0/23")

        expected_hostname = "10-0-%s-$"
        expected_address = "10.0.%s.$"

        directives = list(
            DNSForwardZoneConfig.get_GENERATE_directives(network))
        self.expectThat(len(directives), Equals(2))
        self.expectThat(
            directives[0],
            Equals(("0-255", expected_hostname % "0", expected_address % "0")))
        self.expectThat(
            directives[1],
            Equals(("0-255", expected_hostname % "1", expected_address % "1")))
예제 #7
0
 def test_handles_slash_32_dynamic_range(self):
     target_dir = patch_dns_config_path(self)
     domain = factory.make_string()
     network = factory.make_ipv4_network()
     ipv4_hostname = factory.make_name("host")
     ipv4_ip = factory.pick_ip_in_network(network)
     range_ip = factory.pick_ip_in_network(network, but_not={ipv4_ip})
     ipv6_hostname = factory.make_name("host")
     ipv6_ip = factory.make_ipv6_address()
     ttl = random.randint(10, 300)
     mapping = {
         ipv4_hostname: HostnameIPMapping(None, ttl, {ipv4_ip}),
         ipv6_hostname: HostnameIPMapping(None, ttl, {ipv6_ip}),
     }
     dynamic_range = IPRange(IPAddress(range_ip), IPAddress(range_ip))
     expected_generate_directives = (
         DNSForwardZoneConfig.get_GENERATE_directives(dynamic_range)
     )
     other_mapping = {
         ipv4_hostname: HostnameRRsetMapping(None, {(ttl, "MX", "10 bar")})
     }
     dns_zone_config = DNSForwardZoneConfig(
         domain,
         serial=random.randint(1, 100),
         other_mapping=other_mapping,
         default_ttl=ttl,
         mapping=mapping,
         dynamic_ranges=[dynamic_range],
     )
     dns_zone_config.write_config()
     self.assertThat(
         os.path.join(target_dir, "zone.%s" % domain),
         FileContains(
             matcher=ContainsAll(
                 [
                     "$TTL %d" % ttl,
                     "%s %d IN A %s" % (ipv4_hostname, ttl, ipv4_ip),
                     "%s %d IN AAAA %s" % (ipv6_hostname, ttl, ipv6_ip),
                     "%s %d IN MX 10 bar" % (ipv4_hostname, ttl),
                 ]
                 + [
                     "$GENERATE %s %s IN A %s"
                     % (iterator_values, reverse_dns, hostname)
                     for iterator_values, reverse_dns, hostname in expected_generate_directives
                 ]
             )
         ),
     )
예제 #8
0
 def test_writes_dns_zone_config(self):
     target_dir = patch_dns_config_path(self)
     domain = factory.make_string()
     network = factory.make_ipv4_network()
     ipv4_hostname = factory.make_name('host')
     ipv4_ip = factory.pick_ip_in_network(network)
     ipv6_hostname = factory.make_name('host')
     ipv6_ip = factory.make_ipv6_address()
     ttl = random.randint(10, 300)
     mapping = {
         ipv4_hostname: HostnameIPMapping(None, ttl, {ipv4_ip}),
         ipv6_hostname: HostnameIPMapping(None, ttl, {ipv6_ip}),
     }
     expected_generate_directives = (
         DNSForwardZoneConfig.get_GENERATE_directives(network))
     other_mapping = {
         ipv4_hostname: HostnameRRsetMapping(None, {(ttl, 'MX', '10 bar')})
     }
     dns_zone_config = DNSForwardZoneConfig(
         domain,
         serial=random.randint(1, 100),
         other_mapping=other_mapping,
         default_ttl=ttl,
         mapping=mapping,
         dynamic_ranges=[IPRange(network.first, network.last)])
     dns_zone_config.write_config()
     self.assertThat(
         os.path.join(target_dir, 'zone.%s' % domain),
         FileContains(matcher=ContainsAll([
             '$TTL %d' % ttl,
             '%s %d IN A %s' % (ipv4_hostname, ttl, ipv4_ip),
             '%s %d IN AAAA %s' % (ipv6_hostname, ttl, ipv6_ip),
             '%s %d IN MX 10 bar' % (ipv4_hostname, ttl),
         ] + [
             '$GENERATE %s %s IN A %s' %
             (iterator_values, reverse_dns, hostname) for iterator_values,
             reverse_dns, hostname in expected_generate_directives
         ])))
예제 #9
0
 def test_ignores_network_larger_than_slash_16(self):
     network = IPNetwork("%s/15" % factory.make_ipv4_address())
     self.assertEqual([],
                      DNSForwardZoneConfig.get_GENERATE_directives(network))
예제 #10
0
 def test_returns_single_entry_for_slash_24_network(self):
     network = IPNetwork("%s/24" % factory.make_ipv4_address())
     expected_directives = self.get_expected_generate_directives(network)
     directives = DNSForwardZoneConfig.get_GENERATE_directives(network)
     self.expectThat(directives, HasLength(1))
     self.assertItemsEqual(expected_directives, directives)