| 
									
										
										
										
											2021-09-22 18:05:28 +00:00
										 |  |  | import boto3 | 
					
						
							|  |  |  | import pytest | 
					
						
							| 
									
										
										
										
											2013-07-27 16:24:38 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-18 14:18:57 -01:00
										 |  |  | from moto import mock_autoscaling | 
					
						
							| 
									
										
										
										
											2013-07-27 16:24:38 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-18 14:18:57 -01:00
										 |  |  | from .utils import setup_networking | 
					
						
							| 
									
										
										
										
											2021-01-13 09:02:11 +00:00
										 |  |  | from tests import EXAMPLE_AMI_ID | 
					
						
							| 
									
										
										
										
											2017-12-27 19:17:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-27 16:24:38 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-22 18:05:28 +00:00
										 |  |  | 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"], | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @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] | 
					
						
							| 
									
										
										
										
											2023-06-09 11:16:59 +00:00
										 |  |  |     assert policy["PolicyName"] == "ScaleUp" | 
					
						
							|  |  |  |     assert policy["AdjustmentType"] == "ExactCapacity" | 
					
						
							|  |  |  |     assert policy["AutoScalingGroupName"] == "tester_group" | 
					
						
							|  |  |  |     assert policy["ScalingAdjustment"] == 3 | 
					
						
							|  |  |  |     assert policy["Cooldown"] == 60 | 
					
						
							| 
									
										
										
										
											2021-09-22 18:05:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @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] | 
					
						
							| 
									
										
										
										
											2023-06-09 11:16:59 +00:00
										 |  |  |     assert policy["PolicyName"] == "ScaleUp" | 
					
						
							| 
									
										
										
										
											2021-09-22 18:05:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Defaults | 
					
						
							| 
									
										
										
										
											2023-06-09 11:16:59 +00:00
										 |  |  |     assert policy["Cooldown"] == 300 | 
					
						
							| 
									
										
										
										
											2021-09-22 18:05:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @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, | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-09 11:16:59 +00:00
										 |  |  |     assert len(client.describe_policies()["ScalingPolicies"]) == 1 | 
					
						
							| 
									
										
										
										
											2021-09-22 18:05:28 +00:00
										 |  |  |     policy = client.describe_policies()["ScalingPolicies"][0] | 
					
						
							| 
									
										
										
										
											2023-06-09 11:16:59 +00:00
										 |  |  |     assert policy["ScalingAdjustment"] == 3 | 
					
						
							| 
									
										
										
										
											2021-09-22 18:05:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Now update it by creating another with the same name | 
					
						
							|  |  |  |     client.put_scaling_policy( | 
					
						
							|  |  |  |         PolicyName="ScaleUp", | 
					
						
							|  |  |  |         AdjustmentType="ExactCapacity", | 
					
						
							|  |  |  |         AutoScalingGroupName="tester_group", | 
					
						
							|  |  |  |         ScalingAdjustment=2, | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2023-06-09 11:16:59 +00:00
										 |  |  |     assert len(client.describe_policies()["ScalingPolicies"]) == 1 | 
					
						
							| 
									
										
										
										
											2021-09-22 18:05:28 +00:00
										 |  |  |     policy = client.describe_policies()["ScalingPolicies"][0] | 
					
						
							| 
									
										
										
										
											2023-06-09 11:16:59 +00:00
										 |  |  |     assert policy["ScalingAdjustment"] == 2 | 
					
						
							| 
									
										
										
										
											2021-09-22 18:05:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @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, | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-09 11:16:59 +00:00
										 |  |  |     assert len(client.describe_policies()["ScalingPolicies"]) == 1 | 
					
						
							| 
									
										
										
										
											2021-09-22 18:05:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     client.delete_policy(PolicyName="ScaleUp") | 
					
						
							| 
									
										
										
										
											2023-06-09 11:16:59 +00:00
										 |  |  |     assert len(client.describe_policies()["ScalingPolicies"]) == 0 | 
					
						
							| 
									
										
										
										
											2021-09-22 18:05:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @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() | 
					
						
							| 
									
										
										
										
											2023-06-09 11:16:59 +00:00
										 |  |  |     assert len(instances["AutoScalingInstances"]) == 3 | 
					
						
							| 
									
										
										
										
											2021-09-22 18:05:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @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() | 
					
						
							| 
									
										
										
										
											2023-06-09 11:16:59 +00:00
										 |  |  |     assert len(instances["AutoScalingInstances"]) == 5 | 
					
						
							| 
									
										
										
										
											2021-09-22 18:05:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @pytest.mark.parametrize( | 
					
						
							| 
									
										
										
										
											2022-03-10 13:39:59 -01:00
										 |  |  |     "adjustment,nr_of_instances", [(1, 3), (50, 3), (100, 4), (250, 7)] | 
					
						
							| 
									
										
										
										
											2021-09-22 18:05:28 +00:00
										 |  |  | ) | 
					
						
							|  |  |  | @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
 | 
					
						
							| 
									
										
										
										
											2022-03-10 13:39:59 -01:00
										 |  |  |     If PercentChangeInCapacity returns a value between 0 and 1, | 
					
						
							|  |  |  |     Auto Scaling will round it off to 1."""
 | 
					
						
							| 
									
										
										
										
											2021-09-22 18:05:28 +00:00
										 |  |  |     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() | 
					
						
							| 
									
										
										
										
											2023-06-09 11:16:59 +00:00
										 |  |  |     assert len(instances["AutoScalingInstances"]) == nr_of_instances |