moto/tests/test_autoscaling/test_autoscaling.py

1415 lines
48 KiB
Python
Raw Normal View History

from __future__ import unicode_literals
2013-07-27 20:24:38 +00:00
import boto
2016-01-08 21:56:10 +00:00
import boto3
2015-12-14 03:41:17 +00:00
import boto.ec2.autoscale
2013-07-27 20:24:38 +00:00
from boto.ec2.autoscale.launchconfig import LaunchConfiguration
from boto.ec2.autoscale.group import AutoScalingGroup
from boto.ec2.autoscale import Tag
2015-12-14 03:41:17 +00:00
import boto.ec2.elb
2013-08-03 21:09:36 +00:00
import sure # noqa
from botocore.exceptions import ClientError
from nose.tools import assert_raises
2013-07-27 20:24:38 +00:00
2019-10-31 15:44:26 +00:00
from moto import (
mock_autoscaling,
mock_ec2_deprecated,
mock_elb_deprecated,
mock_elb,
mock_autoscaling_deprecated,
mock_ec2,
)
2013-07-27 20:24:38 +00:00
from tests.helpers import requires_boto_gte
2019-10-31 15:44:26 +00:00
from utils import (
setup_networking,
setup_networking_deprecated,
setup_instance_with_networking,
)
2013-07-27 20:24:38 +00:00
2017-02-16 03:35:45 +00:00
@mock_autoscaling_deprecated
@mock_elb_deprecated
2013-07-27 20:24:38 +00:00
def test_create_autoscaling_group():
mocked_networking = setup_networking_deprecated()
2019-10-31 15:44:26 +00:00
elb_conn = boto.ec2.elb.connect_to_region("us-east-1")
elb_conn.create_load_balancer("test_lb", zones=[], listeners=[(80, 8080, "http")])
2015-12-14 03:41:17 +00:00
2019-10-31 15:44:26 +00:00
conn = boto.ec2.autoscale.connect_to_region("us-east-1")
2013-07-27 20:24:38 +00:00
config = LaunchConfiguration(
2019-10-31 15:44:26 +00:00
name="tester", image_id="ami-abcd1234", instance_type="t2.medium"
2013-07-27 20:24:38 +00:00
)
conn.create_launch_configuration(config)
group = AutoScalingGroup(
2019-10-31 15:44:26 +00:00
name="tester_group",
availability_zones=["us-east-1a", "us-east-1b"],
2013-08-03 21:09:36 +00:00
default_cooldown=60,
2013-07-27 20:24:38 +00:00
desired_capacity=2,
2013-08-03 21:09:36 +00:00
health_check_period=100,
health_check_type="EC2",
2013-07-27 20:24:38 +00:00
max_size=2,
min_size=2,
launch_config=config,
2013-08-03 21:09:36 +00:00
load_balancers=["test_lb"],
placement_group="test_placement",
vpc_zone_identifier="{subnet1},{subnet2}".format(
2019-10-31 15:44:26 +00:00
subnet1=mocked_networking["subnet1"], subnet2=mocked_networking["subnet2"]
),
2013-08-03 21:09:36 +00:00
termination_policies=["OldestInstance", "NewestInstance"],
2019-10-31 15:44:26 +00:00
tags=[
Tag(
resource_id="tester_group",
key="test_key",
value="test_value",
propagate_at_launch=True,
)
],
2013-07-27 20:24:38 +00:00
)
conn.create_auto_scaling_group(group)
group = conn.get_all_groups()[0]
2019-10-31 15:44:26 +00:00
group.name.should.equal("tester_group")
set(group.availability_zones).should.equal(set(["us-east-1a", "us-east-1b"]))
2013-07-27 20:24:38 +00:00
group.desired_capacity.should.equal(2)
group.max_size.should.equal(2)
group.min_size.should.equal(2)
group.instances.should.have.length_of(2)
2019-10-31 15:44:26 +00:00
group.vpc_zone_identifier.should.equal(
"{subnet1},{subnet2}".format(
subnet1=mocked_networking["subnet1"], subnet2=mocked_networking["subnet2"]
)
)
group.launch_config_name.should.equal("tester")
2013-08-03 21:09:36 +00:00
group.default_cooldown.should.equal(60)
group.health_check_period.should.equal(100)
group.health_check_type.should.equal("EC2")
list(group.load_balancers).should.equal(["test_lb"])
group.placement_group.should.equal("test_placement")
2019-10-31 15:44:26 +00:00
list(group.termination_policies).should.equal(["OldestInstance", "NewestInstance"])
len(list(group.tags)).should.equal(1)
tag = list(group.tags)[0]
2019-10-31 15:44:26 +00:00
tag.resource_id.should.equal("tester_group")
tag.key.should.equal("test_key")
tag.value.should.equal("test_value")
tag.propagate_at_launch.should.equal(True)
2013-07-27 20:24:38 +00:00
2017-02-16 03:35:45 +00:00
@mock_autoscaling_deprecated
2013-07-27 20:24:38 +00:00
def test_create_autoscaling_groups_defaults():
""" Test with the minimum inputs and check that all of the proper defaults
are assigned for the other attributes """
mocked_networking = setup_networking_deprecated()
2013-07-27 20:24:38 +00:00
conn = boto.connect_autoscale()
config = LaunchConfiguration(
2019-10-31 15:44:26 +00:00
name="tester", image_id="ami-abcd1234", instance_type="t2.medium"
2013-07-27 20:24:38 +00:00
)
conn.create_launch_configuration(config)
group = AutoScalingGroup(
2019-10-31 15:44:26 +00:00
name="tester_group",
2013-07-27 20:24:38 +00:00
max_size=2,
min_size=2,
launch_config=config,
2019-10-31 15:44:26 +00:00
vpc_zone_identifier=mocked_networking["subnet1"],
2013-07-27 20:24:38 +00:00
)
conn.create_auto_scaling_group(group)
group = conn.get_all_groups()[0]
2019-10-31 15:44:26 +00:00
group.name.should.equal("tester_group")
2013-07-27 20:24:38 +00:00
group.max_size.should.equal(2)
group.min_size.should.equal(2)
2019-10-31 15:44:26 +00:00
group.launch_config_name.should.equal("tester")
2013-07-27 20:24:38 +00:00
# Defaults
2019-10-31 15:44:26 +00:00
list(group.availability_zones).should.equal(["us-east-1a"]) # subnet1
2013-07-27 20:24:38 +00:00
group.desired_capacity.should.equal(2)
2019-10-31 15:44:26 +00:00
group.vpc_zone_identifier.should.equal(mocked_networking["subnet1"])
2013-08-03 21:09:36 +00:00
group.default_cooldown.should.equal(300)
2016-01-08 21:56:10 +00:00
group.health_check_period.should.equal(300)
2013-08-03 21:09:36 +00:00
group.health_check_type.should.equal("EC2")
list(group.load_balancers).should.equal([])
group.placement_group.should.equal(None)
list(group.termination_policies).should.equal([])
2015-10-06 16:21:26 +00:00
list(group.tags).should.equal([])
2013-07-27 20:24:38 +00:00
2017-05-11 01:58:42 +00:00
@mock_autoscaling
def test_list_many_autoscaling_groups():
mocked_networking = setup_networking()
2019-10-31 15:44:26 +00:00
conn = boto3.client("autoscaling", region_name="us-east-1")
conn.create_launch_configuration(LaunchConfigurationName="TestLC")
2017-05-11 01:58:42 +00:00
for i in range(51):
2019-10-31 15:44:26 +00:00
conn.create_auto_scaling_group(
AutoScalingGroupName="TestGroup%d" % i,
MinSize=1,
MaxSize=2,
LaunchConfigurationName="TestLC",
VPCZoneIdentifier=mocked_networking["subnet1"],
)
2017-05-11 01:58:42 +00:00
response = conn.describe_auto_scaling_groups()
groups = response["AutoScalingGroups"]
marker = response["NextToken"]
groups.should.have.length_of(50)
2019-10-31 15:44:26 +00:00
marker.should.equal(groups[-1]["AutoScalingGroupName"])
2017-05-11 01:58:42 +00:00
response2 = conn.describe_auto_scaling_groups(NextToken=marker)
groups.extend(response2["AutoScalingGroups"])
groups.should.have.length_of(51)
2019-10-31 15:44:26 +00:00
assert "NextToken" not in response2.keys()
2017-05-11 01:58:42 +00:00
2017-06-09 20:10:00 +00:00
@mock_autoscaling
@mock_ec2
def test_list_many_autoscaling_groups():
mocked_networking = setup_networking()
2019-10-31 15:44:26 +00:00
conn = boto3.client("autoscaling", region_name="us-east-1")
conn.create_launch_configuration(LaunchConfigurationName="TestLC")
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",
}
],
VPCZoneIdentifier=mocked_networking["subnet1"],
)
ec2 = boto3.client("ec2", region_name="us-east-1")
2017-06-09 20:10:00 +00:00
instances = ec2.describe_instances()
2019-10-31 15:44:26 +00:00
tags = instances["Reservations"][0]["Instances"][0]["Tags"]
tags.should.contain({"Value": "TestTagValue1", "Key": "TestTagKey1"})
tags.should.contain({"Value": "TestGroup1", "Key": "aws:autoscaling:groupName"})
2017-05-11 01:58:42 +00:00
2017-02-16 03:35:45 +00:00
@mock_autoscaling_deprecated
2013-07-27 20:24:38 +00:00
def test_autoscaling_group_describe_filter():
mocked_networking = setup_networking_deprecated()
2013-07-27 20:24:38 +00:00
conn = boto.connect_autoscale()
config = LaunchConfiguration(
2019-10-31 15:44:26 +00:00
name="tester", image_id="ami-abcd1234", instance_type="t2.medium"
2013-07-27 20:24:38 +00:00
)
conn.create_launch_configuration(config)
group = AutoScalingGroup(
2019-10-31 15:44:26 +00:00
name="tester_group",
2013-07-27 20:24:38 +00:00
max_size=2,
min_size=2,
launch_config=config,
2019-10-31 15:44:26 +00:00
vpc_zone_identifier=mocked_networking["subnet1"],
2013-07-27 20:24:38 +00:00
)
conn.create_auto_scaling_group(group)
2019-10-31 15:44:26 +00:00
group.name = "tester_group2"
2013-07-27 20:24:38 +00:00
conn.create_auto_scaling_group(group)
2019-10-31 15:44:26 +00:00
group.name = "tester_group3"
2013-07-27 20:24:38 +00:00
conn.create_auto_scaling_group(group)
2019-10-31 15:44:26 +00:00
conn.get_all_groups(names=["tester_group", "tester_group2"]).should.have.length_of(
2
)
2013-07-27 20:24:38 +00:00
conn.get_all_groups().should.have.length_of(3)
2017-02-16 03:35:45 +00:00
@mock_autoscaling_deprecated
2013-07-27 20:24:38 +00:00
def test_autoscaling_update():
mocked_networking = setup_networking_deprecated()
2013-07-27 20:24:38 +00:00
conn = boto.connect_autoscale()
config = LaunchConfiguration(
2019-10-31 15:44:26 +00:00
name="tester", image_id="ami-abcd1234", instance_type="t2.medium"
2013-07-27 20:24:38 +00:00
)
conn.create_launch_configuration(config)
group = AutoScalingGroup(
2019-10-31 15:44:26 +00:00
name="tester_group",
2013-07-27 20:24:38 +00:00
desired_capacity=2,
max_size=2,
min_size=2,
launch_config=config,
2019-10-31 15:44:26 +00:00
vpc_zone_identifier=mocked_networking["subnet1"],
2013-07-27 20:24:38 +00:00
)
conn.create_auto_scaling_group(group)
group = conn.get_all_groups()[0]
2019-10-31 15:44:26 +00:00
group.availability_zones.should.equal(["us-east-1a"])
group.vpc_zone_identifier.should.equal(mocked_networking["subnet1"])
2013-07-27 20:24:38 +00:00
2019-10-31 15:44:26 +00:00
group.availability_zones = ["us-east-1b"]
group.vpc_zone_identifier = mocked_networking["subnet2"]
2013-07-27 20:24:38 +00:00
group.update()
group = conn.get_all_groups()[0]
2019-10-31 15:44:26 +00:00
group.availability_zones.should.equal(["us-east-1b"])
group.vpc_zone_identifier.should.equal(mocked_networking["subnet2"])
2013-07-27 20:24:38 +00:00
2017-02-16 03:35:45 +00:00
@mock_autoscaling_deprecated
def test_autoscaling_tags_update():
mocked_networking = setup_networking_deprecated()
conn = boto.connect_autoscale()
config = LaunchConfiguration(
2019-10-31 15:44:26 +00:00
name="tester", image_id="ami-abcd1234", instance_type="t2.medium"
)
conn.create_launch_configuration(config)
group = AutoScalingGroup(
2019-10-31 15:44:26 +00:00
name="tester_group",
availability_zones=["us-east-1a"],
desired_capacity=2,
max_size=2,
min_size=2,
launch_config=config,
2019-10-31 15:44:26 +00:00
tags=[
Tag(
resource_id="tester_group",
key="test_key",
value="test_value",
propagate_at_launch=True,
)
],
vpc_zone_identifier=mocked_networking["subnet1"],
)
conn.create_auto_scaling_group(group)
2019-10-31 15:44:26 +00:00
conn.create_or_update_tags(
tags=[
Tag(
resource_id="tester_group",
key="test_key",
value="new_test_value",
propagate_at_launch=True,
),
Tag(
resource_id="tester_group",
key="test_key2",
value="test_value2",
propagate_at_launch=True,
),
]
)
group = conn.get_all_groups()[0]
group.tags.should.have.length_of(2)
2017-02-16 03:35:45 +00:00
@mock_autoscaling_deprecated
2013-07-27 20:24:38 +00:00
def test_autoscaling_group_delete():
mocked_networking = setup_networking_deprecated()
2013-07-27 20:24:38 +00:00
conn = boto.connect_autoscale()
config = LaunchConfiguration(
2019-10-31 15:44:26 +00:00
name="tester", image_id="ami-abcd1234", instance_type="t2.medium"
2013-07-27 20:24:38 +00:00
)
conn.create_launch_configuration(config)
group = AutoScalingGroup(
2019-10-31 15:44:26 +00:00
name="tester_group",
2013-07-27 20:24:38 +00:00
max_size=2,
min_size=2,
launch_config=config,
2019-10-31 15:44:26 +00:00
vpc_zone_identifier=mocked_networking["subnet1"],
2013-07-27 20:24:38 +00:00
)
conn.create_auto_scaling_group(group)
conn.get_all_groups().should.have.length_of(1)
2019-10-31 15:44:26 +00:00
conn.delete_auto_scaling_group("tester_group")
2013-07-27 20:24:38 +00:00
conn.get_all_groups().should.have.length_of(0)
2017-02-16 03:35:45 +00:00
@mock_ec2_deprecated
@mock_autoscaling_deprecated
2013-07-27 20:24:38 +00:00
def test_autoscaling_group_describe_instances():
mocked_networking = setup_networking_deprecated()
2019-10-31 15:44:26 +00:00
conn = boto.ec2.autoscale.connect_to_region("us-east-1")
2013-07-27 20:24:38 +00:00
config = LaunchConfiguration(
2019-10-31 15:44:26 +00:00
name="tester", image_id="ami-abcd1234", instance_type="t2.medium"
2013-07-27 20:24:38 +00:00
)
conn.create_launch_configuration(config)
group = AutoScalingGroup(
2019-10-31 15:44:26 +00:00
name="tester_group",
2013-07-27 20:24:38 +00:00
max_size=2,
min_size=2,
launch_config=config,
2019-10-31 15:44:26 +00:00
vpc_zone_identifier=mocked_networking["subnet1"],
2013-07-27 20:24:38 +00:00
)
conn.create_auto_scaling_group(group)
instances = list(conn.get_all_autoscaling_instances())
instances.should.have.length_of(2)
2019-10-31 15:44:26 +00:00
instances[0].launch_config_name.should.equal("tester")
instances[0].health_status.should.equal("Healthy")
2013-07-27 20:24:38 +00:00
autoscale_instance_ids = [instance.instance_id for instance in instances]
2019-10-31 15:44:26 +00:00
ec2_conn = boto.ec2.connect_to_region("us-east-1")
2013-07-27 20:24:38 +00:00
reservations = ec2_conn.get_all_instances()
instances = reservations[0].instances
instances.should.have.length_of(2)
instance_ids = [instance.id for instance in instances]
set(autoscale_instance_ids).should.equal(set(instance_ids))
2015-08-04 01:52:43 +00:00
instances[0].instance_type.should.equal("t2.medium")
2013-07-27 20:24:38 +00:00
@requires_boto_gte("2.8")
2017-02-16 03:35:45 +00:00
@mock_autoscaling_deprecated
2013-07-27 20:24:38 +00:00
def test_set_desired_capacity_up():
mocked_networking = setup_networking_deprecated()
2013-07-27 20:24:38 +00:00
conn = boto.connect_autoscale()
config = LaunchConfiguration(
2019-10-31 15:44:26 +00:00
name="tester", image_id="ami-abcd1234", instance_type="t2.medium"
2013-07-27 20:24:38 +00:00
)
conn.create_launch_configuration(config)
group = AutoScalingGroup(
2019-10-31 15:44:26 +00:00
name="tester_group",
availability_zones=["us-east-1a"],
2013-07-27 20:24:38 +00:00
desired_capacity=2,
max_size=2,
min_size=2,
launch_config=config,
2019-10-31 15:44:26 +00:00
vpc_zone_identifier=mocked_networking["subnet1"],
2013-07-27 20:24:38 +00:00
)
conn.create_auto_scaling_group(group)
group = conn.get_all_groups()[0]
group.desired_capacity.should.equal(2)
instances = list(conn.get_all_autoscaling_instances())
instances.should.have.length_of(2)
conn.set_desired_capacity("tester_group", 3)
group = conn.get_all_groups()[0]
group.desired_capacity.should.equal(3)
instances = list(conn.get_all_autoscaling_instances())
instances.should.have.length_of(3)
@requires_boto_gte("2.8")
2017-02-16 03:35:45 +00:00
@mock_autoscaling_deprecated
2013-07-27 20:24:38 +00:00
def test_set_desired_capacity_down():
mocked_networking = setup_networking_deprecated()
2013-07-27 20:24:38 +00:00
conn = boto.connect_autoscale()
config = LaunchConfiguration(
2019-10-31 15:44:26 +00:00
name="tester", image_id="ami-abcd1234", instance_type="t2.medium"
2013-07-27 20:24:38 +00:00
)
conn.create_launch_configuration(config)
group = AutoScalingGroup(
2019-10-31 15:44:26 +00:00
name="tester_group",
availability_zones=["us-east-1a"],
2013-07-27 20:24:38 +00:00
desired_capacity=2,
max_size=2,
min_size=2,
launch_config=config,
2019-10-31 15:44:26 +00:00
vpc_zone_identifier=mocked_networking["subnet1"],
2013-07-27 20:24:38 +00:00
)
conn.create_auto_scaling_group(group)
group = conn.get_all_groups()[0]
group.desired_capacity.should.equal(2)
instances = list(conn.get_all_autoscaling_instances())
instances.should.have.length_of(2)
conn.set_desired_capacity("tester_group", 1)
group = conn.get_all_groups()[0]
group.desired_capacity.should.equal(1)
instances = list(conn.get_all_autoscaling_instances())
instances.should.have.length_of(1)
@requires_boto_gte("2.8")
2017-02-16 03:35:45 +00:00
@mock_autoscaling_deprecated
2013-07-27 20:24:38 +00:00
def test_set_desired_capacity_the_same():
mocked_networking = setup_networking_deprecated()
2013-07-27 20:24:38 +00:00
conn = boto.connect_autoscale()
config = LaunchConfiguration(
2019-10-31 15:44:26 +00:00
name="tester", image_id="ami-abcd1234", instance_type="t2.medium"
2013-07-27 20:24:38 +00:00
)
conn.create_launch_configuration(config)
group = AutoScalingGroup(
2019-10-31 15:44:26 +00:00
name="tester_group",
availability_zones=["us-east-1a"],
2013-07-27 20:24:38 +00:00
desired_capacity=2,
max_size=2,
min_size=2,
launch_config=config,
2019-10-31 15:44:26 +00:00
vpc_zone_identifier=mocked_networking["subnet1"],
2013-07-27 20:24:38 +00:00
)
conn.create_auto_scaling_group(group)
group = conn.get_all_groups()[0]
group.desired_capacity.should.equal(2)
instances = list(conn.get_all_autoscaling_instances())
instances.should.have.length_of(2)
conn.set_desired_capacity("tester_group", 2)
group = conn.get_all_groups()[0]
group.desired_capacity.should.equal(2)
instances = list(conn.get_all_autoscaling_instances())
instances.should.have.length_of(2)
2017-02-24 02:37:43 +00:00
2017-02-16 03:35:45 +00:00
@mock_autoscaling_deprecated
@mock_elb_deprecated
def test_autoscaling_group_with_elb():
mocked_networking = setup_networking_deprecated()
elb_conn = boto.connect_elb()
2019-10-31 15:44:26 +00:00
zones = ["us-east-1a", "us-east-1b"]
ports = [(80, 8080, "http"), (443, 8443, "tcp")]
lb = elb_conn.create_load_balancer("my-lb", zones, ports)
instances_health = elb_conn.describe_instance_health("my-lb")
instances_health.should.be.empty
conn = boto.connect_autoscale()
config = LaunchConfiguration(
2019-10-31 15:44:26 +00:00
name="tester", image_id="ami-abcd1234", instance_type="t2.medium"
)
conn.create_launch_configuration(config)
group = AutoScalingGroup(
2019-10-31 15:44:26 +00:00
name="tester_group",
max_size=2,
min_size=2,
launch_config=config,
load_balancers=["my-lb"],
2019-10-31 15:44:26 +00:00
vpc_zone_identifier=mocked_networking["subnet1"],
)
conn.create_auto_scaling_group(group)
group = conn.get_all_groups()[0]
elb = elb_conn.get_all_load_balancers()[0]
group.desired_capacity.should.equal(2)
elb.instances.should.have.length_of(2)
2019-10-31 15:44:26 +00:00
autoscale_instance_ids = set(instance.instance_id for instance in group.instances)
elb_instace_ids = set(instance.id for instance in elb.instances)
autoscale_instance_ids.should.equal(elb_instace_ids)
conn.set_desired_capacity("tester_group", 3)
group = conn.get_all_groups()[0]
elb = elb_conn.get_all_load_balancers()[0]
group.desired_capacity.should.equal(3)
elb.instances.should.have.length_of(3)
2019-10-31 15:44:26 +00:00
autoscale_instance_ids = set(instance.instance_id for instance in group.instances)
elb_instace_ids = set(instance.id for instance in elb.instances)
autoscale_instance_ids.should.equal(elb_instace_ids)
2019-10-31 15:44:26 +00:00
conn.delete_auto_scaling_group("tester_group")
conn.get_all_groups().should.have.length_of(0)
elb = elb_conn.get_all_load_balancers()[0]
elb.instances.should.have.length_of(0)
2016-01-08 21:56:10 +00:00
2019-10-31 15:44:26 +00:00
"""
2016-01-08 21:56:10 +00:00
Boto3
2019-10-31 15:44:26 +00:00
"""
2016-01-08 21:56:10 +00:00
@mock_autoscaling
@mock_elb
def test_describe_load_balancers():
mocked_networking = setup_networking()
INSTANCE_COUNT = 2
2019-10-31 15:44:26 +00:00
elb_client = boto3.client("elb", region_name="us-east-1")
elb_client.create_load_balancer(
2019-10-31 15:44:26 +00:00
LoadBalancerName="my-lb",
Listeners=[{"Protocol": "tcp", "LoadBalancerPort": 80, "InstancePort": 8080}],
AvailabilityZones=["us-east-1a", "us-east-1b"],
)
2019-10-31 15:44:26 +00:00
client = boto3.client("autoscaling", region_name="us-east-1")
client.create_launch_configuration(
2019-10-31 15:44:26 +00:00
LaunchConfigurationName="test_launch_configuration"
)
client.create_auto_scaling_group(
2019-10-31 15:44:26 +00:00
AutoScalingGroupName="test_asg",
LaunchConfigurationName="test_launch_configuration",
LoadBalancerNames=["my-lb"],
MinSize=0,
MaxSize=INSTANCE_COUNT,
DesiredCapacity=INSTANCE_COUNT,
2019-10-31 15:44:26 +00:00
Tags=[
{
"ResourceId": "test_asg",
"Key": "test_key",
"Value": "test_value",
"PropagateAtLaunch": True,
}
],
VPCZoneIdentifier=mocked_networking["subnet1"],
)
2019-10-31 15:44:26 +00:00
response = client.describe_load_balancers(AutoScalingGroupName="test_asg")
assert response["ResponseMetadata"]["RequestId"]
list(response["LoadBalancers"]).should.have.length_of(1)
response["LoadBalancers"][0]["LoadBalancerName"].should.equal("my-lb")
@mock_autoscaling
@mock_elb
def test_create_elb_and_autoscaling_group_no_relationship():
mocked_networking = setup_networking()
INSTANCE_COUNT = 2
2019-10-31 15:44:26 +00:00
ELB_NAME = "my-elb"
2019-10-31 15:44:26 +00:00
elb_client = boto3.client("elb", region_name="us-east-1")
elb_client.create_load_balancer(
LoadBalancerName=ELB_NAME,
2019-10-31 15:44:26 +00:00
Listeners=[{"Protocol": "tcp", "LoadBalancerPort": 80, "InstancePort": 8080}],
AvailabilityZones=["us-east-1a", "us-east-1b"],
)
2019-10-31 15:44:26 +00:00
client = boto3.client("autoscaling", region_name="us-east-1")
client.create_launch_configuration(
2019-10-31 15:44:26 +00:00
LaunchConfigurationName="test_launch_configuration"
)
client.create_auto_scaling_group(
2019-10-31 15:44:26 +00:00
AutoScalingGroupName="test_asg",
LaunchConfigurationName="test_launch_configuration",
MinSize=0,
MaxSize=INSTANCE_COUNT,
DesiredCapacity=INSTANCE_COUNT,
2019-10-31 15:44:26 +00:00
VPCZoneIdentifier=mocked_networking["subnet1"],
)
# autoscaling group and elb should have no relationship
2019-10-31 15:44:26 +00:00
response = client.describe_load_balancers(AutoScalingGroupName="test_asg")
list(response["LoadBalancers"]).should.have.length_of(0)
response = elb_client.describe_load_balancers(LoadBalancerNames=[ELB_NAME])
list(response["LoadBalancerDescriptions"][0]["Instances"]).should.have.length_of(0)
@mock_autoscaling
@mock_elb
def test_attach_load_balancer():
mocked_networking = setup_networking()
INSTANCE_COUNT = 2
2019-10-31 15:44:26 +00:00
elb_client = boto3.client("elb", region_name="us-east-1")
elb_client.create_load_balancer(
2019-10-31 15:44:26 +00:00
LoadBalancerName="my-lb",
Listeners=[{"Protocol": "tcp", "LoadBalancerPort": 80, "InstancePort": 8080}],
AvailabilityZones=["us-east-1a", "us-east-1b"],
)
2019-10-31 15:44:26 +00:00
client = boto3.client("autoscaling", region_name="us-east-1")
client.create_launch_configuration(
2019-10-31 15:44:26 +00:00
LaunchConfigurationName="test_launch_configuration"
)
client.create_auto_scaling_group(
2019-10-31 15:44:26 +00:00
AutoScalingGroupName="test_asg",
LaunchConfigurationName="test_launch_configuration",
MinSize=0,
MaxSize=INSTANCE_COUNT,
DesiredCapacity=INSTANCE_COUNT,
2019-10-31 15:44:26 +00:00
Tags=[
{
"ResourceId": "test_asg",
"Key": "test_key",
"Value": "test_value",
"PropagateAtLaunch": True,
}
],
VPCZoneIdentifier=mocked_networking["subnet1"],
)
response = client.attach_load_balancers(
2019-10-31 15:44:26 +00:00
AutoScalingGroupName="test_asg", LoadBalancerNames=["my-lb"]
)
2019-10-31 15:44:26 +00:00
response["ResponseMetadata"]["HTTPStatusCode"].should.equal(200)
2019-10-31 15:44:26 +00:00
response = elb_client.describe_load_balancers(LoadBalancerNames=["my-lb"])
list(response["LoadBalancerDescriptions"][0]["Instances"]).should.have.length_of(
INSTANCE_COUNT
)
2019-10-31 15:44:26 +00:00
response = client.describe_auto_scaling_groups(AutoScalingGroupNames=["test_asg"])
list(response["AutoScalingGroups"][0]["LoadBalancerNames"]).should.have.length_of(1)
@mock_autoscaling
@mock_elb
def test_detach_load_balancer():
mocked_networking = setup_networking()
INSTANCE_COUNT = 2
2019-10-31 15:44:26 +00:00
elb_client = boto3.client("elb", region_name="us-east-1")
elb_client.create_load_balancer(
2019-10-31 15:44:26 +00:00
LoadBalancerName="my-lb",
Listeners=[{"Protocol": "tcp", "LoadBalancerPort": 80, "InstancePort": 8080}],
AvailabilityZones=["us-east-1a", "us-east-1b"],
)
2019-10-31 15:44:26 +00:00
client = boto3.client("autoscaling", region_name="us-east-1")
client.create_launch_configuration(
2019-10-31 15:44:26 +00:00
LaunchConfigurationName="test_launch_configuration"
)
client.create_auto_scaling_group(
2019-10-31 15:44:26 +00:00
AutoScalingGroupName="test_asg",
LaunchConfigurationName="test_launch_configuration",
LoadBalancerNames=["my-lb"],
MinSize=0,
MaxSize=INSTANCE_COUNT,
DesiredCapacity=INSTANCE_COUNT,
2019-10-31 15:44:26 +00:00
Tags=[
{
"ResourceId": "test_asg",
"Key": "test_key",
"Value": "test_value",
"PropagateAtLaunch": True,
}
],
VPCZoneIdentifier=mocked_networking["subnet1"],
)
response = client.detach_load_balancers(
2019-10-31 15:44:26 +00:00
AutoScalingGroupName="test_asg", LoadBalancerNames=["my-lb"]
)
2019-10-31 15:44:26 +00:00
response["ResponseMetadata"]["HTTPStatusCode"].should.equal(200)
response = elb_client.describe_load_balancers(LoadBalancerNames=["my-lb"])
list(response["LoadBalancerDescriptions"][0]["Instances"]).should.have.length_of(0)
2019-10-31 15:44:26 +00:00
response = client.describe_load_balancers(AutoScalingGroupName="test_asg")
list(response["LoadBalancers"]).should.have.length_of(0)
2016-01-08 21:56:10 +00:00
@mock_autoscaling
def test_create_autoscaling_group_boto3():
mocked_networking = setup_networking()
2019-10-31 15:44:26 +00:00
client = boto3.client("autoscaling", region_name="us-east-1")
2017-02-24 02:37:43 +00:00
_ = client.create_launch_configuration(
2019-10-31 15:44:26 +00:00
LaunchConfigurationName="test_launch_configuration"
2017-02-24 02:37:43 +00:00
)
response = client.create_auto_scaling_group(
2019-10-31 15:44:26 +00:00
AutoScalingGroupName="test_asg",
LaunchConfigurationName="test_launch_configuration",
2017-02-24 02:37:43 +00:00
MinSize=0,
MaxSize=20,
DesiredCapacity=5,
Tags=[
2019-10-31 15:44:26 +00:00
{
"ResourceId": "test_asg",
"ResourceType": "auto-scaling-group",
"Key": "propogated-tag-key",
"Value": "propogate-tag-value",
"PropagateAtLaunch": True,
},
{
"ResourceId": "test_asg",
"ResourceType": "auto-scaling-group",
"Key": "not-propogated-tag-key",
"Value": "not-propogate-tag-value",
"PropagateAtLaunch": False,
},
],
VPCZoneIdentifier=mocked_networking["subnet1"],
NewInstancesProtectedFromScaleIn=False,
2017-02-24 02:37:43 +00:00
)
2019-10-31 15:44:26 +00:00
response["ResponseMetadata"]["HTTPStatusCode"].should.equal(200)
2016-01-08 21:56:10 +00:00
@mock_autoscaling
def test_create_autoscaling_group_from_instance():
2019-10-31 15:44:26 +00:00
autoscaling_group_name = "test_asg"
image_id = "ami-0cc293023f983ed53"
instance_type = "t2.micro"
2019-10-31 15:44:26 +00:00
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,
2019-10-31 15:44:26 +00:00
InstanceId=mocked_instance_with_networking["instance"],
MinSize=1,
MaxSize=3,
DesiredCapacity=2,
Tags=[
2019-10-31 15:44:26 +00:00
{
"ResourceId": "test_asg",
"ResourceType": "auto-scaling-group",
"Key": "propogated-tag-key",
"Value": "propogate-tag-value",
"PropagateAtLaunch": True,
},
{
"ResourceId": "test_asg",
"ResourceType": "auto-scaling-group",
"Key": "not-propogated-tag-key",
"Value": "not-propogate-tag-value",
"PropagateAtLaunch": False,
},
],
VPCZoneIdentifier=mocked_instance_with_networking["subnet1"],
NewInstancesProtectedFromScaleIn=False,
)
2019-10-31 15:44:26 +00:00
response["ResponseMetadata"]["HTTPStatusCode"].should.equal(200)
describe_launch_configurations_response = client.describe_launch_configurations()
2019-10-31 15:44:26 +00:00
describe_launch_configurations_response[
"LaunchConfigurations"
].should.have.length_of(1)
launch_configuration_from_instance = describe_launch_configurations_response[
"LaunchConfigurations"
][0]
launch_configuration_from_instance["LaunchConfigurationName"].should.equal(
"test_asg"
)
launch_configuration_from_instance["ImageId"].should.equal(image_id)
launch_configuration_from_instance["InstanceType"].should.equal(instance_type)
@mock_autoscaling
def test_create_autoscaling_group_from_invalid_instance_id():
2019-10-31 15:44:26 +00:00
invalid_instance_id = "invalid_instance"
mocked_networking = setup_networking()
2019-10-31 15:44:26 +00:00
client = boto3.client("autoscaling", region_name="us-east-1")
with assert_raises(ClientError) as ex:
client.create_auto_scaling_group(
2019-10-31 15:44:26 +00:00
AutoScalingGroupName="test_asg",
InstanceId=invalid_instance_id,
MinSize=9,
MaxSize=15,
DesiredCapacity=12,
2019-10-31 15:44:26 +00:00
VPCZoneIdentifier=mocked_networking["subnet1"],
NewInstancesProtectedFromScaleIn=False,
)
2019-10-31 15:44:26 +00:00
ex.exception.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400)
ex.exception.response["Error"]["Code"].should.equal("ValidationError")
ex.exception.response["Error"]["Message"].should.equal(
"Instance [{0}] is invalid.".format(invalid_instance_id)
)
2016-01-08 21:56:10 +00:00
@mock_autoscaling
def test_describe_autoscaling_groups_boto3():
mocked_networking = setup_networking()
2019-10-31 15:44:26 +00:00
client = boto3.client("autoscaling", region_name="us-east-1")
2017-02-24 02:37:43 +00:00
_ = client.create_launch_configuration(
2019-10-31 15:44:26 +00:00
LaunchConfigurationName="test_launch_configuration"
2017-02-24 02:37:43 +00:00
)
_ = client.create_auto_scaling_group(
2019-10-31 15:44:26 +00:00
AutoScalingGroupName="test_asg",
LaunchConfigurationName="test_launch_configuration",
2017-02-24 02:37:43 +00:00
MinSize=0,
MaxSize=20,
DesiredCapacity=5,
2019-10-31 15:44:26 +00:00
VPCZoneIdentifier=mocked_networking["subnet1"],
NewInstancesProtectedFromScaleIn=True,
2017-02-24 02:37:43 +00:00
)
2019-10-31 15:44:26 +00:00
response = client.describe_auto_scaling_groups(AutoScalingGroupNames=["test_asg"])
response["ResponseMetadata"]["HTTPStatusCode"].should.equal(200)
group = response["AutoScalingGroups"][0]
group["AutoScalingGroupName"].should.equal("test_asg")
group["AvailabilityZones"].should.equal(["us-east-1a"])
group["VPCZoneIdentifier"].should.equal(mocked_networking["subnet1"])
group["NewInstancesProtectedFromScaleIn"].should.equal(True)
for instance in group["Instances"]:
instance["AvailabilityZone"].should.equal("us-east-1a")
instance["ProtectedFromScaleIn"].should.equal(True)
@mock_autoscaling
def test_describe_autoscaling_instances_boto3():
mocked_networking = setup_networking()
2019-10-31 15:44:26 +00:00
client = boto3.client("autoscaling", region_name="us-east-1")
_ = client.create_launch_configuration(
2019-10-31 15:44:26 +00:00
LaunchConfigurationName="test_launch_configuration"
)
_ = client.create_auto_scaling_group(
2019-10-31 15:44:26 +00:00
AutoScalingGroupName="test_asg",
LaunchConfigurationName="test_launch_configuration",
MinSize=0,
MaxSize=20,
DesiredCapacity=5,
2019-10-31 15:44:26 +00:00
VPCZoneIdentifier=mocked_networking["subnet1"],
NewInstancesProtectedFromScaleIn=True,
)
2019-10-31 15:44:26 +00:00
response = client.describe_auto_scaling_groups(AutoScalingGroupNames=["test_asg"])
instance_ids = [
2019-10-31 15:44:26 +00:00
instance["InstanceId"]
for instance in response["AutoScalingGroups"][0]["Instances"]
]
response = client.describe_auto_scaling_instances(InstanceIds=instance_ids)
2019-10-31 15:44:26 +00:00
for instance in response["AutoScalingInstances"]:
instance["AutoScalingGroupName"].should.equal("test_asg")
instance["AvailabilityZone"].should.equal("us-east-1a")
instance["ProtectedFromScaleIn"].should.equal(True)
@mock_autoscaling
def test_update_autoscaling_group_boto3():
mocked_networking = setup_networking()
2019-10-31 15:44:26 +00:00
client = boto3.client("autoscaling", region_name="us-east-1")
_ = client.create_launch_configuration(
2019-10-31 15:44:26 +00:00
LaunchConfigurationName="test_launch_configuration"
)
_ = client.create_auto_scaling_group(
2019-10-31 15:44:26 +00:00
AutoScalingGroupName="test_asg",
LaunchConfigurationName="test_launch_configuration",
MinSize=0,
MaxSize=20,
DesiredCapacity=5,
2019-10-31 15:44:26 +00:00
VPCZoneIdentifier=mocked_networking["subnet1"],
NewInstancesProtectedFromScaleIn=True,
)
_ = client.update_auto_scaling_group(
2019-10-31 15:44:26 +00:00
AutoScalingGroupName="test_asg",
MinSize=1,
VPCZoneIdentifier="{subnet1},{subnet2}".format(
2019-10-31 15:44:26 +00:00
subnet1=mocked_networking["subnet1"], subnet2=mocked_networking["subnet2"]
),
NewInstancesProtectedFromScaleIn=False,
)
2016-05-05 02:10:11 +00:00
2019-10-31 15:44:26 +00:00
response = client.describe_auto_scaling_groups(AutoScalingGroupNames=["test_asg"])
group = response["AutoScalingGroups"][0]
group["MinSize"].should.equal(1)
set(group["AvailabilityZones"]).should.equal({"us-east-1a", "us-east-1b"})
group["NewInstancesProtectedFromScaleIn"].should.equal(False)
@mock_autoscaling
def test_update_autoscaling_group_min_size_desired_capacity_change():
mocked_networking = setup_networking()
2019-10-31 15:44:26 +00:00
client = boto3.client("autoscaling", region_name="us-east-1")
client.create_launch_configuration(
2019-10-31 15:44:26 +00:00
LaunchConfigurationName="test_launch_configuration"
)
client.create_auto_scaling_group(
2019-10-31 15:44:26 +00:00
AutoScalingGroupName="test_asg",
LaunchConfigurationName="test_launch_configuration",
MinSize=2,
MaxSize=20,
DesiredCapacity=3,
2019-10-31 15:44:26 +00:00
VPCZoneIdentifier=mocked_networking["subnet1"],
)
2019-10-31 15:44:26 +00:00
client.update_auto_scaling_group(AutoScalingGroupName="test_asg", MinSize=5)
response = client.describe_auto_scaling_groups(AutoScalingGroupNames=["test_asg"])
group = response["AutoScalingGroups"][0]
group["DesiredCapacity"].should.equal(5)
group["MinSize"].should.equal(5)
group["Instances"].should.have.length_of(5)
@mock_autoscaling
def test_update_autoscaling_group_max_size_desired_capacity_change():
mocked_networking = setup_networking()
2019-10-31 15:44:26 +00:00
client = boto3.client("autoscaling", region_name="us-east-1")
client.create_launch_configuration(
2019-10-31 15:44:26 +00:00
LaunchConfigurationName="test_launch_configuration"
)
client.create_auto_scaling_group(
2019-10-31 15:44:26 +00:00
AutoScalingGroupName="test_asg",
LaunchConfigurationName="test_launch_configuration",
MinSize=2,
MaxSize=20,
DesiredCapacity=10,
2019-10-31 15:44:26 +00:00
VPCZoneIdentifier=mocked_networking["subnet1"],
)
2019-10-31 15:44:26 +00:00
client.update_auto_scaling_group(AutoScalingGroupName="test_asg", MaxSize=5)
response = client.describe_auto_scaling_groups(AutoScalingGroupNames=["test_asg"])
group = response["AutoScalingGroups"][0]
group["DesiredCapacity"].should.equal(5)
group["MaxSize"].should.equal(5)
group["Instances"].should.have.length_of(5)
@mock_autoscaling
def test_autoscaling_taqs_update_boto3():
mocked_networking = setup_networking()
2019-10-31 15:44:26 +00:00
client = boto3.client("autoscaling", region_name="us-east-1")
_ = client.create_launch_configuration(
2019-10-31 15:44:26 +00:00
LaunchConfigurationName="test_launch_configuration"
)
_ = client.create_auto_scaling_group(
2019-10-31 15:44:26 +00:00
AutoScalingGroupName="test_asg",
LaunchConfigurationName="test_launch_configuration",
MinSize=0,
MaxSize=20,
DesiredCapacity=5,
2019-10-31 15:44:26 +00:00
Tags=[
{
"ResourceId": "test_asg",
"Key": "test_key",
"Value": "test_value",
"PropagateAtLaunch": True,
}
],
VPCZoneIdentifier=mocked_networking["subnet1"],
)
2019-10-31 15:44:26 +00:00
client.create_or_update_tags(
Tags=[
{
"ResourceId": "test_asg",
"Key": "test_key",
"Value": "updated_test_value",
"PropagateAtLaunch": True,
},
{
"ResourceId": "test_asg",
"Key": "test_key2",
"Value": "test_value2",
"PropagateAtLaunch": False,
},
]
)
2019-10-31 15:44:26 +00:00
response = client.describe_auto_scaling_groups(AutoScalingGroupNames=["test_asg"])
response["AutoScalingGroups"][0]["Tags"].should.have.length_of(2)
2017-02-24 02:37:43 +00:00
@mock_autoscaling
def test_autoscaling_describe_policies_boto3():
mocked_networking = setup_networking()
2019-10-31 15:44:26 +00:00
client = boto3.client("autoscaling", region_name="us-east-1")
_ = client.create_launch_configuration(
2019-10-31 15:44:26 +00:00
LaunchConfigurationName="test_launch_configuration"
)
_ = client.create_auto_scaling_group(
2019-10-31 15:44:26 +00:00
AutoScalingGroupName="test_asg",
LaunchConfigurationName="test_launch_configuration",
MinSize=0,
MaxSize=20,
DesiredCapacity=5,
2019-10-31 15:44:26 +00:00
Tags=[
{
"ResourceId": "test_asg",
"Key": "test_key",
"Value": "test_value",
"PropagateAtLaunch": True,
}
],
VPCZoneIdentifier=mocked_networking["subnet1"],
)
client.put_scaling_policy(
2019-10-31 15:44:26 +00:00
AutoScalingGroupName="test_asg",
PolicyName="test_policy_down",
PolicyType="SimpleScaling",
AdjustmentType="PercentChangeInCapacity",
ScalingAdjustment=-10,
Cooldown=60,
2019-10-31 15:44:26 +00:00
MinAdjustmentMagnitude=1,
)
client.put_scaling_policy(
2019-10-31 15:44:26 +00:00
AutoScalingGroupName="test_asg",
PolicyName="test_policy_up",
PolicyType="SimpleScaling",
AdjustmentType="PercentChangeInCapacity",
ScalingAdjustment=10,
Cooldown=60,
2019-10-31 15:44:26 +00:00
MinAdjustmentMagnitude=1,
)
response = client.describe_policies()
2019-10-31 15:44:26 +00:00
response["ScalingPolicies"].should.have.length_of(2)
2019-10-31 15:44:26 +00:00
response = client.describe_policies(AutoScalingGroupName="test_asg")
response["ScalingPolicies"].should.have.length_of(2)
2019-10-31 15:44:26 +00:00
response = client.describe_policies(PolicyTypes=["StepScaling"])
response["ScalingPolicies"].should.have.length_of(0)
response = client.describe_policies(
2019-10-31 15:44:26 +00:00
AutoScalingGroupName="test_asg",
PolicyNames=["test_policy_down"],
PolicyTypes=["SimpleScaling"],
)
2019-10-31 15:44:26 +00:00
response["ScalingPolicies"].should.have.length_of(1)
response["ScalingPolicies"][0]["PolicyName"].should.equal("test_policy_down")
@mock_autoscaling
@mock_ec2
def test_detach_one_instance_decrement():
mocked_networking = setup_networking()
2019-10-31 15:44:26 +00:00
client = boto3.client("autoscaling", region_name="us-east-1")
_ = client.create_launch_configuration(
2019-10-31 15:44:26 +00:00
LaunchConfigurationName="test_launch_configuration"
)
client.create_auto_scaling_group(
2019-10-31 15:44:26 +00:00
AutoScalingGroupName="test_asg",
LaunchConfigurationName="test_launch_configuration",
MinSize=0,
MaxSize=2,
DesiredCapacity=2,
2019-10-31 15:44:26 +00:00
Tags=[
{
"ResourceId": "test_asg",
"ResourceType": "auto-scaling-group",
"Key": "propogated-tag-key",
"Value": "propogate-tag-value",
"PropagateAtLaunch": True,
}
],
VPCZoneIdentifier=mocked_networking["subnet1"],
)
2019-10-31 15:44:26 +00:00
response = client.describe_auto_scaling_groups(AutoScalingGroupNames=["test_asg"])
instance_to_detach = response["AutoScalingGroups"][0]["Instances"][0]["InstanceId"]
instance_to_keep = response["AutoScalingGroups"][0]["Instances"][1]["InstanceId"]
2019-10-31 15:44:26 +00:00
ec2_client = boto3.client("ec2", region_name="us-east-1")
response = ec2_client.describe_instances(InstanceIds=[instance_to_detach])
response = client.detach_instances(
2019-10-31 15:44:26 +00:00
AutoScalingGroupName="test_asg",
InstanceIds=[instance_to_detach],
2019-10-31 15:44:26 +00:00
ShouldDecrementDesiredCapacity=True,
)
2019-10-31 15:44:26 +00:00
response["ResponseMetadata"]["HTTPStatusCode"].should.equal(200)
2019-10-31 15:44:26 +00:00
response = client.describe_auto_scaling_groups(AutoScalingGroupNames=["test_asg"])
response["AutoScalingGroups"][0]["Instances"].should.have.length_of(1)
# test to ensure tag has been removed
response = ec2_client.describe_instances(InstanceIds=[instance_to_detach])
2019-10-31 15:44:26 +00:00
tags = response["Reservations"][0]["Instances"][0]["Tags"]
tags.should.have.length_of(1)
# test to ensure tag is present on other instance
response = ec2_client.describe_instances(InstanceIds=[instance_to_keep])
2019-10-31 15:44:26 +00:00
tags = response["Reservations"][0]["Instances"][0]["Tags"]
tags.should.have.length_of(2)
2019-10-31 15:44:26 +00:00
@mock_autoscaling
@mock_ec2
def test_detach_one_instance():
mocked_networking = setup_networking()
2019-10-31 15:44:26 +00:00
client = boto3.client("autoscaling", region_name="us-east-1")
_ = client.create_launch_configuration(
2019-10-31 15:44:26 +00:00
LaunchConfigurationName="test_launch_configuration"
)
client.create_auto_scaling_group(
2019-10-31 15:44:26 +00:00
AutoScalingGroupName="test_asg",
LaunchConfigurationName="test_launch_configuration",
MinSize=0,
MaxSize=2,
DesiredCapacity=2,
2019-10-31 15:44:26 +00:00
Tags=[
{
"ResourceId": "test_asg",
"ResourceType": "auto-scaling-group",
"Key": "propogated-tag-key",
"Value": "propogate-tag-value",
"PropagateAtLaunch": True,
}
],
VPCZoneIdentifier=mocked_networking["subnet1"],
)
2019-10-31 15:44:26 +00:00
response = client.describe_auto_scaling_groups(AutoScalingGroupNames=["test_asg"])
instance_to_detach = response["AutoScalingGroups"][0]["Instances"][0]["InstanceId"]
instance_to_keep = response["AutoScalingGroups"][0]["Instances"][1]["InstanceId"]
2019-10-31 15:44:26 +00:00
ec2_client = boto3.client("ec2", region_name="us-east-1")
response = ec2_client.describe_instances(InstanceIds=[instance_to_detach])
response = client.detach_instances(
2019-10-31 15:44:26 +00:00
AutoScalingGroupName="test_asg",
InstanceIds=[instance_to_detach],
2019-10-31 15:44:26 +00:00
ShouldDecrementDesiredCapacity=False,
)
2019-10-31 15:44:26 +00:00
response["ResponseMetadata"]["HTTPStatusCode"].should.equal(200)
2019-10-31 15:44:26 +00:00
response = client.describe_auto_scaling_groups(AutoScalingGroupNames=["test_asg"])
# test to ensure instance was replaced
2019-10-31 15:44:26 +00:00
response["AutoScalingGroups"][0]["Instances"].should.have.length_of(2)
response = ec2_client.describe_instances(InstanceIds=[instance_to_detach])
2019-10-31 15:44:26 +00:00
tags = response["Reservations"][0]["Instances"][0]["Tags"]
tags.should.have.length_of(1)
response = ec2_client.describe_instances(InstanceIds=[instance_to_keep])
2019-10-31 15:44:26 +00:00
tags = response["Reservations"][0]["Instances"][0]["Tags"]
tags.should.have.length_of(2)
2019-10-31 15:44:26 +00:00
@mock_autoscaling
@mock_ec2
def test_attach_one_instance():
mocked_networking = setup_networking()
2019-10-31 15:44:26 +00:00
client = boto3.client("autoscaling", region_name="us-east-1")
_ = client.create_launch_configuration(
2019-10-31 15:44:26 +00:00
LaunchConfigurationName="test_launch_configuration"
)
client.create_auto_scaling_group(
2019-10-31 15:44:26 +00:00
AutoScalingGroupName="test_asg",
LaunchConfigurationName="test_launch_configuration",
MinSize=0,
MaxSize=4,
DesiredCapacity=2,
2019-10-31 15:44:26 +00:00
Tags=[
{
"ResourceId": "test_asg",
"ResourceType": "auto-scaling-group",
"Key": "propogated-tag-key",
"Value": "propogate-tag-value",
"PropagateAtLaunch": True,
}
],
VPCZoneIdentifier=mocked_networking["subnet1"],
NewInstancesProtectedFromScaleIn=True,
)
2019-10-31 15:44:26 +00:00
ec2 = boto3.resource("ec2", "us-east-1")
instances_to_add = [
x.id for x in ec2.create_instances(ImageId="", MinCount=1, MaxCount=1)
]
response = client.attach_instances(
2019-10-31 15:44:26 +00:00
AutoScalingGroupName="test_asg", InstanceIds=instances_to_add
)
2019-10-31 15:44:26 +00:00
response["ResponseMetadata"]["HTTPStatusCode"].should.equal(200)
2019-10-31 15:44:26 +00:00
response = client.describe_auto_scaling_groups(AutoScalingGroupNames=["test_asg"])
instances = response["AutoScalingGroups"][0]["Instances"]
instances.should.have.length_of(3)
for instance in instances:
2019-10-31 15:44:26 +00:00
instance["ProtectedFromScaleIn"].should.equal(True)
@mock_autoscaling
@mock_ec2
def test_describe_instance_health():
mocked_networking = setup_networking()
2019-10-31 15:44:26 +00:00
client = boto3.client("autoscaling", region_name="us-east-1")
_ = client.create_launch_configuration(
2019-10-31 15:44:26 +00:00
LaunchConfigurationName="test_launch_configuration"
)
client.create_auto_scaling_group(
2019-10-31 15:44:26 +00:00
AutoScalingGroupName="test_asg",
LaunchConfigurationName="test_launch_configuration",
MinSize=2,
MaxSize=4,
DesiredCapacity=2,
2019-10-31 15:44:26 +00:00
VPCZoneIdentifier=mocked_networking["subnet1"],
)
2019-10-31 15:44:26 +00:00
response = client.describe_auto_scaling_groups(AutoScalingGroupNames=["test_asg"])
instance1 = response["AutoScalingGroups"][0]["Instances"][0]
instance1["HealthStatus"].should.equal("Healthy")
2017-10-20 18:25:09 +00:00
@mock_autoscaling
@mock_ec2
def test_set_instance_health():
mocked_networking = setup_networking()
2019-10-31 15:44:26 +00:00
client = boto3.client("autoscaling", region_name="us-east-1")
2017-10-20 18:25:09 +00:00
_ = client.create_launch_configuration(
2019-10-31 15:44:26 +00:00
LaunchConfigurationName="test_launch_configuration"
2017-10-20 18:25:09 +00:00
)
client.create_auto_scaling_group(
2019-10-31 15:44:26 +00:00
AutoScalingGroupName="test_asg",
LaunchConfigurationName="test_launch_configuration",
2017-10-20 18:25:09 +00:00
MinSize=2,
MaxSize=4,
DesiredCapacity=2,
2019-10-31 15:44:26 +00:00
VPCZoneIdentifier=mocked_networking["subnet1"],
2017-10-20 18:25:09 +00:00
)
2019-10-31 15:44:26 +00:00
response = client.describe_auto_scaling_groups(AutoScalingGroupNames=["test_asg"])
2017-10-20 18:25:09 +00:00
2019-10-31 15:44:26 +00:00
instance1 = response["AutoScalingGroups"][0]["Instances"][0]
instance1["HealthStatus"].should.equal("Healthy")
2017-10-20 18:25:09 +00:00
2019-10-31 15:44:26 +00:00
client.set_instance_health(
InstanceId=instance1["InstanceId"], HealthStatus="Unhealthy"
2017-10-20 18:25:09 +00:00
)
2019-10-31 15:44:26 +00:00
response = client.describe_auto_scaling_groups(AutoScalingGroupNames=["test_asg"])
instance1 = response["AutoScalingGroups"][0]["Instances"][0]
instance1["HealthStatus"].should.equal("Unhealthy")
@mock_autoscaling
def test_suspend_processes():
mocked_networking = setup_networking()
2019-10-31 15:44:26 +00:00
client = boto3.client("autoscaling", region_name="us-east-1")
client.create_launch_configuration(LaunchConfigurationName="lc")
client.create_auto_scaling_group(
2019-10-31 15:44:26 +00:00
LaunchConfigurationName="lc",
AutoScalingGroupName="test-asg",
MinSize=1,
MaxSize=1,
2019-10-31 15:44:26 +00:00
VPCZoneIdentifier=mocked_networking["subnet1"],
)
# When we suspend the 'Launch' process on the ASG client
client.suspend_processes(
2019-10-31 15:44:26 +00:00
AutoScalingGroupName="test-asg", ScalingProcesses=["Launch"]
)
2019-10-31 15:44:26 +00:00
res = client.describe_auto_scaling_groups(AutoScalingGroupNames=["test-asg"])
# The 'Launch' process should, in fact, be suspended
launch_suspended = False
2019-10-31 15:44:26 +00:00
for proc in res["AutoScalingGroups"][0]["SuspendedProcesses"]:
if proc.get("ProcessName") == "Launch":
launch_suspended = True
assert launch_suspended is True
2019-10-31 15:44:26 +00:00
@mock_autoscaling
def test_set_instance_protection():
mocked_networking = setup_networking()
2019-10-31 15:44:26 +00:00
client = boto3.client("autoscaling", region_name="us-east-1")
_ = client.create_launch_configuration(
2019-10-31 15:44:26 +00:00
LaunchConfigurationName="test_launch_configuration"
)
_ = client.create_auto_scaling_group(
2019-10-31 15:44:26 +00:00
AutoScalingGroupName="test_asg",
LaunchConfigurationName="test_launch_configuration",
MinSize=0,
MaxSize=20,
DesiredCapacity=5,
2019-10-31 15:44:26 +00:00
VPCZoneIdentifier=mocked_networking["subnet1"],
NewInstancesProtectedFromScaleIn=False,
)
2019-10-31 15:44:26 +00:00
response = client.describe_auto_scaling_groups(AutoScalingGroupNames=["test_asg"])
instance_ids = [
2019-10-31 15:44:26 +00:00
instance["InstanceId"]
for instance in response["AutoScalingGroups"][0]["Instances"]
]
protected = instance_ids[:3]
_ = client.set_instance_protection(
2019-10-31 15:44:26 +00:00
AutoScalingGroupName="test_asg",
InstanceIds=protected,
ProtectedFromScaleIn=True,
)
2019-10-31 15:44:26 +00:00
response = client.describe_auto_scaling_groups(AutoScalingGroupNames=["test_asg"])
for instance in response["AutoScalingGroups"][0]["Instances"]:
instance["ProtectedFromScaleIn"].should.equal(
instance["InstanceId"] in protected
)
@mock_autoscaling
def test_set_desired_capacity_up_boto3():
mocked_networking = setup_networking()
2019-10-31 15:44:26 +00:00
client = boto3.client("autoscaling", region_name="us-east-1")
_ = client.create_launch_configuration(
2019-10-31 15:44:26 +00:00
LaunchConfigurationName="test_launch_configuration"
)
_ = client.create_auto_scaling_group(
2019-10-31 15:44:26 +00:00
AutoScalingGroupName="test_asg",
LaunchConfigurationName="test_launch_configuration",
MinSize=0,
MaxSize=20,
DesiredCapacity=5,
2019-10-31 15:44:26 +00:00
VPCZoneIdentifier=mocked_networking["subnet1"],
NewInstancesProtectedFromScaleIn=True,
)
2019-10-31 15:44:26 +00:00
_ = client.set_desired_capacity(AutoScalingGroupName="test_asg", DesiredCapacity=10)
2019-10-31 15:44:26 +00:00
response = client.describe_auto_scaling_groups(AutoScalingGroupNames=["test_asg"])
instances = response["AutoScalingGroups"][0]["Instances"]
instances.should.have.length_of(10)
for instance in instances:
2019-10-31 15:44:26 +00:00
instance["ProtectedFromScaleIn"].should.equal(True)
@mock_autoscaling
def test_set_desired_capacity_down_boto3():
mocked_networking = setup_networking()
2019-10-31 15:44:26 +00:00
client = boto3.client("autoscaling", region_name="us-east-1")
_ = client.create_launch_configuration(
2019-10-31 15:44:26 +00:00
LaunchConfigurationName="test_launch_configuration"
)
_ = client.create_auto_scaling_group(
2019-10-31 15:44:26 +00:00
AutoScalingGroupName="test_asg",
LaunchConfigurationName="test_launch_configuration",
MinSize=0,
MaxSize=20,
DesiredCapacity=5,
2019-10-31 15:44:26 +00:00
VPCZoneIdentifier=mocked_networking["subnet1"],
NewInstancesProtectedFromScaleIn=True,
)
2019-10-31 15:44:26 +00:00
response = client.describe_auto_scaling_groups(AutoScalingGroupNames=["test_asg"])
instance_ids = [
2019-10-31 15:44:26 +00:00
instance["InstanceId"]
for instance in response["AutoScalingGroups"][0]["Instances"]
]
unprotected, protected = instance_ids[:2], instance_ids[2:]
_ = client.set_instance_protection(
2019-10-31 15:44:26 +00:00
AutoScalingGroupName="test_asg",
InstanceIds=unprotected,
ProtectedFromScaleIn=False,
)
2019-10-31 15:44:26 +00:00
_ = client.set_desired_capacity(AutoScalingGroupName="test_asg", DesiredCapacity=1)
2019-10-31 15:44:26 +00:00
response = client.describe_auto_scaling_groups(AutoScalingGroupNames=["test_asg"])
group = response["AutoScalingGroups"][0]
group["DesiredCapacity"].should.equal(1)
instance_ids = {instance["InstanceId"] for instance in group["Instances"]}
set(protected).should.equal(instance_ids)
set(unprotected).should_not.be.within(instance_ids) # only unprotected killed
@mock_autoscaling
@mock_ec2
def test_terminate_instance_in_autoscaling_group():
mocked_networking = setup_networking()
2019-10-31 15:44:26 +00:00
client = boto3.client("autoscaling", region_name="us-east-1")
_ = client.create_launch_configuration(
2019-10-31 15:44:26 +00:00
LaunchConfigurationName="test_launch_configuration"
)
_ = client.create_auto_scaling_group(
2019-10-31 15:44:26 +00:00
AutoScalingGroupName="test_asg",
LaunchConfigurationName="test_launch_configuration",
MinSize=1,
MaxSize=20,
2019-10-31 15:44:26 +00:00
VPCZoneIdentifier=mocked_networking["subnet1"],
NewInstancesProtectedFromScaleIn=False,
)
2019-10-31 15:44:26 +00:00
response = client.describe_auto_scaling_groups(AutoScalingGroupNames=["test_asg"])
original_instance_id = next(
2019-10-31 15:44:26 +00:00
instance["InstanceId"]
for instance in response["AutoScalingGroups"][0]["Instances"]
)
2019-10-31 15:44:26 +00:00
ec2_client = boto3.client("ec2", region_name="us-east-1")
ec2_client.terminate_instances(InstanceIds=[original_instance_id])
2019-10-31 15:44:26 +00:00
response = client.describe_auto_scaling_groups(AutoScalingGroupNames=["test_asg"])
replaced_instance_id = next(
2019-10-31 15:44:26 +00:00
instance["InstanceId"]
for instance in response["AutoScalingGroups"][0]["Instances"]
)
replaced_instance_id.should_not.equal(original_instance_id)