Ejemplo n.º 1
0
 def test_config_file_is_world_readable(self):
     self.patch(DNSForwardZoneConfig, 'target_dir', self.make_dir())
     network = factory.getRandomNetwork()
     dns_zone_config = DNSForwardZoneConfig(
         factory.getRandomString(), serial=random.randint(1, 100),
         dns_ip=factory.getRandomIPAddress(), networks=[network])
     dns_zone_config.write_config()
     filepath = FilePath(dns_zone_config.target_path)
     self.assertTrue(filepath.getPermissions().other.read)
Ejemplo n.º 2
0
 def test_forward_zone_get_static_mapping_returns_iterator(self):
     name = factory.getRandomString()
     network = IPNetwork('192.12.0.1/30')
     dns_ip = factory.getRandomIPInNetwork(network)
     dns_zone_config = DNSForwardZoneConfig(
         name, networks=[network], dns_ip=dns_ip)
     self.assertThat(
         dns_zone_config.get_static_mapping(),
         MatchesAll(
             IsInstance(Iterable), Not(IsInstance(Sequence))))
Ejemplo n.º 3
0
 def test_forward_zone_get_cname_mapping_skips_identity(self):
     # We don't write cname records to map host names to themselves.
     # Without this, a node would get an invalid cname record upon
     # enlistment.
     zone = factory.make_name('zone')
     network = IPNetwork('10.250.99.0/24')
     ip = factory.getRandomIPInNetwork(network)
     generated_name = generated_hostname(ip)
     dns_zone_config = DNSForwardZoneConfig(
         zone, networks=[network],
         dns_ip=factory.getRandomIPInNetwork(network),
         mapping={generated_name: ip})
     self.assertNotIn(
         generated_name,
         dict(dns_zone_config.get_cname_mapping()))
Ejemplo n.º 4
0
    def test_write_dns_zone_config_writes_file(self):
        command = factory.getRandomString()
        domain = factory.getRandomString()
        network = IPNetwork('192.168.0.3/24')
        ip = factory.getRandomIPInNetwork(network)
        forward_zone = DNSForwardZoneConfig(
            domain,
            serial=random.randint(1, 100),
            mapping={factory.getRandomString(): ip},
            networks=[network])
        reverse_zone = DNSReverseZoneConfig(
            domain,
            serial=random.randint(1, 100),
            mapping={factory.getRandomString(): ip},
            network=network)
        result = write_dns_zone_config.delay(
            zones=[forward_zone, reverse_zone],
            callback=rndc_command.subtask(args=[command]))

        forward_file_name = 'zone.%s' % domain
        reverse_file_name = 'zone.0.168.192.in-addr.arpa'
        self.assertThat((
            result.successful(),
            os.path.join(self.dns_conf_dir, forward_file_name),
            os.path.join(self.dns_conf_dir, reverse_file_name),
            self.rndc_recorder.calls,
        ),
                        MatchesListwise((
                            Equals(True),
                            FileExists(),
                            FileExists(),
                            Equals([((command, ), {})]),
                        )), result)
Ejemplo n.º 5
0
 def test_get_static_mapping_multiple_networks(self):
     name = factory.getRandomString()
     networks = IPNetwork('11.11.11.11/31'), IPNetwork('22.22.22.22/31')
     dns_ip = factory.getRandomIPInNetwork(networks[0])
     dns_zone_config = DNSForwardZoneConfig(
         name, networks=networks, dns_ip=dns_ip)
     self.assertItemsEqual(
         [
             ('%s.' % name, dns_ip),
             (generated_hostname('11.11.11.10'), '11.11.11.10'),
             (generated_hostname('11.11.11.11'), '11.11.11.11'),
             (generated_hostname('22.22.22.22'), '22.22.22.22'),
             (generated_hostname('22.22.22.23'), '22.22.22.23'),
         ],
         dns_zone_config.get_static_mapping(),
         )
Ejemplo n.º 6
0
 def test_get_static_mapping(self):
     name = factory.getRandomString()
     network = IPNetwork('192.12.0.1/30')
     dns_ip = factory.getRandomIPInNetwork(network)
     dns_zone_config = DNSForwardZoneConfig(
         name, networks=[network], dns_ip=dns_ip)
     self.assertItemsEqual(
         [
             ('%s.' % name, dns_ip),
             (generated_hostname('192.12.0.0'), '192.12.0.0'),
             (generated_hostname('192.12.0.1'), '192.12.0.1'),
             (generated_hostname('192.12.0.2'), '192.12.0.2'),
             (generated_hostname('192.12.0.3'), '192.12.0.3'),
         ],
         dns_zone_config.get_static_mapping(),
         )
Ejemplo n.º 7
0
 def test_writes_dns_zone_config_with_NS_record(self):
     target_dir = self.make_dir()
     self.patch(DNSForwardZoneConfig, 'target_dir', target_dir)
     network = factory.getRandomNetwork()
     dns_ip = factory.getRandomIPAddress()
     dns_zone_config = DNSForwardZoneConfig(
         factory.getRandomString(), serial=random.randint(1, 100),
         dns_ip=dns_ip, networks=[network])
     dns_zone_config.write_config()
     self.assertThat(
         os.path.join(target_dir, 'zone.%s' % dns_zone_config.domain),
         FileContains(
             matcher=ContainsAll(
                 [
                     'IN  NS  %s.' % dns_zone_config.domain,
                     '%s. IN A %s' % (dns_zone_config.domain, dns_ip),
                 ])))
Ejemplo n.º 8
0
 def test_writes_dns_zone_config(self):
     target_dir = self.make_dir()
     self.patch(DNSForwardZoneConfig, 'target_dir', target_dir)
     domain = factory.getRandomString()
     hostname = factory.getRandomString()
     network = factory.getRandomNetwork()
     ip = factory.getRandomIPInNetwork(network)
     dns_zone_config = DNSForwardZoneConfig(
         domain, serial=random.randint(1, 100),
         mapping={hostname: ip}, networks=[network])
     dns_zone_config.write_config()
     self.assertThat(
         os.path.join(target_dir, 'zone.%s' % domain),
         FileContains(
             matcher=ContainsAll(
                 [
                     '%s IN CNAME %s' % (hostname, generated_hostname(ip)),
                     '%s IN A %s' % (generated_hostname(ip), ip),
                 ])))
Ejemplo n.º 9
0
 def test_computes_dns_config_file_paths(self):
     domain = factory.make_name('zone')
     dns_zone_config = DNSForwardZoneConfig(domain)
     self.assertEqual(
         (
             locate_config(TEMPLATES_DIR, 'zone.template'),
             os.path.join(conf.DNS_CONFIG_DIR, 'zone.%s' % domain),
         ),
         (
             dns_zone_config.template_path,
             dns_zone_config.target_path,
         ))
Ejemplo n.º 10
0
 def test_fields(self):
     domain = factory.getRandomString()
     serial = random.randint(1, 200)
     hostname = factory.getRandomString()
     network = factory.getRandomNetwork()
     ip = factory.getRandomIPInNetwork(network)
     mapping = {hostname: ip}
     dns_zone_config = DNSForwardZoneConfig(
         domain, serial, mapping, networks=[network])
     self.assertThat(
         dns_zone_config,
         MatchesStructure.byEquality(
             domain=domain,
             serial=serial,
             mapping=mapping,
             networks=[network],
             )
         )
Ejemplo n.º 11
0
 def _gen_forward_zones(nodegroups, serial, mappings, networks):
     """Generator of forward zones, collated by domain name."""
     get_domain = lambda nodegroup: nodegroup.name
     dns_ip = get_dns_server_address()
     forward_nodegroups = sorted(nodegroups, key=get_domain)
     for domain, nodegroups in groupby(forward_nodegroups, get_domain):
         nodegroups = list(nodegroups)
         # A forward zone encompassing all nodes in the same domain.
         yield DNSForwardZoneConfig(
             domain,
             serial=serial,
             dns_ip=dns_ip,
             mapping={
                 hostname: ip
                 for nodegroup in nodegroups
                 for hostname, ip in mappings[nodegroup].items()
             },
             networks={networks[nodegroup]
                       for nodegroup in nodegroups},
         )
Ejemplo n.º 12
0
    def test_write_full_dns_config_sets_up_config(self):
        # write_full_dns_config writes the config file, writes
        # the zone files, and reloads the dns service.
        domain = factory.getRandomString()
        network = IPNetwork('192.168.0.3/24')
        ip = factory.getRandomIPInNetwork(network)
        zones = [
            DNSForwardZoneConfig(domain,
                                 serial=random.randint(1, 100),
                                 mapping={factory.getRandomString(): ip},
                                 networks=[network]),
            DNSReverseZoneConfig(domain,
                                 serial=random.randint(1, 100),
                                 mapping={factory.getRandomString(): ip},
                                 network=network),
        ]
        command = factory.getRandomString()
        result = write_full_dns_config.delay(
            zones=zones,
            callback=rndc_command.subtask(args=[command]),
            upstream_dns=factory.getRandomIPAddress())

        forward_file_name = 'zone.%s' % domain
        reverse_file_name = 'zone.0.168.192.in-addr.arpa'
        self.assertThat((
            result.successful(),
            self.rndc_recorder.calls,
            os.path.join(self.dns_conf_dir, forward_file_name),
            os.path.join(self.dns_conf_dir, reverse_file_name),
            os.path.join(self.dns_conf_dir, MAAS_NAMED_CONF_NAME),
            os.path.join(self.dns_conf_dir,
                         MAAS_NAMED_CONF_OPTIONS_INSIDE_NAME),
        ),
                        MatchesListwise((
                            Equals(True),
                            Equals([((command, ), {})]),
                            FileExists(),
                            FileExists(),
                            FileExists(),
                            FileExists(),
                        )))
Ejemplo n.º 13
0
 def test_write_config_writes_config(self):
     target_dir = self.make_dir()
     self.patch(DNSConfig, 'target_dir', target_dir)
     domain = factory.getRandomString()
     network = IPNetwork('192.168.0.3/24')
     ip = factory.getRandomIPInNetwork(network)
     forward_zone = DNSForwardZoneConfig(
         domain, mapping={factory.getRandomString(): ip},
         networks=[network])
     reverse_zone = DNSReverseZoneConfig(
         domain, mapping={factory.getRandomString(): ip},
         network=network)
     dnsconfig = DNSConfig((forward_zone, reverse_zone))
     dnsconfig.write_config()
     self.assertThat(
         os.path.join(target_dir, MAAS_NAMED_CONF_NAME),
         FileContains(
             matcher=ContainsAll(
                 [
                     'zone.%s' % domain,
                     'zone.0.168.192.in-addr.arpa',
                     MAAS_NAMED_RNDC_CONF_NAME,
                 ])))