def test_install_nni_flows(send_flow_mods_mock): """Test install nni flows method. This test will verify the flows send to the send_flow_mods method. """ uni_a = get_uni_mocked(interface_port=2, tag_value=82, switch_id="switch_uni_a", is_valid=True) uni_z = get_uni_mocked(interface_port=3, tag_value=83, switch_id="switch_uni_z", is_valid=True) attributes = { "controller": get_controller_mock(), "name": "custom_name", "uni_a": uni_a, "uni_z": uni_z, "primary_links": [ get_link_mocked(endpoint_a_port=9, endpoint_b_port=10, metadata={"s_vlan": 5}), get_link_mocked(endpoint_a_port=11, endpoint_b_port=12, metadata={"s_vlan": 6}) ] } evc = EVC(**attributes) # pylint: disable=protected-access evc._install_nni_flows(attributes['primary_links']) in_vlan = evc.primary_links[0].get_metadata('s_vlan').value out_vlan = evc.primary_links[-1].get_metadata('s_vlan').value in_port = evc.primary_links[0].endpoint_b.port_number out_port = evc.primary_links[-1].endpoint_a.port_number expected_flow_mods = [ { 'match': {'in_port': in_port, 'dl_vlan': in_vlan}, 'cookie': evc.get_cookie(), 'actions': [ {'action_type': 'set_vlan', 'vlan_id': out_vlan}, {'action_type': 'output', 'port': out_port} ] }, { 'match': {'in_port': out_port, 'dl_vlan': out_vlan}, 'cookie': evc.get_cookie(), 'actions': [ {'action_type': 'set_vlan', 'vlan_id': in_vlan}, {'action_type': 'output', 'port': in_port} ] } ] switch = evc.primary_links[0].endpoint_b.switch send_flow_mods_mock.assert_called_once_with(switch, expected_flow_mods)
def test_prepare_pop_flow(self): """Test prepare pop flow method.""" attributes = { "controller": get_controller_mock(), "name": "custom_name", "uni_a": get_uni_mocked(interface_port=1, is_valid=True), "uni_z": get_uni_mocked(interface_port=2, is_valid=True), } evc = EVC(**attributes) interface_a = evc.uni_a.interface interface_z = evc.uni_z.interface in_vlan = 10 # pylint: disable=protected-access flow_mod = evc._prepare_pop_flow(interface_a, interface_z, in_vlan) expected_flow_mod = { 'match': { 'in_port': interface_a.port_number, 'dl_vlan': in_vlan }, 'cookie': evc.get_cookie(), 'actions': [{ 'action_type': 'pop_vlan' }, { 'action_type': 'output', 'port': interface_z.port_number }] } self.assertEqual(expected_flow_mod, flow_mod)
def test_prepare_flow_mod(self): """Test prepare flow_mod method.""" interface_a = Interface('eth0', 1, Mock(spec=Switch)) interface_z = Interface('eth1', 3, Mock(spec=Switch)) attributes = { "controller": get_controller_mock(), "name": "custom_name", "uni_a": get_uni_mocked(is_valid=True), "uni_z": get_uni_mocked(is_valid=True), "primary_links": [get_link_mocked(), get_link_mocked()], "enabled": True, "active": True } evc = EVC(**attributes) # pylint: disable=protected-access flow_mod = evc._prepare_flow_mod(interface_a, interface_z) expected_flow_mod = { 'match': { 'in_port': interface_a.port_number }, 'cookie': evc.get_cookie(), 'actions': [{ 'action_type': 'output', 'port': interface_z.port_number }] } self.assertEqual(expected_flow_mod, flow_mod)
def test_prepare_pop_flow(self): """Test prepare pop flow method.""" attributes = { "controller": get_controller_mock(), "name": "custom_name", "uni_a": get_uni_mocked(interface_port=1, is_valid=True), "uni_z": get_uni_mocked(interface_port=2, is_valid=True), } evc = EVC(**attributes) interface_a = evc.uni_a.interface interface_z = evc.uni_z.interface in_vlan = 10 # pylint: disable=protected-access flow_mod = evc._prepare_pop_flow(interface_a, interface_z, None, in_vlan) expected_flow_mod = { "match": { "in_port": interface_a.port_number, "dl_vlan": in_vlan }, "cookie": evc.get_cookie(), "actions": [ { "action_type": "pop_vlan" }, { "action_type": "output", "port": interface_z.port_number }, ], } self.assertEqual(expected_flow_mod, flow_mod)
def test_get_id_from_cookie(): """Test get_id_from_cookie.""" attributes = { "controller": get_controller_mock(), "name": "circuit_name", "enable": True, "uni_a": get_uni_mocked(is_valid=True), "uni_z": get_uni_mocked(is_valid=True) } evc = EVC(**attributes) evc_id = evc.id assert evc_id assert evc.get_id_from_cookie(evc.get_cookie()) == evc_id
def test_prepare_push_flow(self): """Test prepare push flow method.""" attributes = { "controller": get_controller_mock(), "name": "custom_name", "uni_a": get_uni_mocked(interface_port=1, is_valid=True), "uni_z": get_uni_mocked(interface_port=2, is_valid=True), } evc = EVC(**attributes) interface_a = evc.uni_a.interface interface_z = evc.uni_z.interface out_vlan_a = 20 for in_vlan_a in (10, None): for in_vlan_z in (3, None): with self.subTest(in_vlan_a=in_vlan_a, in_vlan_z=in_vlan_z): # pylint: disable=protected-access flow_mod = evc._prepare_push_flow(interface_a, interface_z, in_vlan_a, out_vlan_a, in_vlan_z) expected_flow_mod = { 'match': {'in_port': interface_a.port_number}, 'cookie': evc.get_cookie(), 'actions': [ {'action_type': 'push_vlan', 'tag_type': 's'}, {'action_type': 'set_vlan', 'vlan_id': out_vlan_a}, { 'action_type': 'output', 'port': interface_z.port_number } ] } if in_vlan_a and in_vlan_z: expected_flow_mod['match']['dl_vlan'] = in_vlan_a expected_flow_mod['actions'].insert(0, { 'action_type': 'set_vlan', 'vlan_id': in_vlan_z }) elif in_vlan_a: expected_flow_mod['match']['dl_vlan'] = in_vlan_a expected_flow_mod['actions'].insert(0, { 'action_type': 'pop_vlan' }) elif in_vlan_z: expected_flow_mod['actions'].insert(0, { 'action_type': 'set_vlan', 'vlan_id': in_vlan_z }) expected_flow_mod['actions'].insert(0, { 'action_type': 'push_vlan', 'tag_type': 'c' }) self.assertEqual(expected_flow_mod, flow_mod)
def test_get_id_from_cookie_with_leading_zeros(): """Test get_id_from_cookie with leading zeros.""" attributes = { "controller": get_controller_mock(), "name": "circuit_name", "enable": True, "uni_a": get_uni_mocked(is_valid=True), "uni_z": get_uni_mocked(is_valid=True) } evc = EVC(**attributes) evc_id = "0a2d672d99ff41" # pylint: disable=protected-access evc._id = evc_id # pylint: enable=protected-access assert EVC.get_id_from_cookie(evc.get_cookie()) == evc_id
def test_remove_current_flows(self, send_flow_mods_mocked): """Test remove current flows.""" uni_a = get_uni_mocked(interface_port=2, tag_value=82, switch_id="switch_uni_a", is_valid=True) uni_z = get_uni_mocked(interface_port=3, tag_value=83, switch_id="switch_uni_z", is_valid=True) switch_a = Switch('00:00:00:00:00:01') switch_b = Switch('00:00:00:00:00:02') switch_c = Switch('00:00:00:00:00:03') attributes = { "controller": get_controller_mock(), "name": "custom_name", "uni_a": uni_a, "uni_z": uni_z, "active": True, "enabled": True, "primary_links": [ get_link_mocked(switch_a=switch_a, switch_b=switch_b, endpoint_a_port=9, endpoint_b_port=10, metadata={"s_vlan": 5}), get_link_mocked(switch_a=switch_b, switch_b=switch_c, endpoint_a_port=11, endpoint_b_port=12, metadata={"s_vlan": 6}) ] } evc = EVC(**attributes) # storehouse initialization mock evc._storehouse.box = Mock() # pylint: disable=protected-access evc._storehouse.box.data = {} # pylint: disable=protected-access evc.current_path = evc.primary_links evc.remove_current_flows() self.assertEqual(send_flow_mods_mocked.call_count, 5) self.assertFalse(evc.is_active()) flows = [{'cookie': evc.get_cookie(), 'cookie_mask': 18446744073709551615}] switch_1 = evc.primary_links[0].endpoint_a.switch switch_2 = evc.primary_links[0].endpoint_b.switch send_flow_mods_mocked.assert_any_call(switch_1, flows, 'delete') send_flow_mods_mocked.assert_any_call(switch_2, flows, 'delete')
def test_prepare_push_flow(self): """Test prepare push flow method.""" attributes = { "controller": get_controller_mock(), "name": "custom_name", "uni_a": get_uni_mocked(interface_port=1, is_valid=True), "uni_z": get_uni_mocked(interface_port=2, is_valid=True), } evc = EVC(**attributes) interface_a = evc.uni_a.interface interface_z = evc.uni_z.interface out_vlan_a = 20 for in_vlan_a in (10, None): with self.subTest(in_vlan_a=in_vlan_a): # pylint: disable=protected-access flow_mod = evc._prepare_push_flow(interface_a, interface_z, in_vlan_a, out_vlan_a) expected_flow_mod = { "match": { "in_port": interface_a.port_number }, "cookie": evc.get_cookie(), "actions": [ { "action_type": "push_vlan", "tag_type": "s" }, { "action_type": "set_vlan", "vlan_id": out_vlan_a }, { "action_type": "output", "port": interface_z.port_number, }, ], } if in_vlan_a: expected_flow_mod["match"]["dl_vlan"] = in_vlan_a expected_flow_mod["actions"].insert( 0, {"action_type": "pop_vlan"}) self.assertEqual(expected_flow_mod, flow_mod)
def test_install_nni_flows(send_flow_mods_mock): """Test install nni flows method. This test will verify the flows send to the send_flow_mods method. """ uni_a = get_uni_mocked( interface_port=2, tag_value=82, switch_id="switch_uni_a", is_valid=True, ) uni_z = get_uni_mocked( interface_port=3, tag_value=83, switch_id="switch_uni_z", is_valid=True, ) attributes = { "controller": get_controller_mock(), "name": "custom_name", "uni_a": uni_a, "uni_z": uni_z, "primary_links": [ get_link_mocked( endpoint_a_port=9, endpoint_b_port=10, metadata={"s_vlan": 5}, ), get_link_mocked( endpoint_a_port=11, endpoint_b_port=12, metadata={"s_vlan": 6}, ), ], } evc = EVC(**attributes) # pylint: disable=protected-access evc._install_nni_flows(attributes["primary_links"]) in_vlan = evc.primary_links[0].get_metadata("s_vlan").value out_vlan = evc.primary_links[-1].get_metadata("s_vlan").value in_port = evc.primary_links[0].endpoint_b.port_number out_port = evc.primary_links[-1].endpoint_a.port_number expected_flow_mods = [ { "match": { "in_port": in_port, "dl_vlan": in_vlan }, "cookie": evc.get_cookie(), "actions": [ { "action_type": "set_vlan", "vlan_id": out_vlan }, { "action_type": "output", "port": out_port }, ], }, { "match": { "in_port": out_port, "dl_vlan": out_vlan }, "cookie": evc.get_cookie(), "actions": [ { "action_type": "set_vlan", "vlan_id": in_vlan }, { "action_type": "output", "port": in_port }, ], }, ] switch = evc.primary_links[0].endpoint_b.switch send_flow_mods_mock.assert_called_once_with(switch, expected_flow_mods)