def get_uni_mocked(**kwargs): """Create an uni mocked. Args: interface_name(str): Interface name. Defaults to "eth1". interface_port(int): Interface pror. Defaults to 1. tag_type(int): Type of a tag. Defaults to 1. tag_value(int): Value of a tag. Defaults to 81 is_valid(bool): Value returned by is_valid method. Defaults to False. """ interface_name = kwargs.get("interface_name", "eth1") interface_port = kwargs.get("interface_port", 1) tag_type = kwargs.get("tag_type", 1) tag_value = kwargs.get("tag_value", 81) is_valid = kwargs.get("is_valid", False) switch = Mock(spec=Switch) switch.id = kwargs.get("switch_id", "custom_switch_id") switch.dpid = kwargs.get("switch_dpid", "custom_switch_dpid") interface = Interface(interface_name, interface_port, switch) tag = TAG(tag_type, tag_value) uni = Mock(spec=UNI, interface=interface, user_tag=tag) uni.is_valid.return_value = is_valid uni.as_dict.return_value = { "interface_id": f'switch_mock:{interface_port}', "tag": tag.as_dict() } return uni
class TestTAG(unittest.TestCase): """TAG tests.""" def setUp(self): """Create TAG object.""" self.tag = TAG(1, 123) def test_from_dict(self): """Test from_dict method.""" tag_dict = {'tag_type': 2, 'value': 456} tag = self.tag.from_dict(tag_dict) self.assertEqual(tag.tag_type, 2) self.assertEqual(tag.value, 456) def test_as_dict(self): """Test as_dict method.""" self.assertEqual(self.tag.as_dict(), {'tag_type': 1, 'value': 123}) def test_from_json(self): """Test from_json method.""" tag_json = '{"tag_type": 2, "value": 456}' tag = self.tag.from_json(tag_json) self.assertEqual(tag.tag_type, 2) self.assertEqual(tag.value, 456) def test_as_json(self): """Test as_json method.""" self.assertEqual(self.tag.as_json(), '{"tag_type": 1, "value": 123}') def test__repr__(self): """Test __repr__ method.""" self.assertEqual(repr(self.tag), 'TAG(<TAGType.VLAN: 1>, 123)')
def test_interface_is_tag_available(self): """Test is_tag_available on Interface class.""" max_range = 4096 for i in range(1, max_range): tag = TAG(TAGType.VLAN, i) next_tag = self.iface.is_tag_available(tag) self.assertTrue(next_tag) # test lower limit tag = TAG(TAGType.VLAN, 0) self.assertFalse(self.iface.is_tag_available(tag)) # test upper limit tag = TAG(TAGType.VLAN, max_range) self.assertFalse(self.iface.is_tag_available(tag))
def test__eq__(self): """Test __eq__ method.""" user_tag = TAG(2, 456) interface = Interface('name', 2, MagicMock()) other = UNI(interface, user_tag) self.assertFalse(self.uni.__eq__(other))
def setUp(self): """Create UNI object.""" switch = MagicMock() switch.dpid = '00:00:00:00:00:00:00:01' interface = Interface('name', 1, switch) user_tag = TAG(1, 123) self.uni = UNI(interface, user_tag)
def uni_from_dict(uni_dict, controller): """Create UNI instance from a dictionary.""" intf = MaintenanceWindow.intf_from_dict(uni_dict['interface_id'], controller) tag = TAG.from_dict(uni_dict['tag']) if intf and tag: return UNI(intf, tag) return None
def test_is_valid(self): """Test is_valid method for a valid, invalid and none tag.""" self.assertTrue(self.uni.is_valid()) with self.assertRaises(ValueError): TAG(999999, 123) self.uni.user_tag = None self.assertTrue(self.uni.is_valid())
def link_from_dict(link_dict, controller): """Create a link instance from a dictionary.""" endpoint_a = controller.get_interface_by_id( link_dict['endpoint_a']['id']) endpoint_b = controller.get_interface_by_id( link_dict['endpoint_b']['id']) link = Link(endpoint_a, endpoint_b) if 'metadata' in link_dict: link.extend_metadata(link_dict['metadata']) s_vlan = link.get_metadata('s_vlan') if s_vlan: tag = TAG.from_dict(s_vlan) link.update_metadata('s_vlan', tag) return link
def test_interface_use_tags(self): """Test all use_tag on Interface class.""" tag = TAG(TAGType.VLAN, 100) # check use tag for the first time is_success = self.iface.use_tag(tag) self.assertTrue(is_success) # check use tag for the second time is_success = self.iface.use_tag(tag) self.assertFalse(is_success) # check use tag after returning the tag to the pool self.iface.make_tag_available(tag) is_success = self.iface.use_tag(tag) self.assertTrue(is_success)
def uni_from_dict(self, uni_dict): """Return a UNI object from python dict.""" if uni_dict is None: return False interface_id = uni_dict.get("interface_id") interface = self.controller.get_interface_by_id(interface_id) if interface is None: raise ValueError(f'Could not instantiate interface {interface_id}') tag_dict = uni_dict.get("tag") tag = TAG.from_dict(tag_dict) if tag is False: raise ValueError(f'Could not instantiate tag from dict {tag_dict}') uni = UNI(interface, tag) return uni
def _uni_from_dict(self, uni_dict): """Return a UNI object from python dict.""" if uni_dict is None: return False interface_id = uni_dict.get("interface_id") interface = self.controller.get_interface_by_id(interface_id) if interface is None: raise ValueError(f'Could not instantiate interface {interface_id}') try: tag_dict = uni_dict["tag"] except KeyError: tag = None else: tag = TAG.from_dict(tag_dict) uni = UNI(interface, tag) return uni
def _link_from_dict(self, link_dict): """Return a Link object from python dict.""" id_a = link_dict.get('endpoint_a').get('id') id_b = link_dict.get('endpoint_b').get('id') endpoint_a = self.controller.get_interface_by_id(id_a) endpoint_b = self.controller.get_interface_by_id(id_b) link = Link(endpoint_a, endpoint_b) if 'metadata' in link_dict: link.extend_metadata(link_dict.get('metadata')) s_vlan = link.get_metadata('s_vlan') if s_vlan: tag = TAG.from_dict(s_vlan) if tag is False: error_msg = f'Could not instantiate tag from dict {s_vlan}' raise ValueError(error_msg) link.update_metadata('s_vlan', tag) return link
def _link_from_dict(self, link_dict): """Return a Link object from python dict.""" id_a = link_dict.get("endpoint_a").get("id") id_b = link_dict.get("endpoint_b").get("id") endpoint_a = self.controller.get_interface_by_id(id_a) endpoint_b = self.controller.get_interface_by_id(id_b) link = Link(endpoint_a, endpoint_b) if "metadata" in link_dict: link.extend_metadata(link_dict.get("metadata")) s_vlan = link.get_metadata("s_vlan") if s_vlan: tag = TAG.from_dict(s_vlan) if tag is False: error_msg = f"Could not instantiate tag from dict {s_vlan}" raise ValueError(error_msg) link.update_metadata("s_vlan", tag) return link
def uni_from_dict(self, uni_dict): """Return a UNI object from python dict.""" if uni_dict is None: return False interface_id = uni_dict.get("interface_id") interface = self.controller.get_interface_by_id(interface_id) if interface is None: return False tag = TAG.from_dict(uni_dict.get("tag")) if tag is False: return False try: uni = UNI(interface, tag) except TypeError: return False return uni
def _uni_from_dict(self, uni_dict): """Return a UNI object from python dict.""" if uni_dict is None: return False interface_id = uni_dict.get("interface_id") interface = self.controller.get_interface_by_id(interface_id) if interface is None: result = ( "Error creating UNI:" + f"Could not instantiate interface {interface_id}" ) raise ValueError(result) from ValueError tag_dict = uni_dict.get("tag", None) if tag_dict: tag = TAG.from_dict(tag_dict) else: tag = None uni = UNI(interface, tag) return uni
def setUp(self): """Create TAG object.""" self.tag = TAG(1, 123)