diff --git a/IMPLEMENTATION_COVERAGE.md b/IMPLEMENTATION_COVERAGE.md index 168c2bc7e..48d4b8209 100644 --- a/IMPLEMENTATION_COVERAGE.md +++ b/IMPLEMENTATION_COVERAGE.md @@ -22,7 +22,7 @@ ## apigateway
-43% implemented +44% implemented - [X] create_api_key - [X] create_authorizer @@ -50,7 +50,7 @@ - [ ] delete_gateway_response - [X] delete_integration - [X] delete_integration_response -- [ ] delete_method +- [X] delete_method - [X] delete_method_response - [ ] delete_model - [ ] delete_request_validator diff --git a/tests/test_autoscaling/test_autoscaling.py b/tests/test_autoscaling/test_autoscaling.py index 8668ce4f4..204fd3d86 100644 --- a/tests/test_autoscaling/test_autoscaling.py +++ b/tests/test_autoscaling/test_autoscaling.py @@ -29,6 +29,7 @@ from .utils import ( from tests import EXAMPLE_AMI_ID +# Has boto3 equivalent @mock_autoscaling_deprecated @mock_elb_deprecated @mock_ec2_deprecated @@ -118,6 +119,111 @@ def test_create_autoscaling_group(): len(instances_attached).should.equal(INSTANCE_COUNT_START + INSTANCE_COUNT_GROUP) +@mock_autoscaling +@mock_ec2 +@mock_elb +def test_create_autoscaling_group_boto3_within_elb(): + mocked_networking = setup_networking() + elb_client = boto3.client("elb", region_name="us-east-1") + elb_client.create_load_balancer( + LoadBalancerName="test_lb", + Listeners=[{"Protocol": "http", "LoadBalancerPort": 80, "InstancePort": 8080}], + AvailabilityZones=[], + ) + + # we attach a couple of machines to the load balancer + # that are not managed by the auto scaling group + INSTANCE_COUNT_START = 3 + INSTANCE_COUNT_GROUP = 2 + ec2 = boto3.resource("ec2", region_name="us-east-1") + instances = ec2.create_instances( + ImageId=EXAMPLE_AMI_ID, + InstanceType="t1.micro", + MaxCount=INSTANCE_COUNT_START, + MinCount=INSTANCE_COUNT_START, + SubnetId=mocked_networking["subnet1"], + ) + + instances_ids = [_.id for _ in instances] + elb_client.register_instances_with_load_balancer( + LoadBalancerName="test_lb", + Instances=[{"InstanceId": id} for id in instances_ids], + ) + + as_client = boto3.client("autoscaling", region_name="us-east-1") + as_client.create_launch_configuration( + LaunchConfigurationName="tester", + ImageId=EXAMPLE_AMI_ID, + InstanceType="t2.medium", + ) + + as_client.create_auto_scaling_group( + AutoScalingGroupName="tester_group", + AvailabilityZones=["us-east-1a", "us-east-1b"], + DefaultCooldown=60, + DesiredCapacity=INSTANCE_COUNT_GROUP, + HealthCheckGracePeriod=100, + HealthCheckType="EC2", + LaunchConfigurationName="tester", + LoadBalancerNames=["test_lb"], + MinSize=INSTANCE_COUNT_GROUP, + MaxSize=INSTANCE_COUNT_GROUP, + PlacementGroup="test_placement", + Tags=[ + { + "ResourceId": "tester_group", + "ResourceType": "group", + "Key": "test_key", + "Value": "test_value", + "PropagateAtLaunch": True, + } + ], + TerminationPolicies=["OldestInstance", "NewestInstance"], + VPCZoneIdentifier="{subnet1},{subnet2}".format( + subnet1=mocked_networking["subnet1"], subnet2=mocked_networking["subnet2"] + ), + ) + + group = as_client.describe_auto_scaling_groups()["AutoScalingGroups"][0] + group["AutoScalingGroupName"].should.equal("tester_group") + set(group["AvailabilityZones"]).should.equal(set(["us-east-1a", "us-east-1b"])) + group["DesiredCapacity"].should.equal(2) + group["MaxSize"].should.equal(INSTANCE_COUNT_GROUP) + group["MinSize"].should.equal(INSTANCE_COUNT_GROUP) + group["Instances"].should.have.length_of(INSTANCE_COUNT_GROUP) + group["VPCZoneIdentifier"].should.equal( + "{subnet1},{subnet2}".format( + subnet1=mocked_networking["subnet1"], subnet2=mocked_networking["subnet2"] + ) + ) + group["LaunchConfigurationName"].should.equal("tester") + group["DefaultCooldown"].should.equal(60) + group["HealthCheckGracePeriod"].should.equal(100) + group["HealthCheckType"].should.equal("EC2") + group["LoadBalancerNames"].should.equal(["test_lb"]) + group["PlacementGroup"].should.equal("test_placement") + list(group["TerminationPolicies"]).should.equal( + ["OldestInstance", "NewestInstance"] + ) + list(group["Tags"]).should.have.length_of(1) + tag = group["Tags"][0] + tag["ResourceId"].should.equal("tester_group") + tag["Key"].should.equal("test_key") + tag["Value"].should.equal("test_value") + tag["PropagateAtLaunch"].should.equal(True) + + instances_attached = elb_client.describe_instance_health( + LoadBalancerName="test_lb" + )["InstanceStates"] + instances_attached.should.have.length_of( + INSTANCE_COUNT_START + INSTANCE_COUNT_GROUP + ) + attached_ids = [i["InstanceId"] for i in instances_attached] + for ec2_instance_id in instances_ids: + attached_ids.should.contain(ec2_instance_id) + + +# Has boto3 equivalent @mock_autoscaling_deprecated def test_create_autoscaling_groups_defaults(): """Test with the minimum inputs and check that all of the proper defaults @@ -158,6 +264,46 @@ def test_create_autoscaling_groups_defaults(): list(group.tags).should.equal([]) +@mock_autoscaling +def test_create_autoscaling_groups_defaults_boto3(): + """Test with the minimum inputs and check that all of the proper defaults + are assigned for the other attributes""" + + mocked_networking = setup_networking() + as_client = boto3.client("autoscaling", region_name="us-east-1") + as_client.create_launch_configuration( + LaunchConfigurationName="tester_config", + ImageId=EXAMPLE_AMI_ID, + InstanceType="t2.medium", + ) + + as_client.create_auto_scaling_group( + AutoScalingGroupName="tester_group", + LaunchConfigurationName="tester_config", + MinSize=2, + MaxSize=2, + VPCZoneIdentifier=mocked_networking["subnet1"], + ) + + group = as_client.describe_auto_scaling_groups()["AutoScalingGroups"][0] + group["AutoScalingGroupName"].should.equal("tester_group") + group["MaxSize"].should.equal(2) + group["MinSize"].should.equal(2) + group["LaunchConfigurationName"].should.equal("tester_config") + + # Defaults + group["AvailabilityZones"].should.equal(["us-east-1a"]) # subnet1 + group["DesiredCapacity"].should.equal(2) + group["VPCZoneIdentifier"].should.equal(mocked_networking["subnet1"]) + group["DefaultCooldown"].should.equal(300) + group["HealthCheckGracePeriod"].should.equal(300) + group["HealthCheckType"].should.equal("EC2") + group["LoadBalancerNames"].should.equal([]) + group.shouldnt.have.key("PlacementGroup") + group["TerminationPolicies"].should.equal([]) + group["Tags"].should.equal([]) + + @mock_autoscaling def test_list_many_autoscaling_groups(): mocked_networking = setup_networking() @@ -226,6 +372,7 @@ def test_propogate_tags(): tags.should.contain({"Value": "TestGroup1", "Key": "aws:autoscaling:groupName"}) +# Has boto3 equivalent @mock_autoscaling_deprecated def test_autoscaling_group_describe_filter(): mocked_networking = setup_networking_deprecated() @@ -254,6 +401,7 @@ def test_autoscaling_group_describe_filter(): conn.get_all_groups().should.have.length_of(3) +# Has boto3 equivalent @mock_autoscaling_deprecated def test_autoscaling_update(): mocked_networking = setup_networking_deprecated() @@ -286,6 +434,7 @@ def test_autoscaling_update(): group.vpc_zone_identifier.should.equal(mocked_networking["subnet2"]) +# Has boto3 equivalent @mock_autoscaling_deprecated def test_autoscaling_tags_update(): mocked_networking = setup_networking_deprecated() @@ -334,6 +483,7 @@ def test_autoscaling_tags_update(): group.tags.should.have.length_of(2) +# Has boto3 equivalent @mock_autoscaling_deprecated def test_autoscaling_group_delete(): mocked_networking = setup_networking_deprecated() @@ -358,6 +508,35 @@ def test_autoscaling_group_delete(): conn.get_all_groups().should.have.length_of(0) +@mock_autoscaling +def test_autoscaling_group_delete_boto3(): + mocked_networking = setup_networking() + as_client = boto3.client("autoscaling", region_name="us-east-1") + as_client.create_launch_configuration( + LaunchConfigurationName="tester_config", + ImageId=EXAMPLE_AMI_ID, + InstanceType="t2.medium", + ) + + as_client.create_auto_scaling_group( + AutoScalingGroupName="tester_group", + LaunchConfigurationName="tester_config", + MinSize=2, + MaxSize=2, + VPCZoneIdentifier=mocked_networking["subnet1"], + ) + + as_client.describe_auto_scaling_groups()["AutoScalingGroups"].should.have.length_of( + 1 + ) + + as_client.delete_auto_scaling_group(AutoScalingGroupName="tester_group") + as_client.describe_auto_scaling_groups()["AutoScalingGroups"].should.have.length_of( + 0 + ) + + +# Has boto3 equivalent @mock_ec2_deprecated @mock_autoscaling_deprecated def test_autoscaling_group_describe_instances(): @@ -392,6 +571,7 @@ def test_autoscaling_group_describe_instances(): instances[0].instance_type.should.equal("t2.medium") +# Has boto3 equivalent @requires_boto_gte("2.8") @mock_autoscaling_deprecated def test_set_desired_capacity_up(): @@ -426,6 +606,7 @@ def test_set_desired_capacity_up(): instances.should.have.length_of(3) +# Has boto3 equivalent @requires_boto_gte("2.8") @mock_autoscaling_deprecated def test_set_desired_capacity_down(): @@ -460,6 +641,7 @@ def test_set_desired_capacity_down(): instances.should.have.length_of(1) +# Has boto3 equivalent @requires_boto_gte("2.8") @mock_autoscaling_deprecated def test_set_desired_capacity_the_same(): @@ -494,6 +676,7 @@ def test_set_desired_capacity_the_same(): instances.should.have.length_of(2) +# Has boto3 equivalent @mock_autoscaling_deprecated @mock_elb_deprecated def test_autoscaling_group_with_elb(): @@ -2916,3 +3099,41 @@ def test_autoscaling_lifecyclehook(): ) len(response["LifecycleHooks"]).should.equal(0) + + +@pytest.mark.parametrize( + "original,new", [(2, 1), (2, 3), (1, 5), (1, 1)], +) +@mock_autoscaling +def test_set_desired_capacity_without_protection_boto3(original, new): + mocked_networking = setup_networking() + client = boto3.client("autoscaling", region_name="us-east-1") + client.create_launch_configuration( + LaunchConfigurationName="test_launch_configuration", + ImageId=EXAMPLE_AMI_ID, + InstanceType="t2.medium", + ) + + client.create_auto_scaling_group( + AutoScalingGroupName="tester_group", + LaunchConfigurationName="test_launch_configuration", + AvailabilityZones=["us-east-1a"], + MinSize=original, + MaxSize=original, + DesiredCapacity=original, + VPCZoneIdentifier=mocked_networking["subnet1"], + ) + + group = client.describe_auto_scaling_groups()["AutoScalingGroups"][0] + group["DesiredCapacity"].should.equal(original) + instances = client.describe_auto_scaling_instances()["AutoScalingInstances"] + instances.should.have.length_of(original) + + client.update_auto_scaling_group( + AutoScalingGroupName="tester_group", DesiredCapacity=new + ) + + group = client.describe_auto_scaling_groups()["AutoScalingGroups"][0] + group["DesiredCapacity"].should.equal(new) + instances = client.describe_auto_scaling_instances()["AutoScalingInstances"] + instances.should.have.length_of(new) diff --git a/tests/test_autoscaling/test_launch_configurations.py b/tests/test_autoscaling/test_launch_configurations.py index f0d0c2efa..8b423c40f 100644 --- a/tests/test_autoscaling/test_launch_configurations.py +++ b/tests/test_autoscaling/test_launch_configurations.py @@ -1,4 +1,7 @@ from __future__ import unicode_literals + +import base64 + import boto import boto3 from boto.ec2.autoscale.launchconfig import LaunchConfiguration @@ -15,6 +18,7 @@ from tests.helpers import requires_boto_gte from tests import EXAMPLE_AMI_ID +# Has boto3 equivalent @mock_autoscaling_deprecated def test_create_launch_configuration(): conn = boto.connect_autoscale() @@ -47,6 +51,42 @@ def test_create_launch_configuration(): launch_config.spot_price.should.equal(0.1) +@mock_autoscaling +def test_create_launch_configuration_boto3(): + client = boto3.client("autoscaling", region_name="us-east-1") + client.create_launch_configuration( + LaunchConfigurationName="tester", + ImageId=EXAMPLE_AMI_ID, + InstanceType="t1.micro", + KeyName="the_keys", + SecurityGroups=["default", "default2"], + UserData="This is some user_data", + InstanceMonitoring={"Enabled": True}, + IamInstanceProfile="arn:aws:iam::{}:instance-profile/testing".format( + ACCOUNT_ID + ), + SpotPrice="0.1", + ) + + launch_config = client.describe_launch_configurations()["LaunchConfigurations"][0] + launch_config["LaunchConfigurationName"].should.equal("tester") + launch_config.should.have.key("LaunchConfigurationARN") + launch_config["ImageId"].should.equal(EXAMPLE_AMI_ID) + launch_config["InstanceType"].should.equal("t1.micro") + launch_config["KeyName"].should.equal("the_keys") + set(launch_config["SecurityGroups"]).should.equal(set(["default", "default2"])) + userdata = launch_config["UserData"] + userdata = base64.b64decode(userdata) + userdata.should.equal(b"This is some user_data") + launch_config["InstanceMonitoring"].should.equal({"Enabled": True}) + launch_config["IamInstanceProfile"].should.equal( + "arn:aws:iam::{}:instance-profile/testing".format(ACCOUNT_ID) + ) + launch_config["SpotPrice"].should.equal("0.1") + launch_config["BlockDeviceMappings"].should.equal([]) + + +# Has boto3 equivalent @requires_boto_gte("2.27.0") @mock_autoscaling_deprecated def test_create_launch_configuration_with_block_device_mappings(): @@ -116,6 +156,67 @@ def test_create_launch_configuration_with_block_device_mappings(): returned_mapping["/dev/xvdb"].ephemeral_name.should.equal("ephemeral0") +@mock_autoscaling +def test_create_launch_configuration_with_block_device_mappings_boto3(): + client = boto3.client("autoscaling", region_name="us-east-1") + client.create_launch_configuration( + LaunchConfigurationName="tester", + ImageId=EXAMPLE_AMI_ID, + InstanceType="t1.micro", + KeyName="the_keys", + SecurityGroups=["default", "default2"], + UserData="This is some user_data", + InstanceMonitoring={"Enabled": True}, + IamInstanceProfile="arn:aws:iam::{}:instance-profile/testing".format( + ACCOUNT_ID + ), + SpotPrice="0.1", + BlockDeviceMappings=[ + {"DeviceName": "/dev/xvdb", "VirtualName": "ephemeral0"}, + { + "DeviceName": "/dev/xvdp", + "Ebs": {"SnapshotId": "snap-1234abcd", "VolumeType": "standard"}, + }, + { + "DeviceName": "/dev/xvdh", + "Ebs": { + "VolumeType": "io1", + "VolumeSize": 100, + "Iops": 1000, + "DeleteOnTermination": False, + }, + }, + ], + ) + + launch_config = client.describe_launch_configurations()["LaunchConfigurations"][0] + launch_config["LaunchConfigurationName"].should.equal("tester") + + mappings = launch_config["BlockDeviceMappings"] + mappings.should.have.length_of(3) + + xvdh = [m for m in mappings if m["DeviceName"] == "/dev/xvdh"][0] + xvdp = [m for m in mappings if m["DeviceName"] == "/dev/xvdp"][0] + xvdb = [m for m in mappings if m["DeviceName"] == "/dev/xvdb"][0] + + xvdh.shouldnt.have.key("VirtualName") + xvdh.should.have.key("Ebs") + xvdh["Ebs"]["VolumeSize"].should.equal(100) + xvdh["Ebs"]["VolumeType"].should.equal("io1") + xvdh["Ebs"]["DeleteOnTermination"].should.equal(False) + xvdh["Ebs"]["Iops"].should.equal(1000) + + xvdp.shouldnt.have.key("VirtualName") + xvdp.should.have.key("Ebs") + xvdp["Ebs"]["SnapshotId"].should.equal("snap-1234abcd") + xvdp["Ebs"]["VolumeType"].should.equal("standard") + xvdp["Ebs"]["DeleteOnTermination"].should.equal(False) + + xvdb["VirtualName"].should.equal("ephemeral0") + xvdb.shouldnt.have.key("Ebs") + + +# Has boto3 equivalent @requires_boto_gte("2.12") @mock_autoscaling_deprecated def test_create_launch_configuration_for_2_12(): @@ -129,6 +230,7 @@ def test_create_launch_configuration_for_2_12(): launch_config.ebs_optimized.should.equal(True) +# Has boto3 equivalent @requires_boto_gte("2.25.0") @mock_autoscaling_deprecated def test_create_launch_configuration_using_ip_association(): @@ -142,6 +244,23 @@ def test_create_launch_configuration_using_ip_association(): launch_config.associate_public_ip_address.should.equal(True) +@mock_autoscaling +def test_create_launch_configuration_additional_parameters(): + client = boto3.client("autoscaling", region_name="us-east-1") + client.create_launch_configuration( + LaunchConfigurationName="tester", + ImageId=EXAMPLE_AMI_ID, + InstanceType="t1.micro", + EbsOptimized=True, + AssociatePublicIpAddress=True, + ) + + launch_config = client.describe_launch_configurations()["LaunchConfigurations"][0] + launch_config["EbsOptimized"].should.equal(True) + launch_config["AssociatePublicIpAddress"].should.equal(True) + + +# Has boto3 equivalent @requires_boto_gte("2.25.0") @mock_autoscaling_deprecated def test_create_launch_configuration_using_ip_association_should_default_to_false(): @@ -153,6 +272,21 @@ def test_create_launch_configuration_using_ip_association_should_default_to_fals launch_config.associate_public_ip_address.should.equal(False) +@mock_autoscaling +def test_create_launch_configuration_additional_params_default_to_false(): + client = boto3.client("autoscaling", region_name="us-east-1") + client.create_launch_configuration( + LaunchConfigurationName="tester", + ImageId=EXAMPLE_AMI_ID, + InstanceType="t1.micro", + ) + + launch_config = client.describe_launch_configurations()["LaunchConfigurations"][0] + launch_config["EbsOptimized"].should.equal(False) + launch_config["AssociatePublicIpAddress"].should.equal(False) + + +# Has boto3 equivalent @mock_autoscaling_deprecated def test_create_launch_configuration_defaults(): """Test with the minimum inputs and check that all of the proper defaults @@ -177,6 +311,29 @@ def test_create_launch_configuration_defaults(): launch_config.spot_price.should.equal(None) +@mock_autoscaling +def test_create_launch_configuration_defaults_boto3(): + """Test with the minimum inputs and check that all of the proper defaults + are assigned for the other attributes""" + client = boto3.client("autoscaling", region_name="us-east-1") + client.create_launch_configuration( + LaunchConfigurationName="tester", + ImageId=EXAMPLE_AMI_ID, + InstanceType="m1.small", + ) + + launch_config = client.describe_launch_configurations()["LaunchConfigurations"][0] + + # Defaults + launch_config["KeyName"].should.equal("") + launch_config["SecurityGroups"].should.equal([]) + launch_config["UserData"].should.equal("") + launch_config["InstanceMonitoring"].should.equal({"Enabled": False}) + launch_config.shouldnt.have.key("IamInstanceProfile") + launch_config.shouldnt.have.key("SpotPrice") + + +# Has boto3 equivalent @requires_boto_gte("2.12") @mock_autoscaling_deprecated def test_create_launch_configuration_defaults_for_2_12(): @@ -188,6 +345,7 @@ def test_create_launch_configuration_defaults_for_2_12(): launch_config.ebs_optimized.should.equal(False) +# Has boto3 equivalent @mock_autoscaling_deprecated def test_launch_configuration_describe_filter(): conn = boto.connect_autoscale() @@ -206,6 +364,25 @@ def test_launch_configuration_describe_filter(): conn.get_all_launch_configurations().should.have.length_of(3) +@mock_autoscaling +def test_launch_configuration_describe_filter_boto3(): + client = boto3.client("autoscaling", region_name="us-east-1") + for name in ["tester", "tester2", "tester3"]: + client.create_launch_configuration( + LaunchConfigurationName=name, + ImageId=EXAMPLE_AMI_ID, + InstanceType="m1.small", + ) + + configs = client.describe_launch_configurations( + LaunchConfigurationNames=["tester", "tester2"] + ) + configs["LaunchConfigurations"].should.have.length_of(2) + client.describe_launch_configurations()[ + "LaunchConfigurations" + ].should.have.length_of(3) + + @mock_autoscaling def test_launch_configuration_describe_paginated(): conn = boto3.client("autoscaling", region_name="us-east-1") @@ -229,6 +406,7 @@ def test_launch_configuration_describe_paginated(): assert "NextToken" not in response2.keys() +# Has boto3 equivalent @mock_autoscaling_deprecated def test_launch_configuration_delete(): conn = boto.connect_autoscale() @@ -243,6 +421,26 @@ def test_launch_configuration_delete(): conn.get_all_launch_configurations().should.have.length_of(0) +@mock_autoscaling +def test_launch_configuration_delete_boto3(): + client = boto3.client("autoscaling", region_name="us-east-1") + client.create_launch_configuration( + LaunchConfigurationName="tester", + ImageId=EXAMPLE_AMI_ID, + InstanceType="m1.small", + ) + + client.describe_launch_configurations()[ + "LaunchConfigurations" + ].should.have.length_of(1) + + client.delete_launch_configuration(LaunchConfigurationName="tester") + + client.describe_launch_configurations()[ + "LaunchConfigurations" + ].should.have.length_of(0) + + @pytest.mark.parametrize( "request_params", [ diff --git a/tests/test_autoscaling/test_policies.py b/tests/test_autoscaling/test_policies.py index a801a3852..5dd0552aa 100644 --- a/tests/test_autoscaling/test_policies.py +++ b/tests/test_autoscaling/test_policies.py @@ -1,13 +1,15 @@ from __future__ import unicode_literals import boto +import boto3 from boto.ec2.autoscale.launchconfig import LaunchConfiguration from boto.ec2.autoscale.group import AutoScalingGroup from boto.ec2.autoscale.policy import ScalingPolicy +import pytest import sure # noqa -from moto import mock_autoscaling_deprecated +from moto import mock_autoscaling_deprecated, mock_autoscaling -from .utils import setup_networking_deprecated +from .utils import setup_networking_deprecated, setup_networking from tests import EXAMPLE_AMI_ID @@ -30,6 +32,25 @@ def setup_autoscale_group(): return group +def setup_autoscale_group_boto3(): + mocked_networking = setup_networking() + client = boto3.client("autoscaling", region_name="us-east-1") + client.create_launch_configuration( + LaunchConfigurationName="tester", + ImageId=EXAMPLE_AMI_ID, + InstanceType="m1.small", + ) + + client.create_auto_scaling_group( + AutoScalingGroupName="tester_group", + LaunchConfigurationName="tester", + MinSize=2, + MaxSize=2, + VPCZoneIdentifier=mocked_networking["subnet1"], + ) + + +# Has boto3 equivalent @mock_autoscaling_deprecated def test_create_policy(): setup_autoscale_group() @@ -51,6 +72,27 @@ def test_create_policy(): policy.cooldown.should.equal(60) +@mock_autoscaling +def test_create_policy_boto3(): + setup_autoscale_group_boto3() + client = boto3.client("autoscaling", region_name="us-east-1") + client.put_scaling_policy( + PolicyName="ScaleUp", + AdjustmentType="ExactCapacity", + AutoScalingGroupName="tester_group", + ScalingAdjustment=3, + Cooldown=60, + ) + + policy = client.describe_policies()["ScalingPolicies"][0] + policy["PolicyName"].should.equal("ScaleUp") + policy["AdjustmentType"].should.equal("ExactCapacity") + policy["AutoScalingGroupName"].should.equal("tester_group") + policy["ScalingAdjustment"].should.equal(3) + policy["Cooldown"].should.equal(60) + + +# Has boto3 equivalent @mock_autoscaling_deprecated def test_create_policy_default_values(): setup_autoscale_group() @@ -70,6 +112,25 @@ def test_create_policy_default_values(): policy.cooldown.should.equal(300) +@mock_autoscaling +def test_create_policy_default_values_boto3(): + setup_autoscale_group_boto3() + client = boto3.client("autoscaling", region_name="us-east-1") + client.put_scaling_policy( + PolicyName="ScaleUp", + AdjustmentType="ExactCapacity", + AutoScalingGroupName="tester_group", + ScalingAdjustment=3, + ) + + policy = client.describe_policies()["ScalingPolicies"][0] + policy["PolicyName"].should.equal("ScaleUp") + + # Defaults + policy["Cooldown"].should.equal(300) + + +# Has boto3 equivalent @mock_autoscaling_deprecated def test_update_policy(): setup_autoscale_group() @@ -97,6 +158,34 @@ def test_update_policy(): policy.scaling_adjustment.should.equal(2) +@mock_autoscaling +def test_update_policy_boto3(): + setup_autoscale_group_boto3() + client = boto3.client("autoscaling", region_name="us-east-1") + client.put_scaling_policy( + PolicyName="ScaleUp", + AdjustmentType="ExactCapacity", + AutoScalingGroupName="tester_group", + ScalingAdjustment=3, + ) + + client.describe_policies()["ScalingPolicies"].should.have.length_of(1) + policy = client.describe_policies()["ScalingPolicies"][0] + policy["ScalingAdjustment"].should.equal(3) + + # Now update it by creating another with the same name + client.put_scaling_policy( + PolicyName="ScaleUp", + AdjustmentType="ExactCapacity", + AutoScalingGroupName="tester_group", + ScalingAdjustment=2, + ) + client.describe_policies()["ScalingPolicies"].should.have.length_of(1) + policy = client.describe_policies()["ScalingPolicies"][0] + policy["ScalingAdjustment"].should.equal(2) + + +# Has boto3 equivalent @mock_autoscaling_deprecated def test_delete_policy(): setup_autoscale_group() @@ -115,6 +204,24 @@ def test_delete_policy(): conn.get_all_policies().should.have.length_of(0) +@mock_autoscaling +def test_delete_policy_boto3(): + setup_autoscale_group_boto3() + client = boto3.client("autoscaling", region_name="us-east-1") + client.put_scaling_policy( + PolicyName="ScaleUp", + AdjustmentType="ExactCapacity", + AutoScalingGroupName="tester_group", + ScalingAdjustment=3, + ) + + client.describe_policies()["ScalingPolicies"].should.have.length_of(1) + + client.delete_policy(PolicyName="ScaleUp") + client.describe_policies()["ScalingPolicies"].should.have.length_of(0) + + +# Has boto3 equivalent @mock_autoscaling_deprecated def test_execute_policy_exact_capacity(): setup_autoscale_group() @@ -133,6 +240,24 @@ def test_execute_policy_exact_capacity(): instances.should.have.length_of(3) +@mock_autoscaling +def test_execute_policy_exact_capacity_boto3(): + setup_autoscale_group_boto3() + client = boto3.client("autoscaling", region_name="us-east-1") + client.put_scaling_policy( + PolicyName="ScaleUp", + AdjustmentType="ExactCapacity", + AutoScalingGroupName="tester_group", + ScalingAdjustment=3, + ) + + client.execute_policy(PolicyName="ScaleUp") + + instances = client.describe_auto_scaling_instances() + instances["AutoScalingInstances"].should.have.length_of(3) + + +# Has boto3 equivalent @mock_autoscaling_deprecated def test_execute_policy_positive_change_in_capacity(): setup_autoscale_group() @@ -151,6 +276,24 @@ def test_execute_policy_positive_change_in_capacity(): instances.should.have.length_of(5) +@mock_autoscaling +def test_execute_policy_positive_change_in_capacity_boto3(): + setup_autoscale_group_boto3() + client = boto3.client("autoscaling", region_name="us-east-1") + client.put_scaling_policy( + PolicyName="ScaleUp", + AdjustmentType="ChangeInCapacity", + AutoScalingGroupName="tester_group", + ScalingAdjustment=3, + ) + + client.execute_policy(PolicyName="ScaleUp") + + instances = client.describe_auto_scaling_instances() + instances["AutoScalingInstances"].should.have.length_of(5) + + +# Has boto3 equivalent @mock_autoscaling_deprecated def test_execute_policy_percent_change_in_capacity(): setup_autoscale_group() @@ -169,6 +312,30 @@ def test_execute_policy_percent_change_in_capacity(): instances.should.have.length_of(3) +@pytest.mark.parametrize( + "adjustment,nr_of_instances", [(1, 3), (50, 3), (100, 4), (250, 7)], +) +@mock_autoscaling +def test_execute_policy_percent_change_in_capacity_boto3(adjustment, nr_of_instances): + """http://docs.aws.amazon.com/AutoScaling/latest/DeveloperGuide/as-scale-based-on-demand.html + If PercentChangeInCapacity returns a value between 0 and 1, + Auto Scaling will round it off to 1.""" + setup_autoscale_group_boto3() + client = boto3.client("autoscaling", region_name="us-east-1") + client.put_scaling_policy( + PolicyName="ScaleUp", + AdjustmentType="PercentChangeInCapacity", + AutoScalingGroupName="tester_group", + ScalingAdjustment=adjustment, + ) + + client.execute_policy(PolicyName="ScaleUp") + + instances = client.describe_auto_scaling_instances() + instances["AutoScalingInstances"].should.have.length_of(nr_of_instances) + + +# Has boto3 equivalent @mock_autoscaling_deprecated def test_execute_policy_small_percent_change_in_capacity(): """http://docs.aws.amazon.com/AutoScaling/latest/DeveloperGuide/as-scale-based-on-demand.html