# 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


@pytest.fixture()
def s3(check_task_input):
    bucket_name = check_task_input['config']['log_bucket']
    return boto3.resource('s3').Bucket(bucket_name)


@pytest.mark.webtest
def test_check_task_awsem_fails_if_no_job_started(check_task_input, s3):
    # ensure there is no job started
    jobid = 'notmyjobid'
    check_task_input_modified = check_task_input
    check_task_input_modified['jobid'] = jobid
    check_task_input_modified['config']['start_time'] = datetime.strftime(datetime.now(tzutc()) - timedelta(minutes=4),
                                                                          AWSEM_TIME_STAMP_FORMAT)
    job_started = "%s.job_started" % jobid
    s3.delete_objects(Delete={'Objects': [{'Key': job_started}]})
    with pytest.raises(EC2StartingException) as excinfo:
        service.handler(check_task_input_modified, '')
    assert 'Failed to find jobid' in str(excinfo.value)


@pytest.mark.webtest
def test_check_task_awsem_fails_if_no_job_started_for_too_long(check_task_input, s3):
    # ensure there is no job started
    jobid = 'notmyjobid'
    check_task_input_modified = check_task_input
    check_task_input_modified['jobid'] = jobid
    check_task_input_modified['config']['start_time'] = datetime.strftime(datetime.now(tzutc()) - timedelta(minutes=13),

# 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



@pytest.fixture()
def s3(check_task_input):
    bucket_name = check_task_input['config']['log_bucket']
    return boto3.resource('s3').Bucket(bucket_name)


@pytest.mark.webtest
def test_check_task_awsem_fails_if_no_job_started(check_task_input, s3):
    # ensure there is no job started
    jobid = 'notmyjobid'
    check_task_input_modified = check_task_input
    check_task_input_modified['jobid'] = jobid
    check_task_input_modified['config']['start_time'] = datetime.strftime(datetime.now(tzutc()) - timedelta(minutes=4),
                                                                          AWSEM_TIME_STAMP_FORMAT)
    job_started = "%s.job_started" % jobid
    s3.delete_objects(Delete={'Objects': [{'Key': job_started}]})
    with pytest.raises(EC2StartingException) as excinfo:
        service.handler(check_task_input_modified, '')
    assert 'Failed to find jobid' in str(excinfo.value)


@pytest.mark.webtest
def test_check_task_awsem_fails_if_no_job_started_for_too_long(check_task_input, s3):
    # ensure there is no job started
    jobid = 'notmyjobid'
    check_task_input_modified = check_task_input
    check_task_input_modified['jobid'] = jobid
    check_task_input_modified['config']['start_time'] = datetime.strftime(datetime.now(tzutc()) - timedelta(minutes=13),

                                                                          AWSEM_TIME_STAMP_FORMAT)