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)
Beispiel #2
0
    def test_deploy_to_backup_path1(self, discover_new_paths_mocked,
                                    deploy_to_path_mocked):
        """Test deployment when dynamic_backup_path is False in same switch"""
        uni_a = get_uni_mocked(interface_port=2, tag_value=82, is_valid=True)
        uni_z = get_uni_mocked(interface_port=3, tag_value=83, is_valid=True)

        switch = Mock(spec=Switch)
        uni_a.interface.switch = switch
        uni_z.interface.switch = switch

        attributes = {
            "controller": get_controller_mock(),
            "name": "custom_name",
            "uni_a": uni_a,
            "uni_z": uni_z,
            "enabled": True,
            "dynamic_backup_path": False
        }

        evc = EVC(**attributes)
        discover_new_paths_mocked.return_value = []
        deploy_to_path_mocked.return_value = True

        # storehouse initialization mock
        evc._storehouse.box = Mock()  # pylint: disable=protected-access
        evc._storehouse.box.data = {}  # pylint: disable=protected-access

        deployed = evc.deploy_to_backup_path()

        deploy_to_path_mocked.assert_called_once_with()
        self.assertEqual(deployed, True)
    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_deploy_to_case_2(self, install_uni_flows_mocked,
                              install_nni_flows_mocked, deploy_mocked):
        """Test deploy with all links up."""
        deploy_mocked.return_value = True

        primary_path = [
            get_link_mocked(status=EntityStatus.UP),
            get_link_mocked(status=EntityStatus.UP)
        ]
        attributes = {
            "controller": get_controller_mock(),
            "name": "circuit_name",
            "uni_a": get_uni_mocked(is_valid=True),
            "uni_z": get_uni_mocked(is_valid=True),
            "primary_path": primary_path,
            "enabled": True
        }
        evc = EVC(**attributes)

        # storehouse mock
        evc._storehouse.box = Mock()  # pylint: disable=protected-access
        evc._storehouse.box.data = {}  # pylint: disable=protected-access

        deployed = evc.deploy_to('primary_path', evc.primary_path)
        install_uni_flows_mocked.assert_called_with(evc.primary_path)
        install_nni_flows_mocked.assert_called_with(evc.primary_path)
        self.assertTrue(deployed)
    def test_handle_link_up_case_4(self, *args):
        """Test if not path is found a dynamic path is used."""
        (_install_uni_flows_mocked, _install_nni_flows_mocked,
         get_best_path_mocked, deploy_to_path_mocked) = args

        deploy_to_path_mocked.return_value = True

        primary_path = [
            get_link_mocked(endpoint_a_port=9,
                            endpoint_b_port=10,
                            metadata={"s_vlan": 5},
                            status=EntityStatus.UP),
            get_link_mocked(endpoint_a_port=11,
                            endpoint_b_port=12,
                            metadata={"s_vlan": 6},
                            status=EntityStatus.DOWN),
        ]
        backup_path = [
            get_link_mocked(endpoint_a_port=13,
                            endpoint_b_port=14,
                            metadata={"s_vlan": 5},
                            status=EntityStatus.DOWN),
            get_link_mocked(endpoint_a_port=11,
                            endpoint_b_port=12,
                            metadata={"s_vlan": 6},
                            status=EntityStatus.DOWN),
        ]

        # Setup best_path mock
        best_path = Path()
        best_path.append(primary_path[0])
        get_best_path_mocked.return_value = best_path

        attributes = {
            "controller": get_controller_mock(),
            "name": "circuit_name",
            "uni_a": get_uni_mocked(is_valid=True),
            "uni_z": get_uni_mocked(is_valid=True),
            "primary_path": primary_path,
            "backup_path": backup_path,
            "enabled": True,
            "dynamic_backup_path": True
        }

        evc = EVC(**attributes)
        evc.current_path = Path([])

        # storehouse mock
        evc._storehouse.box = Mock()  # pylint: disable=protected-access
        evc._storehouse.box.data = {}  # pylint: disable=protected-access

        current_handle_link_up = evc.handle_link_up(backup_path[0])

        self.assertEqual(get_best_path_mocked.call_count, 0)
        self.assertEqual(deploy_to_path_mocked.call_count, 1)
        deploy_to_path_mocked.assert_called_once_with()
        self.assertTrue(current_handle_link_up)
Beispiel #6
0
    def test_handle_link_down_case_4(self, deploy_to_mocked,
                                     _send_flow_mods_mocked,
                                     get_best_path_mocked, deploy_mocked,
                                     log_mocked):
        """Test if circuit with dynamic path is return success."""
        deploy_mocked.return_value = True
        deploy_to_mocked.return_value = False
        primary_path = [
            get_link_mocked(endpoint_a_port=9,
                            endpoint_b_port=10,
                            metadata={"s_vlan": 5},
                            status=EntityStatus.DOWN),
            get_link_mocked(endpoint_a_port=11,
                            endpoint_b_port=12,
                            metadata={"s_vlan": 6},
                            status=EntityStatus.UP),
        ]
        backup_path = [
            get_link_mocked(endpoint_a_port=9,
                            endpoint_b_port=10,
                            metadata={"s_vlan": 5},
                            status=EntityStatus.DOWN),
            get_link_mocked(endpoint_a_port=11,
                            endpoint_b_port=12,
                            metadata={"s_vlan": 6},
                            status=EntityStatus.UP),
        ]
        attributes = {
            "controller": get_controller_mock(),
            "name": "circuit_name",
            "uni_a": get_uni_mocked(is_valid=True),
            "uni_z": get_uni_mocked(is_valid=True),
            "primary_path": primary_path,
            "backup_path": backup_path,
            "enabled": True,
            "dynamic_backup_path": True
        }

        evc = EVC(**attributes)
        evc.current_path = evc.backup_path

        # storehouse mock
        evc._storehouse.box = Mock()  # pylint: disable=protected-access
        evc._storehouse.box.data = {}  # pylint: disable=protected-access

        current_handle_link_down = evc.handle_link_down()

        self.assertEqual(get_best_path_mocked.call_count, 6)
        self.assertEqual(deploy_to_mocked.call_count, 1)
        deploy_to_mocked.assert_called_once_with('primary_path',
                                                 evc.primary_path)
        self.assertTrue(current_handle_link_down)
        msg = f"{evc} deployed after link down."
        log_mocked.debug.assert_called_with(msg)
Beispiel #7
0
    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)
Beispiel #8
0
 def test_circuit_representation(self):
     """Test the method __repr__."""
     attributes = {
         "controller": get_controller_mock(),
         "name": "circuit_name",
         "uni_a": get_uni_mocked(is_valid=True),
         "uni_z": get_uni_mocked(is_valid=True)
     }
     evc = EVC(**attributes)
     expected_value = f'EVC({evc.id}, {evc.name})'
     self.assertEqual(str(evc), expected_value)
Beispiel #9
0
 def test_with_invalid_uni_z(self):
     """Test if the EVC raises and error with UNI Z is required."""
     attributes = {
         "controller": get_controller_mock(),
         "name": "circuit_name",
         "uni_a": get_uni_mocked(is_valid=True),
         "uni_z": get_uni_mocked(tag_value=83)
     }
     error_message = "VLAN tag 83 is not available in uni_z"
     with self.assertRaises(ValueError) as handle_error:
         EVC(**attributes)
     self.assertEqual(str(handle_error.exception), error_message)
Beispiel #10
0
    def test_deploy_without_path_case1(self, *args):
        """Test if not path is found a dynamic path is used."""
        # pylint: disable=too-many-locals
        (discover_new_path_mocked, should_deploy_mock, activate_mock,
         install_uni_flows_mock, install_nni_flows, chose_vlans_mock,
         log_mock) = args

        should_deploy_mock.return_value = False
        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,
            "enabled": True,
            "dynamic_backup_path": True
        }

        dynamic_backup_path = Path([
            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)
        discover_new_path_mocked.return_value = dynamic_backup_path

        # storehouse initialization mock
        evc._storehouse.box = Mock()  # pylint: disable=protected-access
        evc._storehouse.box.data = {}  # pylint: disable=protected-access

        deployed = evc.deploy_to_path()

        self.assertEqual(should_deploy_mock.call_count, 1)
        self.assertEqual(discover_new_path_mocked.call_count, 1)
        self.assertEqual(activate_mock.call_count, 1)
        self.assertEqual(install_uni_flows_mock.call_count, 1)
        self.assertEqual(install_nni_flows.call_count, 1)
        self.assertEqual(chose_vlans_mock.call_count, 1)
        self.assertEqual(log_mock.info.call_count, 1)
        self.assertTrue(deployed)
Beispiel #11
0
    def test_deploy_successfully(self, *args):
        """Test if all methods to deploy are called."""
        # pylint: disable=too-many-locals
        (should_deploy_mock, activate_mock, install_uni_flows_mock,
         install_nni_flows, chose_vlans_mock, log_mock) = args

        should_deploy_mock.return_value = True
        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)

        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})
        ]

        attributes = {
            "controller": get_controller_mock(),
            "name": "custom_name",
            "uni_a": uni_a,
            "uni_z": uni_z,
            "primary_links": primary_links
        }
        # Setup path to deploy
        path = Path()
        path.append(primary_links[0])
        path.append(primary_links[1])

        evc = EVC(**attributes)

        # storehouse mock
        evc._storehouse.box = Mock()  # pylint: disable=protected-access
        evc._storehouse.box.data = {}  # pylint: disable=protected-access

        deployed = evc.deploy_to_path(path)

        self.assertEqual(should_deploy_mock.call_count, 1)
        self.assertEqual(activate_mock.call_count, 1)
        self.assertEqual(install_uni_flows_mock.call_count, 1)
        self.assertEqual(install_nni_flows.call_count, 1)
        self.assertEqual(chose_vlans_mock.call_count, 1)
        log_mock.info.assert_called_once_with(f"{evc} was deployed.")
        self.assertTrue(deployed)
    def test_handle_link_up_case_3(self, _install_uni_flows_mocked,
                                   _install_nni_flows_mocked,
                                   get_best_path_mocked, deploy_to_path_mocked,
                                   deploy_mocked):
        """Test if it is deployed after the backup is up."""
        deploy_mocked.return_value = True
        deploy_to_path_mocked.return_value = True
        primary_path = [
            get_link_mocked(endpoint_a_port=9,
                            endpoint_b_port=10,
                            metadata={"s_vlan": 5},
                            status=EntityStatus.DOWN),
            get_link_mocked(endpoint_a_port=11,
                            endpoint_b_port=12,
                            metadata={"s_vlan": 6},
                            status=EntityStatus.UP),
        ]
        backup_path = [
            get_link_mocked(endpoint_a_port=13,
                            endpoint_b_port=14,
                            metadata={"s_vlan": 5},
                            status=EntityStatus.DOWN),
            get_link_mocked(endpoint_a_port=11,
                            endpoint_b_port=12,
                            metadata={"s_vlan": 6},
                            status=EntityStatus.UP),
        ]
        attributes = {
            "controller": get_controller_mock(),
            "name": "circuit_name",
            "uni_a": get_uni_mocked(is_valid=True),
            "uni_z": get_uni_mocked(is_valid=True),
            "primary_path": primary_path,
            "backup_path": backup_path,
            "enabled": True,
            "dynamic_backup_path": True
        }

        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 = Path([])
        current_handle_link_up = evc.handle_link_up(backup_path[0])

        self.assertEqual(get_best_path_mocked.call_count, 0)
        self.assertEqual(deploy_mocked.call_count, 0)
        self.assertEqual(deploy_to_path_mocked.call_count, 1)
        deploy_to_path_mocked.assert_called_once_with(evc.backup_path)
        self.assertTrue(current_handle_link_up)
Beispiel #13
0
    def test_should_deploy_case1(log_mock):
        """Test should deploy method without primary links."""
        log_mock.debug.return_value = True
        attributes = {
            "controller": get_controller_mock(),
            "name": "custom_name",
            "uni_a": get_uni_mocked(is_valid=True),
            "uni_z": get_uni_mocked(is_valid=True)
        }

        evc = EVC(**attributes)
        evc.should_deploy()
        log_mock.debug.assert_called_with('Path is empty.')
Beispiel #14
0
    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)
Beispiel #15
0
    def test_should_deploy_case2(self, log_mock):
        """Test should deploy method with disable circuit."""
        log_mock.debug.return_value = True
        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()]
        }
        evc = EVC(**attributes)

        self.assertFalse(evc.should_deploy(attributes['primary_links']))
        log_mock.debug.assert_called_with(f'{evc} is disabled.')
Beispiel #16
0
 def test_should_deploy_case4(self, log_mock):
     """Test should deploy method with enabled and active circuit."""
     log_mock.debug.return_value = True
     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)
     self.assertFalse(evc.should_deploy(attributes['primary_links']))
Beispiel #17
0
    def test_deploy_fail(self, *args):
        """Test if all methods is ignored when the should_deploy is false."""
        # pylint: disable=too-many-locals
        (sync_mock, should_deploy_mock, activate_mock, install_uni_flows_mock,
         install_nni_flows, chose_vlans_mock, discover_new_path,
         log_mock) = args

        uni_a = get_uni_mocked(interface_port=2,
                               tag_value=82,
                               switch_id="switch_uni_a",
                               switch_dpid="switch_dpid_uni_a",
                               is_valid=True)
        uni_z = get_uni_mocked(interface_port=3,
                               tag_value=83,
                               switch_id="switch_uni_z",
                               switch_dpid="switch_dpid_uni_a",
                               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)
        deployed = evc.deploy_to_path()

        self.assertEqual(discover_new_path.call_count, 1)
        self.assertEqual(should_deploy_mock.call_count, 1)
        self.assertEqual(activate_mock.call_count, 0)
        self.assertEqual(install_uni_flows_mock.call_count, 0)
        self.assertEqual(install_nni_flows.call_count, 0)
        self.assertEqual(chose_vlans_mock.call_count, 0)
        self.assertEqual(log_mock.info.call_count, 0)
        self.assertEqual(sync_mock.call_count, 1)
        self.assertFalse(deployed)
Beispiel #18
0
 def test_update_uni_a(self):
     """Test if raises and error when trying to update the uni_a."""
     attributes = {
         "controller": get_controller_mock(),
         "name": "circuit_name",
         "uni_a": get_uni_mocked(is_valid=True),
         "uni_z": get_uni_mocked(is_valid=True)
     }
     update_dict = {
         "uni_a": get_uni_mocked(is_valid=True)
     }
     error_message = "uni_a can't be be updated."
     with self.assertRaises(ValueError) as handle_error:
         evc = EVC(**attributes)
         evc.update(**update_dict)
     self.assertEqual(str(handle_error.exception), error_message)
Beispiel #19
0
    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')
Beispiel #20
0
    def test_handle_link_down_case_3(self, get_paths_mocked, deploy_to_mocked,
                                     deploy_mocked, log_mocked):
        """Test if circuit without dynamic path is return failed."""
        deploy_mocked.return_value = False
        deploy_to_mocked.return_value = False
        primary_path = [
            get_link_mocked(endpoint_a_port=9,
                            endpoint_b_port=10,
                            metadata={"s_vlan": 5},
                            status=EntityStatus.DOWN),
            get_link_mocked(endpoint_a_port=11,
                            endpoint_b_port=12,
                            metadata={"s_vlan": 6},
                            status=EntityStatus.UP),
        ]
        backup_path = [
            get_link_mocked(endpoint_a_port=9,
                            endpoint_b_port=10,
                            metadata={"s_vlan": 5},
                            status=EntityStatus.DOWN),
            get_link_mocked(endpoint_a_port=11,
                            endpoint_b_port=12,
                            metadata={"s_vlan": 6},
                            status=EntityStatus.UP),
        ]
        attributes = {
            "controller": get_controller_mock(),
            "name": "circuit_name",
            "uni_a": get_uni_mocked(is_valid=True),
            "uni_z": get_uni_mocked(is_valid=True),
            "primary_path": primary_path,
            "backup_path": backup_path,
            "enabled": True
        }

        evc = EVC(**attributes)
        evc.current_path = evc.backup_path
        current_handle_link_down = evc.handle_link_down()

        self.assertEqual(get_paths_mocked.call_count, 0)
        self.assertEqual(deploy_mocked.call_count, 0)
        self.assertEqual(deploy_to_mocked.call_count, 1)
        deploy_to_mocked.assert_called_once_with('primary_path',
                                                 evc.primary_path)
        self.assertFalse(current_handle_link_down)
        msg = f'Failed to re-deploy {evc} after link down.'
        log_mocked.debug.assert_called_once_with(msg)
    def test_handle_link_down_case_1(self, path_status_mocked, deploy_mocked,
                                     deploy_to_mocked, _send_flow_mods_mocked,
                                     log_mocked):
        """Test if deploy_to backup path is called."""
        deploy_mocked.return_value = True
        path_status_mocked.side_effect = [EntityStatus.DOWN, EntityStatus.UP]

        primary_path = [
            get_link_mocked(endpoint_a_port=9,
                            endpoint_b_port=10,
                            metadata={"s_vlan": 5},
                            status=EntityStatus.DOWN),
            get_link_mocked(endpoint_a_port=11,
                            endpoint_b_port=12,
                            metadata={"s_vlan": 6},
                            status=EntityStatus.UP),
        ]
        backup_path = [
            get_link_mocked(endpoint_a_port=9,
                            endpoint_b_port=10,
                            metadata={"s_vlan": 5},
                            status=EntityStatus.UP),
            get_link_mocked(endpoint_a_port=11,
                            endpoint_b_port=12,
                            metadata={"s_vlan": 6},
                            status=EntityStatus.UP),
        ]
        attributes = {
            "controller": get_controller_mock(),
            "name": "circuit_name",
            "uni_a": get_uni_mocked(is_valid=True),
            "uni_z": get_uni_mocked(is_valid=True),
            "primary_path": primary_path,
            "backup_path": backup_path,
            "enabled": True
        }
        evc = EVC(**attributes)

        evc.current_path = evc.primary_path
        current_handle_link_down = evc.handle_link_down()
        self.assertEqual(deploy_mocked.call_count, 0)
        deploy_to_mocked.assert_called_once()

        self.assertTrue(current_handle_link_down)
        msg = f"{evc} deployed after link down."
        log_mocked.debug.assert_called_once_with(msg)
    def test_handle_link_up_case_2(self, deploy_to_path_mocked, deploy_mocked):
        """Test if it is changing from backup_path to primary_path."""
        deploy_mocked.return_value = True
        deploy_to_path_mocked.return_value = True
        primary_path = [
            get_link_mocked(endpoint_a_port=9,
                            endpoint_b_port=10,
                            metadata={"s_vlan": 5},
                            status=EntityStatus.UP),
            get_link_mocked(endpoint_a_port=11,
                            endpoint_b_port=12,
                            metadata={"s_vlan": 6},
                            status=EntityStatus.UP),
        ]
        backup_path = [
            get_link_mocked(endpoint_a_port=9,
                            endpoint_b_port=10,
                            metadata={"s_vlan": 5},
                            status=EntityStatus.UP),
            get_link_mocked(endpoint_a_port=11,
                            endpoint_b_port=12,
                            metadata={"s_vlan": 6},
                            status=EntityStatus.UP),
        ]
        attributes = {
            "controller": get_controller_mock(),
            "name": "circuit_name",
            "uni_a": get_uni_mocked(is_valid=True),
            "uni_z": get_uni_mocked(is_valid=True),
            "primary_path": primary_path,
            "backup_path": backup_path,
            "enabled": True,
            "dynamic_backup_path": True
        }

        evc = EVC(**attributes)
        evc.current_path = evc.backup_path
        current_handle_link_up = evc.handle_link_up(primary_path[0])
        self.assertEqual(deploy_mocked.call_count, 0)
        self.assertEqual(deploy_to_path_mocked.call_count, 1)
        deploy_to_path_mocked.assert_called_once_with(evc.primary_path)
        self.assertTrue(current_handle_link_up)
    def test_is_using_primary_path(self):
        """Test test is using primary path."""
        primary_path = [
            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})
        ]

        attributes = {
            "controller": get_controller_mock(),
            "name": "circuit_name",
            "uni_a": get_uni_mocked(is_valid=True),
            "uni_z": get_uni_mocked(is_valid=True),
            "primary_path": primary_path
        }

        evc = EVC(**attributes)
        self.assertFalse(evc.is_using_primary_path())
        evc.current_path = evc.primary_path
        self.assertTrue(evc.is_using_primary_path())
    def test_deploy_to_case_1(self, log_mocked):
        """Test if the path is equal to current_path."""
        primary_path = [
            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})
        ]
        attributes = {
            "controller": get_controller_mock(),
            "name": "circuit_name",
            "uni_a": get_uni_mocked(is_valid=True),
            "uni_z": get_uni_mocked(is_valid=True),
            "primary_path": primary_path
        }
        evc = EVC(**attributes)
        evc.current_path = evc.primary_path

        expected_deployed = evc.deploy_to('primary_path', evc.primary_path)
        expected_msg = 'primary_path is equal to current_path.'
        log_mocked.debug.assert_called_with(expected_msg)
        self.assertTrue(expected_deployed)
Beispiel #25
0
    def test_comparison_method(self):
        """Test the method __eq__."""
        attributes = {
            "controller": get_controller_mock(),
            "name": "circuit_name",
            "uni_a": get_uni_mocked(is_valid=True),
            "uni_z": get_uni_mocked(is_valid=True)
        }
        evc1 = EVC(**attributes)
        evc2 = EVC(**attributes)

        attributes = {
            "controller": get_controller_mock(),
            "name": "circuit_name_2",
            "uni_a": get_uni_mocked(is_valid=True),
            "uni_z": get_uni_mocked(is_valid=True)
        }
        evc3 = EVC(**attributes)
        evc4 = EVC(**attributes)

        self.assertEqual(evc1 == evc2, True)
        self.assertEqual(evc1 == evc3, False)
        self.assertEqual(evc2 == evc3, False)
        self.assertEqual(evc3 == evc4, True)
    def test_deploy_to_case_3(self, requests_mocked):
        # pylint: disable=unused-argument
        """Test deploy with one link down."""
        link1 = get_link_mocked()
        link2 = get_link_mocked()
        link1.id = 'abc'
        link2.id = 'def'
        primary_path = [link1, link2]
        attributes = {
            "controller": get_controller_mock(),
            "name": "circuit_name",
            "uni_a": get_uni_mocked(is_valid=True),
            "uni_z": get_uni_mocked(is_valid=True),
            "primary_path": primary_path,
            "enabled": True
        }
        evc = EVC(**attributes)

        # storehouse initialization mock
        evc._storehouse.box = Mock()  # pylint: disable=protected-access
        evc._storehouse.box.data = {}  # pylint: disable=protected-access

        deployed = evc.deploy_to('primary_path', evc.primary_path)
        self.assertFalse(deployed)
Beispiel #27
0
    def test_as_dict(self):
        """Test the method as_dict."""
        attributes = {
            "controller": get_controller_mock(),
            "id": "custom_id",
            "name": "custom_name",
            "uni_a": get_uni_mocked(is_valid=True),
            "uni_z": get_uni_mocked(is_valid=True),
            "start_date": '2018-08-21T18:44:54',
            "end_date": '2018-08-21T18:44:55',
            'primary_links': [],
            'request_time': '2018-08-21T19:10:41',
            'creation_time': '2018-08-21T18:44:54',
            'owner': "my_name",
            'circuit_scheduler': [
                CircuitSchedule.from_dict({"id": 234243247, "action": "create",
                                          "frequency": "1 * * * *"}),
                CircuitSchedule.from_dict({"id": 234243239, "action": "create",
                                          "interval": {"hours": 2}})
            ],
            'enabled': True,
            'priority': 2
        }
        evc = EVC(**attributes)

        expected_dict = {
            'id': 'custom_id',
            'name': 'custom_name',
            'uni_a': attributes['uni_a'].as_dict(),
            'uni_z': attributes['uni_z'].as_dict(),
            'start_date': '2018-08-21T18:44:54',
            'end_date': '2018-08-21T18:44:55',
            'bandwidth': 0,
            'primary_links': [],
            'backup_links': [],
            'current_path': [],
            'primary_path': [],
            'backup_path': [],
            'dynamic_backup_path': False,
            'request_time': '2018-08-21T19:10:41',
            'creation_time': '2018-08-21T18:44:54',
            'circuit_scheduler': [
                {
                    "id": 234243247,
                    "action": "create",
                    "frequency": "1 * * * *"
                },
                {
                    "id": 234243239,
                    "action": "create",
                    "interval": {
                        "hours": 2
                    }
                }
            ],
            'active': False,
            'enabled': True,
            'priority': 2
        }
        actual_dict = evc.as_dict()
        for name, value in expected_dict.items():
            actual = actual_dict.get(name)
            self.assertEqual(value, actual)
Beispiel #28
0
    def test_update_circuit(self, *args):
        """Test update a circuit circuit."""
        (evc_as_dict_mock, uni_from_dict_mock, evc_deploy, *mocks) = args

        for mock in mocks:
            mock.return_value = True
        unis = [get_uni_mocked(switch_dpid='00:00:00:00:00:00:00:01'),
                get_uni_mocked(switch_dpid='00:00:00:00:00:00:00:02')]
        uni_from_dict_mock.side_effect = 2 * unis

        api = self.get_app_test_client(self.napp)
        payloads = [
            {
                "name": "my evc1",
                "active": True,
                "uni_a": {
                    "interface_id": "00:00:00:00:00:00:00:01:1",
                    "tag": {
                        "tag_type": 1,
                        "value": 80
                    }
                },
                "uni_z": {
                    "interface_id": "00:00:00:00:00:00:00:02:2",
                    "tag": {
                        "tag_type": 1,
                        "value": 1
                    }
                }
            },
            {
                "primary_path": [
                    {
                        "endpoint_a": {"id": "00:00:00:00:00:00:00:01:1"},
                        "endpoint_b": {"id": "00:00:00:00:00:00:00:02:2"}
                    }
                ]
            },
            {
                "priority": 3
            },
            {
                "enable": True
            }
        ]

        evc_as_dict_mock.return_value = payloads[0]
        response = api.post(f'{self.server_name_url}/v2/evc/',
                            data=json.dumps(payloads[0]),
                            content_type='application/json')
        self.assertEqual(201, response.status_code)

        evc_deploy.reset_mock()
        evc_as_dict_mock.return_value = payloads[1]
        current_data = json.loads(response.data)
        circuit_id = current_data['circuit_id']
        response = api.patch(f'{self.server_name_url}/v2/evc/{circuit_id}',
                             data=json.dumps(payloads[1]),
                             content_type='application/json')
        evc_deploy.assert_called_once()
        self.assertEqual(200, response.status_code)

        evc_deploy.reset_mock()
        evc_as_dict_mock.return_value = payloads[2]
        response = api.patch(f'{self.server_name_url}/v2/evc/{circuit_id}',
                             data=json.dumps(payloads[2]),
                             content_type='application/json')
        evc_deploy.assert_not_called()
        self.assertEqual(200, response.status_code)

        evc_deploy.reset_mock()
        response = api.patch(f'{self.server_name_url}/v2/evc/{circuit_id}',
                             data='{"priority":5,}',
                             content_type='application/json')
        evc_deploy.assert_not_called()
        self.assertEqual(400, response.status_code)

        evc_deploy.reset_mock()
        response = api.patch(f'{self.server_name_url}/v2/evc/{circuit_id}',
                             data=json.dumps(payloads[3]),
                             content_type='application/json')
        evc_deploy.assert_called_once()
        self.assertEqual(200, response.status_code)

        response = api.patch(f'{self.server_name_url}/v2/evc/1234',
                             data=json.dumps(payloads[1]),
                             content_type='application/json')
        current_data = json.loads(response.data)
        expected_data = f'circuit_id 1234 not found'
        self.assertEqual(current_data['description'], expected_data)
        self.assertEqual(404, response.status_code)

        api.delete(f'{self.server_name_url}/v2/evc/{circuit_id}')
        evc_deploy.reset_mock()
        response = api.patch(f'{self.server_name_url}/v2/evc/{circuit_id}',
                             data=json.dumps(payloads[1]),
                             content_type='application/json')
        evc_deploy.assert_not_called()
        self.assertEqual(405, response.status_code)