def test_compute_delete(self, mock_driver): mock_driver.delete.side_effect = [ mock.DEFAULT, exceptions.OctaviaException('boom'), mock.DEFAULT, exceptions.OctaviaException('boom'), exceptions.OctaviaException('boom'), exceptions.OctaviaException('boom'), exceptions.OctaviaException('boom')] delete_compute = compute_tasks.ComputeDelete() # Limit the retry attempts for the test run to save time delete_compute.execute.retry.stop = tenacity.stop_after_attempt(2) delete_compute.execute(_amphora_mock) mock_driver.delete.assert_called_once_with(COMPUTE_ID) # Test retry after a compute exception mock_driver.reset_mock() delete_compute.execute(_amphora_mock) mock_driver.delete.assert_has_calls([mock.call(COMPUTE_ID), mock.call(COMPUTE_ID)]) # Test passive failure mock_driver.reset_mock() delete_compute.execute(_amphora_mock, passive_failure=True) mock_driver.delete.assert_has_calls([mock.call(COMPUTE_ID), mock.call(COMPUTE_ID)]) # Test non-passive failure mock_driver.reset_mock() self.assertRaises(exceptions.OctaviaException, delete_compute.execute, _amphora_mock, passive_failure=False)
def test_update_exception(self, mock_update): mock_update.side_effect = [exceptions.OctaviaException()] update_json = {'description': 'Some availability zone.'} body = self._build_body(update_json) response = self.put(self.AZ_PATH.format(az_name='bogus'), body, status=500) self.assertEqual('An unknown exception occurred.', response.json.get('faultstring'))
def test_update_exception(self, mock_update): mock_update.side_effect = [exceptions.OctaviaException()] update_json = {'name': 'A_Flavor'} body = self._build_body(update_json) response = self.put(self.FLAVOR_PATH.format(flavor_id='bogus'), body, status=500) self.assertEqual('An unknown exception occurred.', response.json.get('faultstring'))
def test_delete_amphorae_on_load_balancer(self, mock_driver): mock_driver.delete.side_effect = [mock.DEFAULT, exceptions.OctaviaException('boom')] delete_amps = compute_tasks.DeleteAmphoraeOnLoadBalancer() delete_amps.execute(_load_balancer_mock) mock_driver.delete.assert_called_once_with(COMPUTE_ID) # Test compute driver exception is raised self.assertRaises(exceptions.OctaviaException, delete_amps.execute, _load_balancer_mock)
def test_decrement_quota(self, mock_get_session, mock_dec_quota): mock_session = mock.MagicMock() mock_get_session.return_value = mock_session mock_dec_quota.side_effect = [mock.DEFAULT, exceptions.OctaviaException('Boom')] self.driver_updater._decrement_quota(self.mock_lb_repo, 'FakeName', self.lb_id) mock_dec_quota.assert_called_once_with( mock_session, self.mock_lb_repo.model_class.__data_model__, self.lb_project_id) mock_session.commit.assert_called_once() mock_session.rollback.assert_not_called() # Test exception path mock_dec_quota.reset_mock() mock_session.reset_mock() self.assertRaises(exceptions.OctaviaException, self.driver_updater._decrement_quota, self.mock_lb_repo, 'FakeName', self.lb_id) mock_dec_quota.assert_called_once_with( mock_session, self.mock_lb_repo.model_class.__data_model__, self.lb_project_id) mock_session.commit.assert_not_called() mock_session.rollback.assert_called_once() # Test already deleted path mock_dec_quota.reset_mock() mock_session.reset_mock() # Create a local mock LB and LB_repo for this test mock_lb = mock.MagicMock() mock_lb.id = self.lb_id mock_lb.provisioning_status = lib_consts.DELETED mock_lb_repo = mock.MagicMock() mock_lb_repo.model_class.__data_model__ = self.lb_data_model mock_lb_repo.get.return_value = mock_lb self.driver_updater._decrement_quota(mock_lb_repo, 'FakeName', self.lb_id) mock_dec_quota.assert_not_called() mock_session.commit.assert_not_called() mock_session.rollback.assert_called_once()
def _test_decrement_quota(self, task, data_model, mock_check_quota_met, mock_decrement_quota, mock_get_session, project_id=None): test_object = None if project_id: test_object = project_id else: project_id = uuidutils.generate_uuid() test_object = mock.MagicMock() test_object.project_id = project_id # execute without exception mock_decrement_quota.reset_mock() with mock.patch('octavia.db.api.' 'get_session') as mock_get_session_local: mock_session = mock.MagicMock() mock_get_session_local.return_value = mock_session if data_model == data_models.Pool: task.execute(test_object, self.zero_pool_child_count) else: task.execute(test_object) mock_decrement_quota.assert_called_once_with( mock_session, data_model, project_id) mock_session.commit.assert_called_once_with() # execute with exception mock_decrement_quota.reset_mock() with mock.patch('octavia.db.api.' 'get_session') as mock_get_session_local: mock_session = mock.MagicMock() mock_get_session_local.return_value = mock_session mock_decrement_quota.side_effect = ( exceptions.OctaviaException('fail')) if data_model == data_models.Pool: self.assertRaises(exceptions.OctaviaException, task.execute, test_object, self.zero_pool_child_count) else: self.assertRaises(exceptions.OctaviaException, task.execute, test_object) mock_decrement_quota.assert_called_once_with( mock_session, data_model, project_id) mock_session.rollback.assert_called_once_with() # revert with instance of failure mock_get_session.reset_mock() mock_check_quota_met.reset_mock() if data_model == data_models.Pool: task.revert(test_object, self.zero_pool_child_count, self._tf_failure_mock) else: task.revert(test_object, self._tf_failure_mock) self.assertFalse(mock_get_session.called) self.assertFalse(mock_check_quota_met.called) # revert mock_check_quota_met.reset_mock() with mock.patch('octavia.db.api.' 'get_session') as mock_get_session_local: mock_session = mock.MagicMock() mock_lock_session = mock.MagicMock() mock_get_session_local.side_effect = [mock_session, mock_lock_session] if data_model == data_models.Pool: task.revert(test_object, self.zero_pool_child_count, None) else: task.revert(test_object, None) mock_check_quota_met.assert_called_once_with( mock_session, mock_lock_session, data_model, project_id) mock_lock_session.commit.assert_called_once_with() # revert with rollback with mock.patch('octavia.db.api.' 'get_session') as mock_get_session_local: mock_session = mock.MagicMock() mock_lock_session = mock.MagicMock() mock_get_session_local.side_effect = [mock_session, mock_lock_session] mock_check_quota_met.side_effect = ( exceptions.OctaviaException('fail')) if data_model == data_models.Pool: task.revert(test_object, self.zero_pool_child_count, None) else: task.revert(test_object, None) mock_lock_session.rollback.assert_called_once_with() # revert with db exception mock_check_quota_met.reset_mock() with mock.patch('octavia.db.api.' 'get_session') as mock_get_session_local: mock_get_session_local.side_effect = Exception('fail') if data_model == data_models.Pool: task.revert(test_object, self.zero_pool_child_count, None) else: task.revert(test_object, None) self.assertFalse(mock_check_quota_met.called)
def test_config_exception(self, mock_cast): mock_cast.side_effect = exceptions.OctaviaException('boom') self.put(self.AMPHORA_CONFIG_PATH.format(amphora_id=self.amp_id), body={}, status=500)
def test_delete_exception(self, mock_delete): mock_delete.side_effect = [exceptions.OctaviaException()] response = self.delete(self.FLAVOR_PATH.format(flavor_id='bogus'), status=500) self.assertEqual('An unknown exception occurred.', response.json.get('faultstring'))
def test_create_vip_base_port(self, mock_get_net_driver): AMP_ID = uuidutils.generate_uuid() PORT_ID = uuidutils.generate_uuid() VIP_NETWORK_ID = uuidutils.generate_uuid() VIP_QOS_ID = uuidutils.generate_uuid() VIP_SG_ID = uuidutils.generate_uuid() VIP_SUBNET_ID = uuidutils.generate_uuid() VIP_IP_ADDRESS = '203.0.113.81' mock_driver = mock.MagicMock() mock_get_net_driver.return_value = mock_driver vip_mock = mock.MagicMock() vip_mock.ip_address = VIP_IP_ADDRESS vip_mock.network_id = VIP_NETWORK_ID vip_mock.qos_policy_id = VIP_QOS_ID vip_mock.subnet_id = VIP_SUBNET_ID port_mock = mock.MagicMock() port_mock.id = PORT_ID mock_driver.create_port.side_effect = [ port_mock, exceptions.OctaviaException('boom'), exceptions.OctaviaException('boom'), exceptions.OctaviaException('boom') ] mock_driver.delete_port.side_effect = [mock.DEFAULT, Exception('boom')] net_task = network_tasks.CreateVIPBasePort() # Limit the retry attempts for the test run to save time net_task.execute.retry.stop = tenacity.stop_after_attempt(2) # Test execute result = net_task.execute(vip_mock, VIP_SG_ID, AMP_ID) self.assertEqual(port_mock, result) mock_driver.create_port.assert_called_once_with( VIP_NETWORK_ID, name=constants.AMP_BASE_PORT_PREFIX + AMP_ID, fixed_ips=[{ constants.SUBNET_ID: VIP_SUBNET_ID }], secondary_ips=[VIP_IP_ADDRESS], security_group_ids=[VIP_SG_ID], qos_policy_id=VIP_QOS_ID) # Test execute exception mock_driver.reset_mock() self.assertRaises(exceptions.OctaviaException, net_task.execute, vip_mock, None, AMP_ID) # Test revert when this task failed mock_driver.reset_mock() net_task.revert(failure.Failure.from_exception(Exception('boom')), vip_mock, VIP_SG_ID, AMP_ID) mock_driver.delete_port.assert_not_called() # Test revert mock_driver.reset_mock() net_task.revert([port_mock], vip_mock, VIP_SG_ID, AMP_ID) mock_driver.delete_port.assert_called_once_with(PORT_ID) # Test revert exception mock_driver.reset_mock() net_task.revert([port_mock], vip_mock, VIP_SG_ID, AMP_ID) mock_driver.delete_port.assert_called_once_with(PORT_ID)
def test_delete_port(self, mock_update_progress, mock_get_net_driver): PORT_ID = uuidutils.generate_uuid() mock_driver = mock.MagicMock() mock_get_net_driver.return_value = mock_driver mock_driver.delete_port.side_effect = [ mock.DEFAULT, exceptions.OctaviaException('boom'), mock.DEFAULT, exceptions.OctaviaException('boom'), exceptions.OctaviaException('boom'), exceptions.OctaviaException('boom'), exceptions.OctaviaException('boom'), exceptions.OctaviaException('boom'), exceptions.OctaviaException('boom') ] mock_driver.admin_down_port.side_effect = [ mock.DEFAULT, exceptions.OctaviaException('boom') ] net_task = network_tasks.DeletePort() # Limit the retry attempts for the test run to save time net_task.execute.retry.stop = tenacity.stop_after_attempt(2) # Test port ID is None (no-op) net_task.execute(None) mock_update_progress.assert_not_called() mock_driver.delete_port.assert_not_called() # Test successful delete mock_update_progress.reset_mock() mock_driver.reset_mock() net_task.execute(PORT_ID) mock_update_progress.assert_called_once_with(0.5) mock_driver.delete_port.assert_called_once_with(PORT_ID) # Test exception and successful retry mock_update_progress.reset_mock() mock_driver.reset_mock() net_task.execute(PORT_ID) mock_update_progress.assert_has_calls([mock.call(0.5), mock.call(1.0)]) mock_driver.delete_port.assert_has_calls( [mock.call(PORT_ID), mock.call(PORT_ID)]) # Test passive failure mock_update_progress.reset_mock() mock_driver.reset_mock() net_task.execute(PORT_ID, passive_failure=True) mock_update_progress.assert_has_calls([mock.call(0.5), mock.call(1.0)]) mock_driver.delete_port.assert_has_calls( [mock.call(PORT_ID), mock.call(PORT_ID)]) mock_driver.admin_down_port.assert_called_once_with(PORT_ID) # Test passive failure admin down failure mock_update_progress.reset_mock() mock_driver.reset_mock() mock_driver.admin_down_port.reset_mock() net_task.execute(PORT_ID, passive_failure=True) mock_update_progress.assert_has_calls([mock.call(0.5), mock.call(1.0)]) mock_driver.delete_port.assert_has_calls( [mock.call(PORT_ID), mock.call(PORT_ID)]) mock_driver.admin_down_port.assert_called_once_with(PORT_ID) # Test non-passive failure mock_update_progress.reset_mock() mock_driver.reset_mock() mock_driver.admin_down_port.reset_mock() mock_driver.admin_down_port.side_effect = [ exceptions.OctaviaException('boom') ] self.assertRaises(exceptions.OctaviaException, net_task.execute, PORT_ID) mock_update_progress.assert_has_calls([mock.call(0.5), mock.call(1.0)]) mock_driver.delete_port.assert_has_calls( [mock.call(PORT_ID), mock.call(PORT_ID)]) mock_driver.admin_down_port.assert_not_called()