diff --git a/tests/unittest/cloud_governance/aws/tag_cluster/test_tag_cluster_resources.py b/tests/unittest/cloud_governance/aws/tag_cluster/test_tag_cluster_resources.py index 1b35a48f7..a4bed8409 100644 --- a/tests/unittest/cloud_governance/aws/tag_cluster/test_tag_cluster_resources.py +++ b/tests/unittest/cloud_governance/aws/tag_cluster/test_tag_cluster_resources.py @@ -2,33 +2,31 @@ import json import os -from moto import mock_ec2, mock_cloudtrail, mock_iam, mock_s3 +import pytest +from moto import mock_ec2, mock_cloudtrail, mock_iam, mock_s3, mock_elb, mock_elbv2 import boto3 from cloud_governance.policy.policy_operations.aws.tag_cluster.tag_cluster_resouces import TagClusterResources -cluster_prefix=["kubernetes.io/cluster", "sigs.k8s.io/cluster-api-provider-aws/cluster"] +cluster_prefix = ["kubernetes.io/cluster", "sigs.k8s.io/cluster-api-provider-aws/cluster"] cluster_name = '' -# cluster_name = 'ocs-test-jlhpd' -# cluster_name = 'opc464-k7jml' - os.environ['SLEEP_SECONDS'] = '0' -# input tags mandatory_tags = {} -# mandatory_tags = { -# "Name": "test-opc464", -# "Owner": "Eli Battat", -# "Email": "ebattat@redhat.com", -# "Purpose": "test", -# "Date": strftime("%Y/%m/%d %H:%M:%S") -# } -# print(strftime("%Y/%m/%d %H:%M:%S", gmtime())) -tag_cluster_resources = TagClusterResources(cluster_prefix=cluster_prefix, cluster_name=cluster_name, - input_tags=mandatory_tags, region='us-east-2') + +@pytest.fixture(scope="module") +def tag_cluster_resources(): + """Create TagClusterResources under mocks so __init__ (IAM list_users, EC2, etc.) does not hit real AWS.""" + with mock_ec2(), mock_iam(), mock_cloudtrail(), mock_s3(), mock_elb(), mock_elbv2(): + yield TagClusterResources( + cluster_prefix=cluster_prefix, + cluster_name=cluster_name, + input_tags=mandatory_tags, + region='us-east-2', + ) -def test_init_cluster_name(): +def test_init_cluster_name(tag_cluster_resources): """ This method search for full cluster key stamp according to part of cluster name :return: @@ -36,7 +34,7 @@ def test_init_cluster_name(): assert len(tag_cluster_resources._TagClusterResources__init_cluster_name()) >= 0 -def test_cluster_instance(): +def test_cluster_instance(tag_cluster_resources): """ This method return all cluster instances :return: @@ -44,7 +42,7 @@ def test_cluster_instance(): assert len(tag_cluster_resources.cluster_instance()) >= 0 -def test_cluster_volume(): +def test_cluster_volume(tag_cluster_resources): """ This method return all cluster volumes :return: @@ -52,7 +50,7 @@ def test_cluster_volume(): assert len(tag_cluster_resources.cluster_volume()) >= 0 -def test_cluster_ami(): +def test_cluster_ami(tag_cluster_resources): """ This method return all cluster ami :return: @@ -60,7 +58,7 @@ def test_cluster_ami(): assert len(tag_cluster_resources.cluster_ami()) >= 0 -def test_cluster_snapshot(): +def test_cluster_snapshot(tag_cluster_resources): """ This method return all cluster snapshot :return: @@ -68,7 +66,7 @@ def test_cluster_snapshot(): assert len(tag_cluster_resources.cluster_snapshot()) >= 0 -def test_cluster_security_group(): +def test_cluster_security_group(tag_cluster_resources): """ This method return all cluster security_group :return: @@ -76,7 +74,7 @@ def test_cluster_security_group(): print(tag_cluster_resources.cluster_security_group()) -def test_cluster_elastic_ip(): +def test_cluster_elastic_ip(tag_cluster_resources): """ This method return all cluster elastic_ip :return: @@ -84,7 +82,7 @@ def test_cluster_elastic_ip(): assert len(tag_cluster_resources.cluster_elastic_ip()) >= 0 -def test_cluster_network_interface(): +def test_cluster_network_interface(tag_cluster_resources): """ This method return all cluster network_interface :return: @@ -92,7 +90,7 @@ def test_cluster_network_interface(): assert len(tag_cluster_resources.cluster_network_interface()) >= 0 -def test_cluster_load_balancer(): +def test_cluster_load_balancer(tag_cluster_resources): """ This method return all cluster load_balancer :return: @@ -100,7 +98,7 @@ def test_cluster_load_balancer(): assert len(tag_cluster_resources.cluster_load_balancer()) >= 0 -def test_cluster_load_balancer_v2(): +def test_cluster_load_balancer_v2(tag_cluster_resources): """ This method return all cluster load_balancer :return: @@ -108,7 +106,7 @@ def test_cluster_load_balancer_v2(): assert len(tag_cluster_resources.cluster_load_balancer_v2()) >= 0 -def test_cluster_vpc(): +def test_cluster_vpc(tag_cluster_resources): """ This method return all cluster cluster_vpc :return: @@ -116,7 +114,7 @@ def test_cluster_vpc(): assert len(tag_cluster_resources.cluster_vpc()) >= 0 -def test_cluster_subnet(): +def test_cluster_subnet(tag_cluster_resources): """ This method return all cluster cluster_subnet :return: @@ -124,7 +122,7 @@ def test_cluster_subnet(): assert len(tag_cluster_resources.cluster_subnet()) >= 0 -def test_cluster_route_table(): +def test_cluster_route_table(tag_cluster_resources): """ This method return all cluster route_table :return: @@ -132,7 +130,7 @@ def test_cluster_route_table(): assert len(tag_cluster_resources.cluster_route_table()) >= 0 -def test_cluster_internet_gateway(): +def test_cluster_internet_gateway(tag_cluster_resources): """ This method return all cluster internet_gateway :return: @@ -140,7 +138,7 @@ def test_cluster_internet_gateway(): assert len(tag_cluster_resources.cluster_internet_gateway()) >= 0 -def test_cluster_dhcp_option(): +def test_cluster_dhcp_option(tag_cluster_resources): """ This method return all cluster dhcp_option :return: @@ -148,7 +146,7 @@ def test_cluster_dhcp_option(): assert len(tag_cluster_resources.cluster_dhcp_option()) >= 0 -def test_cluster_vpc_endpoint(): +def test_cluster_vpc_endpoint(tag_cluster_resources): """ This method return all cluster vpc_endpoint :return: @@ -156,7 +154,7 @@ def test_cluster_vpc_endpoint(): assert len(tag_cluster_resources.cluster_vpc_endpoint()) >= 0 -def test_cluster_nat_gateway(): +def test_cluster_nat_gateway(tag_cluster_resources): """ This method return all cluster nat_gateway :return: @@ -164,7 +162,7 @@ def test_cluster_nat_gateway(): assert len(tag_cluster_resources.cluster_nat_gateway()) >= 0 -def test_cluster_network_acl(): +def test_cluster_network_acl(tag_cluster_resources): """ This method return all cluster network_acl :return: @@ -172,7 +170,7 @@ def test_cluster_network_acl(): assert len(tag_cluster_resources.cluster_network_acl()) >= 0 -def test_cluster_role(): +def test_cluster_role(tag_cluster_resources): """ This method return all cluster role :return: @@ -180,7 +178,7 @@ def test_cluster_role(): assert len(tag_cluster_resources.cluster_role()) >= 0 -def test_cluster_user(): +def test_cluster_user(tag_cluster_resources): """ This method return all cluster role :return: @@ -188,7 +186,7 @@ def test_cluster_user(): print(tag_cluster_resources.cluster_user()) -def test_cluster_s3_bucket(): +def test_cluster_s3_bucket(tag_cluster_resources): """ This method return all cluster s3_bucket :return: diff --git a/tests/unittest/cloud_governance/aws/tag_non_cluster/test_tag_non_cluster_resources.py b/tests/unittest/cloud_governance/aws/tag_non_cluster/test_tag_non_cluster_resources.py index 7602b13bb..7c75c13b3 100644 --- a/tests/unittest/cloud_governance/aws/tag_non_cluster/test_tag_non_cluster_resources.py +++ b/tests/unittest/cloud_governance/aws/tag_non_cluster/test_tag_non_cluster_resources.py @@ -9,7 +9,7 @@ mandatory_tags = {'test': 'ec2-update'} region_name = 'us-east-2' -tag_resources = TagNonClusterResources(input_tags=mandatory_tags, dry_run='no') +# Do not instantiate at module level: __init__ calls IAM list_users. Each test creates its own under mocks. os.environ['SLEEP_SECONDS'] = '0' diff --git a/tests/unittest/cloud_governance/aws/zombie_cluster/test_zombie_cluster_resources.py b/tests/unittest/cloud_governance/aws/zombie_cluster/test_zombie_cluster_resources.py index bdfd6b155..c824a32e0 100644 --- a/tests/unittest/cloud_governance/aws/zombie_cluster/test_zombie_cluster_resources.py +++ b/tests/unittest/cloud_governance/aws/zombie_cluster/test_zombie_cluster_resources.py @@ -1,16 +1,25 @@ import pytest -# TEST DRY RUN: delete=False +from moto import mock_ec2, mock_elb, mock_elbv2, mock_iam, mock_s3 + from cloud_governance.policy.aws.zombie_cluster_resource import ZombieClusterResources -zombie_cluster_resources = ZombieClusterResources(cluster_prefix=["kubernetes.io/cluster", "sigs.k8s.io/cluster-api-provider-aws/cluster"], delete=False, - region='us-east-2') + +@pytest.fixture(scope="module") +def zombie_cluster_resources(): + """Create ZombieClusterResources under mocks so __init__ does not hit real AWS.""" + with mock_ec2(), mock_elb(), mock_elbv2(), mock_iam(), mock_s3(): + yield ZombieClusterResources( + cluster_prefix=["kubernetes.io/cluster", "sigs.k8s.io/cluster-api-provider-aws/cluster"], + delete=False, + region='us-east-2', + ) -def test_all_clusters(): +def test_all_clusters(zombie_cluster_resources): assert len(zombie_cluster_resources.all_cluster_instance()) >= 0 -def test_cluster_instance(): +def test_cluster_instance(zombie_cluster_resources): """ This method returns all cluster instances, its a private method :return: @@ -18,7 +27,7 @@ def test_cluster_instance(): assert len(zombie_cluster_resources._cluster_instance()) >= 0 -def test_zombie_cluster_volume(): +def test_zombie_cluster_volume(zombie_cluster_resources): """ This method returns all cluster volumes :return: @@ -26,7 +35,7 @@ def test_zombie_cluster_volume(): assert len(zombie_cluster_resources.zombie_cluster_volume()[0]) >= 0 -def test_zombie_cluster_ami(): +def test_zombie_cluster_ami(zombie_cluster_resources): """ This method returns all cluster ami :return: @@ -34,7 +43,7 @@ def test_zombie_cluster_ami(): assert len(zombie_cluster_resources.zombie_cluster_ami()[0]) >= 0 -def test_cluster_snapshot(): +def test_cluster_snapshot(zombie_cluster_resources): """ This method returns all cluster snapshot :return: @@ -42,7 +51,7 @@ def test_cluster_snapshot(): assert len(zombie_cluster_resources.zombie_cluster_snapshot()[0]) >= 0 -def test_zombie_cluster_security_group(): +def test_zombie_cluster_security_group(zombie_cluster_resources): """ This method returns all cluster security_group :return: @@ -50,7 +59,7 @@ def test_zombie_cluster_security_group(): assert len(zombie_cluster_resources.zombie_cluster_security_group()[0]) >= 0 -def test_zombie_cluster_elastic_ip(): +def test_zombie_cluster_elastic_ip(zombie_cluster_resources): """ This method return all cluster elastic_ip :return: @@ -58,7 +67,7 @@ def test_zombie_cluster_elastic_ip(): assert len(zombie_cluster_resources.zombie_cluster_elastic_ip()[0]) >= 0 -def test_zombie_cluster_network_interface(): +def test_zombie_cluster_network_interface(zombie_cluster_resources): """ This method return all cluster network_interface :return: @@ -66,7 +75,7 @@ def test_zombie_cluster_network_interface(): assert len(zombie_cluster_resources.zombie_cluster_network_interface()[0]) >= 0 -def test_zombie_cluster_load_balancer(): +def test_zombie_cluster_load_balancer(zombie_cluster_resources): """ This method return all zombie cluster load_balancer :return: @@ -74,7 +83,7 @@ def test_zombie_cluster_load_balancer(): assert len(zombie_cluster_resources.zombie_cluster_load_balancer()[0]) >= 0 -def test_zombie_cluster_load_balancer_v2(): +def test_zombie_cluster_load_balancer_v2(zombie_cluster_resources): """ This method return all cluster load_balancer :return: @@ -82,7 +91,7 @@ def test_zombie_cluster_load_balancer_v2(): assert len(zombie_cluster_resources.zombie_cluster_load_balancer_v2()[0]) >= 0 -def test_zombie_cluster_cluster_vpc(): +def test_zombie_cluster_cluster_vpc(zombie_cluster_resources): """ This method return all cluster cluster_vpc :return: @@ -90,7 +99,7 @@ def test_zombie_cluster_cluster_vpc(): assert len(zombie_cluster_resources.zombie_cluster_vpc()[0]) >= 0 -def test_zombie_cluster_subnet(): +def test_zombie_cluster_subnet(zombie_cluster_resources): """ This method return all cluster cluster_subnet :return: @@ -98,7 +107,7 @@ def test_zombie_cluster_subnet(): assert len(zombie_cluster_resources.zombie_cluster_subnet()[0]) >= 0 -def test_zombie_cluster_route_table(): +def test_zombie_cluster_route_table(zombie_cluster_resources): """ This method return all cluster route_table :return: @@ -106,7 +115,7 @@ def test_zombie_cluster_route_table(): assert len(zombie_cluster_resources.zombie_cluster_route_table()[0]) >= 0 -def test_zombie_cluster_internet_gateway(): +def test_zombie_cluster_internet_gateway(zombie_cluster_resources): """ This method return all cluster internet_gateway :return: @@ -114,7 +123,7 @@ def test_zombie_cluster_internet_gateway(): assert len(zombie_cluster_resources.zombie_cluster_internet_gateway()[0]) >= 0 -def test_zombie_cluster_dhcp_option(): +def test_zombie_cluster_dhcp_option(zombie_cluster_resources): """ This method return all cluster dhcp_option :return: @@ -122,7 +131,7 @@ def test_zombie_cluster_dhcp_option(): assert len(zombie_cluster_resources.zombie_cluster_dhcp_option()[0]) >= 0 -def test_zombie_cluster_vpc_endpoint(): +def test_zombie_cluster_vpc_endpoint(zombie_cluster_resources): """ This method return all cluster vpc_endpoint :return: @@ -130,7 +139,7 @@ def test_zombie_cluster_vpc_endpoint(): assert len(zombie_cluster_resources.zombie_cluster_vpc_endpoint()[0]) >= 0 -def test_zombie_cluster_nat_gateway(): +def test_zombie_cluster_nat_gateway(zombie_cluster_resources): """ This method return all cluster nat_gateway :return: @@ -138,7 +147,7 @@ def test_zombie_cluster_nat_gateway(): assert len(zombie_cluster_resources.zombie_cluster_nat_gateway()[0]) >= 0 -def test_zombie_cluster_network_acl(): +def test_zombie_cluster_network_acl(zombie_cluster_resources): """ This method return zombie network_acl, cross between vpc id in network acl and existing vpcs :return: @@ -146,7 +155,7 @@ def test_zombie_cluster_network_acl(): assert len(zombie_cluster_resources.zombie_cluster_network_acl()[0]) >= 0 -def test_zombie_cluster_role(): +def test_zombie_cluster_role(zombie_cluster_resources): """ This method return all zombie cluster role, scan cluster in all regions :return: @@ -155,7 +164,7 @@ def test_zombie_cluster_role(): @pytest.mark.skip(reason='Skipping the zombie cluster user') -def test_zombie_cluster_user(): +def test_zombie_cluster_user(zombie_cluster_resources): """ This method return all zombie cluster user, scan cluster in all regions :return: @@ -163,7 +172,7 @@ def test_zombie_cluster_user(): assert len(zombie_cluster_resources.zombie_cluster_user()[0]) >= 0 -def test_zombie_cluster_s3_bucket(): +def test_zombie_cluster_s3_bucket(zombie_cluster_resources): """ This method return all cluster s3 bucket, scan cluster in all regions :return: diff --git a/tests/unittest/cloud_governance/common/clouds/aws/price/test_price.py b/tests/unittest/cloud_governance/common/clouds/aws/price/test_price.py index 33575a8ab..06254feac 100644 --- a/tests/unittest/cloud_governance/common/clouds/aws/price/test_price.py +++ b/tests/unittest/cloud_governance/common/clouds/aws/price/test_price.py @@ -1,31 +1,71 @@ import datetime +import json import os +from unittest.mock import MagicMock, patch from cloud_governance.common.clouds.aws.price.price import AWSPrice -def test_price(): +def _make_price_list_response(usd_price: str = "0.05"): + """Build a PriceList response that AWSPrice.get_price() knows how to parse.""" + return { + "PriceList": [ + json.dumps({ + "terms": { + "OnDemand": { + "offer-id": { + "priceDimensions": { + "dim-id": {"pricePerUnit": {"USD": usd_price}} + } + } + } + } + }) + ] + } + + +@patch("cloud_governance.common.clouds.aws.price.price.get_boto3_client") +def test_price(mock_get_boto3_client): + mock_client = MagicMock() + mock_client.get_products.return_value = _make_price_list_response("0.192") + mock_get_boto3_client.return_value = mock_client + __aws_price = AWSPrice() - ec2_type_cost = __aws_price.get_price(region=__aws_price.get_region_name('us-east-1'), instance='m5.xlarge', os='Linux') + ec2_type_cost = __aws_price.get_price( + region=__aws_price.get_region_name("us-east-1"), + instance="m5.xlarge", + os="Linux", + ) assert ec2_type_cost -def test_ebs_price(): - os.environ['AWS_DEFAULT_REGION'] = 'us-east-2' +@patch("cloud_governance.common.clouds.aws.price.price.get_boto3_client") +def test_ebs_price(mock_get_boto3_client): + os.environ["AWS_DEFAULT_REGION"] = "us-east-2" + mock_client = MagicMock() + mock_client.get_products.return_value = _make_price_list_response("0.10") + mock_get_boto3_client.return_value = mock_client + __aws_price = AWSPrice() - ec2_type_cost = __aws_price.get_ec2_price(resource='ebs', item_data={'VolumeType': 'gp2', 'Size': 10}) + ec2_type_cost = __aws_price.get_ec2_price( + resource="ebs", item_data={"VolumeType": "gp2", "Size": 10} + ) assert ec2_type_cost -def test_ec2_price(): - os.environ['AWS_DEFAULT_REGION'] = 'us-east-2' +@patch("cloud_governance.common.clouds.aws.price.price.get_boto3_client") +def test_ec2_price(mock_get_boto3_client): + os.environ["AWS_DEFAULT_REGION"] = "us-east-2" + mock_client = MagicMock() + mock_client.get_products.return_value = _make_price_list_response("0.0116") + mock_get_boto3_client.return_value = mock_client + __aws_price = AWSPrice() item_data = { - 'InstanceType': 't2.micro', - 'LaunchTime': datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S+00:00"), - 'State': { - 'Name': 'running' - } + "InstanceType": "t2.micro", + "LaunchTime": datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S+00:00"), + "State": {"Name": "running"}, } - ec2_type_cost = __aws_price.get_ec2_price(resource='ec2', item_data=item_data) + ec2_type_cost = __aws_price.get_ec2_price(resource="ec2", item_data=item_data) assert ec2_type_cost >= 0