def test_get_from_cache_prefix1(self): """Test case where we dont' have a matching prefix in cache""" ip = IP(self.IP) # clear out db to test specific function self.mock_cache.clear() self.assertFalse(ip.cached_prefix_found) self.assertEquals(ip.get_from_cached_prefix(), None)
def test_get_from_cache_prefix1(self): """Test case where we dont' have a matching prefix in cache""" ip = IP(self.IP) # clear out db to test specific function self.db = {} self.assertFalse(ip.cached_prefix_found) self.assertEquals(ip.get_from_cached_prefix(), None)
def test_loopback6(self): """IPv6 loopback address""" det = IP("::1") self.assertEquals(det.asn, None) self.assertEquals(det.is_querable(), False) # no query to stat self.assertEquals(self.mock_get.call_count, 0) # no access to cache get self.assertEquals(self.mock_cache.get.call_count, 0)
def test_get_from_cache_prefix(self): """Test case where we have a matching prefix in cache""" details = {'Prefix': '193.0.0.0/21', 'Holder': 'test', 'ASN': 'test'} self.mock_cache.set("IPDetailsPrefix:193.0.0.0/20", details, 1) ip = IP(self.IP) self.assertTrue(ip.cached_prefix_found) self.assertEquals(ip.asn, "test") self.assertEquals(ip.holder, "test") self.assertEquals(ip.get_from_cached_prefix(), details)
def test_invalid_query_stat2(self): """Test case where stat returns exception""" self.mock_get.side_effect = requests.exceptions.RequestException ip = IP(self.IP) self.assertEqual(ip.query_stat(), {}) self.mock_get.return_value = FakeErrorResponse() ip = IP(self.IP) self.assertEqual(ip.query_stat(), {})
def test_valid_query_stat(self): """Test case for valid stat response""" ip = IP(self.IP) self.assertEqual( ip.query_stat(), { 'Prefix': '193.0.0.0/21', 'Holder': 'RIPE-NCC-AS Reseaux IP Europeens Network Coordination Centre (RIPE NCC),NL', 'ASN': '3333' })
def test_valid_query_stat(self): """Test case for valid stat response""" ip = IP(self.IP) self.assertEqual( ip.query_stat(), { 'Prefix': '193.0.0.0/21', 'Holder': 'RIPE-NCC-AS Reseaux IP Europeens Network Coordination Centre (RIPE NCC),NL', 'ASN': '3333' } )
def test_fakecache_sameip(self): """Fake cache, same IP""" det = IP(self.IP) det = IP(self.IP) self.assertEquals(det.asn, self.ASN) # query to stat self.assertEquals(self.mock_get.call_count, 1) # access to cache get self.assertEquals(self.mock_cache.get.call_count, 2) # access to cache set self.assertEquals(self.mock_cache.set.call_count, 2)
def test_fakecache_sameprefix(self): """Fake cache, same prefix""" det1 = IP(self.IP) det2 = IP(self.SAME_PREFIX_IP) self.assertEquals(det1.asn, self.ASN) self.assertEquals(det2.asn, det1.asn) # query to stat self.assertEquals(self.mock_get.call_count, 1) # access to cache get self.assertEquals(self.mock_cache.get.call_count, 3) # access to cache set self.assertEquals(self.mock_cache.set.call_count, 3)
def test_get_from_cache_prefix(self): """Test case where we have a matching prefix in cache""" details = { 'Prefix': '193.0.0.0/21', 'Holder': 'test', 'ASN': 'test' } self.mock_cache.set("IPDetailsPrefix:193.0.0.0/20", details, 1) ip = IP(self.IP) self.assertTrue(ip.cached_prefix_found) self.assertEquals(ip.asn, "test") self.assertEquals(ip.holder, "test") self.assertEquals(ip.get_from_cached_prefix(), details)
def test_invalid_query_stat(self): """Test case where stat returns not ok status""" self.mock_get.return_value = FakeResponse( json_return={"status": "notok"} ) ip = IP(self.IP) self.assertEqual(ip.query_stat(), {}) self.mock_get.return_value = FakeResponse( json_return={} ) ip = IP(self.IP) self.assertEqual(ip.query_stat(), {})
def test_fakecache_notannounced(self): """Fake cache, IP not announced""" self.mock_get.return_value = FakeResponse( json_return=self.MOCK_RESULTS[self.NOT_ANNOUNCED_IP]) det = IP(self.NOT_ANNOUNCED_IP) self.assertEquals(det.asn, None) self.assertEquals(self.mock_get.call_count, 1) IP(self.NOT_ANNOUNCED_IP) # now it should be cached self.assertEquals(det.asn, None) self.assertEquals(self.mock_get.call_count, 2)
def test_fakecache_diffprefix(self): """Fake cache, same AS, different prefix""" self.mock_get.return_value = FakeResponse( json_return=self.MOCK_RESULTS[self.IP]) det1 = IP(self.IP) self.mock_get.return_value = FakeResponse( json_return=self.MOCK_RESULTS[self.SAME_AS_DIFFERENT_PREFIX_IP]) det2 = IP(self.SAME_AS_DIFFERENT_PREFIX_IP) self.assertEquals(det1.asn, self.ASN) self.assertEquals(det2.asn, det1.asn) self.assertEquals(self.mock_get.call_count, 2) # access to cache get self.assertEquals(self.mock_cache.get.call_count, 3) # access to cache set self.assertEquals(self.mock_cache.set.call_count, 4)
def test_nocache(self): """No cache""" det = IP(self.IP) self.assertEquals(det.asn, self.ASN) self.assertEquals(det.prefix, self.PREFIX) self.assertEquals(det.holder, self.HOLDER) # query to stat self.assertEquals(self.mock_get.call_count, 1) # access to cache get self.assertEquals(self.mock_cache.get.call_count, 1) # access to cache set self.assertEquals(self.mock_cache.set.call_count, 2)
def test_update_cache1(self): """Test case where we store only address""" details = { 'Prefix': '193.0.0.0/21', 'Holder': 'RIPE-NCC-AS Reseaux IP Europeens Network Coordination Centre (RIPE NCC),NL', 'ASN': '3333' } self.mock_cache.set("IPDetailsPrefix:193.0.0.0/21", details, 1) IP(self.IP) # we already called it above, so it should be 2 by bow self.assertEquals(self.mock_cache.set.call_count, 2) self.assertEquals(self.mock_cache.get("IPDetails:193.0.6.1"), details) self.assertEquals(self.mock_cache.get("IPDetailsPrefix:193.0.0.0/21"), details)
def test_update_cache(self): """Test case where we store both prefix/address""" details = { 'Prefix': '193.0.0.0/21', 'Holder': 'RIPE-NCC-AS Reseaux IP Europeens Network Coordination Centre (RIPE NCC),NL', 'ASN': '3333' } IP(self.IP) self.assertEquals(self.mock_cache.set.call_count, 2) self.assertEquals(self.mock_cache.get("IPDetails:193.0.6.1"), details) self.assertEquals(self.mock_cache.get("IPDetailsPrefix:193.0.0.0/21"), details)
def test_invalid_query_stat1(self): """Test case where stat returns not valid data structure""" # no data at all self.mock_get.return_value = FakeResponse( json_return={"status": "ok"} ) ip = IP(self.IP) self.assertEqual(ip.query_stat(), {}) # data is dict but no asns key self.mock_get.return_value = FakeResponse( json_return={"status": "ok", "data": {"bla": "bla"}} ) ip = IP(self.IP) self.assertEqual(ip.query_stat(), {}) # data is not dict self.mock_get.return_value = FakeResponse( json_return={"status": "ok", "data": [1, 2]} ) ip = IP(self.IP) self.assertEqual(ip.query_stat(), {}) # asns is not list self.mock_get.return_value = FakeResponse( json_return={"status": "ok", "data": {"asns": {}}} ) ip = IP(self.IP) self.assertEqual(ip.query_stat(), {}) # asns is empty self.mock_get.return_value = FakeResponse( json_return={"status": "ok", "data": {"asns": [{}]}} ) ip = IP(self.IP) self.assertEqual(ip.query_stat(), {})
def test_invalid_query_stat(self): """Test case where stat returns not ok status""" self.mock_get.return_value = FakeResponse( json_return={"status": "notok"}) ip = IP(self.IP) self.assertEqual(ip.query_stat(), {}) self.mock_get.return_value = FakeResponse(json_return={}) ip = IP(self.IP) self.assertEqual(ip.query_stat(), {})
def test_is_querable1(self): """Test case where IP is not quearable""" ip = IP("127.0.0.1") self.assertFalse(ip.is_querable())
def test_is_querable(self): """Test case where IP is quearable""" ip = IP(self.IP) self.assertTrue(ip.is_querable())
def test_invalid_query_stat1(self): """Test case where stat returns not valid data structure""" # no data at all self.mock_get.return_value = FakeResponse(json_return={"status": "ok"}) ip = IP(self.IP) self.assertEqual(ip.query_stat(), {}) # data is dict but no asns key self.mock_get.return_value = FakeResponse(json_return={ "status": "ok", "data": { "bla": "bla" } }) ip = IP(self.IP) self.assertEqual(ip.query_stat(), {}) # data is not dict self.mock_get.return_value = FakeResponse(json_return={ "status": "ok", "data": [1, 2] }) ip = IP(self.IP) self.assertEqual(ip.query_stat(), {}) # asns is not list self.mock_get.return_value = FakeResponse(json_return={ "status": "ok", "data": { "asns": {} } }) ip = IP(self.IP) self.assertEqual(ip.query_stat(), {}) # asns is empty self.mock_get.return_value = FakeResponse(json_return={ "status": "ok", "data": { "asns": [{}] } }) ip = IP(self.IP) self.assertEqual(ip.query_stat(), {})