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)
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)
    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
        )
        client.stop_db_instance(DBInstanceIdentifier=db_id)
        waiter_db_instance_stopped(aws_region, db_id)
        rds_scheduler = RdsScheduler(aws_region)
        rds_scheduler.start(scheduler_tag["key"], scheduler_tag["value"])
        if db_tag["key"] == "tostop-rds-test-2" and db_tag["value"] == "true":
            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
        )
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.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
        )
Exemple #4
0
def test_list_rds(aws_region, tag_key, tag_value, result_count):
    """Verify list rds instance function."""
    launch_rds_instance(aws_region, "tostop", "true")
    rds_scheduler = RdsScheduler(aws_region)
    taglist = rds_scheduler.list_instances(tag_key, tag_value)
    assert len(list(taglist)) == result_count