2022-08-28 14:41:11 +00:00
|
|
|
import unittest
|
2023-11-30 15:55:51 +00:00
|
|
|
from uuid import uuid4
|
|
|
|
|
|
|
|
import boto3
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
from moto import mock_aws
|
2021-01-27 19:49:33 +00:00
|
|
|
from tests import EXAMPLE_AMI_ID
|
2023-11-30 15:55:51 +00:00
|
|
|
|
2022-08-28 14:41:11 +00:00
|
|
|
from .utils import setup_networking
|
2017-12-27 19:17:59 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2022-08-28 14:41:11 +00:00
|
|
|
class TestAutoscalignELBv2(unittest.TestCase):
|
|
|
|
def setUp(self) -> None:
|
|
|
|
self.mocked_networking = setup_networking()
|
|
|
|
self.instance_count = 2
|
|
|
|
self.as_client = boto3.client("autoscaling", region_name="us-east-1")
|
|
|
|
self.elbv2_client = boto3.client("elbv2", region_name="us-east-1")
|
|
|
|
|
|
|
|
self.target_name = str(uuid4())[0:6]
|
|
|
|
self.lc_name = str(uuid4())[0:6]
|
|
|
|
self.asg_name = str(uuid4())[0:6]
|
|
|
|
response = self.elbv2_client.create_target_group(
|
|
|
|
Name=self.target_name,
|
|
|
|
Protocol="HTTP",
|
|
|
|
Port=8080,
|
|
|
|
VpcId=self.mocked_networking["vpc"],
|
|
|
|
HealthCheckProtocol="HTTP",
|
|
|
|
HealthCheckPort="8080",
|
|
|
|
HealthCheckPath="/",
|
|
|
|
HealthCheckIntervalSeconds=5,
|
2023-09-14 12:52:14 +00:00
|
|
|
HealthCheckTimeoutSeconds=3,
|
2022-08-28 14:41:11 +00:00
|
|
|
HealthyThresholdCount=5,
|
|
|
|
UnhealthyThresholdCount=2,
|
|
|
|
Matcher={"HttpCode": "200"},
|
|
|
|
)
|
|
|
|
self.target_group_arn = response["TargetGroups"][0]["TargetGroupArn"]
|
|
|
|
|
|
|
|
self.as_client.create_launch_configuration(
|
|
|
|
LaunchConfigurationName=self.lc_name,
|
|
|
|
ImageId=EXAMPLE_AMI_ID,
|
|
|
|
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"],
|
|
|
|
)
|
|
|
|
|
|
|
|
response = self.as_client.describe_load_balancer_target_groups(
|
|
|
|
AutoScalingGroupName=self.asg_name
|
|
|
|
)
|
2023-06-09 11:16:59 +00:00
|
|
|
assert len(list(response["LoadBalancerTargetGroups"])) == 1
|
2022-08-28 14:41:11 +00:00
|
|
|
|
|
|
|
response = self.elbv2_client.describe_target_health(
|
|
|
|
TargetGroupArn=self.target_group_arn
|
|
|
|
)
|
2023-06-09 11:16:59 +00:00
|
|
|
assert len(list(response["TargetHealthDescriptions"])) == self.instance_count
|
2022-08-28 14:41:11 +00:00
|
|
|
|
|
|
|
def test_attach_detach_target_groups(self):
|
|
|
|
# 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"],
|
|
|
|
)
|
|
|
|
|
|
|
|
self.as_client.attach_load_balancer_target_groups(
|
|
|
|
AutoScalingGroupName=asg_name2, TargetGroupARNs=[self.target_group_arn]
|
|
|
|
)
|
|
|
|
|
|
|
|
response = self.elbv2_client.describe_target_health(
|
|
|
|
TargetGroupArn=self.target_group_arn
|
|
|
|
)
|
2023-06-09 11:16:59 +00:00
|
|
|
assert (
|
|
|
|
len(list(response["TargetHealthDescriptions"])) == self.instance_count * 2
|
2022-08-28 14:41:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
response = self.as_client.detach_load_balancer_target_groups(
|
|
|
|
AutoScalingGroupName=asg_name2, TargetGroupARNs=[self.target_group_arn]
|
|
|
|
)
|
|
|
|
response = self.elbv2_client.describe_target_health(
|
|
|
|
TargetGroupArn=self.target_group_arn
|
|
|
|
)
|
2023-06-09 11:16:59 +00:00
|
|
|
assert len(list(response["TargetHealthDescriptions"])) == self.instance_count
|
2022-08-28 14:41:11 +00:00
|
|
|
|
|
|
|
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]
|
|
|
|
)
|
|
|
|
|
|
|
|
response = self.elbv2_client.describe_target_health(
|
|
|
|
TargetGroupArn=self.target_group_arn
|
|
|
|
)
|
2023-06-09 11:16:59 +00:00
|
|
|
assert len(list(response["TargetHealthDescriptions"])) == 0
|
2022-08-28 14:41:11 +00:00
|
|
|
response = self.as_client.describe_load_balancer_target_groups(
|
|
|
|
AutoScalingGroupName=self.asg_name
|
|
|
|
)
|
2023-06-09 11:16:59 +00:00
|
|
|
assert len(list(response["LoadBalancerTargetGroups"])) == 0
|