2024-03-16 23:17:15 +00:00
|
|
|
from datetime import datetime
|
2023-11-30 15:55:51 +00:00
|
|
|
from uuid import uuid4
|
|
|
|
|
2016-01-08 21:56:10 +00:00
|
|
|
import boto3
|
2020-10-06 05:54:49 +00:00
|
|
|
import pytest
|
2022-08-28 14:41:11 +00:00
|
|
|
from botocore.exceptions import ClientError
|
2023-11-30 15:55:51 +00:00
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
from moto import mock_aws
|
2022-08-13 09:49:43 +00:00
|
|
|
from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID
|
2021-01-13 09:02:11 +00:00
|
|
|
from tests import EXAMPLE_AMI_ID
|
2023-11-30 15:55:51 +00:00
|
|
|
|
|
|
|
from .utils import setup_instance_with_networking, setup_networking
|
2022-07-28 14:42:32 +00:00
|
|
|
|
2021-09-22 18:05:28 +00:00
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2020-08-26 14:15:07 +00:00
|
|
|
def test_propogate_tags():
|
2017-12-27 19:17:59 +00:00
|
|
|
mocked_networking = setup_networking()
|
2017-06-09 20:10:00 +00:00
|
|
|
conn = boto3.client("autoscaling", region_name="us-east-1")
|
2021-01-27 19:49:33 +00:00
|
|
|
conn.create_launch_configuration(
|
|
|
|
LaunchConfigurationName="TestLC",
|
|
|
|
ImageId=EXAMPLE_AMI_ID,
|
|
|
|
InstanceType="t2.medium",
|
|
|
|
)
|
2019-10-31 15:44:26 +00:00
|
|
|
|
2017-06-09 20:10:00 +00:00
|
|
|
conn.create_auto_scaling_group(
|
|
|
|
AutoScalingGroupName="TestGroup1",
|
|
|
|
MinSize=1,
|
|
|
|
MaxSize=2,
|
|
|
|
LaunchConfigurationName="TestLC",
|
|
|
|
Tags=[
|
|
|
|
{
|
|
|
|
"ResourceId": "TestGroup1",
|
|
|
|
"ResourceType": "auto-scaling-group",
|
|
|
|
"PropagateAtLaunch": True,
|
|
|
|
"Key": "TestTagKey1",
|
|
|
|
"Value": "TestTagValue1",
|
2017-12-27 19:17:59 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
VPCZoneIdentifier=mocked_networking["subnet1"],
|
|
|
|
)
|
2019-10-31 15:44:26 +00:00
|
|
|
|
2017-06-09 20:10:00 +00:00
|
|
|
ec2 = boto3.client("ec2", region_name="us-east-1")
|
|
|
|
instances = ec2.describe_instances()
|
|
|
|
|
|
|
|
tags = instances["Reservations"][0]["Instances"][0]["Tags"]
|
2023-06-09 11:16:59 +00:00
|
|
|
assert {"Value": "TestTagValue1", "Key": "TestTagKey1"} in tags
|
|
|
|
assert {"Value": "TestGroup1", "Key": "aws:autoscaling:groupName"} in tags
|
2017-05-11 01:58:42 +00:00
|
|
|
|
2017-06-21 16:58:01 +00:00
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2019-07-17 18:15:59 +00:00
|
|
|
def test_create_autoscaling_group_from_instance():
|
|
|
|
autoscaling_group_name = "test_asg"
|
2021-01-13 09:02:11 +00:00
|
|
|
image_id = EXAMPLE_AMI_ID
|
2019-07-17 18:15:59 +00:00
|
|
|
instance_type = "t2.micro"
|
|
|
|
|
|
|
|
mocked_instance_with_networking = setup_instance_with_networking(
|
|
|
|
image_id, instance_type
|
|
|
|
)
|
|
|
|
client = boto3.client("autoscaling", region_name="us-east-1")
|
|
|
|
response = client.create_auto_scaling_group(
|
|
|
|
AutoScalingGroupName=autoscaling_group_name,
|
2023-02-05 20:26:25 +00:00
|
|
|
InstanceId=mocked_instance_with_networking["instances"][0].id,
|
2019-07-17 18:15:59 +00:00
|
|
|
MinSize=1,
|
|
|
|
MaxSize=3,
|
|
|
|
DesiredCapacity=2,
|
|
|
|
VPCZoneIdentifier=mocked_instance_with_networking["subnet1"],
|
|
|
|
NewInstancesProtectedFromScaleIn=False,
|
|
|
|
)
|
2023-06-09 11:16:59 +00:00
|
|
|
assert response["ResponseMetadata"]["HTTPStatusCode"] == 200
|
2019-07-17 18:15:59 +00:00
|
|
|
|
|
|
|
describe_launch_configurations_response = client.describe_launch_configurations()
|
2023-06-09 11:16:59 +00:00
|
|
|
assert len(describe_launch_configurations_response["LaunchConfigurations"]) == 1
|
|
|
|
config = describe_launch_configurations_response["LaunchConfigurations"][0]
|
|
|
|
assert config["LaunchConfigurationName"] == "test_asg"
|
|
|
|
assert config["ImageId"] == image_id
|
|
|
|
assert config["InstanceType"] == instance_type
|
2019-07-17 18:15:59 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2023-02-05 20:26:25 +00:00
|
|
|
def test_create_autoscaling_group_from_instance_with_security_groups():
|
|
|
|
autoscaling_group_name = "test_asg"
|
|
|
|
image_id = EXAMPLE_AMI_ID
|
|
|
|
instance_type = "t2.micro"
|
|
|
|
|
|
|
|
mocked_instance_with_networking = setup_instance_with_networking(
|
|
|
|
image_id, instance_type
|
|
|
|
)
|
|
|
|
instance = mocked_instance_with_networking["instances"][0]
|
|
|
|
|
|
|
|
# create sg
|
|
|
|
ec2 = boto3.resource("ec2", region_name="us-east-1")
|
|
|
|
sg_id = ec2.create_security_group(GroupName=str(uuid4()), Description="d").id
|
|
|
|
instance.modify_attribute(Groups=[sg_id])
|
|
|
|
|
|
|
|
client = boto3.client("autoscaling", region_name="us-east-1")
|
|
|
|
response = client.create_auto_scaling_group(
|
|
|
|
AutoScalingGroupName=autoscaling_group_name,
|
|
|
|
InstanceId=instance.id,
|
|
|
|
MinSize=1,
|
|
|
|
MaxSize=3,
|
|
|
|
DesiredCapacity=2,
|
|
|
|
VPCZoneIdentifier=mocked_instance_with_networking["subnet1"],
|
|
|
|
NewInstancesProtectedFromScaleIn=False,
|
|
|
|
)
|
|
|
|
# Just verifying this works - used to throw an error when supplying a instance that belonged to an SG
|
2023-06-09 11:16:59 +00:00
|
|
|
assert response["ResponseMetadata"]["HTTPStatusCode"] == 200
|
2023-02-05 20:26:25 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2019-07-17 18:15:59 +00:00
|
|
|
def test_create_autoscaling_group_from_invalid_instance_id():
|
|
|
|
invalid_instance_id = "invalid_instance"
|
|
|
|
|
|
|
|
mocked_networking = setup_networking()
|
|
|
|
client = boto3.client("autoscaling", region_name="us-east-1")
|
2020-10-06 05:54:49 +00:00
|
|
|
with pytest.raises(ClientError) as ex:
|
2019-07-17 18:15:59 +00:00
|
|
|
client.create_auto_scaling_group(
|
|
|
|
AutoScalingGroupName="test_asg",
|
|
|
|
InstanceId=invalid_instance_id,
|
|
|
|
MinSize=9,
|
|
|
|
MaxSize=15,
|
|
|
|
DesiredCapacity=12,
|
|
|
|
VPCZoneIdentifier=mocked_networking["subnet1"],
|
|
|
|
NewInstancesProtectedFromScaleIn=False,
|
|
|
|
)
|
2023-06-09 11:16:59 +00:00
|
|
|
err = ex.value.response["Error"]
|
|
|
|
assert ex.value.response["ResponseMetadata"]["HTTPStatusCode"] == 400
|
|
|
|
assert err["Code"] == "ValidationError"
|
|
|
|
assert err["Message"] == f"Instance [{invalid_instance_id}] is invalid."
|
2019-07-17 18:15:59 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2020-08-26 14:15:07 +00:00
|
|
|
def test_create_autoscaling_group_from_template():
|
2017-12-27 19:17:59 +00:00
|
|
|
mocked_networking = setup_networking()
|
2020-08-26 14:15:07 +00:00
|
|
|
|
|
|
|
ec2_client = boto3.client("ec2", region_name="us-east-1")
|
|
|
|
template = ec2_client.create_launch_template(
|
|
|
|
LaunchTemplateName="test_launch_template",
|
2021-01-13 09:02:11 +00:00
|
|
|
LaunchTemplateData={"ImageId": EXAMPLE_AMI_ID, "InstanceType": "t2.micro"},
|
2020-08-26 14:15:07 +00:00
|
|
|
)["LaunchTemplate"]
|
2017-02-24 02:37:43 +00:00
|
|
|
client = boto3.client("autoscaling", region_name="us-east-1")
|
2020-08-26 14:15:07 +00:00
|
|
|
response = client.create_auto_scaling_group(
|
|
|
|
AutoScalingGroupName="test_asg",
|
|
|
|
LaunchTemplate={
|
|
|
|
"LaunchTemplateId": template["LaunchTemplateId"],
|
|
|
|
"Version": str(template["LatestVersionNumber"]),
|
|
|
|
},
|
|
|
|
MinSize=1,
|
|
|
|
MaxSize=3,
|
|
|
|
DesiredCapacity=2,
|
|
|
|
VPCZoneIdentifier=mocked_networking["subnet1"],
|
|
|
|
NewInstancesProtectedFromScaleIn=False,
|
|
|
|
)
|
2023-06-09 11:16:59 +00:00
|
|
|
assert response["ResponseMetadata"]["HTTPStatusCode"] == 200
|
2020-08-26 14:15:07 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2022-03-29 21:46:06 +00:00
|
|
|
def test_create_auto_scaling_from_template_version__latest():
|
|
|
|
ec2_client = boto3.client("ec2", region_name="us-west-1")
|
|
|
|
launch_template_name = "tester"
|
|
|
|
ec2_client.create_launch_template(
|
|
|
|
LaunchTemplateName=launch_template_name,
|
|
|
|
LaunchTemplateData={"ImageId": EXAMPLE_AMI_ID, "InstanceType": "t2.medium"},
|
|
|
|
)
|
|
|
|
asg_client = boto3.client("autoscaling", region_name="us-west-1")
|
|
|
|
asg_client.create_auto_scaling_group(
|
|
|
|
AutoScalingGroupName="name",
|
|
|
|
DesiredCapacity=1,
|
|
|
|
MinSize=1,
|
|
|
|
MaxSize=1,
|
|
|
|
LaunchTemplate={
|
|
|
|
"LaunchTemplateName": launch_template_name,
|
|
|
|
"Version": "$Latest",
|
|
|
|
},
|
2022-10-02 13:03:03 +00:00
|
|
|
AvailabilityZones=["us-west-1a"],
|
2022-03-29 21:46:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
response = asg_client.describe_auto_scaling_groups(AutoScalingGroupNames=["name"])[
|
|
|
|
"AutoScalingGroups"
|
|
|
|
][0]
|
2023-06-09 11:16:59 +00:00
|
|
|
assert "LaunchTemplate" in response
|
|
|
|
assert response["LaunchTemplate"]["LaunchTemplateName"] == launch_template_name
|
|
|
|
assert response["LaunchTemplate"]["Version"] == "$Latest"
|
2022-03-29 21:46:06 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2022-03-29 21:46:06 +00:00
|
|
|
def test_create_auto_scaling_from_template_version__default():
|
|
|
|
ec2_client = boto3.client("ec2", region_name="us-west-1")
|
|
|
|
launch_template_name = "tester"
|
|
|
|
ec2_client.create_launch_template(
|
|
|
|
LaunchTemplateName=launch_template_name,
|
|
|
|
LaunchTemplateData={"ImageId": EXAMPLE_AMI_ID, "InstanceType": "t2.medium"},
|
|
|
|
)
|
|
|
|
ec2_client.create_launch_template_version(
|
|
|
|
LaunchTemplateName=launch_template_name,
|
|
|
|
LaunchTemplateData={"ImageId": EXAMPLE_AMI_ID, "InstanceType": "t3.medium"},
|
|
|
|
VersionDescription="v2",
|
|
|
|
)
|
|
|
|
asg_client = boto3.client("autoscaling", region_name="us-west-1")
|
|
|
|
asg_client.create_auto_scaling_group(
|
|
|
|
AutoScalingGroupName="name",
|
|
|
|
DesiredCapacity=1,
|
|
|
|
MinSize=1,
|
|
|
|
MaxSize=1,
|
|
|
|
LaunchTemplate={
|
|
|
|
"LaunchTemplateName": launch_template_name,
|
|
|
|
"Version": "$Default",
|
|
|
|
},
|
2022-10-02 13:03:03 +00:00
|
|
|
AvailabilityZones=["us-west-1a"],
|
2022-03-29 21:46:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
response = asg_client.describe_auto_scaling_groups(AutoScalingGroupNames=["name"])[
|
|
|
|
"AutoScalingGroups"
|
|
|
|
][0]
|
2023-06-09 11:16:59 +00:00
|
|
|
assert "LaunchTemplate" in response
|
|
|
|
assert response["LaunchTemplate"]["LaunchTemplateName"] == launch_template_name
|
|
|
|
assert response["LaunchTemplate"]["Version"] == "$Default"
|
2022-03-29 21:46:06 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2022-08-23 16:13:54 +00:00
|
|
|
def test_create_auto_scaling_from_template_version__no_version():
|
|
|
|
ec2_client = boto3.client("ec2", region_name="us-west-1")
|
|
|
|
launch_template_name = "tester"
|
|
|
|
ec2_client.create_launch_template(
|
|
|
|
LaunchTemplateName=launch_template_name,
|
|
|
|
LaunchTemplateData={"ImageId": EXAMPLE_AMI_ID, "InstanceType": "t2.medium"},
|
|
|
|
)
|
|
|
|
asg_client = boto3.client("autoscaling", region_name="us-west-1")
|
|
|
|
asg_client.create_auto_scaling_group(
|
|
|
|
AutoScalingGroupName="name",
|
|
|
|
DesiredCapacity=1,
|
|
|
|
MinSize=1,
|
|
|
|
MaxSize=1,
|
|
|
|
LaunchTemplate={"LaunchTemplateName": launch_template_name},
|
2022-10-02 13:03:03 +00:00
|
|
|
AvailabilityZones=["us-west-1a"],
|
2022-08-23 16:13:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
response = asg_client.describe_auto_scaling_groups(AutoScalingGroupNames=["name"])[
|
|
|
|
"AutoScalingGroups"
|
|
|
|
][0]
|
2023-06-09 11:16:59 +00:00
|
|
|
assert "LaunchTemplate" in response
|
2023-01-29 23:47:50 +00:00
|
|
|
# We never specified the version - and AWS will not return anything if we don't
|
2023-06-09 11:16:59 +00:00
|
|
|
assert "Version" not in response["LaunchTemplate"]
|
2022-08-23 16:13:54 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2020-08-26 14:15:07 +00:00
|
|
|
def test_create_autoscaling_group_no_template_ref():
|
|
|
|
mocked_networking = setup_networking()
|
|
|
|
|
|
|
|
ec2_client = boto3.client("ec2", region_name="us-east-1")
|
|
|
|
template = ec2_client.create_launch_template(
|
|
|
|
LaunchTemplateName="test_launch_template",
|
2021-01-13 09:02:11 +00:00
|
|
|
LaunchTemplateData={"ImageId": EXAMPLE_AMI_ID, "InstanceType": "t2.micro"},
|
2020-08-26 14:15:07 +00:00
|
|
|
)["LaunchTemplate"]
|
|
|
|
client = boto3.client("autoscaling", region_name="us-east-1")
|
|
|
|
|
2020-10-06 06:04:09 +00:00
|
|
|
with pytest.raises(ClientError) as ex:
|
2020-08-26 14:15:07 +00:00
|
|
|
client.create_auto_scaling_group(
|
|
|
|
AutoScalingGroupName="test_asg",
|
|
|
|
LaunchTemplate={"Version": str(template["LatestVersionNumber"])},
|
|
|
|
MinSize=0,
|
|
|
|
MaxSize=20,
|
|
|
|
DesiredCapacity=5,
|
|
|
|
VPCZoneIdentifier=mocked_networking["subnet1"],
|
|
|
|
NewInstancesProtectedFromScaleIn=False,
|
|
|
|
)
|
2023-06-09 11:16:59 +00:00
|
|
|
err = ex.value.response["Error"]
|
|
|
|
assert ex.value.response["ResponseMetadata"]["HTTPStatusCode"] == 400
|
|
|
|
assert err["Code"] == "ValidationError"
|
|
|
|
assert (
|
|
|
|
err["Message"]
|
|
|
|
== "Valid requests must contain either launchTemplateId or LaunchTemplateName"
|
2020-08-26 14:15:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2020-08-26 14:15:07 +00:00
|
|
|
def test_create_autoscaling_group_multiple_template_ref():
|
|
|
|
mocked_networking = setup_networking()
|
|
|
|
|
|
|
|
ec2_client = boto3.client("ec2", region_name="us-east-1")
|
|
|
|
template = ec2_client.create_launch_template(
|
|
|
|
LaunchTemplateName="test_launch_template",
|
2021-01-13 09:02:11 +00:00
|
|
|
LaunchTemplateData={"ImageId": EXAMPLE_AMI_ID, "InstanceType": "t2.micro"},
|
2020-08-26 14:15:07 +00:00
|
|
|
)["LaunchTemplate"]
|
|
|
|
client = boto3.client("autoscaling", region_name="us-east-1")
|
|
|
|
|
2020-10-06 06:04:09 +00:00
|
|
|
with pytest.raises(ClientError) as ex:
|
2020-08-26 14:15:07 +00:00
|
|
|
client.create_auto_scaling_group(
|
|
|
|
AutoScalingGroupName="test_asg",
|
|
|
|
LaunchTemplate={
|
|
|
|
"LaunchTemplateId": template["LaunchTemplateId"],
|
|
|
|
"LaunchTemplateName": template["LaunchTemplateName"],
|
|
|
|
"Version": str(template["LatestVersionNumber"]),
|
|
|
|
},
|
|
|
|
MinSize=0,
|
|
|
|
MaxSize=20,
|
|
|
|
DesiredCapacity=5,
|
|
|
|
VPCZoneIdentifier=mocked_networking["subnet1"],
|
|
|
|
NewInstancesProtectedFromScaleIn=False,
|
|
|
|
)
|
2023-06-09 11:16:59 +00:00
|
|
|
err = ex.value.response["Error"]
|
|
|
|
assert ex.value.response["ResponseMetadata"]["HTTPStatusCode"] == 400
|
|
|
|
assert err["Code"] == "ValidationError"
|
|
|
|
assert (
|
|
|
|
err["Message"]
|
|
|
|
== "Valid requests must contain either launchTemplateId or LaunchTemplateName"
|
2020-08-26 14:15:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2022-03-29 21:46:06 +00:00
|
|
|
def test_create_autoscaling_group_no_launch_configuration():
|
2020-08-26 14:15:07 +00:00
|
|
|
mocked_networking = setup_networking()
|
|
|
|
client = boto3.client("autoscaling", region_name="us-east-1")
|
2020-10-06 06:04:09 +00:00
|
|
|
with pytest.raises(ClientError) as ex:
|
2020-08-26 14:15:07 +00:00
|
|
|
client.create_auto_scaling_group(
|
|
|
|
AutoScalingGroupName="test_asg",
|
|
|
|
MinSize=0,
|
|
|
|
MaxSize=20,
|
|
|
|
DesiredCapacity=5,
|
|
|
|
VPCZoneIdentifier=mocked_networking["subnet1"],
|
|
|
|
NewInstancesProtectedFromScaleIn=False,
|
|
|
|
)
|
2023-06-09 11:16:59 +00:00
|
|
|
err = ex.value.response["Error"]
|
|
|
|
assert ex.value.response["ResponseMetadata"]["HTTPStatusCode"] == 400
|
|
|
|
assert err["Code"] == "ValidationError"
|
|
|
|
assert (
|
|
|
|
err["Message"]
|
|
|
|
== "Valid requests must contain either LaunchTemplate, LaunchConfigurationName, InstanceId or MixedInstancesPolicy parameter."
|
2020-08-26 14:15:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2022-03-29 21:46:06 +00:00
|
|
|
def test_create_autoscaling_group_multiple_launch_configurations():
|
2020-08-26 14:15:07 +00:00
|
|
|
mocked_networking = setup_networking()
|
|
|
|
|
|
|
|
ec2_client = boto3.client("ec2", region_name="us-east-1")
|
|
|
|
template = ec2_client.create_launch_template(
|
|
|
|
LaunchTemplateName="test_launch_template",
|
2021-01-13 09:02:11 +00:00
|
|
|
LaunchTemplateData={"ImageId": EXAMPLE_AMI_ID, "InstanceType": "t2.micro"},
|
2020-08-26 14:15:07 +00:00
|
|
|
)["LaunchTemplate"]
|
|
|
|
client = boto3.client("autoscaling", region_name="us-east-1")
|
|
|
|
client.create_launch_configuration(
|
2021-01-27 19:49:33 +00:00
|
|
|
LaunchConfigurationName="test_launch_configuration",
|
|
|
|
ImageId=EXAMPLE_AMI_ID,
|
|
|
|
InstanceType="t2.medium",
|
2017-02-24 02:37:43 +00:00
|
|
|
)
|
2020-08-26 14:15:07 +00:00
|
|
|
|
2020-10-06 06:04:09 +00:00
|
|
|
with pytest.raises(ClientError) as ex:
|
2020-08-26 14:15:07 +00:00
|
|
|
client.create_auto_scaling_group(
|
|
|
|
AutoScalingGroupName="test_asg",
|
|
|
|
LaunchConfigurationName="test_launch_configuration",
|
|
|
|
LaunchTemplate={
|
|
|
|
"LaunchTemplateId": template["LaunchTemplateId"],
|
|
|
|
"Version": str(template["LatestVersionNumber"]),
|
|
|
|
},
|
|
|
|
MinSize=0,
|
|
|
|
MaxSize=20,
|
|
|
|
DesiredCapacity=5,
|
|
|
|
VPCZoneIdentifier=mocked_networking["subnet1"],
|
|
|
|
NewInstancesProtectedFromScaleIn=False,
|
|
|
|
)
|
2023-06-09 11:16:59 +00:00
|
|
|
err = ex.value.response["Error"]
|
|
|
|
assert ex.value.response["ResponseMetadata"]["HTTPStatusCode"] == 400
|
|
|
|
assert err["Code"] == "ValidationError"
|
|
|
|
assert (
|
|
|
|
err["Message"]
|
|
|
|
== "Valid requests must contain either LaunchTemplate, LaunchConfigurationName, InstanceId or MixedInstancesPolicy parameter."
|
2020-08-26 14:15:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2022-03-29 21:46:06 +00:00
|
|
|
def test_describe_autoscaling_groups_launch_template():
|
2018-11-24 10:32:39 +00:00
|
|
|
mocked_networking = setup_networking()
|
2020-08-26 14:15:07 +00:00
|
|
|
ec2_client = boto3.client("ec2", region_name="us-east-1")
|
|
|
|
template = ec2_client.create_launch_template(
|
|
|
|
LaunchTemplateName="test_launch_template",
|
2021-01-13 09:02:11 +00:00
|
|
|
LaunchTemplateData={"ImageId": EXAMPLE_AMI_ID, "InstanceType": "t2.micro"},
|
2020-08-26 14:15:07 +00:00
|
|
|
)["LaunchTemplate"]
|
2018-11-24 10:32:39 +00:00
|
|
|
client = boto3.client("autoscaling", region_name="us-east-1")
|
2020-08-26 14:15:07 +00:00
|
|
|
client.create_auto_scaling_group(
|
|
|
|
AutoScalingGroupName="test_asg",
|
|
|
|
LaunchTemplate={"LaunchTemplateName": "test_launch_template", "Version": "1"},
|
|
|
|
MinSize=0,
|
|
|
|
MaxSize=20,
|
|
|
|
DesiredCapacity=5,
|
|
|
|
VPCZoneIdentifier=mocked_networking["subnet1"],
|
|
|
|
NewInstancesProtectedFromScaleIn=True,
|
2018-11-24 10:32:39 +00:00
|
|
|
)
|
2020-08-26 14:15:07 +00:00
|
|
|
expected_launch_template = {
|
|
|
|
"LaunchTemplateId": template["LaunchTemplateId"],
|
|
|
|
"LaunchTemplateName": "test_launch_template",
|
|
|
|
"Version": "1",
|
|
|
|
}
|
|
|
|
|
|
|
|
response = client.describe_auto_scaling_groups(AutoScalingGroupNames=["test_asg"])
|
2023-06-09 11:16:59 +00:00
|
|
|
assert response["ResponseMetadata"]["HTTPStatusCode"] == 200
|
2020-08-26 14:15:07 +00:00
|
|
|
group = response["AutoScalingGroups"][0]
|
2023-06-09 11:16:59 +00:00
|
|
|
assert group["AutoScalingGroupName"] == "test_asg"
|
|
|
|
assert group["LaunchTemplate"] == expected_launch_template
|
|
|
|
assert "LaunchConfigurationName" not in group
|
|
|
|
assert group["AvailabilityZones"] == ["us-east-1a"]
|
|
|
|
assert group["VPCZoneIdentifier"] == mocked_networking["subnet1"]
|
|
|
|
assert group["NewInstancesProtectedFromScaleIn"] is True
|
2020-08-26 14:15:07 +00:00
|
|
|
for instance in group["Instances"]:
|
2023-06-09 11:16:59 +00:00
|
|
|
assert instance["LaunchTemplate"] == expected_launch_template
|
|
|
|
assert "LaunchConfigurationName" not in instance
|
|
|
|
assert instance["AvailabilityZone"] == "us-east-1a"
|
|
|
|
assert instance["ProtectedFromScaleIn"] is True
|
|
|
|
assert instance["InstanceType"] == "t2.micro"
|
2020-08-26 14:15:07 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2022-03-29 21:46:06 +00:00
|
|
|
def test_describe_autoscaling_instances_launch_config():
|
2020-08-26 14:15:07 +00:00
|
|
|
mocked_networking = setup_networking()
|
|
|
|
client = boto3.client("autoscaling", region_name="us-east-1")
|
|
|
|
client.create_launch_configuration(
|
2021-01-27 19:49:33 +00:00
|
|
|
LaunchConfigurationName="test_launch_configuration",
|
|
|
|
InstanceType="t2.micro",
|
|
|
|
ImageId=EXAMPLE_AMI_ID,
|
2020-08-26 14:15:07 +00:00
|
|
|
)
|
|
|
|
client.create_auto_scaling_group(
|
2018-11-24 10:32:39 +00:00
|
|
|
AutoScalingGroupName="test_asg",
|
|
|
|
LaunchConfigurationName="test_launch_configuration",
|
|
|
|
MinSize=0,
|
|
|
|
MaxSize=20,
|
|
|
|
DesiredCapacity=5,
|
|
|
|
VPCZoneIdentifier=mocked_networking["subnet1"],
|
|
|
|
NewInstancesProtectedFromScaleIn=True,
|
|
|
|
)
|
|
|
|
|
2020-04-12 03:44:16 +00:00
|
|
|
response = client.describe_auto_scaling_instances()
|
2023-06-09 11:16:59 +00:00
|
|
|
assert len(response["AutoScalingInstances"]) == 5
|
2020-04-12 03:44:16 +00:00
|
|
|
for instance in response["AutoScalingInstances"]:
|
2023-06-09 11:16:59 +00:00
|
|
|
assert instance["LaunchConfigurationName"] == "test_launch_configuration"
|
|
|
|
assert "LaunchTemplate" not in instance
|
|
|
|
assert instance["AutoScalingGroupName"] == "test_asg"
|
|
|
|
assert instance["AvailabilityZone"] == "us-east-1a"
|
|
|
|
assert instance["ProtectedFromScaleIn"] is True
|
|
|
|
assert instance["InstanceType"] == "t2.micro"
|
2020-08-26 14:15:07 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2022-03-29 21:46:06 +00:00
|
|
|
def test_describe_autoscaling_instances_launch_template():
|
2020-08-26 14:15:07 +00:00
|
|
|
mocked_networking = setup_networking()
|
|
|
|
ec2_client = boto3.client("ec2", region_name="us-east-1")
|
|
|
|
template = ec2_client.create_launch_template(
|
|
|
|
LaunchTemplateName="test_launch_template",
|
2021-01-13 09:02:11 +00:00
|
|
|
LaunchTemplateData={"ImageId": EXAMPLE_AMI_ID, "InstanceType": "t2.micro"},
|
2020-08-26 14:15:07 +00:00
|
|
|
)["LaunchTemplate"]
|
|
|
|
client = boto3.client("autoscaling", region_name="us-east-1")
|
|
|
|
client.create_auto_scaling_group(
|
|
|
|
AutoScalingGroupName="test_asg",
|
|
|
|
LaunchTemplate={"LaunchTemplateName": "test_launch_template", "Version": "1"},
|
|
|
|
MinSize=0,
|
|
|
|
MaxSize=20,
|
|
|
|
DesiredCapacity=5,
|
|
|
|
VPCZoneIdentifier=mocked_networking["subnet1"],
|
|
|
|
NewInstancesProtectedFromScaleIn=True,
|
|
|
|
)
|
|
|
|
expected_launch_template = {
|
|
|
|
"LaunchTemplateId": template["LaunchTemplateId"],
|
|
|
|
"LaunchTemplateName": "test_launch_template",
|
|
|
|
"Version": "1",
|
|
|
|
}
|
|
|
|
|
|
|
|
response = client.describe_auto_scaling_instances()
|
2023-06-09 11:16:59 +00:00
|
|
|
assert len(response["AutoScalingInstances"]) == 5
|
2020-08-26 14:15:07 +00:00
|
|
|
for instance in response["AutoScalingInstances"]:
|
2023-06-09 11:16:59 +00:00
|
|
|
assert instance["LaunchTemplate"] == expected_launch_template
|
|
|
|
assert "LaunchConfigurationName" not in instance
|
|
|
|
assert instance["AutoScalingGroupName"] == "test_asg"
|
|
|
|
assert instance["AvailabilityZone"] == "us-east-1a"
|
|
|
|
assert instance["ProtectedFromScaleIn"] is True
|
|
|
|
assert instance["InstanceType"] == "t2.micro"
|
2020-04-12 03:44:16 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2020-04-12 03:44:16 +00:00
|
|
|
def test_describe_autoscaling_instances_instanceid_filter():
|
|
|
|
mocked_networking = setup_networking()
|
|
|
|
client = boto3.client("autoscaling", region_name="us-east-1")
|
|
|
|
_ = client.create_launch_configuration(
|
2021-01-27 19:49:33 +00:00
|
|
|
LaunchConfigurationName="test_launch_configuration",
|
|
|
|
ImageId=EXAMPLE_AMI_ID,
|
|
|
|
InstanceType="t2.medium",
|
2020-04-12 03:44:16 +00:00
|
|
|
)
|
|
|
|
_ = client.create_auto_scaling_group(
|
|
|
|
AutoScalingGroupName="test_asg",
|
|
|
|
LaunchConfigurationName="test_launch_configuration",
|
|
|
|
MinSize=0,
|
|
|
|
MaxSize=20,
|
|
|
|
DesiredCapacity=5,
|
|
|
|
VPCZoneIdentifier=mocked_networking["subnet1"],
|
|
|
|
NewInstancesProtectedFromScaleIn=True,
|
|
|
|
)
|
|
|
|
|
2018-11-24 10:32:39 +00:00
|
|
|
response = client.describe_auto_scaling_groups(AutoScalingGroupNames=["test_asg"])
|
|
|
|
instance_ids = [
|
|
|
|
instance["InstanceId"]
|
|
|
|
for instance in response["AutoScalingGroups"][0]["Instances"]
|
|
|
|
]
|
|
|
|
|
2020-04-12 07:08:40 +00:00
|
|
|
response = client.describe_auto_scaling_instances(
|
|
|
|
InstanceIds=instance_ids[0:2]
|
|
|
|
) # Filter by first 2 of 5
|
2023-06-09 11:16:59 +00:00
|
|
|
assert len(response["AutoScalingInstances"]) == 2
|
2018-11-24 10:32:39 +00:00
|
|
|
for instance in response["AutoScalingInstances"]:
|
2023-06-09 11:16:59 +00:00
|
|
|
assert instance["AutoScalingGroupName"] == "test_asg"
|
|
|
|
assert instance["AvailabilityZone"] == "us-east-1a"
|
|
|
|
assert instance["ProtectedFromScaleIn"] is True
|
2016-05-02 02:34:16 +00:00
|
|
|
|
2016-06-18 01:51:28 +00:00
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2022-03-29 21:46:06 +00:00
|
|
|
def test_update_autoscaling_group_launch_config():
|
2017-12-27 19:17:59 +00:00
|
|
|
mocked_networking = setup_networking()
|
2016-05-02 02:34:16 +00:00
|
|
|
client = boto3.client("autoscaling", region_name="us-east-1")
|
2020-08-26 14:15:07 +00:00
|
|
|
client.create_launch_configuration(
|
2021-01-27 19:49:33 +00:00
|
|
|
LaunchConfigurationName="test_launch_configuration",
|
|
|
|
ImageId=EXAMPLE_AMI_ID,
|
|
|
|
InstanceType="t2.medium",
|
2016-05-02 02:34:16 +00:00
|
|
|
)
|
2020-08-26 14:15:07 +00:00
|
|
|
client.create_launch_configuration(
|
2021-01-27 19:49:33 +00:00
|
|
|
LaunchConfigurationName="test_launch_configuration_new",
|
|
|
|
ImageId=EXAMPLE_AMI_ID,
|
|
|
|
InstanceType="t2.medium",
|
2020-08-26 14:15:07 +00:00
|
|
|
)
|
|
|
|
client.create_auto_scaling_group(
|
2016-05-02 02:34:16 +00:00
|
|
|
AutoScalingGroupName="test_asg",
|
|
|
|
LaunchConfigurationName="test_launch_configuration",
|
|
|
|
MinSize=0,
|
|
|
|
MaxSize=20,
|
2017-12-27 19:17:59 +00:00
|
|
|
DesiredCapacity=5,
|
|
|
|
VPCZoneIdentifier=mocked_networking["subnet1"],
|
2018-11-24 10:32:39 +00:00
|
|
|
NewInstancesProtectedFromScaleIn=True,
|
2016-05-02 02:34:16 +00:00
|
|
|
)
|
|
|
|
|
2020-08-26 14:15:07 +00:00
|
|
|
client.update_auto_scaling_group(
|
2016-05-02 02:34:16 +00:00
|
|
|
AutoScalingGroupName="test_asg",
|
2020-08-26 14:15:07 +00:00
|
|
|
LaunchConfigurationName="test_launch_configuration_new",
|
2016-05-02 02:34:16 +00:00
|
|
|
MinSize=1,
|
2022-11-17 22:41:08 +00:00
|
|
|
VPCZoneIdentifier=f"{mocked_networking['subnet1']},{mocked_networking['subnet2']}",
|
2018-11-24 10:32:39 +00:00
|
|
|
NewInstancesProtectedFromScaleIn=False,
|
2016-05-02 02:34:16 +00:00
|
|
|
)
|
2016-05-05 02:10:11 +00:00
|
|
|
|
|
|
|
response = client.describe_auto_scaling_groups(AutoScalingGroupNames=["test_asg"])
|
2018-11-24 10:32:39 +00:00
|
|
|
group = response["AutoScalingGroups"][0]
|
2023-06-09 11:16:59 +00:00
|
|
|
assert group["LaunchConfigurationName"] == "test_launch_configuration_new"
|
|
|
|
assert group["MinSize"] == 1
|
|
|
|
assert set(group["AvailabilityZones"]) == {"us-east-1a", "us-east-1b"}
|
|
|
|
assert group["NewInstancesProtectedFromScaleIn"] is False
|
2020-08-26 14:15:07 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2022-03-29 21:46:06 +00:00
|
|
|
def test_update_autoscaling_group_launch_template():
|
2020-08-26 14:15:07 +00:00
|
|
|
mocked_networking = setup_networking()
|
|
|
|
ec2_client = boto3.client("ec2", region_name="us-east-1")
|
|
|
|
ec2_client.create_launch_template(
|
|
|
|
LaunchTemplateName="test_launch_template",
|
2021-01-13 09:02:11 +00:00
|
|
|
LaunchTemplateData={"ImageId": EXAMPLE_AMI_ID, "InstanceType": "t2.micro"},
|
2020-08-26 14:15:07 +00:00
|
|
|
)
|
|
|
|
template = ec2_client.create_launch_template(
|
|
|
|
LaunchTemplateName="test_launch_template_new",
|
|
|
|
LaunchTemplateData={
|
|
|
|
"ImageId": "ami-1ea5b10a3d8867db4",
|
|
|
|
"InstanceType": "t2.micro",
|
|
|
|
},
|
|
|
|
)["LaunchTemplate"]
|
|
|
|
client = boto3.client("autoscaling", region_name="us-east-1")
|
|
|
|
client.create_auto_scaling_group(
|
|
|
|
AutoScalingGroupName="test_asg",
|
|
|
|
LaunchTemplate={"LaunchTemplateName": "test_launch_template", "Version": "1"},
|
|
|
|
MinSize=0,
|
|
|
|
MaxSize=20,
|
|
|
|
DesiredCapacity=5,
|
|
|
|
VPCZoneIdentifier=mocked_networking["subnet1"],
|
|
|
|
NewInstancesProtectedFromScaleIn=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
client.update_auto_scaling_group(
|
|
|
|
AutoScalingGroupName="test_asg",
|
|
|
|
LaunchTemplate={
|
|
|
|
"LaunchTemplateName": "test_launch_template_new",
|
|
|
|
"Version": "1",
|
|
|
|
},
|
|
|
|
MinSize=1,
|
2022-11-17 22:41:08 +00:00
|
|
|
VPCZoneIdentifier=f"{mocked_networking['subnet1']},{mocked_networking['subnet2']}",
|
2020-08-26 14:15:07 +00:00
|
|
|
NewInstancesProtectedFromScaleIn=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
expected_launch_template = {
|
|
|
|
"LaunchTemplateId": template["LaunchTemplateId"],
|
|
|
|
"LaunchTemplateName": "test_launch_template_new",
|
|
|
|
"Version": "1",
|
|
|
|
}
|
|
|
|
|
|
|
|
response = client.describe_auto_scaling_groups(AutoScalingGroupNames=["test_asg"])
|
|
|
|
group = response["AutoScalingGroups"][0]
|
2023-06-09 11:16:59 +00:00
|
|
|
assert group["LaunchTemplate"] == expected_launch_template
|
|
|
|
assert group["MinSize"] == 1
|
|
|
|
assert set(group["AvailabilityZones"]) == {"us-east-1a", "us-east-1b"}
|
|
|
|
assert group["NewInstancesProtectedFromScaleIn"] is False
|
2016-06-18 01:51:28 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2019-07-16 07:12:03 +00:00
|
|
|
def test_update_autoscaling_group_min_size_desired_capacity_change():
|
|
|
|
mocked_networking = setup_networking()
|
|
|
|
client = boto3.client("autoscaling", region_name="us-east-1")
|
|
|
|
|
|
|
|
client.create_launch_configuration(
|
2021-01-27 19:49:33 +00:00
|
|
|
LaunchConfigurationName="test_launch_configuration",
|
|
|
|
ImageId=EXAMPLE_AMI_ID,
|
|
|
|
InstanceType="t2.medium",
|
2019-07-16 07:12:03 +00:00
|
|
|
)
|
|
|
|
client.create_auto_scaling_group(
|
|
|
|
AutoScalingGroupName="test_asg",
|
|
|
|
LaunchConfigurationName="test_launch_configuration",
|
|
|
|
MinSize=2,
|
|
|
|
MaxSize=20,
|
|
|
|
DesiredCapacity=3,
|
|
|
|
VPCZoneIdentifier=mocked_networking["subnet1"],
|
|
|
|
)
|
2018-11-24 10:32:39 +00:00
|
|
|
client.update_auto_scaling_group(AutoScalingGroupName="test_asg", MinSize=5)
|
2019-07-16 07:12:03 +00:00
|
|
|
response = client.describe_auto_scaling_groups(AutoScalingGroupNames=["test_asg"])
|
|
|
|
group = response["AutoScalingGroups"][0]
|
2023-06-09 11:16:59 +00:00
|
|
|
assert group["DesiredCapacity"] == 5
|
|
|
|
assert group["MinSize"] == 5
|
|
|
|
assert len(group["Instances"]) == 5
|
2019-07-16 07:12:03 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2019-07-16 07:12:03 +00:00
|
|
|
def test_update_autoscaling_group_max_size_desired_capacity_change():
|
|
|
|
mocked_networking = setup_networking()
|
|
|
|
client = boto3.client("autoscaling", region_name="us-east-1")
|
|
|
|
|
|
|
|
client.create_launch_configuration(
|
2021-01-27 19:49:33 +00:00
|
|
|
LaunchConfigurationName="test_launch_configuration",
|
|
|
|
ImageId=EXAMPLE_AMI_ID,
|
|
|
|
InstanceType="t2.medium",
|
2019-07-16 07:12:03 +00:00
|
|
|
)
|
|
|
|
client.create_auto_scaling_group(
|
|
|
|
AutoScalingGroupName="test_asg",
|
|
|
|
LaunchConfigurationName="test_launch_configuration",
|
|
|
|
MinSize=2,
|
|
|
|
MaxSize=20,
|
|
|
|
DesiredCapacity=10,
|
|
|
|
VPCZoneIdentifier=mocked_networking["subnet1"],
|
|
|
|
)
|
|
|
|
client.update_auto_scaling_group(AutoScalingGroupName="test_asg", MaxSize=5)
|
|
|
|
response = client.describe_auto_scaling_groups(AutoScalingGroupNames=["test_asg"])
|
|
|
|
group = response["AutoScalingGroups"][0]
|
2023-06-09 11:16:59 +00:00
|
|
|
assert group["DesiredCapacity"] == 5
|
|
|
|
assert group["MaxSize"] == 5
|
|
|
|
assert len(group["Instances"]) == 5
|
2019-07-16 07:12:03 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2022-03-29 21:46:06 +00:00
|
|
|
def test_autoscaling_describe_policies():
|
2017-12-27 19:17:59 +00:00
|
|
|
mocked_networking = setup_networking()
|
2017-01-12 01:40:57 +00:00
|
|
|
client = boto3.client("autoscaling", region_name="us-east-1")
|
|
|
|
_ = client.create_launch_configuration(
|
2021-01-27 19:49:33 +00:00
|
|
|
LaunchConfigurationName="test_launch_configuration",
|
|
|
|
ImageId=EXAMPLE_AMI_ID,
|
|
|
|
InstanceType="t2.medium",
|
2017-01-12 01:40:57 +00:00
|
|
|
)
|
|
|
|
_ = client.create_auto_scaling_group(
|
|
|
|
AutoScalingGroupName="test_asg",
|
|
|
|
LaunchConfigurationName="test_launch_configuration",
|
|
|
|
MinSize=0,
|
|
|
|
MaxSize=20,
|
|
|
|
DesiredCapacity=5,
|
|
|
|
Tags=[
|
|
|
|
{
|
|
|
|
"ResourceId": "test_asg",
|
|
|
|
"Key": "test_key",
|
|
|
|
"Value": "test_value",
|
|
|
|
"PropagateAtLaunch": True,
|
2017-12-27 19:17:59 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
VPCZoneIdentifier=mocked_networking["subnet1"],
|
2017-01-12 01:40:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
client.put_scaling_policy(
|
|
|
|
AutoScalingGroupName="test_asg",
|
|
|
|
PolicyName="test_policy_down",
|
|
|
|
PolicyType="SimpleScaling",
|
2022-03-29 21:46:06 +00:00
|
|
|
MetricAggregationType="Minimum",
|
2017-01-12 01:40:57 +00:00
|
|
|
AdjustmentType="PercentChangeInCapacity",
|
|
|
|
ScalingAdjustment=-10,
|
|
|
|
Cooldown=60,
|
|
|
|
MinAdjustmentMagnitude=1,
|
|
|
|
)
|
|
|
|
client.put_scaling_policy(
|
|
|
|
AutoScalingGroupName="test_asg",
|
|
|
|
PolicyName="test_policy_up",
|
|
|
|
PolicyType="SimpleScaling",
|
|
|
|
AdjustmentType="PercentChangeInCapacity",
|
|
|
|
ScalingAdjustment=10,
|
|
|
|
Cooldown=60,
|
|
|
|
MinAdjustmentMagnitude=1,
|
|
|
|
)
|
|
|
|
|
|
|
|
response = client.describe_policies()
|
2023-06-09 11:16:59 +00:00
|
|
|
assert len(response["ScalingPolicies"]) == 2
|
2017-01-12 01:40:57 +00:00
|
|
|
|
|
|
|
response = client.describe_policies(AutoScalingGroupName="test_asg")
|
2023-06-09 11:16:59 +00:00
|
|
|
assert len(response["ScalingPolicies"]) == 2
|
2017-01-12 01:40:57 +00:00
|
|
|
|
|
|
|
response = client.describe_policies(PolicyTypes=["StepScaling"])
|
2023-06-09 11:16:59 +00:00
|
|
|
assert len(response["ScalingPolicies"]) == 0
|
2017-01-12 01:40:57 +00:00
|
|
|
|
|
|
|
response = client.describe_policies(
|
|
|
|
AutoScalingGroupName="test_asg",
|
|
|
|
PolicyNames=["test_policy_down"],
|
|
|
|
PolicyTypes=["SimpleScaling"],
|
|
|
|
)
|
2023-06-09 11:16:59 +00:00
|
|
|
assert len(response["ScalingPolicies"]) == 1
|
2021-10-20 17:49:23 +00:00
|
|
|
policy = response["ScalingPolicies"][0]
|
2023-06-09 11:16:59 +00:00
|
|
|
assert policy["PolicyType"] == "SimpleScaling"
|
|
|
|
assert policy["MetricAggregationType"] == "Minimum"
|
|
|
|
assert policy["AdjustmentType"] == "PercentChangeInCapacity"
|
|
|
|
assert policy["ScalingAdjustment"] == -10
|
|
|
|
assert policy["Cooldown"] == 60
|
|
|
|
assert (
|
|
|
|
policy["PolicyARN"]
|
|
|
|
== f"arn:aws:autoscaling:us-east-1:{ACCOUNT_ID}:scalingPolicy:c322761b-3172-4d56-9a21-0ed9d6161d67:autoScalingGroupName/test_asg:policyName/test_policy_down"
|
2021-10-20 17:49:23 +00:00
|
|
|
)
|
2023-06-09 11:16:59 +00:00
|
|
|
assert policy["PolicyName"] == "test_policy_down"
|
|
|
|
assert "TargetTrackingConfiguration" not in policy
|
2019-10-31 15:44:26 +00:00
|
|
|
|
2017-10-17 00:09:51 +00:00
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2021-10-20 17:49:23 +00:00
|
|
|
def test_create_autoscaling_policy_with_policytype__targettrackingscaling():
|
2022-10-02 13:03:03 +00:00
|
|
|
mocked_networking = setup_networking(region_name="us-west-1")
|
|
|
|
client = boto3.client("autoscaling", region_name="us-west-1")
|
2021-10-20 17:49:23 +00:00
|
|
|
configuration_name = "test"
|
|
|
|
asg_name = "asg_test"
|
|
|
|
|
|
|
|
client.create_launch_configuration(
|
|
|
|
LaunchConfigurationName=configuration_name,
|
|
|
|
ImageId=EXAMPLE_AMI_ID,
|
|
|
|
InstanceType="m1.small",
|
|
|
|
)
|
|
|
|
client.create_auto_scaling_group(
|
|
|
|
LaunchConfigurationName=configuration_name,
|
|
|
|
AutoScalingGroupName=asg_name,
|
|
|
|
MinSize=1,
|
|
|
|
MaxSize=2,
|
|
|
|
VPCZoneIdentifier=mocked_networking["subnet1"],
|
|
|
|
)
|
|
|
|
|
|
|
|
client.put_scaling_policy(
|
|
|
|
AutoScalingGroupName=asg_name,
|
|
|
|
PolicyName=configuration_name,
|
|
|
|
PolicyType="TargetTrackingScaling",
|
|
|
|
EstimatedInstanceWarmup=100,
|
|
|
|
TargetTrackingConfiguration={
|
|
|
|
"PredefinedMetricSpecification": {
|
|
|
|
"PredefinedMetricType": "ASGAverageNetworkIn",
|
|
|
|
},
|
|
|
|
"TargetValue": 1000000.0,
|
2023-05-13 17:08:51 +00:00
|
|
|
"CustomizedMetricSpecification": {
|
|
|
|
"Metrics": [
|
|
|
|
{
|
|
|
|
"Label": "Get ASGAverageCPUUtilization",
|
|
|
|
"Id": "cpu",
|
|
|
|
"MetricStat": {
|
|
|
|
"Metric": {
|
|
|
|
"MetricName": "CPUUtilization",
|
|
|
|
"Namespace": "AWS/EC2",
|
|
|
|
"Dimensions": [
|
|
|
|
{"Name": "AutoScalingGroupName", "Value": asg_name}
|
|
|
|
],
|
|
|
|
},
|
|
|
|
"Stat": "Average",
|
|
|
|
},
|
|
|
|
"ReturnData": False,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Label": "Calculate square cpu",
|
|
|
|
"Id": "load",
|
|
|
|
"Expression": "cpu^2",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2021-10-20 17:49:23 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
resp = client.describe_policies(AutoScalingGroupName=asg_name)
|
|
|
|
policy = resp["ScalingPolicies"][0]
|
2023-06-09 11:16:59 +00:00
|
|
|
assert policy["PolicyName"] == configuration_name
|
|
|
|
assert (
|
|
|
|
policy["PolicyARN"]
|
|
|
|
== f"arn:aws:autoscaling:us-west-1:{ACCOUNT_ID}:scalingPolicy:c322761b-3172-4d56-9a21-0ed9d6161d67:autoScalingGroupName/{asg_name}:policyName/{configuration_name}"
|
|
|
|
)
|
|
|
|
assert policy["PolicyType"] == "TargetTrackingScaling"
|
|
|
|
assert policy["TargetTrackingConfiguration"] == {
|
|
|
|
"PredefinedMetricSpecification": {
|
|
|
|
"PredefinedMetricType": "ASGAverageNetworkIn",
|
|
|
|
},
|
|
|
|
"CustomizedMetricSpecification": {
|
|
|
|
"MetricName": "None",
|
|
|
|
"Namespace": "None",
|
|
|
|
"Dimensions": [],
|
|
|
|
"Statistic": "None",
|
|
|
|
"Metrics": [
|
|
|
|
{
|
|
|
|
"Label": "Get ASGAverageCPUUtilization",
|
|
|
|
"Id": "cpu",
|
|
|
|
"MetricStat": {
|
|
|
|
"Metric": {
|
|
|
|
"MetricName": "CPUUtilization",
|
|
|
|
"Namespace": "AWS/EC2",
|
|
|
|
"Dimensions": [
|
|
|
|
{"Name": "AutoScalingGroupName", "Value": asg_name}
|
|
|
|
],
|
2023-05-13 17:08:51 +00:00
|
|
|
},
|
2023-06-09 11:16:59 +00:00
|
|
|
"Stat": "Average",
|
|
|
|
"Unit": "None",
|
2023-05-13 17:08:51 +00:00
|
|
|
},
|
2023-06-09 11:16:59 +00:00
|
|
|
"ReturnData": False,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Label": "Calculate square cpu",
|
|
|
|
"Id": "load",
|
|
|
|
"Expression": "cpu^2",
|
|
|
|
"ReturnData": True,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
"TargetValue": 1000000.0,
|
|
|
|
}
|
|
|
|
assert "ScalingAdjustment" not in policy
|
|
|
|
assert "Cooldown" not in policy
|
2021-10-20 17:49:23 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2021-10-20 17:49:23 +00:00
|
|
|
def test_create_autoscaling_policy_with_policytype__stepscaling():
|
|
|
|
mocked_networking = setup_networking(region_name="eu-west-1")
|
|
|
|
client = boto3.client("autoscaling", region_name="eu-west-1")
|
|
|
|
launch_config_name = "lg_name"
|
|
|
|
asg_name = "asg_test"
|
|
|
|
|
|
|
|
client.create_launch_configuration(
|
|
|
|
LaunchConfigurationName=launch_config_name,
|
|
|
|
ImageId=EXAMPLE_AMI_ID,
|
|
|
|
InstanceType="m1.small",
|
|
|
|
)
|
|
|
|
client.create_auto_scaling_group(
|
|
|
|
LaunchConfigurationName=launch_config_name,
|
|
|
|
AutoScalingGroupName=asg_name,
|
|
|
|
MinSize=1,
|
|
|
|
MaxSize=2,
|
|
|
|
VPCZoneIdentifier=mocked_networking["subnet1"],
|
|
|
|
)
|
|
|
|
|
|
|
|
client.put_scaling_policy(
|
|
|
|
AutoScalingGroupName=asg_name,
|
|
|
|
PolicyName=launch_config_name,
|
|
|
|
PolicyType="StepScaling",
|
|
|
|
StepAdjustments=[
|
|
|
|
{
|
|
|
|
"MetricIntervalLowerBound": 2,
|
|
|
|
"MetricIntervalUpperBound": 8,
|
|
|
|
"ScalingAdjustment": 1,
|
|
|
|
}
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
resp = client.describe_policies(AutoScalingGroupName=asg_name)
|
|
|
|
policy = resp["ScalingPolicies"][0]
|
2023-06-09 11:16:59 +00:00
|
|
|
assert policy["PolicyName"] == launch_config_name
|
|
|
|
assert (
|
|
|
|
policy["PolicyARN"]
|
|
|
|
== f"arn:aws:autoscaling:eu-west-1:{ACCOUNT_ID}:scalingPolicy:c322761b-3172-4d56-9a21-0ed9d6161d67:autoScalingGroupName/{asg_name}:policyName/{launch_config_name}"
|
2021-10-20 17:49:23 +00:00
|
|
|
)
|
2023-06-09 11:16:59 +00:00
|
|
|
assert policy["PolicyType"] == "StepScaling"
|
|
|
|
assert policy["StepAdjustments"] == [
|
|
|
|
{
|
|
|
|
"MetricIntervalLowerBound": 2,
|
|
|
|
"MetricIntervalUpperBound": 8,
|
|
|
|
"ScalingAdjustment": 1,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
assert "TargetTrackingConfiguration" not in policy
|
|
|
|
assert "ScalingAdjustment" not in policy
|
|
|
|
assert "Cooldown" not in policy
|
2021-10-20 17:49:23 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2022-03-29 21:46:06 +00:00
|
|
|
def test_create_autoscaling_policy_with_predictive_scaling_config():
|
|
|
|
mocked_networking = setup_networking(region_name="eu-west-1")
|
|
|
|
client = boto3.client("autoscaling", region_name="eu-west-1")
|
|
|
|
launch_config_name = "lg_name"
|
|
|
|
asg_name = "asg_test"
|
|
|
|
|
|
|
|
client.create_launch_configuration(
|
|
|
|
LaunchConfigurationName=launch_config_name,
|
|
|
|
ImageId=EXAMPLE_AMI_ID,
|
|
|
|
InstanceType="m1.small",
|
|
|
|
)
|
|
|
|
client.create_auto_scaling_group(
|
|
|
|
LaunchConfigurationName=launch_config_name,
|
|
|
|
AutoScalingGroupName=asg_name,
|
|
|
|
MinSize=1,
|
|
|
|
MaxSize=2,
|
|
|
|
VPCZoneIdentifier=mocked_networking["subnet1"],
|
|
|
|
)
|
|
|
|
|
|
|
|
client.put_scaling_policy(
|
|
|
|
AutoScalingGroupName=asg_name,
|
|
|
|
PolicyName=launch_config_name,
|
|
|
|
PolicyType="PredictiveScaling",
|
|
|
|
PredictiveScalingConfiguration={
|
|
|
|
"MetricSpecifications": [{"TargetValue": 5}],
|
|
|
|
"SchedulingBufferTime": 7,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
resp = client.describe_policies(AutoScalingGroupName=asg_name)
|
|
|
|
policy = resp["ScalingPolicies"][0]
|
2023-06-09 11:16:59 +00:00
|
|
|
assert policy["PredictiveScalingConfiguration"] == {
|
|
|
|
"MetricSpecifications": [{"TargetValue": 5.0}],
|
|
|
|
"SchedulingBufferTime": 7,
|
|
|
|
}
|
2022-03-29 21:46:06 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2023-08-21 20:33:16 +00:00
|
|
|
@pytest.mark.parametrize("include_instances_distribution", [True, False])
|
|
|
|
def test_create_auto_scaling_group_with_mixed_instances_policy(
|
|
|
|
include_instances_distribution,
|
|
|
|
):
|
2022-08-30 10:04:12 +00:00
|
|
|
mocked_networking = setup_networking(region_name="eu-west-1")
|
|
|
|
client = boto3.client("autoscaling", region_name="eu-west-1")
|
|
|
|
ec2_client = boto3.client("ec2", region_name="eu-west-1")
|
|
|
|
asg_name = "asg_test"
|
|
|
|
|
|
|
|
lt = ec2_client.create_launch_template(
|
|
|
|
LaunchTemplateName="launchie",
|
|
|
|
LaunchTemplateData={"ImageId": EXAMPLE_AMI_ID},
|
|
|
|
)["LaunchTemplate"]
|
2023-08-21 20:33:16 +00:00
|
|
|
input_policy = {
|
|
|
|
"LaunchTemplate": {
|
|
|
|
"LaunchTemplateSpecification": {
|
|
|
|
"LaunchTemplateName": "launchie",
|
|
|
|
"Version": "$DEFAULT",
|
2022-08-30 10:04:12 +00:00
|
|
|
}
|
2023-08-21 20:33:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if include_instances_distribution:
|
|
|
|
input_policy["InstancesDistribution"] = {
|
|
|
|
"OnDemandAllocationStrategy": "string",
|
|
|
|
"OnDemandBaseCapacity": 123,
|
|
|
|
"OnDemandPercentageAboveBaseCapacity": 123,
|
|
|
|
"SpotAllocationStrategy": "string",
|
|
|
|
"SpotInstancePools": 123,
|
|
|
|
"SpotMaxPrice": "string",
|
|
|
|
}
|
|
|
|
client.create_auto_scaling_group(
|
|
|
|
MixedInstancesPolicy=input_policy,
|
2022-08-30 10:04:12 +00:00
|
|
|
AutoScalingGroupName=asg_name,
|
|
|
|
MinSize=2,
|
|
|
|
MaxSize=2,
|
|
|
|
VPCZoneIdentifier=mocked_networking["subnet1"],
|
|
|
|
)
|
|
|
|
|
|
|
|
# Assert we can describe MixedInstancesPolicy
|
|
|
|
response = client.describe_auto_scaling_groups(AutoScalingGroupNames=[asg_name])
|
|
|
|
group = response["AutoScalingGroups"][0]
|
2023-08-21 20:33:16 +00:00
|
|
|
|
|
|
|
input_policy["LaunchTemplate"]["LaunchTemplateSpecification"][
|
|
|
|
"LaunchTemplateId"
|
|
|
|
] = lt["LaunchTemplateId"]
|
|
|
|
assert group["MixedInstancesPolicy"] == input_policy
|
2022-08-30 10:04:12 +00:00
|
|
|
|
|
|
|
# Assert the LaunchTemplate is known for the resulting instances
|
|
|
|
response = client.describe_auto_scaling_instances()
|
2023-06-09 11:16:59 +00:00
|
|
|
assert len(response["AutoScalingInstances"]) == 2
|
2022-08-30 10:04:12 +00:00
|
|
|
for instance in response["AutoScalingInstances"]:
|
2023-06-09 11:16:59 +00:00
|
|
|
assert instance["LaunchTemplate"] == {
|
|
|
|
"LaunchTemplateId": lt["LaunchTemplateId"],
|
|
|
|
"LaunchTemplateName": "launchie",
|
|
|
|
"Version": "$DEFAULT",
|
|
|
|
}
|
2022-08-30 10:04:12 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2022-08-30 10:04:12 +00:00
|
|
|
def test_create_auto_scaling_group_with_mixed_instances_policy_overrides():
|
|
|
|
mocked_networking = setup_networking(region_name="eu-west-1")
|
|
|
|
client = boto3.client("autoscaling", region_name="eu-west-1")
|
|
|
|
ec2_client = boto3.client("ec2", region_name="eu-west-1")
|
|
|
|
asg_name = "asg_test"
|
|
|
|
|
|
|
|
lt = ec2_client.create_launch_template(
|
|
|
|
LaunchTemplateName="launchie",
|
|
|
|
LaunchTemplateData={"ImageId": EXAMPLE_AMI_ID},
|
|
|
|
)["LaunchTemplate"]
|
|
|
|
client.create_auto_scaling_group(
|
|
|
|
MixedInstancesPolicy={
|
|
|
|
"LaunchTemplate": {
|
|
|
|
"LaunchTemplateSpecification": {
|
|
|
|
"LaunchTemplateName": "launchie",
|
|
|
|
"Version": "$DEFAULT",
|
|
|
|
},
|
|
|
|
"Overrides": [
|
|
|
|
{
|
|
|
|
"InstanceType": "t2.medium",
|
|
|
|
"WeightedCapacity": "50",
|
|
|
|
}
|
|
|
|
],
|
|
|
|
}
|
|
|
|
},
|
|
|
|
AutoScalingGroupName=asg_name,
|
|
|
|
MinSize=2,
|
|
|
|
MaxSize=2,
|
|
|
|
VPCZoneIdentifier=mocked_networking["subnet1"],
|
|
|
|
)
|
|
|
|
|
|
|
|
# Assert we can describe MixedInstancesPolicy
|
|
|
|
response = client.describe_auto_scaling_groups(AutoScalingGroupNames=[asg_name])
|
|
|
|
group = response["AutoScalingGroups"][0]
|
2023-06-09 11:16:59 +00:00
|
|
|
assert group["MixedInstancesPolicy"] == {
|
|
|
|
"LaunchTemplate": {
|
|
|
|
"LaunchTemplateSpecification": {
|
|
|
|
"LaunchTemplateId": lt["LaunchTemplateId"],
|
|
|
|
"LaunchTemplateName": "launchie",
|
|
|
|
"Version": "$DEFAULT",
|
|
|
|
},
|
|
|
|
"Overrides": [
|
|
|
|
{
|
|
|
|
"InstanceType": "t2.medium",
|
|
|
|
"WeightedCapacity": "50",
|
|
|
|
}
|
|
|
|
],
|
2022-08-30 10:04:12 +00:00
|
|
|
}
|
2023-06-09 11:16:59 +00:00
|
|
|
}
|
2022-08-30 10:04:12 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2018-11-24 10:32:39 +00:00
|
|
|
def test_set_instance_protection():
|
|
|
|
mocked_networking = setup_networking()
|
|
|
|
client = boto3.client("autoscaling", region_name="us-east-1")
|
|
|
|
_ = client.create_launch_configuration(
|
2021-01-27 19:49:33 +00:00
|
|
|
LaunchConfigurationName="test_launch_configuration",
|
|
|
|
ImageId=EXAMPLE_AMI_ID,
|
|
|
|
InstanceType="t2.medium",
|
2018-11-24 10:32:39 +00:00
|
|
|
)
|
|
|
|
_ = client.create_auto_scaling_group(
|
|
|
|
AutoScalingGroupName="test_asg",
|
|
|
|
LaunchConfigurationName="test_launch_configuration",
|
|
|
|
MinSize=0,
|
|
|
|
MaxSize=20,
|
|
|
|
DesiredCapacity=5,
|
|
|
|
VPCZoneIdentifier=mocked_networking["subnet1"],
|
|
|
|
NewInstancesProtectedFromScaleIn=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
response = client.describe_auto_scaling_groups(AutoScalingGroupNames=["test_asg"])
|
|
|
|
instance_ids = [
|
|
|
|
instance["InstanceId"]
|
|
|
|
for instance in response["AutoScalingGroups"][0]["Instances"]
|
|
|
|
]
|
|
|
|
protected = instance_ids[:3]
|
|
|
|
|
|
|
|
_ = client.set_instance_protection(
|
|
|
|
AutoScalingGroupName="test_asg",
|
|
|
|
InstanceIds=protected,
|
|
|
|
ProtectedFromScaleIn=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
response = client.describe_auto_scaling_groups(AutoScalingGroupNames=["test_asg"])
|
|
|
|
for instance in response["AutoScalingGroups"][0]["Instances"]:
|
2023-06-09 11:16:59 +00:00
|
|
|
assert instance["ProtectedFromScaleIn"] is (instance["InstanceId"] in protected)
|
2018-11-24 10:32:39 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2022-03-29 21:46:06 +00:00
|
|
|
def test_set_desired_capacity_up():
|
2018-11-24 10:32:39 +00:00
|
|
|
mocked_networking = setup_networking()
|
|
|
|
client = boto3.client("autoscaling", region_name="us-east-1")
|
|
|
|
_ = client.create_launch_configuration(
|
2021-01-27 19:49:33 +00:00
|
|
|
LaunchConfigurationName="test_launch_configuration",
|
|
|
|
ImageId=EXAMPLE_AMI_ID,
|
|
|
|
InstanceType="t2.medium",
|
2018-11-24 10:32:39 +00:00
|
|
|
)
|
|
|
|
_ = client.create_auto_scaling_group(
|
|
|
|
AutoScalingGroupName="test_asg",
|
|
|
|
LaunchConfigurationName="test_launch_configuration",
|
|
|
|
MinSize=0,
|
|
|
|
MaxSize=20,
|
|
|
|
DesiredCapacity=5,
|
|
|
|
VPCZoneIdentifier=mocked_networking["subnet1"],
|
|
|
|
NewInstancesProtectedFromScaleIn=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
_ = client.set_desired_capacity(AutoScalingGroupName="test_asg", DesiredCapacity=10)
|
|
|
|
|
|
|
|
response = client.describe_auto_scaling_groups(AutoScalingGroupNames=["test_asg"])
|
|
|
|
instances = response["AutoScalingGroups"][0]["Instances"]
|
2023-06-09 11:16:59 +00:00
|
|
|
assert len(instances) == 10
|
2018-11-24 10:32:39 +00:00
|
|
|
for instance in instances:
|
2023-06-09 11:16:59 +00:00
|
|
|
assert instance["ProtectedFromScaleIn"] is True
|
2018-11-24 10:32:39 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2022-03-29 21:46:06 +00:00
|
|
|
def test_set_desired_capacity_down():
|
2018-11-24 10:32:39 +00:00
|
|
|
mocked_networking = setup_networking()
|
|
|
|
client = boto3.client("autoscaling", region_name="us-east-1")
|
|
|
|
_ = client.create_launch_configuration(
|
2021-01-27 19:49:33 +00:00
|
|
|
LaunchConfigurationName="test_launch_configuration",
|
|
|
|
ImageId=EXAMPLE_AMI_ID,
|
|
|
|
InstanceType="t2.medium",
|
2018-11-24 10:32:39 +00:00
|
|
|
)
|
|
|
|
_ = client.create_auto_scaling_group(
|
|
|
|
AutoScalingGroupName="test_asg",
|
|
|
|
LaunchConfigurationName="test_launch_configuration",
|
|
|
|
MinSize=0,
|
|
|
|
MaxSize=20,
|
|
|
|
DesiredCapacity=5,
|
|
|
|
VPCZoneIdentifier=mocked_networking["subnet1"],
|
|
|
|
NewInstancesProtectedFromScaleIn=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
response = client.describe_auto_scaling_groups(AutoScalingGroupNames=["test_asg"])
|
|
|
|
instance_ids = [
|
|
|
|
instance["InstanceId"]
|
|
|
|
for instance in response["AutoScalingGroups"][0]["Instances"]
|
|
|
|
]
|
|
|
|
unprotected, protected = instance_ids[:2], instance_ids[2:]
|
|
|
|
|
|
|
|
_ = client.set_instance_protection(
|
|
|
|
AutoScalingGroupName="test_asg",
|
|
|
|
InstanceIds=unprotected,
|
|
|
|
ProtectedFromScaleIn=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
_ = client.set_desired_capacity(AutoScalingGroupName="test_asg", DesiredCapacity=1)
|
|
|
|
|
|
|
|
response = client.describe_auto_scaling_groups(AutoScalingGroupNames=["test_asg"])
|
|
|
|
group = response["AutoScalingGroups"][0]
|
2023-06-09 11:16:59 +00:00
|
|
|
assert group["DesiredCapacity"] == 1
|
2018-11-24 10:32:39 +00:00
|
|
|
instance_ids = {instance["InstanceId"] for instance in group["Instances"]}
|
2023-06-09 11:16:59 +00:00
|
|
|
assert set(protected) == instance_ids
|
|
|
|
for x in unprotected:
|
|
|
|
assert x not in instance_ids # only unprotected killed
|
2019-07-18 18:30:07 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2020-04-13 00:50:01 +00:00
|
|
|
def test_terminate_instance_via_ec2_in_autoscaling_group():
|
2019-07-18 18:30:07 +00:00
|
|
|
mocked_networking = setup_networking()
|
|
|
|
client = boto3.client("autoscaling", region_name="us-east-1")
|
|
|
|
_ = client.create_launch_configuration(
|
2021-01-27 19:49:33 +00:00
|
|
|
LaunchConfigurationName="test_launch_configuration",
|
|
|
|
ImageId=EXAMPLE_AMI_ID,
|
|
|
|
InstanceType="t2.medium",
|
2019-07-18 18:30:07 +00:00
|
|
|
)
|
|
|
|
_ = client.create_auto_scaling_group(
|
|
|
|
AutoScalingGroupName="test_asg",
|
|
|
|
LaunchConfigurationName="test_launch_configuration",
|
|
|
|
MinSize=1,
|
|
|
|
MaxSize=20,
|
|
|
|
VPCZoneIdentifier=mocked_networking["subnet1"],
|
|
|
|
NewInstancesProtectedFromScaleIn=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
response = client.describe_auto_scaling_groups(AutoScalingGroupNames=["test_asg"])
|
|
|
|
original_instance_id = next(
|
|
|
|
instance["InstanceId"]
|
|
|
|
for instance in response["AutoScalingGroups"][0]["Instances"]
|
|
|
|
)
|
|
|
|
ec2_client = boto3.client("ec2", region_name="us-east-1")
|
|
|
|
ec2_client.terminate_instances(InstanceIds=[original_instance_id])
|
|
|
|
|
|
|
|
response = client.describe_auto_scaling_groups(AutoScalingGroupNames=["test_asg"])
|
|
|
|
replaced_instance_id = next(
|
|
|
|
instance["InstanceId"]
|
|
|
|
for instance in response["AutoScalingGroups"][0]["Instances"]
|
|
|
|
)
|
2023-06-09 11:16:59 +00:00
|
|
|
assert replaced_instance_id != original_instance_id
|
2020-04-13 00:50:01 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2021-09-01 17:50:53 +00:00
|
|
|
def test_attach_instances():
|
|
|
|
asg_client = boto3.client("autoscaling", region_name="us-east-1")
|
|
|
|
ec2_client = boto3.client("ec2", region_name="us-east-1")
|
|
|
|
|
|
|
|
kwargs = {
|
|
|
|
"KeyName": "foobar",
|
2022-05-26 16:04:36 +00:00
|
|
|
"ImageId": EXAMPLE_AMI_ID,
|
2021-09-01 17:50:53 +00:00
|
|
|
"MinCount": 1,
|
|
|
|
"MaxCount": 1,
|
|
|
|
"InstanceType": "c4.2xlarge",
|
|
|
|
"TagSpecifications": [
|
|
|
|
{"ResourceType": "instance", "Tags": [{"Key": "key", "Value": "val"}]},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
fake_instance = ec2_client.run_instances(**kwargs)["Instances"][0]
|
2021-10-18 19:44:29 +00:00
|
|
|
asg_client.create_launch_configuration(
|
2021-09-01 17:50:53 +00:00
|
|
|
LaunchConfigurationName="test_launch_configuration",
|
|
|
|
ImageId="ami-pytest",
|
|
|
|
InstanceType="t3.micro",
|
|
|
|
KeyName="foobar",
|
|
|
|
)
|
2021-10-18 19:44:29 +00:00
|
|
|
asg_client.create_auto_scaling_group(
|
2021-09-01 17:50:53 +00:00
|
|
|
AutoScalingGroupName="test_asg",
|
|
|
|
LaunchConfigurationName="test_launch_configuration",
|
|
|
|
MinSize=0,
|
|
|
|
MaxSize=1,
|
|
|
|
AvailabilityZones=[fake_instance["Placement"]["AvailabilityZone"]],
|
|
|
|
)
|
|
|
|
asg_client.attach_instances(
|
|
|
|
InstanceIds=[fake_instance["InstanceId"]], AutoScalingGroupName="test_asg"
|
|
|
|
)
|
|
|
|
response = asg_client.describe_auto_scaling_instances()
|
2023-06-09 11:16:59 +00:00
|
|
|
assert len(response["AutoScalingInstances"]) == 1
|
2021-09-01 17:50:53 +00:00
|
|
|
for instance in response["AutoScalingInstances"]:
|
2023-06-09 11:16:59 +00:00
|
|
|
assert instance["LaunchConfigurationName"] == "test_launch_configuration"
|
|
|
|
assert instance["AutoScalingGroupName"] == "test_asg"
|
|
|
|
assert instance["InstanceType"] == "c4.2xlarge"
|
2021-09-02 13:10:11 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2021-09-02 13:10:11 +00:00
|
|
|
def test_autoscaling_lifecyclehook():
|
|
|
|
mocked_networking = setup_networking()
|
|
|
|
client = boto3.client("autoscaling", region_name="us-east-1")
|
2021-10-18 19:44:29 +00:00
|
|
|
client.create_launch_configuration(
|
2021-09-02 13:10:11 +00:00
|
|
|
LaunchConfigurationName="test_launch_configuration",
|
|
|
|
ImageId="ami-pytest",
|
|
|
|
InstanceType="t3.micro",
|
|
|
|
KeyName="foobar",
|
|
|
|
)
|
2021-10-18 19:44:29 +00:00
|
|
|
client.create_auto_scaling_group(
|
2021-09-02 13:10:11 +00:00
|
|
|
AutoScalingGroupName="test_asg",
|
|
|
|
LaunchConfigurationName="test_launch_configuration",
|
|
|
|
MinSize=0,
|
|
|
|
MaxSize=1,
|
|
|
|
VPCZoneIdentifier=mocked_networking["subnet1"],
|
|
|
|
)
|
2021-10-18 19:44:29 +00:00
|
|
|
client.put_lifecycle_hook(
|
2021-09-02 13:10:11 +00:00
|
|
|
LifecycleHookName="test-lifecyclehook",
|
|
|
|
AutoScalingGroupName="test_asg",
|
|
|
|
LifecycleTransition="autoscaling:EC2_INSTANCE_TERMINATING",
|
|
|
|
)
|
|
|
|
|
|
|
|
response = client.describe_lifecycle_hooks(
|
|
|
|
AutoScalingGroupName="test_asg", LifecycleHookNames=["test-lifecyclehook"]
|
|
|
|
)
|
2023-06-09 11:16:59 +00:00
|
|
|
assert len(response["LifecycleHooks"]) == 1
|
2021-09-02 13:10:11 +00:00
|
|
|
for hook in response["LifecycleHooks"]:
|
2023-06-09 11:16:59 +00:00
|
|
|
assert hook["LifecycleHookName"] == "test-lifecyclehook"
|
|
|
|
assert hook["AutoScalingGroupName"] == "test_asg"
|
|
|
|
assert hook["LifecycleTransition"] == "autoscaling:EC2_INSTANCE_TERMINATING"
|
2021-09-02 13:10:11 +00:00
|
|
|
|
|
|
|
client.delete_lifecycle_hook(
|
|
|
|
LifecycleHookName="test-lifecyclehook", AutoScalingGroupName="test_asg"
|
|
|
|
)
|
|
|
|
|
|
|
|
response = client.describe_lifecycle_hooks(
|
|
|
|
AutoScalingGroupName="test_asg", LifecycleHookNames=["test-lifecyclehook"]
|
|
|
|
)
|
|
|
|
|
2023-06-09 11:16:59 +00:00
|
|
|
assert len(response["LifecycleHooks"]) == 0
|
2021-09-22 18:05:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("original,new", [(2, 1), (2, 3), (1, 5), (1, 1)])
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2022-03-29 21:46:06 +00:00
|
|
|
def test_set_desired_capacity_without_protection(original, new):
|
2021-09-22 18:05:28 +00:00
|
|
|
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]
|
2023-06-09 11:16:59 +00:00
|
|
|
assert group["DesiredCapacity"] == original
|
2021-09-22 18:05:28 +00:00
|
|
|
instances = client.describe_auto_scaling_instances()["AutoScalingInstances"]
|
2023-06-09 11:16:59 +00:00
|
|
|
assert len(instances) == original
|
2021-09-22 18:05:28 +00:00
|
|
|
|
|
|
|
client.update_auto_scaling_group(
|
|
|
|
AutoScalingGroupName="tester_group", DesiredCapacity=new
|
|
|
|
)
|
|
|
|
|
|
|
|
group = client.describe_auto_scaling_groups()["AutoScalingGroups"][0]
|
2023-06-09 11:16:59 +00:00
|
|
|
assert group["DesiredCapacity"] == new
|
2021-09-22 18:05:28 +00:00
|
|
|
instances = client.describe_auto_scaling_instances()["AutoScalingInstances"]
|
2023-06-09 11:16:59 +00:00
|
|
|
assert len(instances) == new
|
2022-04-21 14:19:36 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2022-04-21 14:19:36 +00:00
|
|
|
def test_create_template_with_block_device():
|
|
|
|
ec2_client = boto3.client("ec2", region_name="ap-southeast-2")
|
|
|
|
ec2_client.create_launch_template(
|
|
|
|
LaunchTemplateName="launchie",
|
|
|
|
LaunchTemplateData={
|
|
|
|
"ImageId": EXAMPLE_AMI_ID,
|
|
|
|
"BlockDeviceMappings": [
|
|
|
|
{
|
|
|
|
"DeviceName": "/dev/sda1",
|
|
|
|
"Ebs": {
|
|
|
|
"VolumeSize": 20,
|
|
|
|
"DeleteOnTermination": True,
|
|
|
|
"VolumeType": "gp3",
|
|
|
|
"Encrypted": True,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
ec2_client.run_instances(
|
|
|
|
MaxCount=1, MinCount=1, LaunchTemplate={"LaunchTemplateName": "launchie"}
|
|
|
|
)
|
|
|
|
ec2_client = boto3.client("ec2", region_name="ap-southeast-2")
|
|
|
|
volumes = ec2_client.describe_volumes()["Volumes"]
|
2022-04-22 15:40:30 +00:00
|
|
|
# The standard root volume
|
2023-06-09 11:16:59 +00:00
|
|
|
assert volumes[0]["VolumeType"] == "gp2"
|
|
|
|
assert volumes[0]["Size"] == 8
|
2022-04-22 15:40:30 +00:00
|
|
|
# Our Ebs-volume
|
2023-06-09 11:16:59 +00:00
|
|
|
assert volumes[1]["VolumeType"] == "gp3"
|
|
|
|
assert volumes[1]["Size"] == 20
|
2024-03-16 23:17:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_aws
|
|
|
|
def test_sets_created_time():
|
|
|
|
mocked_networking = setup_networking()
|
|
|
|
conn = boto3.client("autoscaling", region_name="us-east-1")
|
|
|
|
conn.create_launch_configuration(
|
|
|
|
LaunchConfigurationName="TestLC",
|
|
|
|
ImageId=EXAMPLE_AMI_ID,
|
|
|
|
InstanceType="t2.medium",
|
|
|
|
)
|
|
|
|
|
|
|
|
conn.create_auto_scaling_group(
|
|
|
|
AutoScalingGroupName="TestGroup1",
|
|
|
|
MinSize=1,
|
|
|
|
MaxSize=2,
|
|
|
|
LaunchConfigurationName="TestLC",
|
|
|
|
VPCZoneIdentifier=mocked_networking["subnet1"],
|
|
|
|
)
|
|
|
|
|
|
|
|
asgs = conn.describe_auto_scaling_groups()["AutoScalingGroups"]
|
|
|
|
assert len(asgs) == 1
|
|
|
|
assert asgs[0]["CreatedTime"] != datetime.strptime(
|
|
|
|
"2013-05-06T17:47:15.107Z", "%Y-%m-%dT%H:%M:%S.%f%z"
|
|
|
|
)
|