# Path: tibanna/lambdas/check_task_awsem.py
# def handler(event, context):
# 
# Path: tibanna/exceptions.py
# class EC2StartingException(Exception):
#     """EC2 AWSEM instance is still starting (job not complete)"""
#     pass
# 
# class StillRunningException(Exception):
#     """EC2 AWSEM instance is still running (job not complete)"""
#     pass
# 
# class MetricRetrievalException(Exception):
#     pass
# 
# class EC2IdleException(Exception):
#     pass
# 
# class JobAbortedException(Exception):
#     pass
# 
# Path: tibanna/vars.py
# AWSEM_TIME_STAMP_FORMAT = '%Y%m%d-%H:%M:%S-UTC'


from tibanna.lambdas import check_task_awsem as service
from tibanna.exceptions import (
    EC2StartingException,
    StillRunningException,
    MetricRetrievalException,
    EC2IdleException,
    JobAbortedException
)
from datetime import datetime, timedelta
from dateutil.tz import tzutc
from tibanna.vars import AWSEM_TIME_STAMP_FORMAT
import pytest
import boto3
import random
import string
import json
    with pytest.raises(MetricRetrievalException) as excinfo:
        retval = service.handler(check_task_input_modified, '')
    assert 'error getting metrics' in str(excinfo.value)
    s3.delete_objects(Delete={'Objects': [{'Key': job_started}]})
    s3.delete_objects(Delete={'Objects': [{'Key': job_success}]})
    s3.delete_objects(Delete={'Objects': [{'Key': postrunjson}]})
    #assert 'postrunjson' in retval
    #assert retval['postrunjson'] == jsondict
    #del retval['postrunjson']
    #assert retval == check_task_input_modified


@pytest.mark.webtest
def test_check_task_awsem_with_long_postrunjson(check_task_input, s3):
    jobid = 'some_uniq_jobid'
    check_task_input_modified = check_task_input
    check_task_input_modified['jobid'] = jobid
    job_started = "%s.job_started" % jobid
    s3.put_object(Body=b'', Key=job_started)
    job_success = "%s.success" % jobid
    s3.put_object(Body=b'', Key=job_success)
    postrunjson = "%s.postrun.json" % jobid
    verylongstring = ''.join(random.choice(string.ascii_uppercase) for _ in range(50000))
    jsondict = {"config": {"log_bucket": "somelogbucket"},
                "Job": {"JOBID": jobid, "start_time": '20190814-21:01:07-UTC',
                        "App": {}, "Output": {},
                        "Input": {'Input_files_data': {}, 'Input_parameters': {}, 'Secondary_files_data': {}}},
                "commands": verylongstring}
    jsoncontent = json.dumps(jsondict)
    s3.put_object(Body=jsoncontent.encode(), Key=postrunjson)

# Path: tibanna/lambdas/check_task_awsem.py
# def handler(event, context):
# 
# Path: tibanna/exceptions.py
# class EC2StartingException(Exception):
#     """EC2 AWSEM instance is still starting (job not complete)"""
#     pass
# 
# class StillRunningException(Exception):
#     """EC2 AWSEM instance is still running (job not complete)"""
#     pass
# 
# class MetricRetrievalException(Exception):
#     pass
# 
# class EC2IdleException(Exception):
#     pass
# 
# class JobAbortedException(Exception):
#     pass
# 
# Path: tibanna/vars.py
# AWSEM_TIME_STAMP_FORMAT = '%Y%m%d-%H:%M:%S-UTC'


# Path: tests/tibanna/unicorn/check_task_awsem/test_handler.py
from tibanna.lambdas import check_task_awsem as service
from tibanna.exceptions import (
    EC2StartingException,
    StillRunningException,
    MetricRetrievalException,
    EC2IdleException,
    JobAbortedException
)
from datetime import datetime, timedelta
from dateutil.tz import tzutc
from tibanna.vars import AWSEM_TIME_STAMP_FORMAT
import pytest
import boto3
import random
import string
import json

    with pytest.raises(MetricRetrievalException) as excinfo:
        retval = service.handler(check_task_input_modified, '')
    assert 'error getting metrics' in str(excinfo.value)
    s3.delete_objects(Delete={'Objects': [{'Key': job_started}]})
    s3.delete_objects(Delete={'Objects': [{'Key': job_success}]})
    s3.delete_objects(Delete={'Objects': [{'Key': postrunjson}]})
    #assert 'postrunjson' in retval
    #assert retval['postrunjson'] == jsondict
    #del retval['postrunjson']
    #assert retval == check_task_input_modified


@pytest.mark.webtest
def test_check_task_awsem_with_long_postrunjson(check_task_input, s3):
    jobid = 'some_uniq_jobid'
    check_task_input_modified = check_task_input
    check_task_input_modified['jobid'] = jobid
    job_started = "%s.job_started" % jobid
    s3.put_object(Body=b'', Key=job_started)
    job_success = "%s.success" % jobid
    s3.put_object(Body=b'', Key=job_success)
    postrunjson = "%s.postrun.json" % jobid
    verylongstring = ''.join(random.choice(string.ascii_uppercase) for _ in range(50000))
    jsondict = {"config": {"log_bucket": "somelogbucket"},
                "Job": {"JOBID": jobid, "start_time": '20190814-21:01:07-UTC',
                        "App": {}, "Output": {},
                        "Input": {'Input_files_data': {}, 'Input_parameters': {}, 'Secondary_files_data': {}}},
                "commands": verylongstring}
    jsoncontent = json.dumps(jsondict)
    s3.put_object(Body=jsoncontent.encode(), Key=postrunjson)

    with pytest.raises(MetricRetrievalException) as excinfo: