def test_ns_manual_scale_asynchronous_tasks_success( self, mock_get, mock_sleep, mock_run, mock_vnf_data_info_list): mock_run.return_value = None mock_sleep.return_value = None mock_get.return_value = JobModel(progress=JOB_PROGRESS.FINISHED) mock_vnf_data_info_list.return_value = [{ "vnfd_id": "nf_001", "vnf_scaleAspectId": "gpu", "numberOfSteps": "123" }, { "vnfd_id": "nf_002", "vnf_scaleAspectId": "gpu", "numberOfSteps": "456" }] scale_ns_json = SCALE_NS_THREAD_DICT.copy() ns_heal_service = NSManualScaleService(self.ns_inst_id, scale_ns_json, self.job_id) ns_heal_service.run() jobs = JobModel.objects.filter(jobid=self.job_id) self.assertEqual( NSInstModel.objects.get(id=self.ns_inst_id).status, NS_INST_STATUS.ACTIVE) self.assertEqual(JOB_PROGRESS.FINISHED, jobs[0].progress) self.assertEqual( NSLcmOpOccModel.objects.filter( id=ns_heal_service.occ_id).first().operation_state, 'COMPLETED')
def test_clear_job(self): job_id = "1" JobModel.objects.filter().delete() JobModel(jobid=job_id, jobtype="1", jobaction="2", resid="3", status=0).save() JobUtil.clear_job(job_id) self.assertEqual(0, len(JobModel.objects.filter(jobid=job_id)))
def test_is_job_exists(self): job_id = "1" JobModel.objects.filter().delete() JobModel(jobid=job_id, jobtype="1", jobaction="2", resid="3", status=0).save() self.assertTrue(JobUtil.is_job_exists(job_id)) JobModel.objects.filter().delete()
def test_ns_update_asynchronous_tasks_when_nf_update_error(self, mock_get, mock_run, mock_sleep): mock_sleep.return_value = None mock_run.return_value = None mock_get.return_value = JobModel(progress=JOB_PROGRESS.ERROR) ns_heal_service = NSUpdateService(self.ns_inst_id, UPDATE_NS_DICT, self.job_id) ns_heal_service.run() self.assertEqual(NSInstModel.objects.filter(id=self.ns_inst_id).first().status, NS_INST_STATUS.UPDATING) self.assertEqual(JobModel.objects.filter(jobid=self.job_id).first().progress, JOB_PROGRESS.ERROR) self.assertEqual(NSLcmOpOccModel.objects.filter(id=ns_heal_service.occ_id).first().operation_state, 'FAILED')
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_heal_ns_thread(self, mock_get, mock_sleep, mock_run): mock_run.return_value = None mock_sleep.return_value = None mock_get.return_value = JobModel(progress=JOB_PROGRESS.FINISHED) heal_ns_json = HEAL_NS_DICT.copy() heal_ns_json['healNsData']['vnfInstanceId'] = self.nf_inst_id NSHealService(self.ns_inst_id, heal_ns_json, self.job_id).run() self.assertEqual( NSInstModel.objects.get(id=self.ns_inst_id).status, NS_INST_STATUS.ACTIVE)
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_add_job_status_normal(self): job_id = "1" JobModel.objects.filter().delete() JobStatusModel.objects.filter().delete() JobModel(jobid=job_id, jobtype="1", jobaction="2", resid="3", status=0).save() JobUtil.add_job_status(job_id="1", progress=1, status_decs="2", error_code="0") self.assertEqual(1, len(JobStatusModel.objects.filter(jobid=job_id))) JobStatusModel.objects.filter().delete() JobModel.objects.filter().delete()
def test_get_unfinished_jobs(self): JobModel.objects.filter().delete() JobModel(jobid="11", jobtype="InstVnf", jobaction="2", resid="3", status=0).save() JobModel(jobid="22", jobtype="InstVnf", jobaction="2", resid="3", status=0).save() JobModel(jobid="33", jobtype="InstVnf", jobaction="2", resid="3", status=0).save() progresses = JobUtil.get_unfinished_jobs(url_prefix="/vnfinst", inst_id="3", inst_type="InstVnf") expect_progresses = ['/vnfinst/11', '/vnfinst/22', '/vnfinst/33'] self.assertEqual(expect_progresses, progresses) JobModel.objects.filter().delete()
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_heal_ns_thread_when_nf_heal_failed(self, mock_get, mock_sleep, mock_run): mock_run.return_value = None mock_sleep.return_value = None mock_get.return_value = JobModel(progress=JOB_PROGRESS.ERROR) heal_ns_json = HEAL_NS_DICT.copy() heal_ns_json['healNsData']['vnfInstanceId'] = self.nf_inst_id ns_heal_service = NSHealService(self.ns_inst_id, heal_ns_json, self.job_id) ns_heal_service.run() self.assertEqual( JobModel.objects.filter(jobid=self.job_id).first().progress, JOB_PROGRESS.ERROR) self.assertEqual( NSLcmOpOccModel.objects.filter( id=ns_heal_service.occ_id).first().operation_state, 'FAILED')
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 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 create_job(job_type, job_action, inst_id, user='', job_id=None, res_name=''): if job_id is None: job_id = JobUtil.__gen_job_id('%s-%s-%s' % (str(job_type).replace( ' ', '_'), str(job_action).replace(' ', '_'), str(inst_id))) job = JobModel() job.jobid = job_id job.jobtype = job_type job.jobaction = job_action job.resid = str(inst_id) job.status = JOB_STATUS.PROCESSING job.user = user job.starttime = datetime.datetime.now().strftime('%Y-%m-%d %X') job.progress = JOB_PROGRESS.STARTED job.resname = res_name logger.debug( "create a new job, jobid=%s, jobtype=%s, jobaction=%s, resid=%s, status=%d" % (job.jobid, job.jobtype, job.jobaction, job.resid, job.status)) job.save() return job_id