def test_clear_job_status(self): job_id = "1" JobStatusModel.objects.filter().delete() JobStatusModel(indexid=1, jobid=job_id, status="success", progress=10).save() JobUtil.clear_job_status(job_id) self.assertEqual(0, len(JobStatusModel.objects.filter(jobid=job_id)))
def test_query_job_status(self): job_id = "1" JobStatusModel.objects.filter().delete() JobStatusModel(indexid=1, jobid=job_id, status="success", progress=10).save() JobStatusModel(indexid=2, jobid=job_id, status="success", progress=50).save() JobStatusModel(indexid=3, jobid=job_id, status="success", progress=100).save() jobs = JobUtil.query_job_status(job_id) self.assertEqual(1, len(jobs)) self.assertEqual(3, jobs[0].indexid) jobs = JobUtil.query_job_status(job_id, 1) self.assertEqual(2, len(jobs)) self.assertEqual(3, jobs[0].indexid) self.assertEqual(2, jobs[1].indexid) JobStatusModel.objects.filter().delete()
def test_job(self): JobModel(jobid=self.job_id, jobtype='VNF', jobaction='INST', resid='1').save() JobStatusModel(indexid=1, jobid=self.job_id, status='inst', progress=20, descp='inst').save() response = self.client.get("/openoapi/nslcm/v1/jobs/%s" % self.job_id) self.failUnlessEqual(status.HTTP_200_OK, response.status_code)
def test_job(self): JobModel(jobid=self.job_id, jobtype='VNF', jobaction='INST', resid='1').save() JobStatusModel(indexid=1, jobid=self.job_id, status='inst', progress=20, descp='inst').save() response = self.client.get( "/api/vnflcm/v1/vnf_lc_ops/%s?responseId=123456jhj" % self.job_id) self.assertEqual(status.HTTP_200_OK, response.status_code)
def test_query_job_with_response_id(self): JobModel(jobid=self.job_id, jobtype=JOB_TYPE.NS, jobaction=JOB_ACTION.INSTANTIATE, resid='1').save() JobStatusModel(indexid=1, jobid=self.job_id, status=JOB_STATUS.PROCESSING, progress=20, descp='NS instantiation progress is 20%.', errcode="0").save() JobStatusModel(indexid=2, jobid=self.job_id, status=JOB_STATUS.PROCESSING, progress=50, descp='NS instantiation progress is 50%.', errcode="0").save() JobStatusModel(indexid=3, jobid=self.job_id, status=JOB_STATUS.PROCESSING, progress=80, descp='NS instantiation progress is 80%.', errcode="0").save() JobStatusModel(indexid=4, jobid=self.job_id, status=JOB_STATUS.FINISH, progress=100, descp='Finish to instantiate NS.', errcode="0").save() url = JOB_INSTANCE_URI % self.job_id + "?responseId=2" response = self.client.get(url) self.assertEqual(status.HTTP_200_OK, response.status_code) self.assertEqual(self.job_id, response.data.get('jobId')) self.assertIn('responseDescriptor', response.data) self.assertEqual(100, response.data['responseDescriptor']['progress']) self.assertEqual( 1, len(response.data['responseDescriptor']['responseHistoryList']))
def test_update_job_with_bad_req(self): JobModel(jobid=self.job_id, jobtype=JOB_TYPE.NS, jobaction=JOB_ACTION.INSTANTIATE, resid='1').save() JobStatusModel(indexid=1, jobid=self.job_id, status=JOB_STATUS.PROCESSING, progress=20, descp='NS instantiation progress is 20%.', errcode="0").save() response = self.client.post(JOB_INSTANCE_URI % self.job_id, format='json', data=UPDATE_JOB_BAD_REQ_DICT) self.assertEqual(status.HTTP_400_BAD_REQUEST, response.status_code)
def test_query_ns_job(self): JobModel(jobid=self.job_id, jobtype=JOB_TYPE.NS, jobaction=JOB_ACTION.INSTANTIATE, resid='1').save() JobStatusModel(indexid=1, jobid=self.job_id, status=JOB_STATUS.PROCESSING, progress=20, descp='Finish to instantiate NS.', errcode="0").save() response = self.client.get(JOB_INSTANCE_URI % self.job_id) self.assertEqual(status.HTTP_200_OK, response.status_code, response.data) self.assertIn('jobId', response.data) self.assertIn('responseDescriptor', response.data) self.assertEqual(20, response.data['responseDescriptor']['progress'])
def add_job_status(job_id, progress, status_decs, error_code=""): jobs = JobModel.objects.filter(jobid=job_id) if not jobs: logger.error("Job[%s] is not exists, please create job first." % job_id) raise Exception("Job[%s] is not exists." % job_id) try: int_progress = int(progress) job_status = JobStatusModel() job_status.indexid = int(idutil.get_auto_id(job_id)) job_status.jobid = job_id job_status.status = "processing" job_status.progress = int_progress if job_status.progress == 0: job_status.status = "started" elif job_status.progress == 100: job_status.status = "finished" elif job_status.progress == 101: job_status.status = "partly_finished" elif job_status.progress > 101: job_status.status = "error" if error_code == "255": job_status.status = "error" job_status.descp = status_decs job_status.errcode = error_code if error_code else "0" job_status.addtime = datetime.datetime.now().strftime( '%Y-%m-%d %X') job_status.save() logger.debug( "Add a new job status, jobid=%s, indexid=%d," " status=%s, description=%s, progress=%d, errcode=%s, addtime=%r" % (job_status.jobid, job_status.indexid, job_status.status, job_status.descp, job_status.progress, job_status.errcode, job_status.addtime)) job = jobs[0] job.progress = int_progress if job_status.progress >= 100: job.status = JOB_STATUS.FINISH job.endtime = datetime.datetime.now().strftime('%Y-%m-%d %X') job.save() logger.debug("update job, jobid=%s, progress=%d" % (job_status.jobid, int_progress)) except: logger.error(traceback.format_exc())