Пример #1
0
 def test_get_most_recent_returns_record_with_highest_id(self):
     DNSPublication(serial=3).save()
     DNSPublication(serial=30).save()
     DNSPublication(serial=10).save()
     self.assertThat(
         DNSPublication.objects.get_most_recent(),
         MatchesStructure(serial=Equals(10)),
     )
Пример #2
0
 def test_create_empty(self):
     pub = DNSPublication()
     pub.save()
     self.assertThat(
         pub,
         MatchesStructure(
             serial=IsInstance(int),
             created=IsInstance(datetime),
             source=Equals(""),
         ))
Пример #3
0
    def test_collect_garbage_leaves_records_older_than_specified(self):
        publications = {
            timedelta(days=1): DNSPublication(source="1 day ago"),
            timedelta(minutes=1): DNSPublication(source="1 minute ago"),
            timedelta(seconds=0): DNSPublication(source="now"),
        }

        with connection.cursor() as cursor:
            cursor.execute("SELECT now()")
            [now] = cursor.fetchone()

        # Work from oldest to youngest so that the youngest gets the highest
        # primary key; the primary key is used to determine the most recent.
        for delta in sorted(publications, reverse=True):
            publication = publications[delta]
            publication.save()
            # Use SQL to set `created`; Django's field validation prevents it.
            with connection.cursor() as cursor:
                cursor.execute(
                    "UPDATE maasserver_dnspublication SET created = %s"
                    " WHERE id = %s",
                    [now - delta, publication.id],
                )

        def get_ages():
            pubs = DNSPublication.objects.all()
            return {now - pub.created for pub in pubs}

        deltas = set(publications)
        self.assertThat(get_ages(), Equals(deltas))

        one_second = timedelta(seconds=1)
        # Work from oldest to youngest again, collecting garbage each time.
        while len(deltas) > 1:
            delta = max(deltas)
            # Publications of exactly the specified age are not deleted.
            DNSPublication.objects.collect_garbage(now - delta)
            self.assertThat(get_ages(), Equals(deltas))
            # Publications of just a second over are deleted.
            DNSPublication.objects.collect_garbage(now - delta + one_second)
            self.assertThat(get_ages(), Equals(deltas - {delta}))
            # We're done with this one.
            deltas.discard(delta)

        # The most recent publication will never be deleted.
        DNSPublication.objects.collect_garbage()
        self.assertThat(get_ages(), Equals(deltas))
        self.assertThat(deltas, HasLength(1))
Пример #4
0
 def setUp(self):
     super().setUp()
     # Ensure there's an initial DNS publication. Outside of tests this is
     # guaranteed by a migration.
     DNSPublication(source="Initial").save()
     # Allow test-local changes to configuration.
     self.useFixture(RegionConfigurationFixture())
     # Immediately make DNS changes as they're needed.
     self.patch(dns_config_module, "DNS_DEFER_UPDATES", False)
     # Create a DNS server.
     self.bind = self.useFixture(BINDServer())
     # Use the dnspython resolver for at least some queries.
     self.resolver = dns.resolver.Resolver()
     self.resolver.nameservers = ["127.0.0.1"]
     self.resolver.port = self.bind.config.port
     patch_dns_config_path(self, self.bind.config.homedir)
     # Use a random port for rndc.
     patch_dns_rndc_port(self, allocate_ports("localhost")[0])
     # This simulates what should happen when the package is
     # installed:
     # Create MAAS-specific DNS configuration files.
     parser = ArgumentParser()
     setup_dns.add_arguments(parser)
     setup_dns.run(parser.parse_args([]))
     # Register MAAS-specific DNS configuration files with the
     # system's BIND instance.
     parser = ArgumentParser()
     get_named_conf.add_arguments(parser)
     get_named_conf.run(
         parser.parse_args(
             ["--edit", "--config-path", self.bind.config.conf_file]))
     # Reload BIND.
     self.bind.runner.rndc("reload")
Пример #5
0
 def test_collect_garbage_removes_all_but_most_recent_record(self):
     for serial in range(10):
         DNSPublication(serial=serial).save()
     self.assertThat(DNSPublication.objects.all(), HasLength(10))
     DNSPublication.objects.collect_garbage()
     self.assertThat(DNSPublication.objects.all(), HasLength(1))
     self.assertThat(DNSPublication.objects.get_most_recent(),
                     MatchesStructure(serial=Equals(serial)))
Пример #6
0
 def test_create_with_values(self):
     serial = randint(1, 5000)
     created = datetime.now() - timedelta(minutes=1098)
     source = factory.make_name("source")
     pub = DNSPublication(serial=serial, created=created, source=source)
     pub.save()
     self.assertThat(
         pub,
         MatchesStructure(
             serial=Equals(serial),
             created=MatchesAll(
                 IsInstance(datetime),
                 # `created` is always set; given values are ignored.
                 Not(Equals(created)),
                 first_only=True,
             ),
             source=Equals(source),
         ))
Пример #7
0
 def test_current_zone_serial_returns_serial_of_latest_publication(self):
     publication = DNSPublication(source=factory.make_name("source"))
     publication.save()
     self.assertThat(int(current_zone_serial()), Equals(publication.serial))
Пример #8
0
def dns_force_reload():
    """Force the DNS to be regenerated."""
    DNSPublication(source="Force reload").save()