class NetBoxTestCase(TestCase): def setUp(self): super().setUp() self.netbox = NetBox() self.netbox.api = pynetbox.api("http://netbox.example.net", token="test") @patch( "requests.sessions.Session.get", return_value=MockedResponse(fixture="netbox/tests/fixtures/devices.json"), ) def test_get_devices(self, *_): devices = self.netbox.get_devices() self.assertEqual(2, len(devices)) self.assertEqual("router01.example.net", next(devices).name) self.assertEqual("router02.example.net", next(devices).name) @patch( "pynetbox.core.endpoint.RODetailEndpoint.list", return_value=MockedGenerator( { "get_facts": MockedResponse( fixture="netbox/tests/fixtures/device_facts.json" ).json() } ), ) def test_napalm(self, *_): with patch( "requests.sessions.Session.get", return_value=MockedResponse(fixture="netbox/tests/fixtures/device.json"), ): facts = self.netbox.napalm(1, "get_facts") self.assertEqual("router01", facts["hostname"])
def mocked_synchronization(*args, **kwargs): namespace = args[0].split("/")[-1] if namespace in ["ixpfx", "net", "netixlan", "poc"]: return MockedResponse( fixture="peeringdb/tests/fixtures/sync_{}.json".format(namespace)) return MockedResponse(status_code=500)
def mocked_get_autonomous_system(*args, **kwargs): if "asn" in kwargs["params"]: return MockedResponse( fixture="peeringdb/tests/fixtures/netixlans_as{}.json".format( kwargs["params"]["asn"])) return MockedResponse(status_code=500)
def test_get_health(self, *_): with self.assertRaises(NotImplementedError): self.ix_api.get_health() i = IXAPI.objects.get(name="IXP 2") with patch("requests.get", return_value=MockedResponse(content={"status": "up"})): self.assertEqual("healthy", i.get_health()) with patch("requests.get", return_value=MockedResponse(content={"status": "warn"})): self.assertEqual("degraded", i.get_health()) with patch("requests.get", return_value=MockedResponse(content={"status": "error"})): self.assertEqual("unhealthy", i.get_health())
def test_get_peers_for_ix(self): api = PeeringDB() with patch("peeringdb.http.requests.get", return_value=MockedResponse(status_code=404)): # Must not be found self.assertIsNone(api.get_peers_for_ix(0)) with patch( "peeringdb.http.requests.get", return_value=MockedResponse( fixture="peeringdb/tests/fixtures/netixlan_by_ix_id.json"), ): # Must have some peers self.assertEqual(len(api.get_peers_for_ix(1)), 2)
def test_napalm(self, *_): with patch( "requests.sessions.Session.get", return_value=MockedResponse(fixture="netbox/tests/fixtures/device.json"), ): facts = self.netbox.napalm(1, "get_facts") self.assertEqual("router01", facts["hostname"])
def test_get_products(self, *_): with patch( "requests.get", return_value=MockedResponse( fixture="extras/tests/fixtures/ix_api/products.json"), ): self.assertEqual("1234", self.ix_api.get_products()[0]["id"])
def test_get_autonomous_system(self, *_): api = PeeringDB() asn = 65536 # Must not exist with patch("peeringdb.http.requests.get", return_value=MockedResponse(status_code=404)): self.assertIsNone(api.get_autonomous_system(64500)) # Using an API call (no cached data) autonomous_system = api.get_autonomous_system(asn) self.assertEqual(autonomous_system.asn, asn) # Save the data inside the cache details = { "id": autonomous_system.id, "asn": autonomous_system.asn, "name": autonomous_system.name, } network = Network(**details) network.save() # Using no API calls (cached data) autonomous_system = api.get_autonomous_system(asn) self.assertEqual(autonomous_system.asn, asn)
def test_get_ix_network(self, *_): api = PeeringDB() ix_network_id = 1 with patch("peeringdb.http.requests.get", return_value=MockedResponse(status_code=404)): # Must not exist self.assertIsNone(api.get_ix_network(0)) # Using an API call (no cached data) ix_network = api.get_ix_network(ix_network_id) self.assertEqual(ix_network.id, ix_network_id) # Save the data inside the cache details = { "id": ix_network.id, "asn": ix_network.asn, "name": ix_network.name, "ix_id": ix_network.ix_id, "ixlan_id": ix_network.ixlan_id, } network_ixlan = NetworkIXLAN(**details) network_ixlan.save() # Using no API calls (cached data) ix_network = api.get_ix_network(ix_network_id) self.assertEqual(ix_network.id, ix_network_id)
def test_get_macs(self, *_): with patch( "requests.get", return_value=MockedResponse( fixture="extras/tests/fixtures/ix_api/macs.json"), ): i = self.ix_api.get_macs() self.assertEqual("1234", i[0].id) self.assertEqual("AA:BB:CC:DD:EE:FF", i[0].address)
def test_get_network_services(self, *_): with patch( "requests.get", return_value=MockedResponse( fixture="extras/tests/fixtures/ix_api/network_services.json" ), ): i = self.ix_api.get_network_services() self.assertEqual("1234", i[0].id) self.assertEqual(1234, i[0].peeringdb_ixid)
def test_get_customers(self, *_): with patch( "requests.get", return_value=MockedResponse( fixture="extras/tests/fixtures/ix_api/customers.json"), ): c = self.ix_api.get_customers() self.assertEqual(2, len(c)) self.assertEqual("1234", c[0]["id"]) self.assertEqual("5678", c[1]["id"])
def test_get_identity(self, *_): with patch( "requests.get", return_value=MockedResponse(content=[{ "id": "1234", "name": "Customer 1" }]), ): self.assertIsNone(self.ix_api.get_identity()) self.ix_api.identity = "1234" self.assertEqual("1234", self.ix_api.get_identity()["id"])
def test_get_network_service_configs(self, *_): with patch( "requests.get", return_value=MockedResponse( fixture= "extras/tests/fixtures/ix_api/network_service_configs.json" ), ): i = self.ix_api.get_network_service_configs() self.assertEqual("1234", i[0].id) self.assertEqual("production", i[0].state)
def test_get_network_features(self, *_): with patch( "requests.get", return_value=MockedResponse( fixture="extras/tests/fixtures/ix_api/network_features.json" ), ): i = self.ix_api.get_network_features() self.assertEqual("1234", i[0].id) self.assertEqual(64500, i[0].asn) self.assertTrue(i[0].required)
def test_customers(self, *_): ixapi = IXAPI.objects.get(name="IXP 1") mocked = MockedResponse( fixture="extras/tests/fixtures/ix_api/customers.json") url = reverse("extras-api:ixapi-customers") with patch("requests.get", return_value=mocked): # Query params required response = self.client.get(url, format="json", **self.header) self.assertHttpStatus(response, status.HTTP_400_BAD_REQUEST) # With query params response = self.client.get(url, data={ "url": ixapi.url, "api_key": ixapi.api_key, "api_secret": ixapi.api_secret, }, format="json", **self.header) self.assertHttpStatus(response, status.HTTP_200_OK) self.assertListEqual(mocked.json(), response.json())
def test_get_ips(self, *_): with patch( "requests.get", return_value=MockedResponse( fixture="extras/tests/fixtures/ix_api/ips.json"), ): i = self.ix_api.get_ips() self.assertEqual("1234", i[0].id) self.assertEqual("5678", i[1].id) self.assertIsInstance(i[0].network, ipaddress.IPv6Network) self.assertIsInstance(i[1].network, ipaddress.IPv4Network) self.assertIsInstance(i[0].address, ipaddress.IPv6Address) self.assertIsInstance(i[1].address, ipaddress.IPv4Address)
def test_get_prefixes_for_ix_network(self, *_): api = PeeringDB() ix_network_id = 29146 with patch("peeringdb.http.requests.get", return_value=MockedResponse(status_code=404)): # Must be empty self.assertFalse(api.get_prefixes_for_ix_network(0)) known_prefixes = [ ipaddress.ip_network("2001:db8:1337::/64"), ipaddress.ip_network("203.0.113.0/24"), ] found_prefixes = [] for ix_prefix in api.get_prefixes_for_ix_network(ix_network_id): self.assertIn(ix_prefix, known_prefixes)
def test_get_common_ix_networks_for_asns(self, *_): api = PeeringDB() asn1 = 65536 asn2 = 65537 with patch("peeringdb.http.requests.get", return_value=MockedResponse(status_code=404)): # Empty list should be returned self.assertFalse(api.get_common_ix_networks_for_asns(asn1, 64500)) # Found common IX networks found_ix_networks = [] for n1, n2 in api.get_common_ix_networks_for_asns(asn1, asn2): self.assertEqual(n1.ixlan_id, n2.ixlan_id) found_ix_networks.append(n1.ixlan_id) self.assertEqual([1], found_ix_networks)
def test_get_ix_networks_for_asn(self, *_): api = PeeringDB() asn = 65536 with patch("peeringdb.http.requests.get", return_value=MockedResponse(status_code=404)): # Must not exist self.assertIsNone(api.get_ix_networks_for_asn(64500)) known_ix_networks = [1, 2] found_ix_networks = [] ix_networks = api.get_ix_networks_for_asn(asn) for ix_network in ix_networks: found_ix_networks.append(ix_network.id) self.assertEqual(sorted(found_ix_networks), sorted(known_ix_networks))
class IXAPITest(TestCase): @classmethod def setUpTestData(cls): IXAPI.objects.bulk_create([ IXAPI( name="IXP 1", url="https://ixp1-ixapi.example.net/v1/", api_key="key-ixp1", api_secret="secret-ixp1", ), IXAPI( name="IXP 2", url="https://ixp2-ixapi.example.net/v2/", api_key="key-ixp2", api_secret="secret-ixp2", ), IXAPI( name="IXP 3", url="https://ixp3-ixapi.example.net/v3/", api_key="key-ixp3", api_secret="secret-ixp3", ), ]) cls.ix_api = IXAPI.objects.get(name="IXP 1") def test_version(self): self.assertEqual(1, IXAPI.objects.get(name="IXP 1").version) self.assertEqual(2, IXAPI.objects.get(name="IXP 2").version) self.assertEqual(3, IXAPI.objects.get(name="IXP 3").version) @patch( "requests.post", return_value=MockedResponse(content={ "access_token": "1234", "refresh_token": "1234" }), ) def test_dial(self, *_): c = self.ix_api.dial() self.assertEqual("1234", c.access_token) self.assertEqual("1234", c.refresh_token) @patch( "requests.post", return_value=MockedResponse(content={ "access_token": "1234", "refresh_token": "1234" }), ) def test_get_health(self, *_): with self.assertRaises(NotImplementedError): self.ix_api.get_health() i = IXAPI.objects.get(name="IXP 2") with patch( "requests.get", return_value=MockedResponse(content={"status": "up"}), ): self.assertEqual("healthy", i.get_health()) with patch( "requests.get", return_value=MockedResponse(content={"status": "warn"}), ): self.assertEqual("degraded", i.get_health()) with patch( "requests.get", return_value=MockedResponse(content={"status": "error"}), ): self.assertEqual("unhealthy", i.get_health()) @patch( "requests.post", return_value=MockedResponse(content={ "access_token": "1234", "refresh_token": "1234" }), ) def test_get_customers(self, *_): with patch( "requests.get", return_value=MockedResponse( fixture="extras/tests/fixtures/ix_api/customers.json"), ): c = self.ix_api.get_customers() self.assertEqual(2, len(c)) self.assertEqual("1234", c[0]["id"]) self.assertEqual("5678", c[1]["id"]) @patch( "requests.post", return_value=MockedResponse(content={ "access_token": "1234", "refresh_token": "1234" }), ) def test_get_identity(self, *_): with patch( "requests.get", return_value=MockedResponse(content=[{ "id": "1234", "name": "Customer 1" }]), ): self.assertIsNone(self.ix_api.get_identity()) self.ix_api.identity = "1234" self.assertEqual("1234", self.ix_api.get_identity()["id"]) @patch( "requests.post", return_value=MockedResponse(content={ "access_token": "1234", "refresh_token": "1234" }), ) def test_get_ips(self, *_): with patch( "requests.get", return_value=MockedResponse( fixture="extras/tests/fixtures/ix_api/ips.json"), ): i = self.ix_api.get_ips() self.assertEqual("1234", i[0].id) self.assertEqual("5678", i[1].id) self.assertIsInstance(i[0].network, ipaddress.IPv6Network) self.assertIsInstance(i[1].network, ipaddress.IPv4Network) self.assertIsInstance(i[0].address, ipaddress.IPv6Address) self.assertIsInstance(i[1].address, ipaddress.IPv4Address) @patch( "requests.post", return_value=MockedResponse(content={ "access_token": "1234", "refresh_token": "1234" }), ) def test_get_macs(self, *_): with patch( "requests.get", return_value=MockedResponse( fixture="extras/tests/fixtures/ix_api/macs.json"), ): i = self.ix_api.get_macs() self.assertEqual("1234", i[0].id) self.assertEqual("AA:BB:CC:DD:EE:FF", i[0].address) @patch( "requests.post", return_value=MockedResponse(content={ "access_token": "1234", "refresh_token": "1234" }), ) def test_get_network_features(self, *_): with patch( "requests.get", return_value=MockedResponse( fixture="extras/tests/fixtures/ix_api/network_features.json" ), ): i = self.ix_api.get_network_features() self.assertEqual("1234", i[0].id) self.assertEqual(64500, i[0].asn) self.assertTrue(i[0].required) @patch( "requests.post", return_value=MockedResponse(content={ "access_token": "1234", "refresh_token": "1234" }), ) def test_get_network_service_configs(self, *_): with patch( "requests.get", return_value=MockedResponse( fixture= "extras/tests/fixtures/ix_api/network_service_configs.json" ), ): i = self.ix_api.get_network_service_configs() self.assertEqual("1234", i[0].id) self.assertEqual("production", i[0].state) @patch( "requests.post", return_value=MockedResponse(content={ "access_token": "1234", "refresh_token": "1234" }), ) def test_get_network_services(self, *_): with patch( "requests.get", return_value=MockedResponse( fixture="extras/tests/fixtures/ix_api/network_services.json" ), ): i = self.ix_api.get_network_services() self.assertEqual("1234", i[0].id) self.assertEqual(1234, i[0].peeringdb_ixid) @patch( "requests.post", return_value=MockedResponse(content={ "access_token": "1234", "refresh_token": "1234" }), ) def test_get_products(self, *_): with patch( "requests.get", return_value=MockedResponse( fixture="extras/tests/fixtures/ix_api/products.json"), ): self.assertEqual("1234", self.ix_api.get_products()[0]["id"])
class IXAPITest(StandardAPITestCases.View): model = IXAPI brief_fields = ["id", "display", "name", "url"] create_data = [ { "name": "IXP 4", "url": "https://ixp4-ixapi.example.net", "api_key": "key-ixp4", "api_secret": "secret-ixp4", "identity": "1234", }, { "name": "IXP 5", "url": "https://ixp5-ixapi.example.net", "api_key": "key-ixp5", "api_secret": "secret-ixp5", "identity": "1234", }, { "name": "IXP 6", "url": "https://ixp6-ixapi.example.net", "api_key": "key-ixp6", "api_secret": "secret-ixp6", "identity": "1234", }, ] @classmethod def setUpTestData(cls): IXAPI.objects.bulk_create([ IXAPI( name="IXP 1", url="https://ixp1-ixapi.example.net/v1/", api_key="key-ixp1", api_secret="secret-ixp1", identity="1234", ), IXAPI( name="IXP 2", url="https://ixp2-ixapi.example.net/v2/", api_key="key-ixp2", api_secret="secret-ixp2", identity="1234", ), IXAPI( name="IXP 3", url="https://ixp3-ixapi.example.net/v3/", api_key="key-ixp3", api_secret="secret-ixp3", identity="1234", ), ]) @patch( "requests.post", return_value=MockedResponse(content={ "access_token": "1234", "refresh_token": "1234" }), ) def test_customers(self, *_): ixapi = IXAPI.objects.get(name="IXP 1") mocked = MockedResponse( fixture="extras/tests/fixtures/ix_api/customers.json") url = reverse("extras-api:ixapi-customers") with patch("requests.get", return_value=mocked): # Query params required response = self.client.get(url, format="json", **self.header) self.assertHttpStatus(response, status.HTTP_400_BAD_REQUEST) # With query params response = self.client.get(url, data={ "url": ixapi.url, "api_key": ixapi.api_key, "api_secret": ixapi.api_secret, }, format="json", **self.header) self.assertHttpStatus(response, status.HTTP_200_OK) self.assertListEqual(mocked.json(), response.json())
class PeeringDBHTTPTestCase(TestCase): def test_get_last_synchronization(self): api = PeeringDB() # Test when no sync has been done self.assertIsNone(api.get_last_synchronization()) # Test of sync record with no objects time_of_sync = timezone.now() api.record_last_sync(time_of_sync, { "added": 0, "updated": 0, "deleted": 0 }) self.assertEqual(api.get_last_sync_time(), 0) # Test of sync record with one object time_of_sync = timezone.now() api.record_last_sync(time_of_sync, { "added": 1, "updated": 0, "deleted": 0 }) self.assertEqual( int(api.get_last_synchronization().time.timestamp()), int(time_of_sync.timestamp()), ) def test_time_last_sync(self): api = PeeringDB() # Test when no sync has been done self.assertEqual(api.get_last_sync_time(), 0) # Test of sync record with no objects time_of_sync = timezone.now() api.record_last_sync(time_of_sync, { "added": 0, "updated": 0, "deleted": 0 }) self.assertEqual(api.get_last_sync_time(), 0) # Test of sync record with one object time_of_sync = timezone.now() api.record_last_sync(time_of_sync, { "added": 1, "updated": 0, "deleted": 0 }) self.assertEqual(api.get_last_sync_time(), int(time_of_sync.timestamp())) @patch("peeringdb.http.requests.get", side_effect=mocked_synchronization) def test_update_local_database(self, *_): sync_result = PeeringDB().update_local_database(0) self.assertEqual(8, sync_result.added) self.assertEqual(0, sync_result.updated) self.assertEqual(0, sync_result.deleted) def test_clear_local_database(self): try: PeeringDB().clear_local_database() except Exception: self.fail("Unexpected exception raised.") @patch( "peeringdb.http.requests.get", return_value=MockedResponse( fixture="peeringdb/tests/fixtures/as65536.json"), ) def test_get_autonomous_system(self, *_): api = PeeringDB() asn = 65536 # Must not exist with patch("peeringdb.http.requests.get", return_value=MockedResponse(status_code=404)): self.assertIsNone(api.get_autonomous_system(64500)) # Using an API call (no cached data) autonomous_system = api.get_autonomous_system(asn) self.assertEqual(autonomous_system.asn, asn) # Save the data inside the cache details = { "id": autonomous_system.id, "asn": autonomous_system.asn, "name": autonomous_system.name, } network = Network(**details) network.save() # Using no API calls (cached data) autonomous_system = api.get_autonomous_system(asn) self.assertEqual(autonomous_system.asn, asn) @patch( "peeringdb.http.requests.get", return_value=MockedResponse( fixture="peeringdb/tests/fixtures/netixlan.json"), ) def test_get_ix_network(self, *_): api = PeeringDB() ix_network_id = 1 with patch("peeringdb.http.requests.get", return_value=MockedResponse(status_code=404)): # Must not exist self.assertIsNone(api.get_ix_network(0)) # Using an API call (no cached data) ix_network = api.get_ix_network(ix_network_id) self.assertEqual(ix_network.id, ix_network_id) # Save the data inside the cache details = { "id": ix_network.id, "asn": ix_network.asn, "name": ix_network.name, "ix_id": ix_network.ix_id, "ixlan_id": ix_network.ixlan_id, } network_ixlan = NetworkIXLAN(**details) network_ixlan.save() # Using no API calls (cached data) ix_network = api.get_ix_network(ix_network_id) self.assertEqual(ix_network.id, ix_network_id) @patch( "peeringdb.http.requests.get", return_value=MockedResponse( fixture="peeringdb/tests/fixtures/netixlan.json"), ) def test_get_ix_network_by_ip_address(self, *_): api = PeeringDB() ipv6_address = "2001:db8:1337::1" ipv4_address = "203.0.113.1" ix_network_id = 1 # No IP given we cannot guess what the user wants self.assertIsNone(api.get_ix_network_by_ip_address()) # Using an API call (no cached data) ix_network = api.get_ix_network_by_ip_address( ipv6_address=ipv6_address) self.assertEqual(ix_network.id, ix_network_id) ix_network = api.get_ix_network_by_ip_address( ipv4_address=ipv4_address) self.assertEqual(ix_network.id, ix_network_id) ix_network = api.get_ix_network_by_ip_address( ipv6_address=ipv6_address, ipv4_address=ipv4_address) self.assertEqual(ix_network.id, ix_network_id) # Save the data inside the cache details = { "id": ix_network.id, "asn": ix_network.asn, "name": ix_network.name, "ipaddr6": ipv6_address, "ipaddr4": ipv4_address, "ix_id": ix_network.ix_id, "ixlan_id": ix_network.ixlan_id, } network_ixlan = NetworkIXLAN(**details) network_ixlan.save() # Using no API calls (cached data) ix_network = api.get_ix_network_by_ip_address( ipv6_address=ipv6_address) self.assertEqual(ix_network.id, ix_network_id) ix_network = api.get_ix_network_by_ip_address( ipv4_address=ipv4_address) self.assertEqual(ix_network.id, ix_network_id) ix_network = api.get_ix_network_by_ip_address( ipv6_address=ipv6_address, ipv4_address=ipv4_address) self.assertEqual(ix_network.id, ix_network_id) @patch( "peeringdb.http.requests.get", return_value=MockedResponse( fixture="peeringdb/tests/fixtures/netixlans_as65536.json"), ) def test_get_ix_networks_for_asn(self, *_): api = PeeringDB() asn = 65536 with patch("peeringdb.http.requests.get", return_value=MockedResponse(status_code=404)): # Must not exist self.assertIsNone(api.get_ix_networks_for_asn(64500)) known_ix_networks = [1, 2] found_ix_networks = [] ix_networks = api.get_ix_networks_for_asn(asn) for ix_network in ix_networks: found_ix_networks.append(ix_network.id) self.assertEqual(sorted(found_ix_networks), sorted(known_ix_networks)) @patch("peeringdb.http.requests.get", side_effect=mocked_get_autonomous_system) def test_get_common_ix_networks_for_asns(self, *_): api = PeeringDB() asn1 = 65536 asn2 = 65537 with patch("peeringdb.http.requests.get", return_value=MockedResponse(status_code=404)): # Empty list should be returned self.assertFalse(api.get_common_ix_networks_for_asns(asn1, 64500)) # Found common IX networks found_ix_networks = [] for n1, n2 in api.get_common_ix_networks_for_asns(asn1, asn2): self.assertEqual(n1.ixlan_id, n2.ixlan_id) found_ix_networks.append(n1.ixlan_id) self.assertEqual([1], found_ix_networks) @patch( "peeringdb.http.requests.get", return_value=MockedResponse( fixture="peeringdb/tests/fixtures/ixpfx.json"), ) def test_get_prefixes_for_ix_network(self, *_): api = PeeringDB() ix_network_id = 29146 with patch("peeringdb.http.requests.get", return_value=MockedResponse(status_code=404)): # Must be empty self.assertFalse(api.get_prefixes_for_ix_network(0)) known_prefixes = [ ipaddress.ip_network("2001:db8:1337::/64"), ipaddress.ip_network("203.0.113.0/24"), ] found_prefixes = [] for ix_prefix in api.get_prefixes_for_ix_network(ix_network_id): self.assertIn(ix_prefix, known_prefixes) def test_get_peers_for_ix(self): api = PeeringDB() with patch("peeringdb.http.requests.get", return_value=MockedResponse(status_code=404)): # Must not be found self.assertIsNone(api.get_peers_for_ix(0)) with patch( "peeringdb.http.requests.get", return_value=MockedResponse( fixture="peeringdb/tests/fixtures/netixlan_by_ix_id.json"), ): # Must have some peers self.assertEqual(len(api.get_peers_for_ix(1)), 2)
def mocked_peeringdb(*args, **kwargs): if "asn" in kwargs["params"] and kwargs["params"]["asn"] == 65536: return MockedResponse(fixture="peeringdb/tests/fixtures/as65536.json") return MockedResponse(status_code=404)
def mocked_synchronization(*args, **kwargs): namespace = args[0].split("/")[-1] if namespace in NAMESPACES: return MockedResponse(fixture=f"peeringdb/tests/fixtures/{namespace}.json") return MockedResponse(status_code=500)