Beispiel #1
0
def test_list_ec2_instance(aws_region, tag_key, tag_value, result_count):
    """Verify list ec2 instances function."""
    client = boto3.client("ec2", region_name=aws_region)
    launch_ec2_instances(3, aws_region, "tostop", "true")
    ec2_scheduler = InstanceScheduler(aws_region)
    taglist = ec2_scheduler.list_instances(tag_key, tag_value)
    assert len(list(taglist)) == result_count
def test_start_ec2_scheduler(aws_region, instance_tag, scheduler_tag,
                             result_count):
    """Verify start ec2 scheduler class method."""
    client = boto3.client("ec2", region_name=aws_region)
    tag_key = instance_tag[0]["Key"]
    tag_value = "".join(instance_tag[0]["Values"])
    instances = launch_ec2_instances(2, aws_region, tag_key, tag_value)
    instance_ids = [x["InstanceId"] for x in instances["Instances"]]

    try:
        client.get_waiter("instance_running").wait(InstanceIds=instance_ids)
        client.stop_instances(InstanceIds=instance_ids)
        client.get_waiter("instance_stopped").wait(InstanceIds=instance_ids)
        ec2_scheduler = InstanceScheduler(aws_region)
        ec2_scheduler.cloudwatch_alarm = CloudWatchAlarmScheduler(aws_region)
        ec2_scheduler.start(scheduler_tag)
        if scheduler_tag == instance_tag:
            client.get_waiter("instance_running").wait(
                InstanceIds=instance_ids)

        ec2_describe = client.describe_instances(InstanceIds=instance_ids)
        for ec2 in ec2_describe["Reservations"][0]["Instances"]:
            assert ec2["State"] == result_count
    finally:
        # Clean aws account
        client.terminate_instances(InstanceIds=instance_ids)
def test_do_not_stop_asg_instance(aws_region, aws_tags, result_count):
    client = boto3.client("ec2", region_name=aws_region)
    launch_asg(aws_region, "tostop", "true")

    ec2_scheduler = InstanceScheduler(aws_region)
    ec2_scheduler.cloudwatch_alarm = CloudWatchAlarmScheduler(aws_region)
    ec2_scheduler.stop(aws_tags)
    instances = client.describe_instances()["Reservations"][0]["Instances"]
    assert len(instances) == 3
    for instance in instances:
        assert instance["State"] == result_count
Beispiel #4
0
def test_stop_ec2_instance(aws_region, tag_key, tag_value, result_count):
    """Verify stop ec2 instance function."""
    client = boto3.client("ec2", region_name=aws_region)
    launch_ec2_instances(3, aws_region, tag_key, tag_value)

    ec2_scheduler = InstanceScheduler(aws_region)
    ec2_scheduler.cloudwatch_alarm = CloudWatchAlarmScheduler(aws_region)
    ec2_scheduler.stop("tostop", "true")
    instances = client.describe_instances()["Reservations"][0]["Instances"]
    assert len(instances) == 3
    for instance in instances:
        assert instance["State"] == result_count
Beispiel #5
0
def test_start_ec2_instance(aws_region, tag_key, tag_value, result_count):
    """Verify start ec2 instance function."""
    client = boto3.client("ec2", region_name=aws_region)
    launch_ec2_instances(3, aws_region, "tostop", "true")
    for ec2 in client.describe_instances()["Reservations"][0]["Instances"]:
        client.stop_instances(InstanceIds=[ec2["InstanceId"]])

    ec2_scheduler = InstanceScheduler(aws_region)
    ec2_scheduler.cloudwatch_alarm = CloudWatchAlarmScheduler(aws_region)
    ec2_scheduler.start(tag_key, tag_value)
    for ec2 in client.describe_instances()["Reservations"][0]["Instances"]:
        assert ec2["State"] == result_count
def test_stop_ec2_scheduler(aws_region, tag_key, tag_value, result_count):
    """Verify stop ec2 scheduler class method."""
    client = boto3.client("ec2", region_name=aws_region)
    instances = launch_ec2_instances(2, aws_region, tag_key, tag_value)
    instance_ids = [x["InstanceId"] for x in instances["Instances"]]

    try:
        client.get_waiter("instance_running").wait(InstanceIds=instance_ids)
        ec2_scheduler = InstanceScheduler(aws_region)
        ec2_scheduler.cloudwatch_alarm = CloudWatchAlarmScheduler(aws_region)
        ec2_scheduler.stop("tostop-ec2-test-1", "true")
        if tag_key == "tostop-ec2-test-1" and tag_value == "true":
            client.get_waiter("instance_stopped").wait(
                InstanceIds=instance_ids)

        ec2_describe = client.describe_instances(InstanceIds=instance_ids)
        for ec2 in ec2_describe["Reservations"][0]["Instances"]:
            assert ec2["State"] == result_count
    finally:
        # Clean aws account
        client.terminate_instances(InstanceIds=instance_ids)