Ejemplo n.º 1
0
    def test_getConfiguration_returns_configuration_object(self):
        service = ntp.RegionNetworkTimeProtocolService(reactor)

        # Configure example time references.
        ntp_servers = {factory.make_name("ntp-server") for _ in range(5)}
        Config.objects.set_config("ntp_servers", " ".join(ntp_servers))

        # Put all addresses in the same space so they're mutually routable.
        space = factory.make_Space()
        # Populate the database with "this" region and an example peer.
        region, _, _ = make_region_with_address(space)
        self.useFixture(MAASIDFixture(region.system_id))
        peer, addr4, addr6 = make_region_with_address(space)

        observed = service._getConfiguration()
        self.assertThat(observed, IsInstance(ntp._Configuration))

        expected_references = Equals(frozenset(ntp_servers))
        expected_peers = AllMatch(ContainedBy({addr4.ip, addr6.ip}))

        self.assertThat(
            observed,
            MatchesStructure(
                references=expected_references, peers=expected_peers
            ),
        )
Ejemplo n.º 2
0
    def test_tryUpdate_logs_errors_from_broken_method(self):
        service = ntp.RegionNetworkTimeProtocolService(reactor)
        broken_method = self.patch_autospec(service, self.method)
        broken_method.side_effect = factory.make_exception()

        # Ensure that we never actually execute against systemd or write an
        # actual configuration file.
        self.patch_autospec(
            ntp, "deferToThread"
        ).side_effect = always_succeed_with(None)
        self.patch_autospec(service_monitor, "restartService")

        with TwistedLoggerFixture() as logger:
            yield service._tryUpdate()

        self.assertThat(
            logger.output,
            DocTestMatches(
                """
                Failed to update NTP configuration.
                Traceback (most recent call last):
                ...
                maastesting.factory.TestException#...
                """
            ),
        )
Ejemplo n.º 3
0
 def test__tryUpdate_updates_ntp_server(self):
     service = ntp.RegionNetworkTimeProtocolService(reactor)
     refs, peers = yield deferToDatabase(self.make_example_configuration)
     configure_region = self.patch_autospec(ntp, "configure_region")
     restartService = self.patch_autospec(service_monitor, "restartService")
     yield service._tryUpdate()
     self.assertThat(
         configure_region,
         MockCalledOnceWith(refs, Matches(AllMatch(ContainedBy(peers)))))
     self.assertThat(restartService, MockCalledOnceWith("ntp_region"))
     # If the configuration has not changed then a second call to
     # `_tryUpdate` does not result in another call to `configure_region`.
     yield service._tryUpdate()
     self.assertThat(configure_region, MockCalledOnce())
     self.assertThat(restartService, MockCalledOnceWith("ntp_region"))
Ejemplo n.º 4
0
 def test_service_iterates_every_30_seconds(self):
     service = ntp.RegionNetworkTimeProtocolService(reactor)
     self.assertThat(service.step, Equals(30.0))
Ejemplo n.º 5
0
 def test_service_uses__tryUpdate_as_periodic_function(self):
     service = ntp.RegionNetworkTimeProtocolService(reactor)
     self.assertThat(service.call, Equals((service._tryUpdate, (), {})))
Ejemplo n.º 6
0
def make_NetworkTimeProtocolService():
    from maasserver.regiondservices import ntp
    return ntp.RegionNetworkTimeProtocolService(reactor)