def test_write_config_writes_config_if_no_existing_file(self): # If DNSConfig is created with overwrite=False, the config file # will be written if no config file exists. target_dir = patch_dns_config_path(self) dnsconfig = DNSConfig() dnsconfig.write_config(overwrite=False) self.assertThat(os.path.join(target_dir, MAAS_NAMED_CONF_NAME), FileExists())
def handle(self, *args, **options): no_clobber = options.get('no_clobber') set_up_rndc() set_up_options_conf(overwrite=not no_clobber) config = DNSConfig() config.write_config(overwrite=not no_clobber, zone_names=(), reverse_zone_names=())
def handle(self, *args, **options): no_clobber = options.get('no_clobber') setup_rndc() upstream_dns = Config.objects.get_config("upstream_dns") set_up_options_conf( overwrite=not no_clobber, upstream_dns=upstream_dns) config = DNSConfig() config.write_config( overwrite=not no_clobber, zone_names=(), reverse_zone_names=())
def test_write_config_writes_config_if_no_existing_file(self): # If DNSConfig is created with overwrite=False, the config file # will be written if no config file exists. target_dir = self.make_dir() self.patch(DNSConfig, 'target_dir', target_dir) dnsconfig = DNSConfig() dnsconfig.write_config(overwrite=False) self.assertThat( os.path.join(target_dir, MAAS_NAMED_CONF_NAME), FileExists())
def test_get_include_snippet_returns_snippet(self): target_dir = self.make_dir() self.patch(DNSConfig, 'target_dir', target_dir) dnsconfig = DNSConfig() snippet = dnsconfig.get_include_snippet() self.assertThat( snippet, MatchesAll( Not(StartsWith('\n')), EndsWith('\n'), Contains(target_dir), Contains('include "%s"' % dnsconfig.target_path)))
def test_write_config_skips_writing_if_overwrite_false(self): # If DNSConfig is created with overwrite=False, it won't # overwrite an existing config file. target_dir = patch_dns_config_path(self) random_content = factory.make_string() factory.make_file(location=target_dir, name=MAAS_NAMED_CONF_NAME, contents=random_content) dnsconfig = DNSConfig() dnsconfig.write_config(overwrite=False) self.assertThat(os.path.join(target_dir, MAAS_NAMED_CONF_NAME), FileContains(random_content))
def run(args, stdout=sys.stdout, stderr=sys.stderr): """Setup MAAS DNS configuration. :param args: Parsed output of the arguments added in `add_arguments()`. :param stdout: Standard output stream to write to. :param stderr: Standard error stream to write to. """ set_up_rndc() set_up_options_conf(overwrite=not args.no_clobber) config = DNSConfig() config.write_config(overwrite=not args.no_clobber, zone_names=(), reverse_zone_names=())
def write_dns_config(zones=(), callback=None, **kwargs): """Write out the DNS configuration file. :param zones: List of zones to include as part of the main config. :type zones: list of :class:`DNSZoneData` :param callback: Callback subtask. :type callback: callable :param **kwargs: Keyword args passed to DNSConfig.write_config() """ dns_config = DNSConfig(zones=zones) dns_config.write_config(**kwargs) if callback is not None: callback.delay()
def test_write_config_skips_writing_if_overwrite_false(self): # If DNSConfig is created with overwrite=False, it won't # overwrite an existing config file. target_dir = self.make_dir() self.patch(DNSConfig, 'target_dir', target_dir) random_content = factory.getRandomString() factory.make_file( location=target_dir, name=MAAS_NAMED_CONF_NAME, contents=random_content) dnsconfig = DNSConfig() dnsconfig.write_config(overwrite=False) self.assertThat( os.path.join(target_dir, MAAS_NAMED_CONF_NAME), FileContains(random_content))
def write_full_dns_config(zones=None, callback=None, **kwargs): """Write out the DNS configuration files: the main configuration file and the zone files. :param zones: List of zones to write. :type zones: list of :class:`DNSZoneData` :param callback: Callback subtask. :type callback: callable :param **kwargs: Keyword args passed to DNSConfig.write_config() """ if zones is not None: for zone in zones: zone.write_config() # Write main config file. dns_config = DNSConfig(zones=zones) dns_config.write_config(**kwargs) if callback is not None: callback.delay()
def bind_write_configuration(zones, trusted_networks, forwarded_zones=None): """Write BIND's configuration. :param zones: Those zones to include in main config. :type zones: Sequence of :py:class:`DomainData`. :param trusted_networks: A sequence of CIDR network specifications that are permitted to use the DNS server as a forwarder. """ # trusted_networks was formerly specified as a single IP address with # netmask. These assertions are here to prevent code that assumes that # slipping through. assert not isinstance(trusted_networks, (bytes, str)) assert isinstance(trusted_networks, Sequence) dns_config = DNSConfig(zones=zones, forwarded_zones=forwarded_zones) dns_config.write_config(trusted_networks=trusted_networks)
def test_write_config_writes_config(self): target_dir = patch_dns_config_path(self) domain = factory.make_string() network = IPNetwork('192.168.0.3/24') ip = factory.pick_ip_in_network(network) forward_zone = DNSForwardZoneConfig( domain, mapping={factory.make_string(): ip}) reverse_zone = DNSReverseZoneConfig(domain, 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, ])))
def test_DNSConfig_defaults(self): dnsconfig = DNSConfig() self.assertEqual( ( locate_config(TEMPLATES_DIR, 'named.conf.template'), os.path.join(conf.DNS_CONFIG_DIR, MAAS_NAMED_CONF_NAME) ), (dnsconfig.template_path, dnsconfig.target_path))
def handle(self, *args, **options): edit = options.get('edit') config_path = options.get('config_path') include_snippet = DNSConfig().get_include_snippet() if edit is True: with open(config_path, "ab") as conf_file: conf_file.write(include_snippet) else: return INCLUDE_SNIPPET_COMMENT + include_snippet
def test_get_include_snippet_returns_snippet(self): target_dir = patch_dns_config_path(self) snippet = DNSConfig.get_include_snippet() self.assertThat( snippet, MatchesAll( Not(StartsWith('\n')), EndsWith('\n'), Contains(target_dir), Contains('include "%s/%s"' % ( config.get_dns_config_dir(), DNSConfig.target_file_name, ))))
def handle(self, *args, **options): edit = options.get('edit') config_path = options.get('config_path') include_snippet = DNSConfig.get_include_snippet() if edit is True: # XXX: GavinPanella: I've not been able to discover what character # set BIND expects for its configuration, so I've gone with a safe # choice of ASCII. If we find that this fails we can revisit this # and experiment to discover a better choice. with open(config_path, "a", encoding="ascii") as conf_file: conf_file.write(include_snippet) else: return INCLUDE_SNIPPET_COMMENT + include_snippet
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, ])))
def test_write_config_with_forwarded_zones(self): name = factory.make_name("domain") ip = factory.make_ip_address() forwarded_zones = [(name, [ip])] target_dir = patch_dns_config_path(self) DNSConfig(forwarded_zones=forwarded_zones).write_config() config_path = os.path.join(target_dir, MAAS_NAMED_CONF_NAME) expected_content = dedent(f""" zone "{name}" {{ type forward; forward only; forwarders {{ {ip}; }}; }}; """) config = read_isc_file(config_path) expected = parse_isc_string(expected_content) self.assertEqual(expected[f'zone "{name}"'], config[f'zone "{name}"'])
def run(args, stdout=sys.stdout, stderr=sys.stderr): """Return the named configuration snippet. :param args: Parsed output of the arguments added in `add_arguments()`. :param stdout: Standard output stream to write to. :param stderr: Standard error stream to write to. """ include_snippet = DNSConfig.get_include_snippet() if args.edit is True: # XXX: GavinPanella: I've not been able to discover what character # set BIND expects for its configuration, so I've gone with a safe # choice of ASCII. If we find that this fails we can revisit this # and experiment to discover a better choice. with open(args.config_path, "a", encoding="ascii") as conf_file: conf_file.write(include_snippet) else: stdout.write(INCLUDE_SNIPPET_COMMENT + include_snippet) stdout.write("\n") stdout.flush()
def test_get_template_retrieves_template(self): dnsconfig = DNSConfig() template = dnsconfig.get_template() self.assertIsInstance(template, tempita.Template) self.assertThat( dnsconfig.template_path, FileContains(template.content))
def test_render_template(self): dnsconfig = DNSConfig() random_content = factory.getRandomString() template = tempita.Template("{{test}}") rendered = dnsconfig.render_template(template, test=random_content) self.assertEqual(random_content, rendered)
def test_write_config_makes_config_world_readable(self): target_dir = patch_dns_config_path(self) DNSConfig().write_config() config_file = FilePath(os.path.join(target_dir, MAAS_NAMED_CONF_NAME)) self.assertTrue(config_file.getPermissions().other.read)
def test_render_template_raises_DNSConfigFail(self): dnsconfig = DNSConfig() template = tempita.Template("template: {{test}}") exception = self.assertRaises( DNSConfigFail, dnsconfig.render_template, template) self.assertIn("'test' is not defined", unicode(exception))
def test_write_config_errors_if_unexpected_exception(self): dnsconfig = DNSConfig() exception = IOError(errno.EBUSY, factory.make_string()) self.patch(config, "atomic_write", Mock(side_effect=exception)) self.assertRaises(IOError, dnsconfig.write_config)
def test_write_config_DNSConfigDirectoryMissing_if_dir_missing(self): dnsconfig = DNSConfig() dir_name = patch_dns_config_path(self) os.rmdir(dir_name) self.assertRaises(DNSConfigDirectoryMissing, dnsconfig.write_config)
def test_write_config_DNSConfigDirectoryMissing_if_dir_missing(self): dnsconfig = DNSConfig() dir_name = self.make_dir() os.rmdir(dir_name) self.patch(DNSConfig, 'target_dir', dir_name) self.assertRaises(DNSConfigDirectoryMissing, dnsconfig.write_config)
def test_write_config_errors_if_unexpected_exception(self): dnsconfig = DNSConfig() exception = IOError(errno.EBUSY, factory.getRandomString()) self.patch( DNSConfig, 'inner_write_config', Mock(side_effect=exception)) self.assertRaises(IOError, dnsconfig.write_config)