def test_raises_if_not_admin(self): user = factory.make_User() handler = DiscoveryHandler(user, {}, None) factory.make_Discovery() num_discoveries = Discovery.objects.count() self.assertThat(num_discoveries, Equals(1)) with ExpectedException(HandlerPermissionError): handler.clear()
def test__exposes_mdns_when_nothing_better_available(self): rack = factory.make_RackController() iface = factory.make_Interface(node=rack) ip = factory.make_ip_address(ipv6=False) mdns_hostname = factory.make_hostname() factory.make_Discovery(hostname=mdns_hostname, interface=iface, ip=ip) discovery = Discovery.objects.first() self.assertThat(discovery.hostname, Equals(mdns_hostname))
def make_discovery(): """Make a discovery in its own transaction so each last_seen time is different.""" random_rack = random.choice(RackController.objects.all()) random_interface = random.choice(random_rack.interface_set.all()) random_subnet = random.choice(random_interface.vlan.subnet_set.all()) factory.make_Discovery(interface=random_interface, ip=factory.pick_ip_in_Subnet(random_subnet))
def test_clears_all_by_default(self): user = factory.make_admin() handler = DiscoveryHandler(user, {}, None) factory.make_Discovery() num_discoveries = Discovery.objects.count() self.assertThat(num_discoveries, Equals(1)) handler.clear() num_discoveries = Discovery.objects.count() self.assertThat(num_discoveries, Equals(0))
def test__associates_best_subnet(self): rack = factory.make_RackController() iface = factory.make_Interface(node=rack) # Seems unlikely, but we'll test it anyway. ;-) subnet = factory.make_Subnet(cidr="10.0.0.0/24", vlan=iface.vlan) factory.make_Subnet(cidr="10.0.0.0/8", vlan=iface.vlan) factory.make_Discovery(interface=iface, ip="10.0.0.1") self.assertThat(Discovery.objects.first().subnet, Equals(subnet)) self.assertThat(Discovery.objects.count(), Equals(1))
def test_list(self): user = factory.make_User() handler = DiscoveryHandler(user, {}, None) factory.make_Discovery() factory.make_Discovery() expected_discoveries = [ self.dehydrate_discovery(discovery, for_list=True) for discovery in Discovery.objects.all() ] self.assertItemsEqual(expected_discoveries, handler.list({}))
def test__prefers_rdns_to_mdns(self): rack = factory.make_RackController() iface = factory.make_Interface(node=rack) ip = factory.make_ip_address(ipv6=False) mdns_hostname = factory.make_hostname() rdns_hostname = factory.make_hostname() factory.make_Discovery(hostname="", interface=iface, ip=ip) factory.make_MDNS(hostname=mdns_hostname, ip=ip, interface=iface) factory.make_RDNS(hostname=rdns_hostname, ip=ip, observer=rack) discovery = Discovery.objects.first() self.assertThat(discovery.hostname, Equals(rdns_hostname))
def test__is_external_dhcp(self): rack = factory.make_RackController() iface = factory.make_Interface(node=rack) factory.make_Subnet(cidr="10.0.0.0/8", vlan=iface.vlan) factory.make_Discovery(interface=iface, ip="10.0.0.1") discovery = Discovery.objects.first() self.assertThat(discovery.is_external_dhcp, Equals(False)) iface.vlan.external_dhcp = "10.0.0.1" iface.vlan.save() discovery = Discovery.objects.first() self.assertThat(discovery.is_external_dhcp, Equals(True))
def make_discovery(racks=None): """Make a discovery in its own transaction so each last_seen time is different. """ if racks is None: racks = _prefetch_racks() rack = random.choice(racks) interface = random.choice(rack.interface_set.all()) subnet = random.choice(interface.vlan.subnet_set.all()) factory.make_Discovery(interface=interface, ip=factory.pick_ip_in_Subnet(subnet))
def test_clears_mdns_only_upon_request(self): user = factory.make_admin() handler = DiscoveryHandler(user, {}, None) factory.make_Discovery(hostname="useful-towel") num_discoveries = Discovery.objects.count() num_mdns = MDNS.objects.count() self.assertThat(num_discoveries, Equals(1)) self.assertThat(num_mdns, Equals(1)) handler.clear({"mdns": True}) num_discoveries = Discovery.objects.count() num_mdns = MDNS.objects.count() self.assertThat(num_discoveries, Equals(1)) self.assertThat(num_mdns, Equals(0))
def test__ignores_duplicate_macs(self): rack1 = factory.make_RackController() rack2 = factory.make_RackController() iface1 = factory.make_Interface(node=rack1) iface2 = factory.make_Interface(node=rack2) # Simulate a single device being observed from two different rack # interfaces. d1 = factory.make_Discovery(interface=iface1) factory.make_Discovery(interface=iface2, mac_address=d1.mac_address, ip=d1.ip) # ... the Discovery view should only display one entry. self.assertThat(Discovery.objects.count(), Equals(1))
def make_discoveries(count=3, interface=None): return [ factory.make_Discovery(interface=interface, time=time, updated=datetime.fromtimestamp(time)) for time in range(count) ]
def test_list_orders_by_creation_date(self): user = factory.make_User() handler = DiscoveryHandler(user, {}, None) now = datetime.now() d0 = factory.make_Discovery(created=now) d4 = factory.make_Discovery(created=(now + timedelta(days=4))) d3 = factory.make_Discovery(created=(now + timedelta(days=3))) d1 = factory.make_Discovery(created=(now + timedelta(days=1))) d2 = factory.make_Discovery(created=(now + timedelta(days=2))) # Test for the expected order independent of how the database # decided to sort. expected_discoveries = [ self.dehydrate_discovery(discovery, for_list=True) for discovery in [d0, d1, d2, d3, d4] ] self.assertEquals(expected_discoveries, handler.list({}))
def test_clear_by_mac_and_ip_allowed_for_admin(self): self.become_admin() rack = factory.make_RackController() iface = rack.interface_set.first() make_discoveries(interface=iface, count=3) neigh = factory.make_Discovery() uri = get_discoveries_uri() response = self.client.post( uri, { "op": "clear_by_mac_and_ip", "ip": neigh.ip, "mac": neigh.mac_address, }, ) self.assertEqual(204, response.status_code, response.content) # The second time, we should get a NOT_HERE result. response = self.client.post( uri, { "op": "clear_by_mac_and_ip", "ip": neigh.ip, "mac": neigh.mac_address, }, ) self.assertEqual(410, response.status_code, response.content)
def test_get(self): user = factory.make_User() handler = DiscoveryHandler(user, {}, None) discovery = factory.make_Discovery() self.assertEqual( self.dehydrate_discovery(discovery), handler.get({"discovery_id": discovery.discovery_id}), )
def test_raises_if_missing_mac(self): user = factory.make_User() handler = DiscoveryHandler(user, {}, None) disco = factory.make_Discovery() num_discoveries = Discovery.objects.count() self.assertThat(num_discoveries, Equals(1)) with ExpectedException(HandlerPermissionError): handler.delete_by_mac_and_ip(dict(ip=disco.ip))
def test__query_by_unknown_ip(self): rack = factory.make_RackController() iface = factory.make_Interface(node=rack) discovery = factory.make_Discovery(interface=iface, ip="10.0.0.1") self.assertThat(Discovery.objects.by_unknown_ip().count(), Equals(1)) factory.make_StaticIPAddress(ip=discovery.ip, cidr="10.0.0.0/8") # Now that we have a known IP address that matches, the discovery # should disappear from this query. self.assertThat(Discovery.objects.by_unknown_ip().count(), Equals(0))
def test__query_by_unknown_mac(self): rack = factory.make_RackController() iface = factory.make_Interface(node=rack) discovery = factory.make_Discovery(interface=iface) self.assertThat(Discovery.objects.by_unknown_mac().count(), Equals(1)) factory.make_Interface(mac_address=discovery.mac_address) # Now that we have a known interface with the same MAC, the discovery # should disappear from this query. self.assertThat(Discovery.objects.by_unknown_mac().count(), Equals(0))
def test__by_unknown_ip_and_mac__known_ip(self): rack = factory.make_RackController() iface = factory.make_Interface(node=rack) discovery = factory.make_Discovery(interface=iface, ip="10.0.0.1") results = self.get_api_results({'op': 'by_unknown_ip_and_mac'}) self.assertThat(len(results), Equals(1)) factory.make_StaticIPAddress(ip=discovery.ip, cidr="10.0.0.0/8") # Known IP address, unexpected MAC. results = self.get_api_results({'op': 'by_unknown_ip_and_mac'}) self.assertThat(len(results), Equals(0))
def test__query_by_unknown_ip_and_mac__known_mac(self): rack = factory.make_RackController() iface = factory.make_Interface(node=rack) discovery = factory.make_Discovery(interface=iface) self.assertThat(Discovery.objects.by_unknown_ip_and_mac().count(), Equals(1)) # Known MAC, unknown IP. factory.make_Interface(mac_address=discovery.mac_address) self.assertThat(Discovery.objects.by_unknown_ip_and_mac().count(), Equals(0))
def test__query_by_unknown_ip_and_mac__known_ip(self): rack = factory.make_RackController() iface = factory.make_Interface(node=rack) discovery = factory.make_Discovery(interface=iface, ip="10.0.0.1") self.assertThat(Discovery.objects.by_unknown_ip_and_mac().count(), Equals(1)) factory.make_StaticIPAddress(ip=discovery.ip, cidr="10.0.0.0/8") # Known IP address, unexpected MAC. self.assertThat(Discovery.objects.by_unknown_ip_and_mac().count(), Equals(0))
def test__by_unknown_ip_and_mac__known_mac(self): rack = factory.make_RackController() iface = factory.make_Interface(node=rack) discovery = factory.make_Discovery(interface=iface) results = self.get_api_results({'op': 'by_unknown_ip_and_mac'}) self.assertThat(len(results), Equals(1)) # Known MAC, unknown IP. factory.make_Interface(mac_address=discovery.mac_address) results = self.get_api_results({'op': 'by_unknown_ip_and_mac'}) self.assertThat(len(results), Equals(0))
def test__by_unknown_ip(self): rack = factory.make_RackController() iface = factory.make_Interface(node=rack) discovery = factory.make_Discovery(interface=iface, ip="10.0.0.1") results = self.get_api_results({'op': 'by_unknown_ip'}) self.assertThat(len(results), Equals(1)) factory.make_StaticIPAddress(ip=discovery.ip, cidr="10.0.0.0/8") # Now that we have a known IP address that matches, the discovery # should disappear from this query. results = self.get_api_results({'op': 'by_unknown_ip'}) self.assertThat(len(results), Equals(0))
def test_deletes_discovery_and_returns_number_deleted(self): user = factory.make_admin() handler = DiscoveryHandler(user, {}, None) disco = factory.make_Discovery() num_discoveries = Discovery.objects.count() self.assertThat(num_discoveries, Equals(1)) result = handler.delete_by_mac_and_ip( dict(ip=disco.ip, mac=disco.mac_address)) num_discoveries = Discovery.objects.count() self.assertThat(num_discoveries, Equals(0)) self.assertThat(result, Equals(1))
def test__by_unknown_mac(self): rack = factory.make_RackController() iface = factory.make_Interface(node=rack) discovery = factory.make_Discovery(interface=iface) results = self.get_api_results({'op': 'by_unknown_mac'}) self.assertThat(len(results), Equals(1)) factory.make_Interface(mac_address=discovery.mac_address) # Now that we have a known interface with the same MAC, the discovery # should disappear from this query. results = self.get_api_results({'op': 'by_unknown_mac'}) self.assertThat(len(results), Equals(0))
def test_list_starts_after_first_seen(self): user = factory.make_User() handler = DiscoveryHandler(user, {}, None) now = datetime.now() factory.make_Discovery(created=now) d4 = factory.make_Discovery(created=(now + timedelta(days=4))) d3 = factory.make_Discovery(created=(now + timedelta(days=3))) factory.make_Discovery(created=(now + timedelta(days=1))) factory.make_Discovery(created=(now + timedelta(days=2))) first_seen = now + timedelta(days=2) first_seen = str( time.mktime(first_seen.timetuple()) + first_seen.microsecond / 1e6) # Test for the expected order independent of how the database # decided to sort. expected_discoveries = [ self.dehydrate_discovery(discovery, for_list=True) for discovery in [d3, d4] ] self.assertEquals(expected_discoveries, handler.list({"start": first_seen}))
def test__associates_known_subnet(self): rack = factory.make_RackController() iface = factory.make_Interface(node=rack) subnet = factory.make_Subnet(cidr="10.0.0.0/8", vlan=iface.vlan) factory.make_Discovery(interface=iface, ip="10.0.0.1") self.assertThat(Discovery.objects.first().subnet, Equals(subnet))
def test__does_not_fail_if_cannot_find_subnet(self): rack = factory.make_RackController() iface = factory.make_Interface(node=rack) factory.make_Discovery(interface=iface, ip="10.0.0.1") self.assertThat(Discovery.objects.first().subnet, Is(None))
def test_mac_organization(self): discovery = factory.make_Discovery(mac_address="48:51:b7:00:00:00") self.assertThat(discovery.mac_organization, IsNonEmptyString)
def test_handler_path(self): discovery = factory.make_Discovery() self.assertEqual( '/MAAS/api/2.0/discovery/%s/' % discovery.discovery_id, get_discovery_uri(discovery))