Fix/asg attach instances (#4258)

This commit is contained in:
mello7tre 2021-09-01 19:50:53 +02:00 committed by GitHub
parent c53183db70
commit cfc793014f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 0 deletions

View File

@ -35,11 +35,14 @@ class InstanceState(object):
lifecycle_state="InService", lifecycle_state="InService",
health_status="Healthy", health_status="Healthy",
protected_from_scale_in=False, protected_from_scale_in=False,
autoscaling_group=None,
): ):
self.instance = instance self.instance = instance
self.lifecycle_state = lifecycle_state self.lifecycle_state = lifecycle_state
self.health_status = health_status self.health_status = health_status
self.protected_from_scale_in = protected_from_scale_in self.protected_from_scale_in = protected_from_scale_in
if not hasattr(self.instance, "autoscaling_group"):
self.instance.autoscaling_group = autoscaling_group
class FakeScalingPolicy(BaseModel): class FakeScalingPolicy(BaseModel):
@ -821,6 +824,7 @@ class AutoScalingBackend(BaseBackend):
InstanceState( InstanceState(
self.ec2_backend.get_instance(x), self.ec2_backend.get_instance(x),
protected_from_scale_in=group.new_instances_protected_from_scale_in, protected_from_scale_in=group.new_instances_protected_from_scale_in,
autoscaling_group=group,
) )
for x in instance_ids for x in instance_ids
] ]

View File

@ -2832,3 +2832,44 @@ def test_delete_tags_by_key():
tags = group["Tags"] tags = group["Tags"]
tags.should.contain(tag_to_keep) tags.should.contain(tag_to_keep)
tags.should_not.contain(tag_to_delete) tags.should_not.contain(tag_to_delete)
@mock_ec2
@mock_autoscaling
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",
"ImageId": "ami-pytest",
"MinCount": 1,
"MaxCount": 1,
"InstanceType": "c4.2xlarge",
"TagSpecifications": [
{"ResourceType": "instance", "Tags": [{"Key": "key", "Value": "val"}]},
],
}
fake_instance = ec2_client.run_instances(**kwargs)["Instances"][0]
fake_lc = asg_client.create_launch_configuration(
LaunchConfigurationName="test_launch_configuration",
ImageId="ami-pytest",
InstanceType="t3.micro",
KeyName="foobar",
)
fake_asg = asg_client.create_auto_scaling_group(
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()
len(response["AutoScalingInstances"]).should.equal(1)
for instance in response["AutoScalingInstances"]:
instance["LaunchConfigurationName"].should.equal("test_launch_configuration")
instance["AutoScalingGroupName"].should.equal("test_asg")
instance["InstanceType"].should.equal("c4.2xlarge")