def should_not_start_ec2_instances_for_18_Shutdown_tag_if_already_stopped():
    os.environ['AVAILABILITY_TAG_VALUES'] = '18_Shutdown'
    os.environ['CURR_TIME'] = '2020-11-15T18:00:00+11:00'

    region = 'ap-southeast-2'
    client = boto3.client('ec2', region_name=region)
    reservation = client.run_instances(ImageId='ami-1234abcd',
                                       MinCount=1,
                                       MaxCount=1)
    instance_id = reservation['Instances'][0]['InstanceId']
    client.stop_instances(InstanceIds=[instance_id])

    tags = list(
        map(lambda x: create_tag_obj(x),
            os.environ['AVAILABILITY_TAG_VALUES'].split(",")))

    client.create_tags(Resources=[instance_id], Tags=tags)

    handler.ec2_stop_start(None, None)

    ec2 = boto3.resource('ec2', region_name=region)
    instances = ec2.instances.filter(Filters=[{
        'Name': 'instance-state-name',
        'Values': ['stopped']
    }])
    instance_id_list = list(map(lambda x: x.id, instances))
    filtered_instance_ids = list(
        filter(lambda x: x == instance_id, instance_id_list))
    assert instance_id in filtered_instance_ids
def should_start_ec2_instances_for_24x7_Mon_Sun_tag_if_stopped():
    os.environ['AVAILABILITY_TAG_VALUES'] = '24x7_Mon-Sun'
    os.environ['CURR_TIME'] = '2020-11-14T00:01:00+11:00'
    # os.environ['CURR_TIME'] = datetime_object.strftime("%m/%d/%Y, %H:%M:%S")
    region = 'ap-southeast-2'
    client = boto3.client('ec2', region_name=region)
    reservation = client.run_instances(ImageId='ami-1234abcd',
                                       MinCount=1,
                                       MaxCount=1)
    instance_id = reservation['Instances'][0]['InstanceId']

    tags = list(
        map(lambda x: create_tag_obj(x),
            os.environ['AVAILABILITY_TAG_VALUES'].split(",")))

    client.create_tags(Resources=[instance_id], Tags=tags)
    client.stop_instances(InstanceIds=[instance_id])

    handler.ec2_stop_start(None, None)

    ec2 = boto3.resource('ec2', region_name=region)
    instances = ec2.instances.filter(Filters=[{
        'Name': 'instance-state-name',
        'Values': ['running']
    }])
    instance_id_list = list(map(lambda x: x.id, instances))
    filtered_instance_ids = list(
        filter(lambda x: x == instance_id, instance_id_list))
    assert instance_id in filtered_instance_ids
def should_stop_availability_tag_with_invalid_value():
    os.environ['AVAILABILITY_TAG_VALUES'] = '24x7_Mon-Sun'
    os.environ['CURR_TIME'] = '2020-11-14T00:01:00+11:00'
    region = 'ap-southeast-2'
    client = boto3.client('ec2', region_name=region)
    reservation = client.run_instances(ImageId='ami-1234abcd',
                                       MinCount=1,
                                       MaxCount=1)
    instance_id = reservation['Instances'][0]['InstanceId']

    tags = list(map(lambda x: create_tag_obj(x), 'blah'.split(",")))

    client.create_tags(Resources=[instance_id], Tags=tags)

    handler.ec2_stop_start(None, None)

    ec2 = boto3.resource('ec2', region_name=region)
    instances = ec2.instances.filter(Filters=[{
        'Name': 'instance-state-name',
        'Values': ['stopped']
    }])
    instance_id_list = list(map(lambda x: x.id, instances))
    filtered_instance_ids = list(
        filter(lambda x: x == instance_id, instance_id_list))
    assert instance_id in filtered_instance_ids
def should_send_email_for_invalid_and_maintenance_availability_tag_value():
    os.environ['AVAILABILITY_TAG_VALUES'] = 'blah'
    os.environ['EMAIL_ALERTS_FLAG'] = 'true'
    region = 'ap-southeast-2'
    client = boto3.client('ec2', region_name=region)
    reservation = client.run_instances(ImageId='ami-1234abcd',
                                       MinCount=2,
                                       MaxCount=2)
    instance_id_one = reservation['Instances'][0]['InstanceId']
    instance_id_two = reservation['Instances'][1]['InstanceId']

    tags = list(map(lambda x: create_tag_obj(x), 'blah'.split(",")))

    client.create_tags(Resources=[instance_id_one], Tags=tags)

    tags = list(map(lambda x: create_tag_obj(x), 'Maintenance'.split(",")))

    client.create_tags(Resources=[instance_id_two], Tags=tags)

    conn = boto3.client("ses", region_name="us-west-2")
    conn.verify_email_address(EmailAddress=os.environ['EMAIL_FROM'])
    conn.verify_email_address(EmailAddress=os.environ['EMAIL_TO'])

    handler.ec2_stop_start(None, None)

    send_quota = conn.get_send_quota()
    sent_count = int(send_quota["SentLast24Hours"])
    assert sent_count == 1
def should_not_send_email_if_alert_flag_false():
    os.environ['AVAILABILITY_TAG_VALUES'] = '24x5_Mon-Fri'
    os.environ['EMAIL_ALERTS_FLAG'] = 'false'
    region = 'ap-southeast-2'
    client = boto3.client('ec2', region_name=region)
    reservation = client.run_instances(ImageId='ami-1234abcd',
                                       MinCount=1,
                                       MaxCount=1)
    instance_id = reservation['Instances'][0]['InstanceId']

    tags = list(map(lambda x: create_tag_obj(x), 'blah'.split(",")))

    client.create_tags(Resources=[instance_id], Tags=tags)
    conn = boto3.client("ses", region_name="us-west-2")
    conn.verify_email_address(EmailAddress=os.environ['EMAIL_FROM'])
    conn.verify_email_address(EmailAddress=os.environ['EMAIL_TO'])

    handler.ec2_stop_start(None, None)

    send_quota = conn.get_send_quota()
    sent_count = int(send_quota["SentLast24Hours"])
    assert sent_count == 0