def test_list_cloudwatch_alarms(aws_region, cloudwatch_tag, scheduler_tag,
                                result_count):
    """Verify list_alarms scheduler class method."""
    client = boto3.client("cloudwatch", region_name=aws_region)
    fake_tag = {"Key": "faketag", "Value": "true"}
    try:
        alarm1 = create_cloudwatch_alarm(aws_region, fake_tag)
        alarm2 = create_cloudwatch_alarm(aws_region, cloudwatch_tag)
        cloudwatch_scheduler = CloudWatchAlarmScheduler(aws_region)
        list_alarms = cloudwatch_scheduler.filter_alarms(
            scheduler_tag["Key"], scheduler_tag["Value"])
        assert len(list(list_alarms)) == result_count
    finally:
        client.delete_alarms(AlarmNames=[alarm1, alarm2])
def test_start_db_instance(aws_region, db_tag, scheduler_tag, result_count):
    """Verify start rds db scheduler class method."""
    client = boto3.client("rds", region_name=aws_region)
    tag_key = db_tag[0]["Key"]
    tag_value = "".join(db_tag[0]["Values"])
    db = launch_rds_instance(aws_region, tag_key, tag_value)
    db_id = db["DBInstance"]["DBInstanceIdentifier"]

    try:
        client.get_waiter("db_instance_available").wait(
            DBInstanceIdentifier=db_id)
        client.stop_db_instance(DBInstanceIdentifier=db_id)
        waiter_db_instance_stopped(aws_region, db_id)
        rds_scheduler = RdsScheduler(aws_region)
        rds_scheduler.cloudwatch_alarm = CloudWatchAlarmScheduler(aws_region)
        rds_scheduler.start(scheduler_tag)
        if db_tag == scheduler_tag:
            client.get_waiter("db_instance_available").wait(
                DBInstanceIdentifier=db_id)

        db_state = client.describe_db_instances(DBInstanceIdentifier=db_id)
        assert db_state["DBInstances"][0]["DBInstanceStatus"] == result_count
    finally:
        # Clean aws account
        client.delete_db_instance(DBInstanceIdentifier=db_id,
                                  SkipFinalSnapshot=True)
コード例 #3
0
def test_start_asg_scheduler(aws_region, asg_tag, scheduler_tag, result_count):
    """Verify start asg scheduler class method."""
    client = boto3.client("autoscaling", region_name=aws_region)
    launch_conf_name = "lc-test" + str(randint(0, 1000000000))
    asg_name = "asg-test" + str(randint(0, 1000000000))
    tag_key = asg_tag[0]["Key"]
    tag_value = "".join(asg_tag[0]["Values"])
    launch_asg(aws_region, tag_key, tag_value, launch_conf_name, asg_name)

    try:
        client.suspend_processes(AutoScalingGroupName=asg_name)
        asg_scheduler = AutoscalingScheduler(aws_region)
        asg_scheduler.cloudwatch_alarm = CloudWatchAlarmScheduler(aws_region)
        asg_scheduler.start(scheduler_tag)

        suspend_process = client.describe_auto_scaling_groups(
            AutoScalingGroupNames=[asg_name]
        )["AutoScalingGroups"][0]["SuspendedProcesses"]
        assert len([x["ProcessName"] for x in suspend_process]) == result_count
    finally:
        # Clean aws account
        client.delete_auto_scaling_group(AutoScalingGroupName=asg_name,
                                         ForceDelete=True)
        client.delete_launch_configuration(
            LaunchConfigurationName=launch_conf_name)
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_stop_cloudwatch_alarms(aws_region, cloudwatch_tag, scheduler_tag,
                                result_count):
    """Verify cloudwatch stop scheduler class method."""
    client = boto3.client("cloudwatch", region_name=aws_region)
    fake_tag = {"Key": "faketag", "Value": "true"}
    try:
        alarm1 = create_cloudwatch_alarm(aws_region, fake_tag)
        alarm2 = create_cloudwatch_alarm(aws_region, cloudwatch_tag)
        cloudwatch_scheduler = CloudWatchAlarmScheduler(aws_region)
        cloudwatch_scheduler.stop(scheduler_tag["Key"], scheduler_tag["Value"])
        alarm1_status = client.describe_alarms(AlarmNames=[alarm1])
        alarm2_status = client.describe_alarms(AlarmNames=[alarm2])
        assert alarm1_status["MetricAlarms"][0]["ActionsEnabled"] == True
        assert (
            alarm2_status["MetricAlarms"][0]["ActionsEnabled"] == result_count)
    finally:
        client.delete_alarms(AlarmNames=[alarm1, alarm2])
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
def test_terminate_ec2_spot(aws_region, aws_tags, result_count):
    """Verify terminate ec2 spot instance."""
    client = boto3.client("ec2", region_name=aws_region)

    launch_ec2_spot(3, aws_region, "tostop", "true")
    spot_scheduler = SpotScheduler(aws_region)
    spot_scheduler.cloudwatch_alarm = CloudWatchAlarmScheduler(aws_region)
    spot_scheduler.terminate(aws_tags)
    instances = client.describe_instances()["Reservations"][0]["Instances"]
    assert len(instances) == 3
    for instance in instances:
        assert instance["State"] == result_count
def test_asg_instance_stop(aws_region, aws_tags, result_count):
    """Verify autoscaling instances stop function."""
    client = boto3.client("ec2", region_name=aws_region)

    launch_asg(aws_region, "tostop", "true")
    asg_scheduler = AutoscalingScheduler(aws_region)
    asg_scheduler.cloudwatch_alarm = CloudWatchAlarmScheduler(aws_region)
    asg_scheduler.stop(aws_tags)
    asg_instance = client.describe_instances()["Reservations"][0]["Instances"]
    assert len(asg_instance) == 3
    for instance in asg_instance:
        assert instance["State"] == result_count
コード例 #9
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
コード例 #10
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
コード例 #11
0
def test_start_db_cluster(aws_region, db_tag, scheduler_tag, result_count):
    """Verify syaty rds db scheduler class method."""
    client = boto3.client("rds", region_name=aws_region)
    cluster, db = launch_rds_cluster(aws_region, db_tag["key"],
                                     db_tag["value"])
    cluster_id = cluster["DBCluster"]["DBClusterIdentifier"]
    db_id = db["DBInstance"]["DBInstanceIdentifier"]

    try:
        client.get_waiter("db_instance_available").wait(
            DBInstanceIdentifier=db_id)
        waitter_db_cluster_available(aws_region, cluster_id)
        client.stop_db_cluster(DBClusterIdentifier=cluster_id)
        waitter_db_cluster_stopped(aws_region, cluster_id)
        waiter_db_instance_stopped(aws_region, db_id)

        rds_scheduler = RdsScheduler(aws_region)
        rds_scheduler.cloudwatch_alarm = CloudWatchAlarmScheduler(aws_region)
        rds_scheduler.start(scheduler_tag["key"], scheduler_tag["value"])
        if db_tag["key"] == "tostop-rds-test-4" and db_tag["value"] == "true":
            waitter_db_cluster_available(aws_region, cluster_id)
            client.get_waiter("db_instance_available").wait(
                DBInstanceIdentifier=db_id)

        cluster_state = client.describe_db_clusters(
            DBClusterIdentifier=cluster_id)["DBClusters"][0]["Status"]
        assert cluster_state == result_count
    finally:
        # Clean aws account
        if cluster_state == "stopped":
            client.start_db_cluster(DBClusterIdentifier=cluster_id)
            waitter_db_cluster_available(aws_region, cluster_id)
            client.get_waiter("db_instance_available").wait(
                DBInstanceIdentifier=db_id)
        elif cluster_state == "stopping":
            waitter_db_cluster_stopped(aws_region, cluster_id)
            waiter_db_instance_stopped(aws_region, db_id)
            client.start_db_cluster(DBClusterIdentifier=cluster_id)
            waitter_db_cluster_available(aws_region, cluster_id)
            client.get_waiter("db_instance_available").wait(
                DBInstanceIdentifier=db_id)
        client.delete_db_instance(DBInstanceIdentifier=db_id,
                                  SkipFinalSnapshot=True)
        client.delete_db_cluster(DBClusterIdentifier=cluster_id,
                                 SkipFinalSnapshot=True)
コード例 #12
0
def test_stop_db_instance(aws_region, db_tag, scheduler_tag, result_count):
    """Verify stop rds db scheduler class method."""
    client = boto3.client("rds", region_name=aws_region)
    db = launch_rds_instance(aws_region, db_tag["key"], db_tag["value"])
    db_id = db["DBInstance"]["DBInstanceIdentifier"]

    try:
        client.get_waiter("db_instance_available").wait(
            DBInstanceIdentifier=db_id)
        rds_scheduler = RdsScheduler(aws_region)
        rds_scheduler.cloudwatch_alarm = CloudWatchAlarmScheduler(aws_region)
        rds_scheduler.stop(scheduler_tag["key"], scheduler_tag["value"])
        if db_tag["key"] == "tostop-rds-test-1" and db_tag["value"] == "true":
            waiter_db_instance_stopped(aws_region, db_id)

        db_state = client.describe_db_instances(DBInstanceIdentifier=db_id)
        assert db_state["DBInstances"][0]["DBInstanceStatus"] == result_count
    finally:
        # Clean aws account
        client.delete_db_instance(DBInstanceIdentifier=db_id,
                                  SkipFinalSnapshot=True)
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)