fix(autoscaling): register instances with a Classic Load Balancer on attach and deregister them on detach (#8496)

* fix(autoscaling): register instances with a Classic Load Balancer on attach and deregister them on detach
* fix(autoscaling): put members back in what they left when a detach is refused
* test(autoscaling): share the attachment tests' groups in one fixture
This commit is contained in:
mrfelek authored and GitHub committed 2026-11-15 18:29:22 -04:00
1 parent 065d7194b0
commit e1c3242978
9 files changed
+480 -149

No files matched your search

@@ -0,0 +1,212 @@
package io.floci.aws.services.autoscaling;
import io.floci.aws.core.common.AwsException;
import io.floci.aws.services.autoscaling.model.AutoScalingGroup;
import io.floci.aws.services.elb.ElbClassicService;
import io.floci.aws.services.elbv2.ElbV2Service;
import org.junit.jupiter.api.Test;
import org.mockito.InOrder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static io.floci.aws.services.autoscaling.AutoScalingGroupFixtures.FIRST;
import static io.floci.aws.services.autoscaling.AutoScalingGroupFixtures.GROUP;
import static io.floci.aws.services.autoscaling.AutoScalingGroupFixtures.IN_SERVICE;
import static io.floci.aws.services.autoscaling.AutoScalingGroupFixtures.NODE;
import static io.floci.aws.services.autoscaling.AutoScalingGroupFixtures.REGION;
import static io.floci.aws.services.autoscaling.AutoScalingGroupFixtures.SECOND;
import static io.floci.aws.services.autoscaling.AutoScalingGroupFixtures.STANDBY;
import static io.floci.aws.services.autoscaling.AutoScalingGroupFixtures.groupsSharingTheNode;
import static io.floci.aws.services.autoscaling.AutoScalingGroupFixtures.node;
import static io.floci.aws.services.autoscaling.AutoScalingGroupFixtures.nodeGroupTags;
import static io.floci.aws.services.autoscaling.AutoScalingGroupFixtures.ordinaryGroup;
import static io.floci.aws.services.autoscaling.TargetMatchers.exactlyTargets;
import static io.floci.aws.services.autoscaling.TargetMatchers.onlyTarget;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.clearInvocations;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
/**
* Attaching a Classic Load Balancer to a group registers the group's in-service instances with it, and detaching one
* deregisters the group's instances from it, as AWS does and as {@link AutoScalingTargetGroupAttachmentTest} checks
* for target groups: by name and as {@code elb} traffic sources, for an ordinary group and for EKS groups sharing
* their cluster's node.
*/
class AutoScalingClassicLoadBalancerAttachmentTest {
private final ElbV2Service targets = mock(ElbV2Service.class);
private final ElbClassicService classic = mock(ElbClassicService.class);
private final AutoScalingService service = AutoScalingGroupFixtures.service(targets, classic);
private final AutoScalingGroup group = ordinaryGroup(service);
AutoScalingClassicLoadBalancerAttachmentTest() {
group.setLoadBalancerNames(new ArrayList<>(List.of("lb-attached")));
}
@Test
void attachingAClassicLoadBalancerRegistersTheGroupsInServiceInstancesWithIt() {
service.attachLoadBalancers(REGION, GROUP, List.of("lb-attached", "lb-new"));
// Only the load balancer the attach added, and only the instance in service: one in standby is not.
verify(classic).registerInstances(REGION, "lb-new", List.of(IN_SERVICE));
verifyNoMoreInteractions(classic);
assertEquals(List.of("lb-attached", "lb-new"), group.getLoadBalancerNames());
}
@Test
void detachingAClassicLoadBalancerDeregistersTheGroupsInstancesFromIt() {
group.getLoadBalancerNames().add("lb-kept");
service.detachLoadBalancers(REGION, GROUP, List.of("lb-attached"));
verify(classic).deregisterInstances(REGION, "lb-attached", List.of(IN_SERVICE, STANDBY));
verifyNoMoreInteractions(classic);
assertEquals(List.of("lb-kept"), group.getLoadBalancerNames());
}
@Test
void anElbTrafficSourceRegistersAndDeregistersTheInstancesAsAClassicLoadBalancerDoes() {
service.attachTrafficSources(REGION, GROUP, List.of(
new AutoScalingService.TrafficSourceIdentifier("lb-source", "elb")));
verify(classic).registerInstances(REGION, "lb-source", List.of(IN_SERVICE));
service.detachTrafficSources(REGION, GROUP, List.of(
new AutoScalingService.TrafficSourceIdentifier("lb-source", "elb")));
verify(classic).deregisterInstances(REGION, "lb-source", List.of(IN_SERVICE, STANDBY));
verifyNoMoreInteractions(classic);
assertEquals(Map.of(), group.getTrafficSourceTypeByIdentifier());
}
/** A group deleted with its instances takes them out of its {@code elb} traffic sources too. */
@Test
void aForceDeleteDeregistersTheInstancesFromAnElbTrafficSource() {
group.getTrafficSourceTypeByIdentifier().put("lb-source", "elb");
service.deleteAutoScalingGroup(REGION, GROUP, true);
verify(classic).deregisterInstances(REGION, "lb-attached", List.of(IN_SERVICE, STANDBY));
verify(classic).deregisterInstances(REGION, "lb-source", List.of(IN_SERVICE, STANDBY));
}
@Test
void aDetachTheLoadBalancerRefusesLeavesItAttachedSoTheDetachCanBeSentAgain() {
AwsException refusal = new AwsException("Throttling", "Rate exceeded", 400);
when(classic.deregisterInstances(REGION, "lb-attached", List.of(IN_SERVICE, STANDBY)))
.thenThrow(refusal).thenReturn(List.of());
assertSame(refusal, assertThrows(AwsException.class,
() -> service.detachLoadBalancers(REGION, GROUP, List.of("lb-attached"))));
assertEquals(List.of("lb-attached"), group.getLoadBalancerNames());
service.detachLoadBalancers(REGION, GROUP, List.of("lb-attached"));
verify(classic, times(2)).deregisterInstances(REGION, "lb-attached", List.of(IN_SERVICE, STANDBY));
assertEquals(List.of(), group.getLoadBalancerNames());
}
@Test
void aClassicLoadBalancerDeletedBeforeItsDetachIsDetached() {
when(classic.deregisterInstances(REGION, "lb-attached", List.of(IN_SERVICE, STANDBY)))
.thenThrow(new AwsException("LoadBalancerNotFound",
"There is no ACTIVE Load Balancer named 'lb-attached'", 400));
service.detachLoadBalancers(REGION, GROUP, List.of("lb-attached"));
assertEquals(List.of(), group.getLoadBalancerNames());
}
/**
* A detach whose Classic Load Balancer refuses after the members left the target group it also named puts the
* in-service member back in that target group and leaves both attached, as they were, and sending the detach
* again finishes it.
*/
@Test
void aDetachWhoseClassicLoadBalancerRefusesPutsTheMembersBackInWhatTheyAlreadyLeft() {
group.getTrafficSourceTypeByIdentifier().put("tg-source", "elbv2");
group.getTrafficSourceTypeByIdentifier().put("lb-source", "elb");
AwsException refusal = new AwsException("Throttling", "Rate exceeded", 400);
when(classic.deregisterInstances(REGION, "lb-source", List.of(IN_SERVICE, STANDBY)))
.thenThrow(refusal).thenReturn(List.of());
List<AutoScalingService.TrafficSourceIdentifier> both = List.of(
new AutoScalingService.TrafficSourceIdentifier("tg-source", "elbv2"),
new AutoScalingService.TrafficSourceIdentifier("lb-source", "elb"));
assertSame(refusal, assertThrows(AwsException.class,
() -> service.detachTrafficSources(REGION, GROUP, both)));
assertEquals(Map.of("tg-source", "elbv2", "lb-source", "elb"), group.getTrafficSourceTypeByIdentifier());
InOrder order = inOrder(targets);
order.verify(targets).deregisterTargets(eq(REGION), eq("tg-source"), exactlyTargets(IN_SERVICE, STANDBY));
order.verify(targets).registerTargets(eq(REGION), eq("tg-source"), onlyTarget(IN_SERVICE));
service.detachTrafficSources(REGION, GROUP, both);
verify(targets, times(2)).deregisterTargets(eq(REGION), eq("tg-source"), exactlyTargets(IN_SERVICE, STANDBY));
verify(classic, times(2)).deregisterInstances(REGION, "lb-source", List.of(IN_SERVICE, STANDBY));
assertEquals(Map.of(), group.getTrafficSourceTypeByIdentifier());
}
/** The groups of a cluster's node groups share its node, which stays in a load balancer either has attached. */
@Test
void detachingAClassicLoadBalancerLeavesTheSharedNodeInItWhileAnotherEksGroupHasItAttached() {
groupsSharingTheNode(service);
service.attachLoadBalancers(REGION, FIRST, List.of("lb-shared"));
service.attachTrafficSources(REGION, SECOND, List.of(
new AutoScalingService.TrafficSourceIdentifier("lb-shared", "elb")));
verify(classic, times(2)).registerInstances(REGION, "lb-shared", List.of(NODE));
clearInvocations(classic);
service.detachLoadBalancers(REGION, FIRST, List.of("lb-shared"));
verifyNoInteractions(classic);
service.detachTrafficSources(REGION, SECOND, List.of(
new AutoScalingService.TrafficSourceIdentifier("lb-shared", "elb")));
verify(classic).deregisterInstances(REGION, "lb-shared", List.of(NODE));
verifyNoMoreInteractions(classic);
}
/** A node that joins an EKS group after a Classic Load Balancer was attached to it is registered with it. */
@Test
void aNodeJoiningAnEksGroupIsRegisteredWithItsClassicLoadBalancers() {
service.createEksManagedGroup(REGION, FIRST, 1, 3, 2, List.of(), nodeGroupTags("workers"));
service.attachLoadBalancers(REGION, FIRST, List.of("lb-web"));
verifyNoInteractions(classic);
service.addEksManagedInstance(REGION, FIRST, node());
verify(classic).registerInstances(REGION, "lb-web", List.of(NODE));
}
/** Deleting an EKS group, or detaching the node from it, leaves the node in what the other group has attached. */
@Test
void deletingOrDetachingFromAnEksGroupLeavesTheNodeInTheLoadBalancersTheOtherGroupHasAttached() {
groupsSharingTheNode(service);
service.attachLoadBalancers(REGION, FIRST, List.of("lb-shared", "lb-first"));
service.attachLoadBalancers(REGION, SECOND, List.of("lb-shared"));
clearInvocations(classic);
service.detachInstances(REGION, FIRST, List.of(NODE), false);
verify(classic).deregisterInstances(REGION, "lb-first", List.of(NODE));
verifyNoMoreInteractions(classic);
service.deleteEksManagedGroup(REGION, SECOND);
verify(classic).deregisterInstances(REGION, "lb-shared", List.of(NODE));
verifyNoMoreInteractions(classic);
}
}
@@ -1,13 +1,10 @@
package io.floci.aws.services.autoscaling;
import io.floci.aws.core.common.AwsException;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.services.autoscaling.model.AsgInstance;
import io.floci.aws.services.autoscaling.model.AutoScalingGroup;
import io.floci.aws.services.autoscaling.model.InstanceRefresh;
import io.floci.aws.services.ec2.Ec2Service;
import io.floci.aws.services.ec2.model.Instance;
import io.floci.aws.services.ec2.model.Placement;
import io.floci.aws.services.ec2.model.Subnet;
import io.floci.aws.services.elbv2.ElbV2Service;
import io.floci.aws.services.elbv2.model.TargetDescription;
@@ -25,6 +22,13 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import static io.floci.aws.services.autoscaling.AutoScalingGroupFixtures.FIRST;
import static io.floci.aws.services.autoscaling.AutoScalingGroupFixtures.NODE;
import static io.floci.aws.services.autoscaling.AutoScalingGroupFixtures.REGION;
import static io.floci.aws.services.autoscaling.AutoScalingGroupFixtures.SECOND;
import static io.floci.aws.services.autoscaling.AutoScalingGroupFixtures.groupsSharingTheNode;
import static io.floci.aws.services.autoscaling.AutoScalingGroupFixtures.node;
import static io.floci.aws.services.autoscaling.AutoScalingGroupFixtures.nodeGroupTags;
import static io.floci.aws.services.autoscaling.TargetMatchers.onlyTarget;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
@@ -50,17 +54,11 @@ import static org.mockito.Mockito.when;
*/
class AutoScalingEksManagedGroupTest {
private static final String REGION = "us-east-1";
private static final String FIRST = "eks-workers-1a2b3c4d";
private static final String SECOND = "eks-batch-5e6f7a8b";
private static final String NODE = "i-0123456789abcdef0";
private final ElbV2Service targets = mock(ElbV2Service.class);
private final Ec2Service ec2 = mock(Ec2Service.class);
private final AutoScalingService service = new AutoScalingService(targets, null);
private final AutoScalingService service = AutoScalingGroupFixtures.service(targets, null);
AutoScalingEksManagedGroupTest() {
service.regionResolver = new RegionResolver(REGION, "000000000000");
service.ec2Service = ec2;
}
@@ -71,7 +69,7 @@ class AutoScalingEksManagedGroupTest {
subnet.setAvailabilityZone("us-east-1a");
when(ec2.describeSubnets(REGION, List.of("subnet-a", "subnet-unknown"), null)).thenReturn(List.of(subnet));
service.createEksManagedGroup(REGION, FIRST, 1, 3, 2, List.of("subnet-a", "subnet-unknown"), tags("workers"));
service.createEksManagedGroup(REGION, FIRST, 1, 3, 2, List.of("subnet-a", "subnet-unknown"), nodeGroupTags("workers"));
AutoScalingGroup group = service.requireGroup(REGION, FIRST);
assertTrue(group.isEksManaged());
@@ -82,7 +80,7 @@ class AutoScalingEksManagedGroupTest {
assertEquals(2, group.getDesiredCapacity());
assertEquals(List.of("us-east-1a"), group.getAvailabilityZones());
assertEquals(List.of("subnet-a", "subnet-unknown"), group.getSubnetIds());
assertEquals(tags("workers"), group.getTags());
assertEquals(nodeGroupTags("workers"), group.getTags());
assertEquals(Map.of("eks:cluster-name", true, "eks:nodegroup-name", true,
"k8s.io/cluster-autoscaler/enabled", true, "k8s.io/cluster-autoscaler/shop", true),
group.getTagPropagateAtLaunch());
@@ -100,12 +98,12 @@ class AutoScalingEksManagedGroupTest {
assertEquals(1, group.getMinSize());
assertEquals(3, group.getMaxSize());
assertEquals(2, group.getDesiredCapacity());
assertEquals(tags("workers"), group.getTags());
assertEquals(nodeGroupTags("workers"), group.getTags());
}
@Test
void anAddedNodeIsInServiceAndRegisteredWithTheTargetGroupsAlreadyAttached() {
service.createEksManagedGroup(REGION, FIRST, 1, 3, 2, List.of(), tags("workers"));
service.createEksManagedGroup(REGION, FIRST, 1, 3, 2, List.of(), nodeGroupTags("workers"));
service.attachLoadBalancerTargetGroups(REGION, FIRST, List.of("tg-attached"));
service.attachTrafficSources(REGION, FIRST, List.of(
new AutoScalingService.TrafficSourceIdentifier("tg-source", "elbv2"),
@@ -131,7 +129,7 @@ class AutoScalingEksManagedGroupTest {
@Test
void attachingAndDetachingTargetGroupsRegistersAndDeregistersTheNode() {
service.createEksManagedGroup(REGION, FIRST, 1, 3, 2, List.of(), tags("workers"));
service.createEksManagedGroup(REGION, FIRST, 1, 3, 2, List.of(), nodeGroupTags("workers"));
service.addEksManagedInstance(REGION, FIRST, node());
service.attachLoadBalancerTargetGroups(REGION, FIRST, List.of("tg-a"));
@@ -158,7 +156,7 @@ class AutoScalingEksManagedGroupTest {
@Test
void aDetachElbV2RefusesLeavesTheTargetGroupAttachedSoTheDetachCanBeSentAgain() {
service.createEksManagedGroup(REGION, FIRST, 1, 3, 2, List.of(), tags("workers"));
service.createEksManagedGroup(REGION, FIRST, 1, 3, 2, List.of(), nodeGroupTags("workers"));
service.addEksManagedInstance(REGION, FIRST, node());
service.attachLoadBalancerTargetGroups(REGION, FIRST, List.of("tg-a"));
service.attachTrafficSources(REGION, FIRST, List.of(
@@ -186,7 +184,7 @@ class AutoScalingEksManagedGroupTest {
@Test
void detachingATargetGroupLeavesTheSharedNodeInItWhileAnotherGroupHasItAttached() {
groupsSharingTheNode();
groupsSharingTheNode(service);
service.attachLoadBalancerTargetGroups(REGION, FIRST, List.of("tg-shared"));
service.attachTrafficSources(REGION, SECOND, List.of(
new AutoScalingService.TrafficSourceIdentifier("tg-shared", "elbv2")));
@@ -205,7 +203,7 @@ class AutoScalingEksManagedGroupTest {
@Test
void detachingTheNodeLeavesTheTargetGroupsAnotherGroupHoldingItHasAttached() {
groupsSharingTheNode();
groupsSharingTheNode(service);
service.attachLoadBalancerTargetGroups(REGION, FIRST, List.of("tg-first", "tg-shared"));
service.attachTrafficSources(REGION, FIRST, List.of(
new AutoScalingService.TrafficSourceIdentifier("tg-source", "elbv2")));
@@ -229,7 +227,7 @@ class AutoScalingEksManagedGroupTest {
@ValueSource(booleans = {false, true})
void deletingAGroupLeavesTheSharedNodeInTheTargetGroupsAnotherGroupHoldsAndTerminatesNothing(
boolean throughAutoScaling) {
groupsSharingTheNode();
groupsSharingTheNode(service);
service.attachLoadBalancerTargetGroups(REGION, FIRST, List.of("tg-first", "tg-shared"));
service.attachLoadBalancerTargetGroups(REGION, SECOND, List.of("tg-shared"));
clearInvocations(targets);
@@ -256,7 +254,7 @@ class AutoScalingEksManagedGroupTest {
@Test
void attachingWhileAnotherGroupDetachesTheSameTargetGroupLeavesTheSharedNodeRegistered() throws Exception {
groupsSharingTheNode();
groupsSharingTheNode(service);
// The target group's registered targets, as ELB keeps them.
Set<String> registered = ConcurrentHashMap.newKeySet();
CountDownLatch deregistering = new CountDownLatch(1);
@@ -300,7 +298,7 @@ class AutoScalingEksManagedGroupTest {
@Test
void operationsThatWouldMoveOrReplaceTheNodeAreRefusedAndTheRestAreNot() {
service.createEksManagedGroup(REGION, FIRST, 1, 3, 2, List.of(), tags("workers"));
service.createEksManagedGroup(REGION, FIRST, 1, 3, 2, List.of(), nodeGroupTags("workers"));
service.addEksManagedInstance(REGION, FIRST, node());
assertRefused(() -> service.terminateInstanceInAutoScalingGroup(REGION, NODE, false));
@@ -347,31 +345,7 @@ class AutoScalingEksManagedGroupTest {
}
}
/** FIRST and SECOND, the groups of two node groups of one cluster, both holding its node. */
private void groupsSharingTheNode() {
service.createEksManagedGroup(REGION, FIRST, 1, 3, 2, List.of(), tags("workers"));
service.createEksManagedGroup(REGION, SECOND, 1, 1, 1, List.of(), tags("batch"));
service.addEksManagedInstance(REGION, FIRST, node());
service.addEksManagedInstance(REGION, SECOND, node());
}
private static Map<String, String> tags(String nodegroupName) {
return Map.of(
"eks:cluster-name", "shop",
"eks:nodegroup-name", nodegroupName,
"k8s.io/cluster-autoscaler/enabled", "true",
"k8s.io/cluster-autoscaler/shop", "owned");
}
private List<String> memberIds(String group) {
return service.requireGroup(REGION, group).getInstances().stream().map(AsgInstance::getInstanceId).toList();
}
private static Instance node() {
Instance node = new Instance();
node.setInstanceId(NODE);
node.setInstanceType("m5.large");
node.setPlacement(new Placement("us-east-1b"));
return node;
}
}
@@ -0,0 +1,88 @@
package io.floci.aws.services.autoscaling;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.services.autoscaling.model.AsgInstance;
import io.floci.aws.services.autoscaling.model.AutoScalingGroup;
import io.floci.aws.services.ec2.Ec2Service;
import io.floci.aws.services.ec2.model.Instance;
import io.floci.aws.services.ec2.model.Placement;
import io.floci.aws.services.elb.ElbClassicService;
import io.floci.aws.services.elbv2.ElbV2Service;
import java.util.List;
import java.util.Map;
import static org.mockito.Mockito.mock;
/**
* The groups the load balancer attachment tests share: an ordinary group with one member in service and one in
* standby, and the groups of two node groups of one EKS cluster, which both hold the cluster's one node, as Floci runs
* a cluster.
*/
final class AutoScalingGroupFixtures {
static final String REGION = "us-east-1";
static final String GROUP = "web";
static final String IN_SERVICE = "i-0a1b2c3d4e5f60001";
static final String STANDBY = "i-0a1b2c3d4e5f60002";
static final String FIRST = "eks-workers-1a2b3c4d";
static final String SECOND = "eks-batch-5e6f7a8b";
static final String NODE = "i-0123456789abcdef0";
private AutoScalingGroupFixtures() {
}
/** A service in {@link #REGION} on the given load balancer services, with EC2 mocked. */
static AutoScalingService service(ElbV2Service targets, ElbClassicService classic) {
AutoScalingService service = new AutoScalingService(targets, classic);
service.regionResolver = new RegionResolver(REGION, "000000000000");
service.ec2Service = mock(Ec2Service.class);
return service;
}
/**
* {@link #GROUP}, an ordinary group of {@link #REGION} holding {@link #IN_SERVICE} in service and {@link #STANDBY}
* in standby, stored in {@code service}.
*/
static AutoScalingGroup ordinaryGroup(AutoScalingService service) {
AutoScalingGroup group = new AutoScalingGroup();
group.setAutoScalingGroupName(GROUP);
group.setRegion(REGION);
group.getInstances().addAll(List.of(member(IN_SERVICE, "InService"), member(STANDBY, "Standby")));
service.groups.put(REGION + "::" + GROUP, group);
return group;
}
static AsgInstance member(String id, String state) {
AsgInstance member = new AsgInstance();
member.setInstanceId(id);
member.setLifecycleState(state);
return member;
}
/** {@link #FIRST} and {@link #SECOND}, the groups of two node groups of one cluster, both holding its node. */
static void groupsSharingTheNode(AutoScalingService service) {
service.createEksManagedGroup(REGION, FIRST, 1, 3, 2, List.of(), nodeGroupTags("workers"));
service.createEksManagedGroup(REGION, SECOND, 1, 1, 1, List.of(), nodeGroupTags("batch"));
service.addEksManagedInstance(REGION, FIRST, node());
service.addEksManagedInstance(REGION, SECOND, node());
}
/** The tags EKS gives the group of the node group {@code nodegroupName} of the cluster {@code shop}. */
static Map<String, String> nodeGroupTags(String nodegroupName) {
return Map.of(
"eks:cluster-name", "shop",
"eks:nodegroup-name", nodegroupName,
"k8s.io/cluster-autoscaler/enabled", "true",
"k8s.io/cluster-autoscaler/shop", "owned");
}
/** {@link #NODE}, the cluster's node, as EKS adds it to a node group's group. */
static Instance node() {
Instance node = new Instance();
node.setInstanceId(NODE);
node.setInstanceType("m5.large");
node.setPlacement(new Placement("us-east-1b"));
return node;
}
}
@@ -807,10 +807,9 @@ class AutoScalingStandbyTest {
doThrow(failure).doNothing().when(elbV2).registerTargets(eq(REGION), eq(TARGET_GROUP), anyList());
}
// Target groups are registered through AutoScalingService.registerWithTargetGroups, which logs there.
List<LogRecord> records = LogCapture.capture(
classicFailure ? AutoScalingReconciler.class : AutoScalingService.class,
() -> reconciler.reconcile(group()));
// Both are registered through AutoScalingService's registerWithTargetGroups and
// registerWithClassicLoadBalancers, which log there.
List<LogRecord> records = LogCapture.capture(AutoScalingService.class, () -> reconciler.reconcile(group()));
List<LogRecord> warnings = records.stream()
.filter(record -> record.getLevel().intValue() == Level.WARNING.intValue()).toList();
@@ -1,10 +1,7 @@
package io.floci.aws.services.autoscaling;
import io.floci.aws.core.common.AwsException;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.services.autoscaling.model.AsgInstance;
import io.floci.aws.services.autoscaling.model.AutoScalingGroup;
import io.floci.aws.services.ec2.Ec2Service;
import io.floci.aws.services.elb.ElbClassicService;
import io.floci.aws.services.elbv2.ElbV2Service;
import io.floci.aws.services.elbv2.model.TargetDescription;
@@ -16,6 +13,11 @@ import java.util.Arrays;
import java.util.List;
import java.util.Map;
import static io.floci.aws.services.autoscaling.AutoScalingGroupFixtures.GROUP;
import static io.floci.aws.services.autoscaling.AutoScalingGroupFixtures.IN_SERVICE;
import static io.floci.aws.services.autoscaling.AutoScalingGroupFixtures.REGION;
import static io.floci.aws.services.autoscaling.AutoScalingGroupFixtures.STANDBY;
import static io.floci.aws.services.autoscaling.AutoScalingGroupFixtures.ordinaryGroup;
import static io.floci.aws.services.autoscaling.TargetMatchers.exactlyTargets;
import static io.floci.aws.services.autoscaling.TargetMatchers.onlyTarget;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -39,23 +41,12 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
*/
class AutoScalingTargetGroupAttachmentTest {
private static final String REGION = "us-east-1";
private static final String GROUP = "web";
private static final String IN_SERVICE = "i-0a1b2c3d4e5f60001";
private static final String STANDBY = "i-0a1b2c3d4e5f60002";
private final ElbV2Service targets = mock(ElbV2Service.class);
private final AutoScalingService service = new AutoScalingService(targets, mock(ElbClassicService.class));
private final AutoScalingGroup group = new AutoScalingGroup();
private final AutoScalingService service = AutoScalingGroupFixtures.service(targets, mock(ElbClassicService.class));
private final AutoScalingGroup group = ordinaryGroup(service);
AutoScalingTargetGroupAttachmentTest() {
service.regionResolver = new RegionResolver(REGION, "000000000000");
service.ec2Service = mock(Ec2Service.class);
group.setAutoScalingGroupName(GROUP);
group.setRegion(REGION);
group.setTargetGroupARNs(new ArrayList<>(List.of("tg-attached")));
group.getInstances().addAll(List.of(member(IN_SERVICE, "InService"), member(STANDBY, "Standby")));
service.groups.put(REGION + "::" + GROUP, group);
}
@Test
@@ -149,11 +140,4 @@ class AutoScalingTargetGroupAttachmentTest {
assertEquals(List.of(), group.getTargetGroupARNs());
}
private static AsgInstance member(String id, String state) {
AsgInstance member = new AsgInstance();
member.setInstanceId(id);
member.setLifecycleState(state);
return member;
}
}