def test_new_circuit_with_run_time( self, validate_mock, scheduler_add_job_mock ): """Test if add new circuit with run_time.""" scheduler_add_job_mock.return_value = True validate_mock.return_value = True time_fmt = "%Y-%m-%dT%H:%M:%S" date = datetime.datetime.now().strftime(time_fmt) circuit_scheduler = CircuitSchedule(action="remove", date=date) options = { "controller": get_controller_mock(), "name": "my evc1", "uni_a": "uni_a", "uni_z": "uni_z", "circuit_scheduler": [circuit_scheduler], } evc = EVC(**options) self.scheduler.add(evc) expected_parameters = { "id": circuit_scheduler.id, "run_date": circuit_scheduler.date, } scheduler_add_job_mock.assert_called_once_with( evc.remove, "date", **expected_parameters )
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_new_circuit_with_interval( self, validate_mock, scheduler_add_job_mock ): """Test if add new circuit with interval.""" scheduler_add_job_mock.return_value = True validate_mock.return_value = True interval = {"hours": 2, "minutes": 3} circuit_scheduler = CircuitSchedule(action="create", interval=interval) options = { "controller": get_controller_mock(), "name": "my evc1", "uni_a": "uni_a", "uni_z": "uni_z", "start_date": "2019-08-09T19:25:06", "circuit_scheduler": [circuit_scheduler], } evc = EVC(**options) self.scheduler.add(evc) expected_parameters = { "id": circuit_scheduler.id, "hours": 2, "minutes": 3, "end_date": None, "start_date": datetime.datetime( 2019, 8, 9, 19, 25, 6, 0, tzinfo=datetime.timezone.utc ), } scheduler_add_job_mock.assert_called_once_with( evc.deploy, "interval", **expected_parameters )
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_attributes_empty(self): """Test if the EVC raises an error with name is required.""" attributes = {"controller": get_controller_mock()} error_message = "name is required." with self.assertRaises(ValueError) as handle_error: EVC(**attributes) self.assertEqual(str(handle_error.exception), error_message)
def test_deploy_to_case_2( self, install_uni_flows_mocked, install_nni_flows_mocked, deploy_mocked, _, requests_mock, ): """Test deploy with all links up.""" deploy_mocked.return_value = True response = MagicMock() response.status_code = 201 requests_mock.return_value = response primary_path = [ get_link_mocked(status=EntityStatus.UP), get_link_mocked(status=EntityStatus.UP), ] attributes = { "controller": get_controller_mock(), "name": "circuit_4", "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) 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_is_using_backup_path(self): """Test test is using backup path.""" attributes = { "controller": get_controller_mock(), "name": "circuit_1", "uni_a": get_uni_mocked(is_valid=True), "uni_z": get_uni_mocked(is_valid=True), "backup_path": [ get_link_mocked( endpoint_a_port=10, endpoint_b_port=9, metadata={"s_vlan": 5}, ), get_link_mocked( endpoint_a_port=12, endpoint_b_port=11, metadata={"s_vlan": 6}, ), ], } evc = EVC(**attributes) self.assertFalse(evc.is_using_backup_path()) evc.current_path = evc.backup_path self.assertTrue(evc.is_using_backup_path())
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=13, endpoint_b_port=14, metadata={"s_vlan": 6}, status=EntityStatus.UP, ), ] attributes = { "controller": get_controller_mock(), "name": "circuit_7", "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 deploy_to_mocked.reset_mock() 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) 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_without_uni_a(self): """Test if the EVC raises and error with UNI A is required.""" attributes = { "controller": get_controller_mock(), "name": "circuit_name", } error_message = "uni_a is required." with self.assertRaises(ValueError) as handle_error: EVC(**attributes) self.assertEqual(str(handle_error.exception), error_message)
def test_handle_link_down_case_2(self, path_status_mocked, deploy_to_mocked, deploy_mocked, log_mocked): """Test if deploy_to backup path is called.""" deploy_mocked.return_value = True deploy_to_mocked.return_value = True path_status_mocked.side_effect = [EntityStatus.UP, EntityStatus.DOWN] primary_path = [ get_link_mocked( endpoint_a_port=7, endpoint_b_port=8, 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=7, endpoint_b_port=10, metadata={"s_vlan": 5}, status=EntityStatus.DOWN, ), get_link_mocked( endpoint_a_port=15, endpoint_b_port=12, metadata={"s_vlan": 6}, status=EntityStatus.UP, ), ] attributes = { "controller": get_controller_mock(), "name": "circuit_13", "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 deploy_to_mocked.reset_mock() 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_with_invalid_uni_a(self): """Test if the EVC raises and error with invalid UNI A.""" attributes = { "controller": get_controller_mock(), "name": "circuit_name", "uni_a": get_uni_mocked(tag_value=82), } error_message = "VLAN tag 82 is not available in uni_a" with self.assertRaises(ValueError) as handle_error: EVC(**attributes) self.assertEqual(str(handle_error.exception), error_message)
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)
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_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.")
def test_update_queue(self, _sync_mock): """Test if evc is set to redeploy.""" 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), } update_dict = {"queue_id": 3} evc = EVC(**attributes) _, redeploy = evc.update(**update_dict) self.assertTrue(redeploy)
def test_update_disable(self, _sync_mock): """Test if evc is disabled.""" 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), } update_dict = {"enable": False} evc = EVC(**attributes) evc.update(**update_dict) self.assertIs(evc.is_enabled(), False)
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=14, metadata={"s_vlan": 5}, status=EntityStatus.UP, ), get_link_mocked( endpoint_a_port=15, endpoint_b_port=12, metadata={"s_vlan": 6}, status=EntityStatus.UP, ), ] attributes = { "controller": get_controller_mock(), "name": "circuit_10", "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 deploy_to_path_mocked.reset_mock() 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_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.")
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"]))
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_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_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_is_using_primary_path(self): """Test test is using primary path.""" primary_path = [ get_link_mocked(endpoint_a_port=10, endpoint_b_port=9, metadata={"s_vlan": 5}), get_link_mocked(endpoint_a_port=12, endpoint_b_port=11, metadata={"s_vlan": 6}), ] attributes = { "controller": get_controller_mock(), "name": "circuit_2", "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_new_circuit_with_frequency( self, validate_mock, scheduler_add_job_mock, trigger_mock ): """Test if add new circuit with frequency.""" scheduler_add_job_mock.return_value = True validate_mock.return_value = True frequency = "* * * * *" circuit_scheduler = CircuitSchedule( action="create", frequency=frequency ) trigger = CronTrigger.from_crontab( circuit_scheduler.frequency, timezone=utc ) trigger_mock.return_value = trigger options = { "controller": get_controller_mock(), "name": "my evc1", "uni_a": "uni_a", "uni_z": "uni_z", "start_date": "2019-08-09T19:25:06", "circuit_scheduler": [circuit_scheduler], } evc = EVC(**options) self.scheduler.add(evc) expected_parameters = { "id": circuit_scheduler.id, "end_date": None, "start_date": datetime.datetime( 2019, 8, 9, 19, 25, 6, 0, tzinfo=datetime.timezone.utc ), } scheduler_add_job_mock.assert_called_once_with( evc.deploy, trigger, **expected_parameters )
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=10, endpoint_b_port=9, metadata={"s_vlan": 5}), get_link_mocked(endpoint_a_port=12, endpoint_b_port=11, metadata={"s_vlan": 6}), ] attributes = { "controller": get_controller_mock(), "name": "circuit_3", "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)
def test_update_read_only(self): """Test if raises an error when trying to update read only attr.""" 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_attr = [ ("archived", True), ("_id", True), ("active", True), ("current_path", []), ("creation_time", "date"), ] for name, value in update_attr: with self.subTest(name=name, value=value): update_dict = {name: value} error_message = f"{name} can't be updated." with self.assertRaises(ValueError) as handle_error: evc = EVC(**attributes) evc.update(**update_dict) self.assertEqual(str(handle_error.exception), error_message)
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_5", "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)
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_12", "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 deploy_to_path_mocked.reset_mock() 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)
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=9, endpoint_b_port=14, metadata={"s_vlan": 5}, status=EntityStatus.DOWN, ), get_link_mocked( endpoint_a_port=15, endpoint_b_port=12, metadata={"s_vlan": 6}, status=EntityStatus.UP, ), ] attributes = { "controller": get_controller_mock(), "name": "circuit_11", "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([]) deploy_to_path_mocked.reset_mock() 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)