def test__log_entries(self): maaslog = self.patch(discovery_module.maaslog, "info") user = factory.make_admin() neigh = factory.make_Neighbour(ip="1.1.1.1", mac_address="00:01:02:03:04:05") Discovery.objects.delete_by_mac_and_ip(ip=neigh.ip, mac=neigh.mac_address, user=user) self.assertThat( maaslog, MockCalledOnceWith( Matches(DocTestMatches("User '%s' cleared..." % user.username))), ) neigh = factory.make_Neighbour(ip="1.1.1.1", mac_address="00:01:02:03:04:05") maaslog = self.patch(discovery_module.maaslog, "info") Discovery.objects.delete_by_mac_and_ip(ip=neigh.ip, mac=neigh.mac_address) self.assertThat( maaslog, MockCalledOnceWith( Matches( DocTestMatches( "Cleared neighbour entry: 1.1.1.1 (00:..."))), )
def test__tryUpdate_updates_syslog_server(self): service = syslog.RegionSyslogService(reactor) port, peers = yield deferToDatabase(self.make_example_configuration) write_config = self.patch_autospec(syslog, "write_config") restartService = self.patch_autospec(service_monitor, "restartService") yield service._tryUpdate() self.assertThat( write_config, MockCalledOnceWith( True, Matches( ContainsAll( [ { "ip": service._formatIP(ip), "name": node.hostname, } for node, ip in peers ] ) ), port=port, ), ) self.assertThat(restartService, MockCalledOnceWith("syslog_region")) # If the configuration has not changed then a second call to # `_tryUpdate` does not result in another call to `write_config`. yield service._tryUpdate() self.assertThat(write_config, MockCalledOnce()) self.assertThat(restartService, MockCalledOnceWith("syslog_region"))
def test__clear_logs_username_if_given(self): user = factory.make_admin() maaslog = self.patch(discovery_module.maaslog, 'info') factory.make_MDNS() factory.make_Neighbour() Discovery.objects.clear(user=user, all=True) self.assertThat( maaslog, MockCalledOnceWith( Matches(DocTestMatches("User '%s' cleared..." % user.username))))
def test__equality(self): matcher = AfterPreprocessing(set, Equals({1, 2, "three"})) self.assertEqual(Matches(matcher), [1, 2, "three"]) self.assertEqual(Matches(matcher), (1, 2, "three")) self.assertEqual(Matches(matcher), dict.fromkeys((1, 2, "three"))) self.assertNotEqual(Matches(matcher), (1, 2, 3)) self.assertNotEqual(Matches(matcher), (1, 2, "three", 4)) self.assertNotEqual(Matches(matcher), dict.fromkeys((2, "three")))
def test_make_ipvN_range_creates_random_network_if_not_supplied(self): self.patch_autospec(factory, "make_ip_range") factory.make_ip_range.return_value = sentinel.ip_range ip_range = self.make_range() self.assertThat(ip_range, Is(sentinel.ip_range)) self.assertThat( factory.make_ip_range, MockCalledOnceWith(network=Matches( MatchesAll( IsInstance(IPNetwork), MatchesStructure.byEquality(version=self.version), first_only=True, ))), )
def test__clear_neighbour_entries(self): maaslog = self.patch(discovery_module.maaslog, 'info') factory.make_MDNS() factory.make_MDNS() factory.make_Neighbour() factory.make_Neighbour() self.assertThat(MDNS.objects.count(), Equals(2)) self.assertThat(Neighbour.objects.count(), Equals(2)) Discovery.objects.clear(neighbours=True) self.assertThat(MDNS.objects.count(), Equals(2)) self.assertThat(Neighbour.objects.count(), Equals(0)) self.assertThat( maaslog, MockCalledOnceWith( Matches(DocTestMatches('Cleared all neighbour entries.'))))
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__clear_mdns_entries(self): maaslog = self.patch(discovery_module.maaslog, "info") factory.make_MDNS() factory.make_MDNS() factory.make_Neighbour() factory.make_Neighbour() self.assertThat(MDNS.objects.count(), Equals(2)) self.assertThat(Neighbour.objects.count(), Equals(2)) Discovery.objects.clear(mdns=True) self.assertThat(MDNS.objects.count(), Equals(0)) self.assertThat(Neighbour.objects.count(), Equals(2)) self.assertThat( maaslog, MockCalledOnceWith( Matches(DocTestMatches("Cleared all mDNS entries."))), )
def test__representation(self): matcher = AfterPreprocessing(set, Equals({1, 2, "three"})) self.assertThat( Matches(matcher), AfterPreprocessing(repr, Equals("<Matches " + str(matcher) + ">")), )
def ArgumentsMatching(**kwargs): """Tests if the output from `argparse` matches our expectations.""" return Matches(MatchesStructure.byEquality(**kwargs))
def MatchesCIDRs(*cidrs): return Matches(AfterPreprocessing(CIDRSet, Equals(CIDRSet(cidrs))))