def test_delete_container_error(self, mock_name, mock_swift): st = create_stack(swiftsignalhandle_template) handle = st['test_wait_condition_handle'] mock_swift_object = mock.Mock() mock_swift.return_value = mock_swift_object mock_swift_object.head_account.return_value = { 'x-account-meta-temp-url-key': "1234" } mock_swift_object.url = "http://fake-host.com:8080/v1/AUTH_1234" obj_name = "%s-%s-abcdefghijkl" % (st.name, handle.name) mock_name.return_value = obj_name st.create() exc = swiftclient_exceptions.ClientException("Object DELETE failed", http_status=404) mock_swift_object.delete_object.side_effect = (None, None, None, exc) exc = swiftclient_exceptions.ClientException("Overlimit", http_status=413) mock_swift_object.delete_container.side_effect = (exc, ) rsrc = st.resources['test_wait_condition_handle'] exc = self.assertRaises(exception.ResourceFailure, scheduler.TaskRunner(rsrc.delete)) self.assertEqual( 'ClientException: ' 'resources.test_wait_condition_handle: ' 'Overlimit: 413', six.text_type(exc))
def delete_container(self, container): if container not in self.kvs: raise swexc.ClientException("No such container", http_status=404) if self.kvs[container]: raise swexc.ClientException("Container not empty", http_status=409) del self.kvs[container]
def test_run_failed(self, kib, update_action, goc): swift = goc.return_value builder = kib.return_value builder.container_images_from_template.return_value = image_entries final_env = { 'environments': [{ 'path': 'overcloud-resource-registry-puppet.yaml' }, { 'path': 'environments/containers-default-parameters.yaml' }, { 'path': 'user-environment.yaml' }] } update_action.return_value.run.return_value = final_env action = container_images.PrepareContainerImageEnv( container='overcloud') self.assertEqual(final_env, action.run(self.ctx)) update_action.return_value.run.return_value = actions.Result( error='Error updating environment for plan overcloud: ouch') self.assertEqual('Error updating environment for plan overcloud: ouch', action.run(self.ctx).error) swift.put_object.side_effect = swiftexceptions.ClientException('nope') self.assertEqual( 'Error updating environments/containers-default-parameters.yaml ' 'for plan overcloud: nope', action.run(self.ctx).error)
def test_get_object_exception(self, mock_swift): mock_swift.get_object.side_effect = swift_exc.ClientException("ERROR") container_name = 'container_name' object_name = 'object_name' self.assertRaises(shade.OpenStackCloudException, self.cloud.get_object, container_name, object_name)
def test_run(self, mock_ctx, get_orchestration_client_mock, mock_get_object_client, mock_get_workflow_client, mock_get_template_contents, mock_process_multiple_environments_and_files, mock_time): mock_ctx.return_value = mock.MagicMock() # setup swift swift = mock.MagicMock(url="http://test.com") swift.get_object.side_effect = swiftexceptions.ClientException( 'atest2') mock_get_object_client.return_value = swift heat = mock.MagicMock() heat.stacks.get.return_value = None get_orchestration_client_mock.return_value = heat mock_mistral = mock.MagicMock() mock_env = mock.MagicMock() mock_env.variables = { 'temp_environment': 'temp_environment', 'template': 'template', 'environments': [{ u'path': u'environments/test.yaml' }], 'parameter_defaults': { 'random_existing_data': 'a_value' }, } mock_mistral.environments.get.return_value = mock_env mock_get_workflow_client.return_value = mock_mistral mock_get_template_contents.return_value = ({}, { 'heat_template_version': '2016-04-30' }) mock_process_multiple_environments_and_files.return_value = ({}, {}) # freeze time at datetime.datetime(2016, 9, 8, 16, 24, 24) mock_time.time.return_value = 1473366264 action = deployment.DeployStackAction(1, 'overcloud') action.run() # verify parameters are as expected expected_defaults = { 'DeployIdentifier': 1473366264, 'StackAction': 'CREATE', 'UpdateIdentifier': '', 'random_existing_data': 'a_value' } self.assertEqual(expected_defaults, mock_env.variables['parameter_defaults']) heat.stacks.create.assert_called_once_with( environment={}, files={}, stack_name='overcloud', template={'heat_template_version': '2016-04-30'}, timeout_mins=1, )
def test_underscore_store_not_exists(self, mock_exists, mock_swiftuploadobj): mock_exists.return_value = False with mock.patch.object(self.swift_store, '_run_operation') as mock_run: mock_run.return_value = self._build_results(['obj']) self.swift_store._store(powervm.TEST_INST1.uuid, 'data') mock_run.assert_called_once_with('upload', 'powervm_nvram', mock.ANY, options={'leave_segments': True}) # Test retry upload mock_run.reset_mock() mock_swiftuploadobj.reset_mock() mock_res_obj = { 'success': False, 'error': swft_exc.ClientException('Error Message.'), 'object': '6ecb1386-53ab-43da-9e04-54e986ad4a9d' } mock_run.side_effect = [mock_res_obj, self._build_results(['obj'])] self.swift_store._store(powervm.TEST_INST1.uuid, 'data') mock_run.assert_called_with('upload', 'powervm_nvram', mock.ANY, options={'leave_segments': True}) self.assertEqual(mock_run.call_count, 2) self.assertEqual(mock_swiftuploadobj.call_count, 2)
def test_delete_container_404(self, mock_swift): """No exception when deleting a container that does not exist""" name = 'test_container' mock_swift.delete_container.side_effect = swift_exc.ClientException( 'ERROR', http_status=404) self.cloud.delete_container(name) mock_swift.delete_container.assert_called_once_with(container=name)
def test_delete_plan(self): # test that stack exists self.heatclient.stacks.get = mock.MagicMock(return_value=self.stack) plan_mgr = plan.PlanManager(self.plan_store, self.heatclient) self.assertRaises(exception.StackInUseError, plan_mgr.delete_plan, self.plan_name) self.heatclient.stacks.get.assert_called_with(self.plan_name) # test that stack doesn't exist yet self.plan_store.delete = mock.MagicMock() self.heatclient.stacks.get = mock.Mock( side_effect=heatexceptions.HTTPNotFound) plan_mgr.delete_plan(self.plan_name) self.plan_store.delete.assert_called_with(self.plan_name) # set side effect of swiftexceptions.ClientException with mock.patch('tripleo_common.core.plan.LOG') as log_mock: self.plan_store.delete = mock.Mock( side_effect=swiftexceptions.ClientException("test-error", http_status=404)) self.assertRaises(exception.PlanDoesNotExistError, plan_mgr.delete_plan, self.plan_name) log_mock.exception.assert_called_with('Swift error deleting plan.') # set side effect of random Exception self.heatclient.stacks.get = mock.Mock(side_effect=ValueError()) self.assertRaises(ValueError, plan_mgr.delete_plan, self.plan_name)
def test_get_plan(self): self.expected_plan.files = { 'some-name.yaml': { 'contents': "some fake contents", 'meta': { 'file-type': 'environment' } }, } self.plan_store.get = mock.MagicMock(return_value=self.expected_plan) plan_mgr = plan.PlanManager(self.plan_store, self.heatclient) self.assertEqual(self.expected_plan, plan_mgr.get_plan(self.plan_name), "Plan mismatch") self.plan_store.get.assert_called_with(self.plan_name) with mock.patch('tripleo_common.core.plan.LOG') as log_mock: # test swift container doesn't exist self.plan_store.get = mock.Mock( side_effect=swiftexceptions.ClientException("test-error", http_status=404)) self.assertRaises(exception.PlanDoesNotExistError, plan_mgr.get_plan, self.plan_name) log_mock.exception.assert_called_with( 'Swift error retrieving plan.') # test other exception occurs self.plan_store.get = mock.Mock(side_effect=ValueError()) self.assertRaises(ValueError, plan_mgr.get_plan, 'overcloud') log_mock.exception.assert_called_with("Error retrieving plan.")
def test_run(self, mock_get_object_client, mock_get_template_contents, mock_process_multiple_environments_and_files): mock_ctx = mock.MagicMock() swift = mock.MagicMock(url="http://test.com") mock_env = yaml.safe_dump({ 'temp_environment': 'temp_environment', 'template': 'template', 'environments': [{u'path': u'environments/test.yaml'}] }, default_flow_style=False) swift.get_object.side_effect = ( ({}, mock_env), swiftexceptions.ClientException('atest2') ) mock_get_object_client.return_value = swift mock_get_template_contents.return_value = ({}, { 'heat_template_version': '2016-04-30' }) mock_process_multiple_environments_and_files.return_value = ({}, {}) # Test action = templates.ProcessTemplatesAction() result = action.run(mock_ctx) # Verify the values we get out self.assertEqual(result, { 'environment': {}, 'files': {}, 'stack_name': constants.DEFAULT_CONTAINER_NAME, 'template': { 'heat_template_version': '2016-04-30' } })
def test_no_plan_environment(self): self.swift_client.get_object.side_effect = ( swift_exc.ClientException("404")) plan_management._update_passwords(self.swift_client, self.plan_name, {'SecretPassword': '******'}) self.swift_client.put_object.assert_not_called()
def test_get_object_not_found(self, mock_swift): mock_swift.get_object.side_effect = swift_exc.ClientException( 'ERROR', http_status=404) container_name = 'container_name' object_name = 'object_name' self.assertIsNone(self.cloud.get_object(container_name, object_name)) mock_swift.get_object.assert_called_once_with( container=container_name, obj=object_name, query_string=None, resp_chunk_size=None)
def test_delete_object_exc(self, connection_mock): swiftapi = swift.SwiftAPI() exc = swift_exception.ClientException("Operation error") connection_obj_mock = connection_mock.return_value connection_obj_mock.delete_object.side_effect = exc self.assertRaises(exception.SwiftOperationError, swiftapi.delete_object, 'container', 'object') connection_obj_mock.delete_object.assert_called_once_with( 'container', 'object')
def put_object(self, container, key, obj): if hasattr(obj, "seek"): obj.seek(0) obj = obj.read() # TODO(jd) Maybe we should reset the seek(), but well… try: self.kvs[container][key] = obj except KeyError: raise swexc.ClientException("No such container", http_status=404)
def test_delete_object_exception(self, mock_swift, mock_get_meta): container_name = 'container_name' object_name = 'object_name' mock_get_meta.return_value = {'object': object_name} mock_swift.delete_object.side_effect = swift_exc.ClientException( "ERROR") self.assertRaises(shade.OpenStackCloudException, self.cloud.delete_object, container_name, object_name)
def test_run_container_does_not_exist(self): self.swift.get_container.side_effect = swiftexceptions.ClientException( self.plan) action = plan.ExportPlanAction(self.plan, self.delete_after, self.exports_container) result = action.run() error = "Error attempting an operation on container: %s" % self.plan self.assertIn(error, result.error)
def setUp(self): super(SwiftTestCase, self).setUp() swift.reset_swift_session() self.swift_exception = swift_exception.ClientException('', '') self.cfg.config(group='swift', os_service_type='object-store', os_endpoint_type='internalURL', os_region='somewhere', max_retries=2) self.addCleanup(swift.reset_swift_session)
def test_delete_object_exc_resource_not_found(self, connection_mock): swiftapi = swift.SwiftAPI() exc = swift_exception.ClientException( "Resource not found", http_status=http_client.NOT_FOUND) connection_obj_mock = connection_mock.return_value connection_obj_mock.delete_object.side_effect = exc self.assertRaises(exception.SwiftObjectNotFoundError, swiftapi.delete_object, 'container', 'object') connection_obj_mock.delete_object.assert_called_once_with( 'container', 'object')
def test_run_with_no_file(self): self.swift.get_object.side_effect = swiftexceptions.ClientException( 'atest2') action = plan.CreatePlanAction(self.container_name) result = action.run() error_str = 'File missing from container: atest2' self.assertEqual(result.error, error_str)
def test_run_missing_file(self): self.swift.get_object.side_effect = swiftexceptions.ClientException( self.plan_environment_name) action = plan.UpdatePlanAction(self.container_name) result = action.run() error_str = ('File missing from container: %s' % self.plan_environment_name) self.assertEqual(result.error, error_str)
def test_no_roles_data_file(self, get_obj_client_mock): swift = mock.MagicMock() swift.get_object.side_effect = swiftexceptions.ClientException("404") get_obj_client_mock.return_value = swift action = plan.ListRolesAction() result = action.run(self.ctx) error_str = ('Error retrieving roles data from deployment plan: 404') self.assertEqual(result.error, error_str)
def test_no_env(self, mock_get_orchestration, mock_get_object): mock_ctx = mock.MagicMock() mock_get_object.return_value.get_object.side_effect = ( swiftexceptions.ClientException("overcast")) action = deployment.OvercloudRcAction("overcast") result = action.run(mock_ctx) self.assertEqual( result.error, "Error retrieving environment for plan " "overcast: overcast")
def setUp(self): super(SwiftTestCase, self).setUp() self.swift_exception = swift_exception.ClientException('', '') self.config(admin_user='******', group='keystone_authtoken') self.config(admin_tenant_name='tenant', group='keystone_authtoken') self.config(admin_password='******', group='keystone_authtoken') self.config(auth_uri='http://authurl', group='keystone_authtoken') self.config(auth_version='2', group='keystone_authtoken') self.config(swift_max_retries=2, group='swift') self.config(insecure=0, group='keystone_authtoken') self.config(cafile='/path/to/ca/file', group='keystone_authtoken')
def test_run(self, mock_ctx, mock_get_object_client, mock_get_workflow_client, mock_get_orchestration_client, mock_get_template_contents, mock_process_multiple_environments_and_files, mock_cache_get, mock_cache_set): mock_ctx.return_value = mock.MagicMock() swift = mock.MagicMock(url="http://test.com") swift.get_object.side_effect = swiftexceptions.ClientException( 'atest2') mock_get_object_client.return_value = swift mock_mistral = mock.MagicMock() mock_env = mock.MagicMock() mock_env.variables = { 'temp_environment': 'temp_environment', 'template': 'template', 'environments': [{ u'path': u'environments/test.yaml' }], } mock_mistral.environments.get.return_value = mock_env mock_get_workflow_client.return_value = mock_mistral mock_get_template_contents.return_value = ({}, { 'heat_template_version': '2016-04-30' }) mock_process_multiple_environments_and_files.return_value = ({}, {}) mock_heat = mock.MagicMock() mock_heat.stacks.validate.return_value = {} mock_get_orchestration_client.return_value = mock_heat mock_cache_get.return_value = None # Test action = parameters.GetParametersAction() action.run() mock_heat.stacks.validate.assert_called_once_with( environment={}, files={}, show_nested=True, template={'heat_template_version': '2016-04-30'}, ) mock_cache_get.assert_called_once_with("overcloud", "tripleo.parameters.get") mock_cache_set.assert_called_once_with( "overcloud", "tripleo.parameters.get", { 'heat_resource_tree': {}, 'mistral_environment_parameters': None })
def setUp(self): super(SwiftTestCase, self).setUp() swift.reset_swift_session() self.swift_exception = swift_exception.ClientException('', '') self.cfg.config(group='swift', os_service_type='object-store', os_endpoint_type='internalURL', os_region='somewhere', max_retries=2) # NOTE(aarefiev) register keystoneauth dynamic options adapter_opts = kloading.get_adapter_conf_options( include_deprecated=False) self.cfg.register_opts(adapter_opts, 'swift') self.addCleanup(swift.reset_swift_session)
def test_empty_resource_tree(self, mock_get_object_client, mock_get_orchestration_client, mock_get_template_contents, mock_process_multiple_environments_and_files, mock_cache_get, mock_cache_set): mock_ctx = mock.MagicMock() mock_cache_get.return_value = None swift = mock.MagicMock(url="http://test.com") mock_env = yaml.safe_dump( { 'temp_environment': 'temp_environment', 'template': 'template', 'environments': [{ u'path': u'environments/test.yaml' }] }, default_flow_style=False) swift.get_object.side_effect = (( {}, mock_env), swiftexceptions.ClientException('atest2'), ({}, mock_env)) mock_get_object_client.return_value = swift mock_get_template_contents.return_value = ({}, { 'heat_template_version': '2016-04-30' }) mock_process_multiple_environments_and_files.return_value = ({}, {}) mock_heat = mock.MagicMock() mock_get_orchestration_client.return_value = mock_heat mock_heat.stacks.validate.return_value = {} expected_value = { 'heat_resource_tree': {}, 'environment_parameters': None, } # Test action = parameters.GetFlattenedParametersAction() result = action.run(mock_ctx) mock_heat.stacks.validate.assert_called_once_with( environment={}, files={}, show_nested=True, template={'heat_template_version': '2016-04-30'}, ) self.assertEqual(result, expected_value)
def test_delete_non_empty_container(self, mock_name, mock_swift): st = create_stack(swiftsignalhandle_template) handle = st['test_wait_condition_handle'] mock_swift_object = mock.Mock() mock_swift.return_value = mock_swift_object mock_swift_object.head_account.return_value = { 'x-account-meta-temp-url-key': "1234" } mock_swift_object.url = "http://fake-host.com:8080/v1/AUTH_1234" obj_name = "%s-%s-abcdefghijkl" % (st.name, handle.name) mock_name.return_value = obj_name st.create() exc = swiftclient_exceptions.ClientException("Object DELETE failed", http_status=404) mock_swift_object.delete_object.side_effect = (None, None, None, exc) exc = swiftclient_exceptions.ClientException("Container DELETE failed", http_status=409) mock_swift_object.delete_container.side_effect = exc rsrc = st.resources['test_wait_condition_handle'] scheduler.TaskRunner(rsrc.delete)() self.assertEqual(('DELETE', 'COMPLETE'), rsrc.state) self.assertEqual(4, mock_swift_object.delete_object.call_count)
def test_run_valid_network_config( self, mock_get_object_client, mock_get_workflow_client, mock_get_orchestration_client, mock_get_template_contents, mock_process_multiple_environments_and_files, mock_cache_get, mock_cache_set): mock_ctx = mock.MagicMock() swift = mock.MagicMock(url="http://test.com") mock_env = yaml.safe_dump({ 'temp_environment': 'temp_environment', 'template': 'template', 'environments': [{u'path': u'environments/test.yaml'}] }, default_flow_style=False) swift.get_object.side_effect = ( ({}, mock_env), swiftexceptions.ClientException('atest2'), ({}, mock_env) ) mock_get_object_client.return_value = swift mock_get_template_contents.return_value = ({}, { 'heat_template_version': '2016-04-30' }) mock_process_multiple_environments_and_files.return_value = ({}, {}) mock_heat = mock.MagicMock() mock_heat.stacks.preview.return_value = mock.Mock(resources=[{ "resource_identity": {"stack_name": "overcloud-TEMP-Compute-0"}, "resource_name": "OsNetConfigImpl", "properties": {"config": "echo \'{\"network_config\": {}}\'"} }]) mock_get_orchestration_client.return_value = mock_heat mock_cache_get.return_value = None expected = {"network_config": {}} # Test action = parameters.GetNetworkConfigAction(container='overcloud', role_name='Compute') result = action.run(mock_ctx) self.assertEqual(expected, result) mock_heat.stacks.preview.assert_called_once_with( environment={}, files={}, template={'heat_template_version': '2016-04-30'}, stack_name='overcloud-TEMP', )
def setUp(self): super(SwiftTestCase, self).setUp() self.swift_exception = swift_exception.ClientException('', '') self.config(admin_user='******', group='keystone_authtoken') self.config(admin_tenant_name='tenant', group='keystone_authtoken') self.config(admin_password='******', group='keystone_authtoken') self.config(auth_uri='http://authurl', group='keystone_authtoken') self.config(auth_version='2', group='keystone_authtoken') self.config(swift_max_retries=2, group='swift') self.config(insecure=0, group='keystone_authtoken') # The constructor of SwiftAPI accepts arguments whose # default values are values of some config options above. So reload # the module to make sure the required values are set. reload(sys.modules['ironic.common.swift'])
def test_run_env_missing(self, get_obj_client_mock): mock_ctx = mock.MagicMock() # setup swift swift = mock.MagicMock() swift.get_object.side_effect = (swiftexceptions.ClientException( self.container_name)) get_obj_client_mock.return_value = swift action = heat_capabilities.UpdateCapabilitiesAction( {}, self.container_name) expected = actions.Result( data=None, error="Error retrieving environment for plan test-container: " "test-container") self.assertEqual(expected, action.run(mock_ctx))