def test_thrifttype_sensitivity(self): thrift_obj = openr_types.PrefixDatabase() thrift_obj.thisNodeName = "some node" raw_msg = serializer.serialize_thrift_object(thrift_obj) recovered_obj = serializer.deserialize_thrift_object( raw_msg, openr_types.PrefixEntry) self.assertTrue(thrift_obj != recovered_obj)
def test_exception_handling(self): thrift_obj = openr_types.PrefixDatabase() thrift_obj.thisNodeName = "some node" raw_msg = serializer.serialize_thrift_object(thrift_obj) # should raise exception due to inconsistency of protocol factor with self.assertRaises(Exception): serializer.deserialize_thrift_object( raw_msg, openr_types.PrefixDatabase, TJSONProtocolFactory )
def test_reverse_equality(self): for _ in range(100): thrift_obj = openr_types.PrefixDatabase() random_string = "".join( random.choice(string.digits) for _ in range(10)) thrift_obj.thisNodeName = random_string raw_msg = serializer.serialize_thrift_object(thrift_obj) recovered_obj = serializer.deserialize_thrift_object( raw_msg, openr_types.PrefixDatabase) self.assertEqual(thrift_obj, recovered_obj) for _ in range(100): thrift_obj = openr_types.PrefixDatabase() random_string = "".join( random.choice(string.digits) for _ in range(10)) thrift_obj.thisNodeName = random_string raw_msg = serializer.serialize_thrift_object( thrift_obj, TJSONProtocolFactory) recovered_obj = serializer.deserialize_thrift_object( raw_msg, openr_types.PrefixDatabase, TJSONProtocolFactory) self.assertEqual(thrift_obj, recovered_obj)
def test_parse_prefix_database(self): bgp1 = openr_types.PrefixEntry( prefix=ipnetwork.ip_str_to_prefix("1.0.0.0/8"), type=network_types.PrefixType.BGP, ) bgp2 = openr_types.PrefixEntry( prefix=ipnetwork.ip_str_to_prefix("2.0.0.0/8"), type=network_types.PrefixType.BGP, ) loop1 = openr_types.PrefixEntry( prefix=ipnetwork.ip_str_to_prefix("10.0.0.1/32"), type=network_types.PrefixType.LOOPBACK, ) prefix_db = openr_types.PrefixDatabase( thisNodeName="node1", prefixEntries=[bgp1, bgp2, loop1], deletePrefix=False, perfEvents=None, ) # No filter and ensure we receive everything back data = {} parse_prefix_database("", "", data, prefix_db) self.assertEqual(data["node1"].prefixEntries, [bgp1, bgp2, loop1]) # Filter on prefix data = {} parse_prefix_database("10.0.0.1/32", "", data, prefix_db) self.assertEqual(data["node1"].prefixEntries, [loop1]) # Filter on type data = {} parse_prefix_database("", "bgp", data, prefix_db) self.assertEqual(data["node1"].prefixEntries, [bgp1, bgp2]) # Filter on prefix and type both data = {} parse_prefix_database("2.0.0.0/8", "bgp", data, prefix_db) self.assertEqual(data["node1"].prefixEntries, [bgp2])