Refactor Autoscaling tests (#5426)

This commit is contained in:
Bert Blommers 2022-08-28 14:41:11 +00:00 committed by GitHub
parent 257d775dfb
commit 067e008716
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 1153 additions and 1414 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,139 +1,110 @@
import boto3 import boto3
import sure # noqa # pylint: disable=unused-import import sure # noqa # pylint: disable=unused-import
import unittest
from moto import mock_autoscaling, mock_elbv2 from moto import mock_autoscaling, mock_elbv2
from .utils import setup_networking
from tests import EXAMPLE_AMI_ID from tests import EXAMPLE_AMI_ID
from uuid import uuid4
from .utils import setup_networking
@mock_elbv2 @mock_elbv2
@mock_autoscaling @mock_autoscaling
def test_attach_detach_target_groups(): class TestAutoscalignELBv2(unittest.TestCase):
mocked_networking = setup_networking() def setUp(self) -> None:
INSTANCE_COUNT = 2 self.mocked_networking = setup_networking()
client = boto3.client("autoscaling", region_name="us-east-1") self.instance_count = 2
elbv2_client = boto3.client("elbv2", region_name="us-east-1") self.as_client = boto3.client("autoscaling", region_name="us-east-1")
self.elbv2_client = boto3.client("elbv2", region_name="us-east-1")
response = elbv2_client.create_target_group( self.target_name = str(uuid4())[0:6]
Name="a-target", self.lc_name = str(uuid4())[0:6]
Protocol="HTTP", self.asg_name = str(uuid4())[0:6]
Port=8080, response = self.elbv2_client.create_target_group(
VpcId=mocked_networking["vpc"], Name=self.target_name,
HealthCheckProtocol="HTTP", Protocol="HTTP",
HealthCheckPort="8080", Port=8080,
HealthCheckPath="/", VpcId=self.mocked_networking["vpc"],
HealthCheckIntervalSeconds=5, HealthCheckProtocol="HTTP",
HealthCheckTimeoutSeconds=5, HealthCheckPort="8080",
HealthyThresholdCount=5, HealthCheckPath="/",
UnhealthyThresholdCount=2, HealthCheckIntervalSeconds=5,
Matcher={"HttpCode": "200"}, HealthCheckTimeoutSeconds=5,
) HealthyThresholdCount=5,
target_group_arn = response["TargetGroups"][0]["TargetGroupArn"] UnhealthyThresholdCount=2,
Matcher={"HttpCode": "200"},
)
self.target_group_arn = response["TargetGroups"][0]["TargetGroupArn"]
client.create_launch_configuration( self.as_client.create_launch_configuration(
LaunchConfigurationName="test_launch_configuration", LaunchConfigurationName=self.lc_name,
ImageId=EXAMPLE_AMI_ID, ImageId=EXAMPLE_AMI_ID,
InstanceType="t2.medium", InstanceType="t2.medium",
) )
self.as_client.create_auto_scaling_group(
AutoScalingGroupName=self.asg_name,
LaunchConfigurationName=self.lc_name,
MinSize=0,
MaxSize=self.instance_count,
DesiredCapacity=self.instance_count,
TargetGroupARNs=[self.target_group_arn],
VPCZoneIdentifier=self.mocked_networking["subnet1"],
)
# create asg, attach to target group on create response = self.as_client.describe_load_balancer_target_groups(
client.create_auto_scaling_group( AutoScalingGroupName=self.asg_name
AutoScalingGroupName="test_asg", )
LaunchConfigurationName="test_launch_configuration", list(response["LoadBalancerTargetGroups"]).should.have.length_of(1)
MinSize=0,
MaxSize=INSTANCE_COUNT,
DesiredCapacity=INSTANCE_COUNT,
TargetGroupARNs=[target_group_arn],
VPCZoneIdentifier=mocked_networking["subnet1"],
)
# create asg without attaching to target group
client.create_auto_scaling_group(
AutoScalingGroupName="test_asg2",
LaunchConfigurationName="test_launch_configuration",
MinSize=0,
MaxSize=INSTANCE_COUNT,
DesiredCapacity=INSTANCE_COUNT,
VPCZoneIdentifier=mocked_networking["subnet2"],
)
response = client.describe_load_balancer_target_groups( response = self.elbv2_client.describe_target_health(
AutoScalingGroupName="test_asg" TargetGroupArn=self.target_group_arn
) )
list(response["LoadBalancerTargetGroups"]).should.have.length_of(1) list(response["TargetHealthDescriptions"]).should.have.length_of(
self.instance_count
)
response = elbv2_client.describe_target_health(TargetGroupArn=target_group_arn) def test_attach_detach_target_groups(self):
list(response["TargetHealthDescriptions"]).should.have.length_of(INSTANCE_COUNT) # create asg without attaching to target group
asg_name2 = str(uuid4())
self.as_client.create_auto_scaling_group(
AutoScalingGroupName=asg_name2,
LaunchConfigurationName=self.lc_name,
MinSize=0,
MaxSize=self.instance_count,
DesiredCapacity=self.instance_count,
VPCZoneIdentifier=self.mocked_networking["subnet2"],
)
client.attach_load_balancer_target_groups( self.as_client.attach_load_balancer_target_groups(
AutoScalingGroupName="test_asg2", TargetGroupARNs=[target_group_arn] AutoScalingGroupName=asg_name2, TargetGroupARNs=[self.target_group_arn]
) )
response = elbv2_client.describe_target_health(TargetGroupArn=target_group_arn) response = self.elbv2_client.describe_target_health(
list(response["TargetHealthDescriptions"]).should.have.length_of(INSTANCE_COUNT * 2) TargetGroupArn=self.target_group_arn
)
list(response["TargetHealthDescriptions"]).should.have.length_of(
self.instance_count * 2
)
response = client.detach_load_balancer_target_groups( response = self.as_client.detach_load_balancer_target_groups(
AutoScalingGroupName="test_asg2", TargetGroupARNs=[target_group_arn] AutoScalingGroupName=asg_name2, TargetGroupARNs=[self.target_group_arn]
) )
response = elbv2_client.describe_target_health(TargetGroupArn=target_group_arn) response = self.elbv2_client.describe_target_health(
list(response["TargetHealthDescriptions"]).should.have.length_of(INSTANCE_COUNT) TargetGroupArn=self.target_group_arn
)
list(response["TargetHealthDescriptions"]).should.have.length_of(
self.instance_count
)
def test_detach_all_target_groups(self):
response = self.as_client.detach_load_balancer_target_groups(
AutoScalingGroupName=self.asg_name, TargetGroupARNs=[self.target_group_arn]
)
@mock_elbv2 response = self.elbv2_client.describe_target_health(
@mock_autoscaling TargetGroupArn=self.target_group_arn
def test_detach_all_target_groups(): )
mocked_networking = setup_networking() list(response["TargetHealthDescriptions"]).should.have.length_of(0)
INSTANCE_COUNT = 2 response = self.as_client.describe_load_balancer_target_groups(
client = boto3.client("autoscaling", region_name="us-east-1") AutoScalingGroupName=self.asg_name
elbv2_client = boto3.client("elbv2", region_name="us-east-1") )
list(response["LoadBalancerTargetGroups"]).should.have.length_of(0)
response = elbv2_client.create_target_group(
Name="a-target",
Protocol="HTTP",
Port=8080,
VpcId=mocked_networking["vpc"],
HealthCheckProtocol="HTTP",
HealthCheckPort="8080",
HealthCheckPath="/",
HealthCheckIntervalSeconds=5,
HealthCheckTimeoutSeconds=5,
HealthyThresholdCount=5,
UnhealthyThresholdCount=2,
Matcher={"HttpCode": "200"},
)
target_group_arn = response["TargetGroups"][0]["TargetGroupArn"]
client.create_launch_configuration(
LaunchConfigurationName="test_launch_configuration",
ImageId=EXAMPLE_AMI_ID,
InstanceType="t2.medium",
)
client.create_auto_scaling_group(
AutoScalingGroupName="test_asg",
LaunchConfigurationName="test_launch_configuration",
MinSize=0,
MaxSize=INSTANCE_COUNT,
DesiredCapacity=INSTANCE_COUNT,
TargetGroupARNs=[target_group_arn],
VPCZoneIdentifier=mocked_networking["subnet1"],
)
response = client.describe_load_balancer_target_groups(
AutoScalingGroupName="test_asg"
)
list(response["LoadBalancerTargetGroups"]).should.have.length_of(1)
response = elbv2_client.describe_target_health(TargetGroupArn=target_group_arn)
list(response["TargetHealthDescriptions"]).should.have.length_of(INSTANCE_COUNT)
response = client.detach_load_balancer_target_groups(
AutoScalingGroupName="test_asg", TargetGroupARNs=[target_group_arn]
)
response = elbv2_client.describe_target_health(TargetGroupArn=target_group_arn)
list(response["TargetHealthDescriptions"]).should.have.length_of(0)
response = client.describe_load_balancer_target_groups(
AutoScalingGroupName="test_asg"
)
list(response["LoadBalancerTargetGroups"]).should.have.length_of(0)