def test_create_fail_invalid_status(requireMocking): with pytest.raises(Exception): create.create( { 'body': json.dumps({ 'sample': 'test', 'status': 'not a valid status' }) }, {})
def test_create_success_with_substatus(requireMocking): timestamp = int(time.time() * 1000) data = json.dumps({'sample': 'myTest', 'status': 'corrected'}) response = create.create({'body': data}, {}) assert 'isBase64Encoded' in response assert response['statusCode'] == 200 assert 'corrected' == json.loads(response['body'])['status'][0]['value'] assert timestamp <= json.loads(response['body'])['status'][0]['time']
def test_create_success(requireMocking, state): timestamp = int(time.time() * 1000) jsonString = json.dumps({'sample': 'myTest', 'status': state}) response = create.create({'body': jsonString}, {}) assert 'isBase64Encoded' in response assert response['statusCode'] == 200 assert state == json.loads(response['body'])['status'][0]['value'] assert timestamp <= json.loads(response['body'])['status'][0]['time']
def test_create_with_fileHandle(requireMocking): timestamp = int(time.time() * 1000) data = json.dumps({ 'sample': 'myTest', 'status': 'entered', 'fileHandle': 'myTest.d' }) response = create.create({'body': data}, {}) assert 'isBase64Encoded' in response assert response['statusCode'] == 200 body = json.loads(response['body']) assert 'entered' == body['status'][0]['value'] assert timestamp <= body['status'][0]['time'] assert 'myTest.d' == body['status'][0]['fileHandle']
def test_create_reinjection(requireMocking): data = { 'sample': 'test_reinjected', 'experiment': 'myReinjectionTest', 'acquisition': { 'instrument': 'test inst', 'name': 'method blah', 'ionisation': 'positive', # psotivie || negative 'method': 'gcms' # gcms || lcms }, 'processing': { 'method': 'gcms' }, 'metadata': { 'class': '12345', 'species': 'alien', 'organ': 'honker' }, 'userdata': { 'label': 'filexxx', 'comment': '' }, } print('bootstraping acquisition data') response = acq_create.create({'body': json.dumps(data)}, {}) assert 'isBase64Encoded' in response assert 200 == response['statusCode'] time.sleep(1) tracking = [{ 'sample': 'test_reinjected_2', 'status': 'acquired', 'fileHandle': 'test_reinjected_2.mzml' }, { 'sample': 'test_reinjected_BU', 'status': 'acquired', 'fileHandle': 'test_reinjected_BU.mzml' }] responses = [create.create({'body': json.dumps(x)}, {}) for x in tracking] print([json.loads(s['body']) for s in responses])
def test_status_merging(requireMocking): data = [{ 'sample': 'processed-sample', 'status': 'entered' }, { 'sample': 'processed-sample', 'status': 'acquired' }, { 'sample': 'processed-sample', 'status': 'converted' }, { 'sample': 'processed-sample', 'status': 'processing' }, { 'sample': 'processed-sample', 'status': 'deconvoluted' }, { 'sample': 'processed-sample', 'status': 'corrected' }, { 'sample': 'processed-sample', 'status': 'annotated' }, { 'sample': 'processed-sample', 'status': 'quantified' }, { 'sample': 'processed-sample', 'status': 'replaced' }, { 'sample': 'processed-sample', 'status': 'exported' }] results = [create.create({'body': json.dumps(x)}, {}) for x in data] print("RESULTS: %s" % json.dumps(results, indent=2))
def test_create_with_experiment(requireMocking): data = { 'sample': 'test_experiment', 'experiment': '1', 'acquisition': { 'instrument': 'test inst', 'name': 'method blah', 'ionisation': 'positive', # psotivie || negative 'method': 'gcms' # gcms || lcms }, 'processing': { 'method': 'gcms' }, 'metadata': { 'class': '12345', 'species': 'alien', 'organ': 'honker' }, 'userdata': { 'label': 'filexxx', 'comment': '' }, } print('bootstraping acquisition data') response = acq_create.create({'body': json.dumps(data)}, {}) assert 'isBase64Encoded' in response assert 200 == response['statusCode'] time.sleep(1) track = {'sample': 'test_experiment', 'status': 'entered'} response = create.create({'body': json.dumps(track)}, {}) assert 'isBase64Encoded' in response assert 200 == response['statusCode'] assert '1' == json.loads(response['body'])['experiment'] assert 'test_experiment' == json.loads(response['body'])['sample']
def schedule_steac_to_fargate(event, context): """ submits a new task to the cluster - a fargate task will run it :param event: :param context: :return: """ body = json.loads(event['body']) try: validate(body, __SCHEDULE__) import boto3 overrides = { "containerOverrides": [{ "name": "carrot-steac", "environment": [ { "name": "SPRING_PROFILES_ACTIVE", "value": "{}{}".format('aws', os.getenv('current_stage')) # AWS profile needs to be active for this system to connect to the AWS database }, { "name": "CARROT_METHOD", "value": "{}".format(body['method']) }, ] }] } task_name = "{}-{}".format(os.getenv("current_stage"), SECURE_CARROT_STEAC) if 'key' in body and body['key'] is not None: overrides['containerOverrides'][0]['environment'].append({ "name": "STASIS_KEY", "value": body['key'] }) send_to_fargate(overrides=overrides, task_name=task_name) return { 'statusCode': 200, 'isBase64Encoded': False, 'headers': __HTTP_HEADERS__, 'body': { "message": "scheduled a steac aggregation" } } except ValidationError as e: logger.info("validation error") logger.info(body) traceback.print_exc() return { 'body': json.dumps(str(e)), 'statusCode': 503, 'isBase64Encoded': False, 'headers': __HTTP_HEADERS__ } pass except Exception as e: logger.info(body) traceback.print_exc() create( { "body": json.dumps({ 'sample': body['sample'], 'status': FAILED, 'reason': str(e) }) }, {}) return { 'body': json.dumps(str(e)), 'statusCode': 503, 'isBase64Encoded': False, 'headers': __HTTP_HEADERS__ }
def test_status(requireMocking): for x in range(0, 100): # upload data data = json.dumps({ "job": "123456", "sample": "abc_{}".format(x), "state": "scheduled" }) # store in stasis or we doomed!!! response = create.create({'body': json.dumps({'sample': 'abc_{}'.format(x), 'status': 'scheduled'})}, {}) # store in the job state table result = tracking.create({'body': data}, {}) assert result['statusCode'] == 200 set_job_state(job="123456", method="test", profile="test", state=SCHEDULED) result = tracking.status({ "pathParameters": { "job": "123456", } }, {}) assert result is not None assert result['statusCode'] == 200 assert json.loads(result['body'])['job_state'] == 'scheduled' for x in range(0, 10): # pretend stasis has now exported the data response = create.create({'body': json.dumps({'sample': 'abc_{}'.format(x), 'status': 'exported'})}, {}) calculate_job_state("123456") result = tracking.status({ "pathParameters": { "job": "123456", } }, {}) # since not yet aggregated, the job should be in state processing assert json.loads(result['body'])['job_state'] == 'processing' for x in range(0, 100): # pretend all samples are finished response = create.create({'body': json.dumps({'sample': 'abc_{}'.format(x), 'status': "finished"})}, {}) result = tracking.status({ "pathParameters": { "job": "123456", } }, {}) monitor_jobs({}, {}) assert json.loads(result['body'])['job_state'] == PROCESSING result = tracking.status({ "pathParameters": { "job": "123456", }, "body": json.dumps({ 'job_state': FAILED }) }, {}) assert json.loads(result['body'])['job_state'] == FAILED