def test__tryUpdate_logs_errors_from_broken_method(self): # Patch the logger in the clusterservice so no log messages are printed # because the tests run in debug mode. self.patch(common.log, 'debug') rpc_service, _ = yield prepareRegion(self) self.patch_autospec(ntp, "configure_rack") # No-op configuration. service = ntp.RackNetworkTimeProtocolService(rpc_service, reactor) broken_method = self.patch_autospec(service, self.method) broken_method.side_effect = factory.make_exception() # Ensure that we never actually execute against systemd. self.patch_autospec(service_monitor, "restartService") self.useFixture(MAASRootFixture()) 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#... """))
def test_is_silent_and_does_nothing_when_region_is_not_available(self): self.useFixture(MAASRootFixture()) service = ntp.RackNetworkTimeProtocolService( StubClusterClientService(), reactor) self.patch_autospec(service, "_maybeApplyConfiguration") with TwistedLoggerFixture() as logger: yield service._tryUpdate() self.assertThat(logger.output, Equals("")) self.assertThat(service._maybeApplyConfiguration, MockNotCalled())
def test_is_silent_and_does_nothing_when_rack_is_not_recognised(self): self.useFixture(MAASRootFixture()) rpc_service, protocol = yield prepareRegion(self) protocol.GetControllerType.side_effect = exceptions.NoSuchNode service = ntp.RackNetworkTimeProtocolService(rpc_service, reactor) self.patch_autospec(service, "_maybeApplyConfiguration") with TwistedLoggerFixture() as logger: yield service._tryUpdate() self.assertThat(logger.output, Equals("")) self.assertThat(service._maybeApplyConfiguration, MockNotCalled())
def test_is_silent_and_does_nothing_when_region_is_not_available(self): # Patch the logger in the clusterservice so no log messages are printed # because the tests run in debug mode. self.patch(common.log, 'debug') self.useFixture(MAASRootFixture()) service = ntp.RackNetworkTimeProtocolService( StubClusterClientService(), reactor) self.patch_autospec(service, "_maybeApplyConfiguration") with TwistedLoggerFixture() as logger: yield service._tryUpdate() self.assertThat(logger.output, Equals("")) self.assertThat(service._maybeApplyConfiguration, MockNotCalled())
def test__tryUpdate_updates_ntp_server(self): self.useFixture(MAASRootFixture()) servers, peers = self.make_servers_and_peers() rpc_service, _ = yield prepareRegion(self, servers=servers, peers=peers) service = ntp.RackNetworkTimeProtocolService(rpc_service, reactor) configure_rack = self.patch_autospec(ntp, "configure_rack") restartService = self.patch_autospec(service_monitor, "restartService") yield service._tryUpdate() self.assertThat(configure_rack, MockCalledOnceWith(servers, peers)) self.assertThat(restartService, MockCalledOnceWith("ntp_rack")) # If the configuration has not changed then a second call to # `_tryUpdate` does not result in another call to `configure`. yield service._tryUpdate() self.assertThat(configure_rack, MockCalledOnceWith(servers, peers)) self.assertThat(restartService, MockCalledOnceWith("ntp_rack"))
def test__getConfiguration_returns_configuration_object(self): is_region, is_rack = factory.pick_bool(), factory.pick_bool() servers, peers = self.make_servers_and_peers() rpc_service, protocol = yield prepareRegion(self, is_region=is_region, is_rack=is_rack, servers=servers, peers=peers) service = ntp.RackNetworkTimeProtocolService(rpc_service, reactor) observed = yield service._getConfiguration() self.assertThat(observed, IsInstance(ntp._Configuration)) self.assertThat( observed, MatchesStructure.byEquality(references=servers, peers=peers, is_region=is_region, is_rack=is_rack))
def test_is_silent_does_nothing_but_saves_config_when_is_region(self): self.useFixture(MAASRootFixture()) rpc_service, _ = yield prepareRegion(self, is_region=True) service = ntp.RackNetworkTimeProtocolService(rpc_service, reactor) self.patch_autospec(ntp, "configure_rack") # No-op configuration. # There is no most recently applied configuration. self.assertThat(service._configuration, Is(None)) with TwistedLoggerFixture() as logger: yield service._tryUpdate() # The most recently applied configuration is set, though it was not # actually "applied" because this host was configured as a region+rack # controller, and the rack should not attempt to manage the NTP server # on a region+rack. self.assertThat(service._configuration, IsInstance(ntp._Configuration)) # The configuration was not applied. self.assertThat(ntp.configure_rack, MockNotCalled()) # Nothing was logged; there's no need for lots of chatter. self.assertThat(logger.output, Equals(""))
def test_service_iterates_every_30_seconds(self): service = ntp.RackNetworkTimeProtocolService( StubClusterClientService(), reactor) self.assertThat(service.step, Equals(30.0))
def test_service_uses__tryUpdate_as_periodic_function(self): service = ntp.RackNetworkTimeProtocolService( StubClusterClientService(), reactor) self.assertThat(service.call, Equals((service._tryUpdate, (), {})))
def _makeNetworkTimeProtocolService(self, rpc_service): from provisioningserver.rackdservices import ntp ntp_service = ntp.RackNetworkTimeProtocolService(rpc_service, reactor) ntp_service.setName("ntp") return ntp_service