def test_read_logs(): requests = [ jobs_pb2.JobsReadLogsRequest( header=BaseClient.get_request_header(), job_id=common_pb2.Identifier( value='432b274a8f754968888807fe1eba237b' ), operator_name="dicom-reader" ) ] responses = [ jobs_pb2.JobsReadLogsResponse( header=common_pb2.ResponseHeader( code=0, messages=[]), job_id=common_pb2.Identifier( value='432b274a8f754968888807fe1eba237b' ), operator_name="Dicom Reader", logs=["Log_String_0", "Log_String_1"] ), jobs_pb2.JobsReadLogsResponse( header=common_pb2.ResponseHeader( code=0, messages=[]), job_id=common_pb2.Identifier( value='432b274a8f754968888807fe1eba237b' ), operator_name="Dicom Reader", logs=["Log_String_2", "Log_String_3"] ) ] stub_method_handlers = [( 'ReadLogs', 'unary_stream', ( requests, responses ) )] MockClaraJobsServiceClient.stub_method_handlers = stub_method_handlers with MockClaraJobsServiceClient('10.0.0.1:50051') as client: job_logs = client.job_logs( job_id=job_types.JobId(value='432b274a8f754968888807fe1eba237b'), operator_name="dicom-reader" ) print(len(job_logs)) assert len(job_logs) == 4 assert job_logs[0] == "Log_String_0" assert job_logs[1] == "Log_String_1" assert job_logs[2] == "Log_String_2" assert job_logs[3] == "Log_String_3"
def test_create_payload(): requests = [ payloads_pb2.PayloadsCreateRequest( header=BaseClient.get_request_header()) ] responses = [ payloads_pb2.PayloadsCreateResponse( header=common_pb2.ResponseHeader(code=0, messages=[]), payload_id=common_pb2.Identifier( value='92656d79fa414db6b294069c0e9e6df5'), type=payloads_pb2.PAYLOAD_TYPE_REUSABLE) ] stub_method_handlers = [('Create', 'unary_unary', (requests, responses))] # set handlers MockClaraPayloadServiceClient.stub_method_handlers = stub_method_handlers with MockClaraPayloadServiceClient('localhost:50051') as client: payload_details = client.create_payload() print(payload_details.payload_id) print(payload_details.payload_type) assert payload_details.payload_id.value == '92656d79fa414db6b294069c0e9e6df5' assert payload_details.payload_type == 2
def test_create_pipeline(): pipeline_yaml = 'pipeline.yaml' requests = [ pipelines_pb2.PipelinesCreateRequest( header=BaseClient.get_request_header(), definition=pipelines_pb2.PipelineDefinitionFile( path='pipeline.yaml', content=PIPELINE_TEXT)) ] responses = [ pipelines_pb2.PipelinesCreateResponse( header=common_pb2.ResponseHeader(code=0, messages=[]), pipeline_id=common_pb2.Identifier( value='92656d79fa414db6b294069c0e9e6df5')) ] stub_method_handlers = [('Create', 'stream_unary', (requests, responses))] # set handlers MockClaraPipelineServiceClient.stub_method_handlers = stub_method_handlers def_list = [ pipeline_types.PipelineDefinition(name=pipeline_yaml, content=PIPELINE_TEXT) ] with MockClaraPipelineServiceClient('localhost:50051') as client: pipeline_id = client.create_pipeline(definition=def_list) print(pipeline_id) assert pipeline_id.value == '92656d79fa414db6b294069c0e9e6df5'
def test_create_job(): requests = [ jobs_pb2.JobsCreateRequest( header=BaseClient.get_request_header(), name='test job', pipeline_id=common_pb2.Identifier( value='92656d79fa414db6b294069c0e9e6df5' ), priority=jobs_pb2.JOB_PRIORITY_NORMAL ) ] responses = [ jobs_pb2.JobsCreateResponse( header=common_pb2.ResponseHeader( code=0, messages=[]), job_id=common_pb2.Identifier( value='432b274a8f754968888807fe1eba237b' ), payload_id=common_pb2.Identifier( value='7ac5c691e13d4f45894a3a70d9925936' ) ) ] stub_method_handlers = [( 'Create', 'unary_unary', ( requests, responses ) )] MockClaraJobsServiceClient.stub_method_handlers = stub_method_handlers with MockClaraJobsServiceClient('localhost:50051') as client: job_info = client.create_job( job_name='test job', pipeline_id=pipeline_types.PipelineId('92656d79fa414db6b294069c0e9e6df5') ) print(job_info.job_id.value, job_info.payload_id.value) assert job_info.job_id.value == '432b274a8f754968888807fe1eba237b' assert job_info.payload_id.value == '7ac5c691e13d4f45894a3a70d9925936'
def test_cancel_job(): requests = [ jobs_pb2.JobsCancelRequest( header=BaseClient.get_request_header(), job_id=common_pb2.Identifier( value='432b274a8f754968888807fe1eba237b' ) ) ] responses = [ jobs_pb2.JobsCancelResponse( header=common_pb2.ResponseHeader( code=0, messages=[]), job_id=common_pb2.Identifier( value='432b274a8f754968888807fe1eba237b' ), job_state=jobs_pb2.JOB_STATE_STOPPED, job_status=jobs_pb2.JOB_STATUS_CANCELED ) ] stub_method_handlers = [( 'Cancel', 'unary_unary', ( requests, responses ) )] MockClaraJobsServiceClient.stub_method_handlers = stub_method_handlers with MockClaraJobsServiceClient('10.0.0.1:50051') as client: job_token = client.cancel_job( job_id=job_types.JobId(value='432b274a8f754968888807fe1eba237b') ) print(job_token.job_id.value, job_token.job_state, job_token.job_status) assert job_token.job_id.value == '432b274a8f754968888807fe1eba237b' assert job_token.job_state == 3 assert job_token.job_status == 3
def test_list_pipeline(): requests = [ pipelines_pb2.PipelinesListRequest( header=BaseClient.get_request_header()) ] responses = [ pipelines_pb2.PipelinesListResponse( header=common_pb2.ResponseHeader(code=0, messages=[]), details=pipelines_pb2.PipelinesListResponse.PipelineDetails( name='Pipeline_1', pipeline_id=common_pb2.Identifier( value='92656d79fa414db6b294069c0e9e6df5'))), pipelines_pb2.PipelinesListResponse( header=common_pb2.ResponseHeader(code=0, messages=[]), details=pipelines_pb2.PipelinesListResponse.PipelineDetails( name='Pipeline_2', pipeline_id=common_pb2.Identifier( value='21656d79fa414db6b294069c0e9e6r23'))) ] stub_method_handlers = [('List', 'unary_stream', (requests, responses))] # set handlers MockClaraPipelineServiceClient.stub_method_handlers = stub_method_handlers with MockClaraPipelineServiceClient('localhost:50051') as client: pipeline_list = client.list_pipelines() print(pipeline_list) assert len(pipeline_list) == 2 assert pipeline_list[ 0].pipeline_id.value == '92656d79fa414db6b294069c0e9e6df5' assert pipeline_list[ 1].pipeline_id.value == '21656d79fa414db6b294069c0e9e6r23'
def test_download_file(): fake_payload_id = '7ac5c691e13d4f45894a3a70d9925936' fake_request_file_name = '/input/highResCT.mhd' requests = [ payloads_pb2.PayloadsDownloadRequest( header=BaseClient.get_request_header(), payload_id=common_pb2.Identifier(value=fake_payload_id), name=fake_request_file_name) ] responses = [ payloads_pb2.PayloadsDownloadResponse( header=common_pb2.ResponseHeader(code=0, messages=[]), details=payloads_pb2.PayloadFileDetails( mode=0, name=fake_request_file_name, size=len(MHD_TEXT)), data=MHD_TEXT.encode('utf-8')) ] stub_method_handlers = [('Download', 'unary_stream', (requests, responses)) ] MockClaraPayloadServiceClient.stub_method_handlers = stub_method_handlers with MockClaraPayloadServiceClient('localhost:50051') as client: if os.path.exists('./highResCT.mhd'): os.remove('./highResCT.mhd') with open('./highResCT.mhd', 'wb+') as wb: file_details = client.download_from( payload_id=payload_types.PayloadId(fake_payload_id), blob_name=fake_request_file_name, dest_obj=wb) assert file_details.mode == 0 assert file_details.name == fake_request_file_name assert file_details.size == len(MHD_TEXT) data = '' with open('./highResCT.mhd', 'r') as file: data = file.read() os.remove('./highResCT.mhd') print("Data Returned: ") print(data) assert data == MHD_TEXT
def test_upload(tmp_path): fake_payload_id = '7ac5c691e13d4f45894a3a70d9925936' fake_file_name = './image.mhd' fake_response_file_name = './input/image.mhd' requests = [ payloads_pb2.PayloadsUploadRequest( header=BaseClient.get_request_header(), payload_id=common_pb2.Identifier(value=fake_payload_id), details=payloads_pb2.PayloadFileDetails( mode=0, name=fake_response_file_name, size=len(MHD_TEXT)), data=MHD_TEXT.encode('utf-8')) ] responses = [ payloads_pb2.PayloadsUploadResponse( header=common_pb2.ResponseHeader(code=0, messages=[]), details=payloads_pb2.PayloadFileDetails( mode=0, name=fake_response_file_name, size=len(MHD_TEXT))) ] stub_method_handlers = [('Upload', 'stream_unary', (requests, responses))] MockClaraPayloadServiceClient.stub_method_handlers = stub_method_handlers with MockClaraPayloadServiceClient('localhost:50051') as client: if os.path.exists(fake_file_name): os.remove(fake_file_name) with open(fake_file_name, 'w') as wb: wb.write(MHD_TEXT) file_details = None with open(fake_file_name, 'rb+') as fp: file_details = client.upload( payload_id=payload_types.PayloadId(fake_payload_id), blob_name=fake_response_file_name, file_object=fp) os.remove(fake_file_name) print(file_details.mode, file_details.name, file_details.size) assert file_details.mode == 0 assert file_details.name == fake_response_file_name assert file_details.size == len(MHD_TEXT)
def test_start_job(): requests = [ jobs_pb2.JobsStartRequest( header=BaseClient.get_request_header(), job_id=common_pb2.Identifier( value='432b274a8f754968888807fe1eba237b' ) ) ] responses = [ jobs_pb2.JobsStartResponse( header=common_pb2.ResponseHeader( code=0, messages=[]), state=jobs_pb2.JOB_STATE_RUNNING, status=jobs_pb2.JOB_STATUS_HEALTHY, priority=jobs_pb2.JOB_PRIORITY_NORMAL ) ] stub_method_handlers = [( 'Start', 'unary_unary', ( requests, responses ) )] MockClaraJobsServiceClient.stub_method_handlers = stub_method_handlers with MockClaraJobsServiceClient('10.0.0.1:50051') as client: job_token = client.start_job( job_id=job_types.JobId(value='432b274a8f754968888807fe1eba237b') ) print(job_token.job_id.value, job_token.job_state, job_token.job_status) assert job_token.job_id.value == '432b274a8f754968888807fe1eba237b' assert job_token.job_state == 2 assert job_token.job_status == 1
def to_grpc_value(self): id = common_pb2.Identifier() id.value = self._value return id
def test_list_jobs(): requests = [ jobs_pb2.JobsListRequest( header=BaseClient.get_request_header() ) ] responses = [ jobs_pb2.JobsListResponse( header=common_pb2.ResponseHeader( code=0, messages=[]), job_details=jobs_pb2.JobsListResponse.JobDetails( job_name="job_1", job_id=common_pb2.Identifier( value="432b274a8f754968888807fe1eba237b" ), payload_id=common_pb2.Identifier( value='532b274a8f754968888807fe1eba237b' ), pipeline_id=common_pb2.Identifier( value='932b274a8f754968888807fe1eba237b' ), created=common_pb2.Timestamp( value=63750823591 ) ) ), jobs_pb2.JobsListResponse( header=common_pb2.ResponseHeader( code=0, messages=[]), job_details=jobs_pb2.JobsListResponse.JobDetails( job_name="job_2", job_id=common_pb2.Identifier( value='212b274a8f754968888807fe1eba237b' ), payload_id=common_pb2.Identifier( value='212b274a8f754968888807fe1eba237b' ), pipeline_id=common_pb2.Identifier( value='322b274a8f754968888807fe1eba237b' ), created=common_pb2.Timestamp( value=63750823591 ) ) ) ] stub_method_handlers = [( 'List', 'unary_stream', ( requests, responses ) )] MockClaraJobsServiceClient.stub_method_handlers = stub_method_handlers with MockClaraJobsServiceClient('10.0.0.1:50051') as client: list_jobs = client.list_jobs() print("Length of list response: " + str(len(list_jobs))) assert len(list_jobs) == 2 assert list_jobs[0].name == "job_1" assert list_jobs[0].job_id.value == "432b274a8f754968888807fe1eba237b" assert list_jobs[0].payload_id.value == "532b274a8f754968888807fe1eba237b" assert list_jobs[0].pipeline_id.value == "932b274a8f754968888807fe1eba237b" assert list_jobs[0].date_created == datetime.datetime(2021, 3, 8, 18, 6, 31, tzinfo=datetime.timezone.utc) assert list_jobs[1].name == "job_2" assert list_jobs[1].job_id.value == '212b274a8f754968888807fe1eba237b' assert list_jobs[1].payload_id.value == '212b274a8f754968888807fe1eba237b' assert list_jobs[1].pipeline_id.value == '322b274a8f754968888807fe1eba237b' assert list_jobs[1].date_created == datetime.datetime(2021, 3, 8, 18, 6, 31, tzinfo=datetime.timezone.utc)
def test_get_status(): requests = [ jobs_pb2.JobsStatusRequest( header=BaseClient.get_request_header(), job_id=common_pb2.Identifier( value='432b274a8f754968888807fe1eba237b' ) ) ] fake_seconds_from_epoch = 63763345820 responses = [ jobs_pb2.JobsStatusResponse( header=common_pb2.ResponseHeader( code=0, messages=[]), name="job_1", job_id=common_pb2.Identifier( value='432b274a8f754968888807fe1eba237b' ), pipeline_id=common_pb2.Identifier( value='92656d79fa414db6b294069c0e9e6df5' ), payload_id=common_pb2.Identifier( value='7ac5c691e13d4f45894a3a70d9925936' ), state=jobs_pb2.JOB_STATE_RUNNING, status=jobs_pb2.JOB_STATUS_HEALTHY, created=common_pb2.Timestamp(value=fake_seconds_from_epoch) ) ] stub_method_handlers = [( 'Status', 'unary_unary', ( requests, responses ) )] MockClaraJobsServiceClient.stub_method_handlers = stub_method_handlers with MockClaraJobsServiceClient('10.0.0.1:50051') as client: job_details = client.get_status( job_id=job_types.JobId(value='432b274a8f754968888807fe1eba237b') ) print(job_details.job_id.value, job_details.job_state, job_details.job_status) print(job_details.date_created) print(datetime.datetime.fromtimestamp(float(fake_seconds_from_epoch) - 62135596800)) assert job_details.name == "job_1" assert job_details.job_id.value == '432b274a8f754968888807fe1eba237b' assert job_details.pipeline_id.value == '92656d79fa414db6b294069c0e9e6df5' assert job_details.payload_id.value == '7ac5c691e13d4f45894a3a70d9925936' assert job_details.job_state == 2 assert job_details.job_status == 1 assert job_details.date_created == datetime.datetime.fromtimestamp( float(fake_seconds_from_epoch) - 62135596800).astimezone(datetime.timezone.utc)