def delete(self, job_id): # delete a job from the database. If the job is running, the delete # operation will fail. In other cases, job will be deleted directly. context = t_context.extract_context_from_environ() if not policy.enforce(context, policy.ADMIN_API_JOB_DELETE): return utils.format_api_error(403, _('Unauthorized to delete a job')) try: db_api.get_job_from_log(context, job_id) return utils.format_api_error( 400, _('Job %(job_id)s is from job log') % {'job_id': job_id}) except Exception: try: job = db_api.get_job(context, job_id) except t_exc.ResourceNotFound: return utils.format_api_error( 404, _('Job %(job_id)s not found') % {'job_id': job_id}) try: # if job status = RUNNING, notify user this new one, delete # operation fails. if job['status'] == constants.JS_Running: return utils.format_api_error( 400, (_('Failed to delete the running job %(job_id)s') % { "job_id": job_id })) # if job status = SUCCESS, move the job entry to job log table, # then delete it from job table. elif job['status'] == constants.JS_Success: db_api.finish_job(context, job_id, True, timeutils.utcnow()) pecan.response.status = 200 return {} db_api.delete_job(context, job_id) pecan.response.status = 200 return {} except Exception as e: LOG.exception('Failed to delete the job: ' '%(exception)s ', {'exception': e}) return utils.format_api_error(500, _('Failed to delete the job'))
def delete(self, job_id): # delete a job from the database. If the job is running, the delete # operation will fail. In other cases, job will be deleted directly. context = t_context.extract_context_from_environ() if not policy.enforce(context, policy.ADMIN_API_JOB_DELETE): return utils.format_api_error( 403, _('Unauthorized to delete a job')) try: db_api.get_job_from_log(context, job_id) return utils.format_api_error( 400, _('Job %(job_id)s is from job log') % {'job_id': job_id}) except Exception: try: job = db_api.get_job(context, job_id) except t_exc.ResourceNotFound: return utils.format_api_error( 404, _('Job %(job_id)s not found') % {'job_id': job_id}) try: # if job status = RUNNING, notify user this new one, delete # operation fails. if job['status'] == constants.JS_Running: return utils.format_api_error( 400, (_('Failed to delete the running job %(job_id)s') % {"job_id": job_id})) # if job status = SUCCESS, move the job entry to job log table, # then delete it from job table. elif job['status'] == constants.JS_Success: db_api.finish_job(context, job_id, True, timeutils.utcnow()) pecan.response.status = 200 return {} db_api.delete_job(context, job_id) pecan.response.status = 200 return {} except Exception as e: LOG.exception('Failed to delete the job: ' '%(exception)s ', {'exception': e}) return utils.format_api_error( 500, _('Failed to delete the job'))
def test_post(self, mock_context): mock_context.return_value = self.context # cover all job types for job_type in self.job_resource_map.keys(): job = self._prepare_job_element(job_type) kw_job = {'job': job} # failure case, only admin can create the job self.context.is_admin = False res = self.controller.post(**kw_job) self._validate_error_code(res, 403) self.context.is_admin = True # failure case, request body not found kw_job_1 = {'job_1': job} res = self.controller.post(**kw_job_1) self._validate_error_code(res, 400) # failure case, wrong job type parameter job_type_backup = job.pop('type') res = self.controller.post(**kw_job) self._validate_error_code(res, 400) job['type'] = '' res = self.controller.post(**kw_job) self._validate_error_code(res, 400) job['type'] = job_type_backup + '_1' res = self.controller.post(**kw_job) self._validate_error_code(res, 400) job['type'] = job_type_backup # failure case, wrong resource parameter job_resource_backup = job.pop('resource') res = self.controller.post(**kw_job) self._validate_error_code(res, 400) job['resource'] = copy.deepcopy(job_resource_backup) job['resource'].popitem() res = self.controller.post(**kw_job) self._validate_error_code(res, 400) fake_resource = 'fake_resource' job['resource'][fake_resource] = fake_resource res = self.controller.post(**kw_job) self._validate_error_code(res, 400) job['resource'] = job_resource_backup # failure case, wrong project id parameter project_id_backup = job.pop('project_id') res = self.controller.post(**kw_job) self._validate_error_code(res, 400) job['project_id'] = '' res = self.controller.post(**kw_job) self._validate_error_code(res, 400) job['project_id'] = uuidutils.generate_uuid() res = self.controller.post(**kw_job) self._validate_error_code(res, 400) job['project_id'] = project_id_backup # successful case, create an entirely new job. Because the job # status returned from controller has been formatted, so we not # only validate the database records, but also validate the return # value of the controller. job_1 = self.controller.post(**kw_job)['job'] job_in_db_1 = db_api.get_job(self.context, job_1['id']) self.assertEqual(job_type, job_in_db_1['type']) self.assertEqual(job['project_id'], job_in_db_1['project_id']) self.assertEqual(constants.JS_New, job_in_db_1['status']) self.assertEqual('NEW', job_1['status']) self.assertEqual(len(constants.job_resource_map[job['type']]), len(job_1['resource'])) self.assertFalse('resource_id' in job_1) self.assertFalse('extra_id' in job_1) db_api.delete_job(self.context, job_1['id']) # successful case, target job already exists in the job table # and its status is NEW, then this newer job will be picked by # job handler. job_2 = self.controller.post(**kw_job)['job'] job_in_db_2 = db_api.get_job(self.context, job_2['id']) job_3 = self.controller.post(**kw_job)['job'] job_in_db_3 = db_api.get_job(self.context, job_3['id']) self.assertEqual(job_type, job_in_db_2['type']) self.assertEqual(job['project_id'], job_in_db_2['project_id']) self.assertEqual(constants.JS_New, job_in_db_2['status']) self.assertEqual('NEW', job_2['status']) self.assertEqual(len(constants.job_resource_map[job['type']]), len(job_2['resource'])) self.assertFalse('resource_id' in job_2) self.assertFalse('extra_id' in job_2) self.assertEqual(job_type, job_in_db_3['type']) self.assertEqual(job['project_id'], job_in_db_3['project_id']) self.assertEqual(constants.JS_New, job_in_db_3['status']) self.assertEqual('NEW', job_3['status']) self.assertEqual(len(constants.job_resource_map[job['type']]), len(job_3['resource'])) self.assertFalse('resource_id' in job_3) self.assertFalse('extra_id' in job_3) db_api.finish_job(self.context, job_3['id'], False, timeutils.utcnow()) db_api.delete_job(self.context, job_3['id'])
def test_put(self, mock_context): mock_context.return_value = self.context # cover all job types for job_type in self.job_resource_map.keys(): job = self._prepare_job_element(job_type) resource_id = '#'.join([ job['resource'][resource_id] for resource_type, resource_id in self.job_resource_map[job_type] ]) # failure case, only admin can redo the job job_1 = db_api.new_job(self.context, job['project_id'], job_type, resource_id) self.context.is_admin = False res = self.controller.put(job_1['id']) self._validate_error_code(res, 403) self.context.is_admin = True db_api.delete_job(self.context, job_1['id']) # failure case, job not found res = self.controller.put(-123) self._validate_error_code(res, 404) # failure case, redo a running job job_2 = db_api.register_job(self.context, job['project_id'], job_type, resource_id) res = self.controller.put(job_2['id']) self._validate_error_code(res, 400) db_api.finish_job(self.context, job_2['id'], False, timeutils.utcnow()) db_api.delete_job(self.context, job_2['id']) # failure case, redo a successful job job_3 = self._prepare_job_element(job_type) resource_id_3 = '#'.join([ job_3['resource'][resource_id_3] for resource_type_3, resource_id_3 in self.job_resource_map[job_type] ]) job_4 = db_api.new_job(self.context, job_3['project_id'], job_type, resource_id_3) with self.context.session.begin(): job_dict = { 'status': constants.JS_Success, 'timestamp': timeutils.utcnow(), 'extra_id': uuidutils.generate_uuid() } core.update_resource(self.context, models.AsyncJob, job_4['id'], job_dict) res = self.controller.put(job_4['id']) self._validate_error_code(res, 400) db_api.finish_job(self.context, job_4['id'], True, timeutils.utcnow()) # successful case, redo a failed job job_5 = db_api.new_job(self.context, job['project_id'], job_type, resource_id) db_api.finish_job(self.context, job_5['id'], False, timeutils.utcnow()) self.controller.put(job_5['id']) db_api.delete_job(self.context, job_5['id']) # successful case, redo a new job job_6 = db_api.new_job(self.context, job['project_id'], job_type, resource_id) self.controller.put(job_6['id']) db_api.delete_job(self.context, job_6['id'])
def test_delete(self, mock_context): mock_context.return_value = self.context # cover all job types. # each 'for' loop adds one item in job log table, we set count variable # to record dynamic total job entries in job log table. count = 1 for job_type in self.job_resource_map.keys(): job = self._prepare_job_element(job_type) resource_id = '#'.join([ job['resource'][resource_id] for resource_type, resource_id in self.job_resource_map[job_type] ]) # failure case, only admin can delete the job job_1 = db_api.new_job(self.context, job['project_id'], job_type, resource_id) self.context.is_admin = False res = self.controller.delete(job_1['id']) self._validate_error_code(res, 403) self.context.is_admin = True db_api.delete_job(self.context, job_1['id']) # failure case, job not found res = self.controller.delete(-123) self._validate_error_code(res, 404) # failure case, delete a running job job_2 = db_api.register_job(self.context, job['project_id'], job_type, resource_id) job = db_api.get_job(self.context, job_2['id']) res = self.controller.delete(job_2['id']) self._validate_error_code(res, 400) # finish the job and delete it db_api.finish_job(self.context, job_2['id'], False, timeutils.utcnow()) db_api.delete_job(self.context, job_2['id']) # successful case, delete a successful job. successful job from # job log can't be deleted, here this successful job is from # job table. job_3 = self._prepare_job_element(job_type) resource_id_3 = '#'.join([ job_3['resource'][resource_id_3] for resource_type_3, resource_id_3 in self.job_resource_map[job_type] ]) job_4 = db_api.new_job(self.context, job_3['project_id'], job_type, resource_id_3) with self.context.session.begin(): job_dict = { 'status': constants.JS_Success, 'timestamp': timeutils.utcnow(), 'extra_id': uuidutils.generate_uuid() } core.update_resource(self.context, models.AsyncJob, job_4['id'], job_dict) job_4_succ = db_api.get_job(self.context, job_4['id']) self.controller.delete(job_4['id']) filters_job_4 = [{ 'key': 'type', 'comparator': 'eq', 'value': job_4_succ['type'] }, { 'key': 'status', 'comparator': 'eq', 'value': job_4_succ['status'] }, { 'key': 'resource_id', 'comparator': 'eq', 'value': job_4_succ['resource_id'] }, { 'key': 'extra_id', 'comparator': 'eq', 'value': job_4_succ['extra_id'] }] self.assertEqual( 0, len(db_api.list_jobs(self.context, filters_job_4))) self.assertEqual(count, len(db_api.list_jobs_from_log(self.context))) count = count + 1 # successful case, delete a new job job_5 = db_api.new_job(self.context, job['project_id'], job_type, resource_id) self.controller.delete(job_5['id']) filters_job_5 = [{ 'key': 'type', 'comparator': 'eq', 'value': job_5['type'] }, { 'key': 'status', 'comparator': 'eq', 'value': job_5['status'] }, { 'key': 'resource_id', 'comparator': 'eq', 'value': job_5['resource_id'] }, { 'key': 'extra_id', 'comparator': 'eq', 'value': job_5['extra_id'] }] self.assertEqual( 0, len(db_api.list_jobs(self.context, filters_job_5))) # successful case, delete a failed job job_6 = db_api.new_job(self.context, job['project_id'], job_type, resource_id) db_api.finish_job(self.context, job_6['id'], False, timeutils.utcnow()) job_6_failed = db_api.get_job(self.context, job_6['id']) self.controller.delete(job_6['id']) filters_job_6 = [{ 'key': 'type', 'comparator': 'eq', 'value': job_6_failed['type'] }, { 'key': 'status', 'comparator': 'eq', 'value': job_6_failed['status'] }, { 'key': 'resource_id', 'comparator': 'eq', 'value': job_6_failed['resource_id'] }, { 'key': 'extra_id', 'comparator': 'eq', 'value': job_6_failed['extra_id'] }] self.assertEqual( 0, len(db_api.list_jobs(self.context, filters_job_6)))
def test_post(self, mock_context): mock_context.return_value = self.context # cover all job types for job_type in self.job_resource_map.keys(): job = self._prepare_job_element(job_type) kw_job = {'job': job} # failure case, only admin can create the job self.context.is_admin = False res = self.controller.post(**kw_job) self._validate_error_code(res, 403) self.context.is_admin = True # failure case, request body not found kw_job_1 = {'job_1': job} res = self.controller.post(**kw_job_1) self._validate_error_code(res, 400) # failure case, wrong job type parameter job_type_backup = job.pop('type') res = self.controller.post(**kw_job) self._validate_error_code(res, 400) job['type'] = '' res = self.controller.post(**kw_job) self._validate_error_code(res, 400) job['type'] = job_type_backup + '_1' res = self.controller.post(**kw_job) self._validate_error_code(res, 400) job['type'] = job_type_backup # failure case, wrong resource parameter job_resource_backup = job.pop('resource') res = self.controller.post(**kw_job) self._validate_error_code(res, 400) job['resource'] = copy.deepcopy(job_resource_backup) job['resource'].popitem() res = self.controller.post(**kw_job) self._validate_error_code(res, 400) fake_resource = 'fake_resource' job['resource'][fake_resource] = fake_resource res = self.controller.post(**kw_job) self._validate_error_code(res, 400) job['resource'] = job_resource_backup # failure case, wrong project id parameter project_id_backup = job.pop('project_id') res = self.controller.post(**kw_job) self._validate_error_code(res, 400) job['project_id'] = '' res = self.controller.post(**kw_job) self._validate_error_code(res, 400) job['project_id'] = uuidutils.generate_uuid() res = self.controller.post(**kw_job) self._validate_error_code(res, 400) job['project_id'] = project_id_backup # successful case, create an entirely new job. Because the job # status returned from controller has been formatted, so we not # only validate the database records, but also validate the return # value of the controller. job_1 = self.controller.post(**kw_job)['job'] job_in_db_1 = db_api.get_job(self.context, job_1['id']) self.assertEqual(job_type, job_in_db_1['type']) self.assertEqual(job['project_id'], job_in_db_1['project_id']) self.assertEqual(constants.JS_New, job_in_db_1['status']) self.assertEqual('NEW', job_1['status']) self.assertEqual(len(constants.job_resource_map[job['type']]), len(job_1['resource'])) self.assertFalse('resource_id' in job_1) self.assertFalse('extra_id' in job_1) db_api.delete_job(self.context, job_1['id']) # successful case, target job already exists in the job table # and its status is NEW, then this newer job will be picked by # job handler. job_2 = self.controller.post(**kw_job)['job'] job_in_db_2 = db_api.get_job(self.context, job_2['id']) job_3 = self.controller.post(**kw_job)['job'] job_in_db_3 = db_api.get_job(self.context, job_3['id']) self.assertEqual(job_type, job_in_db_2['type']) self.assertEqual(job['project_id'], job_in_db_2['project_id']) self.assertEqual(constants.JS_New, job_in_db_2['status']) self.assertEqual('NEW', job_2['status']) self.assertEqual(len(constants.job_resource_map[job['type']]), len(job_2['resource'])) self.assertFalse('resource_id' in job_2) self.assertFalse('extra_id' in job_2) self.assertEqual(job_type, job_in_db_3['type']) self.assertEqual(job['project_id'], job_in_db_3['project_id']) self.assertEqual(constants.JS_New, job_in_db_3['status']) self.assertEqual('NEW', job_3['status']) self.assertEqual(len(constants.job_resource_map[job['type']]), len(job_3['resource'])) self.assertFalse('resource_id' in job_3) self.assertFalse('extra_id' in job_3) db_api.finish_job(self.context, job_3['id'], False, timeutils.utcnow()) db_api.delete_job(self.context, job_3['id'])
def test_put(self, mock_context): mock_context.return_value = self.context # cover all job types for job_type in self.job_resource_map.keys(): job = self._prepare_job_element(job_type) resource_id = '#'.join([job['resource'][resource_id] for resource_type, resource_id in self.job_resource_map[job_type]]) # failure case, only admin can redo the job job_1 = db_api.new_job(self.context, job['project_id'], job_type, resource_id) self.context.is_admin = False res = self.controller.put(job_1['id']) self._validate_error_code(res, 403) self.context.is_admin = True db_api.delete_job(self.context, job_1['id']) # failure case, job not found res = self.controller.put(-123) self._validate_error_code(res, 404) # failure case, redo a running job job_2 = db_api.register_job(self.context, job['project_id'], job_type, resource_id) res = self.controller.put(job_2['id']) self._validate_error_code(res, 400) db_api.finish_job(self.context, job_2['id'], False, timeutils.utcnow()) db_api.delete_job(self.context, job_2['id']) # failure case, redo a successful job job_3 = self._prepare_job_element(job_type) resource_id_3 = '#'.join([job_3['resource'][resource_id_3] for resource_type_3, resource_id_3 in self.job_resource_map[job_type]]) job_4 = db_api.new_job(self.context, job_3['project_id'], job_type, resource_id_3) with self.context.session.begin(): job_dict = {'status': constants.JS_Success, 'timestamp': timeutils.utcnow(), 'extra_id': uuidutils.generate_uuid()} core.update_resource(self.context, models.AsyncJob, job_4['id'], job_dict) res = self.controller.put(job_4['id']) self._validate_error_code(res, 400) db_api.finish_job(self.context, job_4['id'], True, timeutils.utcnow()) # successful case, redo a failed job job_5 = db_api.new_job(self.context, job['project_id'], job_type, resource_id) db_api.finish_job(self.context, job_5['id'], False, timeutils.utcnow()) self.controller.put(job_5['id']) db_api.delete_job(self.context, job_5['id']) # successful case, redo a new job job_6 = db_api.new_job(self.context, job['project_id'], job_type, resource_id) self.controller.put(job_6['id']) db_api.delete_job(self.context, job_6['id'])
def test_delete(self, mock_context): mock_context.return_value = self.context # cover all job types. # each 'for' loop adds one item in job log table, we set count variable # to record dynamic total job entries in job log table. count = 1 for job_type in self.job_resource_map.keys(): job = self._prepare_job_element(job_type) resource_id = '#'.join([job['resource'][resource_id] for resource_type, resource_id in self.job_resource_map[job_type]]) # failure case, only admin can delete the job job_1 = db_api.new_job(self.context, job['project_id'], job_type, resource_id) self.context.is_admin = False res = self.controller.delete(job_1['id']) self._validate_error_code(res, 403) self.context.is_admin = True db_api.delete_job(self.context, job_1['id']) # failure case, job not found res = self.controller.delete(-123) self._validate_error_code(res, 404) # failure case, delete a running job job_2 = db_api.register_job(self.context, job['project_id'], job_type, resource_id) job = db_api.get_job(self.context, job_2['id']) res = self.controller.delete(job_2['id']) self._validate_error_code(res, 400) # finish the job and delete it db_api.finish_job(self.context, job_2['id'], False, timeutils.utcnow()) db_api.delete_job(self.context, job_2['id']) # successful case, delete a successful job. successful job from # job log can't be deleted, here this successful job is from # job table. job_3 = self._prepare_job_element(job_type) resource_id_3 = '#'.join([job_3['resource'][resource_id_3] for resource_type_3, resource_id_3 in self.job_resource_map[job_type]]) job_4 = db_api.new_job(self.context, job_3['project_id'], job_type, resource_id_3) with self.context.session.begin(): job_dict = {'status': constants.JS_Success, 'timestamp': timeutils.utcnow(), 'extra_id': uuidutils.generate_uuid()} core.update_resource(self.context, models.AsyncJob, job_4['id'], job_dict) job_4_succ = db_api.get_job(self.context, job_4['id']) self.controller.delete(job_4['id']) filters_job_4 = [ {'key': 'type', 'comparator': 'eq', 'value': job_4_succ['type']}, {'key': 'status', 'comparator': 'eq', 'value': job_4_succ['status']}, {'key': 'resource_id', 'comparator': 'eq', 'value': job_4_succ['resource_id']}, {'key': 'extra_id', 'comparator': 'eq', 'value': job_4_succ['extra_id']}] self.assertEqual(0, len(db_api.list_jobs(self.context, filters_job_4))) self.assertEqual(count, len(db_api.list_jobs_from_log(self.context))) count = count + 1 # successful case, delete a new job job_5 = db_api.new_job(self.context, job['project_id'], job_type, resource_id) self.controller.delete(job_5['id']) filters_job_5 = [ {'key': 'type', 'comparator': 'eq', 'value': job_5['type']}, {'key': 'status', 'comparator': 'eq', 'value': job_5['status']}, {'key': 'resource_id', 'comparator': 'eq', 'value': job_5['resource_id']}, {'key': 'extra_id', 'comparator': 'eq', 'value': job_5['extra_id']}] self.assertEqual(0, len(db_api.list_jobs(self.context, filters_job_5))) # successful case, delete a failed job job_6 = db_api.new_job(self.context, job['project_id'], job_type, resource_id) db_api.finish_job(self.context, job_6['id'], False, timeutils.utcnow()) job_6_failed = db_api.get_job(self.context, job_6['id']) self.controller.delete(job_6['id']) filters_job_6 = [ {'key': 'type', 'comparator': 'eq', 'value': job_6_failed['type']}, {'key': 'status', 'comparator': 'eq', 'value': job_6_failed['status']}, {'key': 'resource_id', 'comparator': 'eq', 'value': job_6_failed['resource_id']}, {'key': 'extra_id', 'comparator': 'eq', 'value': job_6_failed['extra_id']}] self.assertEqual(0, len(db_api.list_jobs(self.context, filters_job_6)))