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 ), )
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"))
def test_does_not_match_needle_not_in_haystack(self): self.assertMismatch(ContainedBy([]).match("foo"), "'foo' not in []")
def test_matches_needle_in_haystack(self): self.assertThat("foo", ContainedBy({"foo", "bar"}))