fix(autoscaling): use default launch template version when omitted (#7990)

* fix(autoscaling): use default launch template version when omitted

* test(autoscaling): clean up group before response assertion
This commit is contained in:
Jimmy Liu Jia authored and GitHub committed 2026-11-11 10:22:39 -04:00
1 parent 5e0f318a40
commit 11dafb441c
4 files changed
+144 -5

No files matched your search

@@ -964,7 +964,7 @@ public class AutoScalingReconciler {
asg.getRegion(),
launchTemplate.getLaunchTemplateId(),
null,
asg.getLaunchTemplateVersion() == null ? List.of() : List.of(asg.getLaunchTemplateVersion()))
AutoScalingService.launchTemplateVersionSelector(asg.getLaunchTemplateVersion()))
.getFirst();
String resolvedVersion = version.getLatestVersionNumber() != null
? version.getLatestVersionNumber()
@@ -997,7 +997,7 @@ public class AutoScalingReconciler {
asg.getRegion(),
mixedLaunchTemplate.getLaunchTemplateId(),
null,
specification.getVersion() == null ? List.of() : List.of(specification.getVersion()))
AutoScalingService.launchTemplateVersionSelector(specification.getVersion()))
.getFirst();
String resolvedVersion = version.getLatestVersionNumber() != null
? version.getLatestVersionNumber()
@@ -2069,12 +2069,11 @@ public class AutoScalingService implements PassedRoleLookup {
if (ec2Service == null) {
return;
}
List<String> versions = isBlank(launchTemplateVersion) ? List.of() : List.of(launchTemplateVersion);
List<LaunchTemplate> launchTemplateVersions = ec2Service.describeLaunchTemplateVersions(
region,
launchTemplateId,
launchTemplateName,
versions);
launchTemplateVersionSelector(launchTemplateVersion));
if (launchTemplateVersions.isEmpty()) {
throw invalidLaunchTemplate();
}
@@ -2107,7 +2106,7 @@ public class AutoScalingService implements PassedRoleLookup {
List<LaunchTemplate> versions;
try {
versions = ec2Service.describeLaunchTemplateVersions(region, templateId, templateName,
isBlank(version) ? List.of() : List.of(version));
launchTemplateVersionSelector(version));
} catch (AwsException e) {
// A version that does not exist is the operation's to refuse, and passes no role.
LOG.debugv("Launch template {0} has no version {1}, so it passes no role: {2}",
@@ -2153,6 +2152,10 @@ public class AutoScalingService implements PassedRoleLookup {
return value == null || value.isBlank();
}
static List<String> launchTemplateVersionSelector(String version) {
return List.of(isBlank(version) ? "$Default" : version);
}
private record LaunchIdentity(
String launchConfigurationName,
String launchTemplateId,
@@ -0,0 +1,124 @@
package io.floci.aws.services.autoscaling;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.response.ValidatableResponse;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import java.util.UUID;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
@QuarkusTest
class AutoScalingDefaultLaunchTemplateVersionIntegrationTest {
private static final String AUTH =
"AWS4-HMAC-SHA256 Credential=796200000000/20261111/us-east-1/autoscaling/aws4_request";
private static final String EC2_AUTH = AUTH.replace("/autoscaling/", "/ec2/");
@Inject
AutoScalingReconciler reconciler;
@Test
void omittedVersionUsesDefaultWhenLatestVersionHasNoImage() {
String name = "default-lt-" + UUID.randomUUID();
boolean groupCreated = false;
given().header("Authorization", EC2_AUTH)
.formParam("Action", "CreateLaunchTemplate")
.formParam("LaunchTemplateName", name)
.formParam("LaunchTemplateData.ImageId", "ami-12345678")
.formParam("LaunchTemplateData.InstanceType", "t3.micro")
.post("/").then().statusCode(200);
try {
given().header("Authorization", EC2_AUTH)
.formParam("Action", "CreateLaunchTemplateVersion")
.formParam("LaunchTemplateName", name)
.formParam("LaunchTemplateData.InstanceType", "t3.small")
.post("/").then().statusCode(200);
ValidatableResponse created = given().header("Authorization", AUTH)
.formParam("Action", "CreateAutoScalingGroup")
.formParam("AutoScalingGroupName", name)
.formParam("LaunchTemplate.LaunchTemplateName", name)
.formParam("MinSize", "0")
.formParam("MaxSize", "1")
.formParam("DesiredCapacity", "0")
.formParam("AvailabilityZones.member.1", "us-east-1a")
.post("/").then().statusCode(200);
groupCreated = true;
created.body(containsString("CreateAutoScalingGroupResponse"));
} finally {
if (groupCreated) {
given().header("Authorization", AUTH)
.formParam("Action", "DeleteAutoScalingGroup")
.formParam("AutoScalingGroupName", name)
.formParam("ForceDelete", "true")
.post("/").then().statusCode(200);
}
given().header("Authorization", EC2_AUTH)
.formParam("Action", "DeleteLaunchTemplate")
.formParam("LaunchTemplateName", name)
.post("/").then().statusCode(200);
}
}
@ParameterizedTest
@ValueSource(booleans = {false, true})
void omittedVersionLaunchesFromDefaultVersion(boolean mixedInstancesPolicy) {
String name = "default-launch-" + UUID.randomUUID();
boolean groupCreated = false;
given().header("Authorization", EC2_AUTH)
.formParam("Action", "CreateLaunchTemplate")
.formParam("LaunchTemplateName", name)
.formParam("LaunchTemplateData.ImageId", "ami-amazonlinux2023")
.formParam("LaunchTemplateData.InstanceType", "t3.micro")
.post("/").then().statusCode(200);
try {
given().header("Authorization", EC2_AUTH)
.formParam("Action", "CreateLaunchTemplateVersion")
.formParam("LaunchTemplateName", name)
.formParam("LaunchTemplateData.ImageId", "ami-amazonlinux2023")
.formParam("LaunchTemplateData.InstanceType", "t3.small")
.post("/").then().statusCode(200);
String prefix = mixedInstancesPolicy
? "MixedInstancesPolicy.LaunchTemplate.LaunchTemplateSpecification." : "LaunchTemplate.";
given().header("Authorization", AUTH)
.formParam("Action", "CreateAutoScalingGroup")
.formParam("AutoScalingGroupName", name)
.formParam(prefix + "LaunchTemplateName", name)
.formParam("MinSize", "0")
.formParam("MaxSize", "1")
.formParam("DesiredCapacity", "1")
.formParam("AvailabilityZones.member.1", "us-east-1a")
.post("/").then().statusCode(200);
groupCreated = true;
reconciler.reconcileAll();
String instance = "DescribeAutoScalingGroupsResponse.DescribeAutoScalingGroupsResult"
+ ".AutoScalingGroups.member.Instances.member";
given().header("Authorization", AUTH)
.formParam("Action", "DescribeAutoScalingGroups")
.formParam("AutoScalingGroupNames.member.1", name)
.post("/").then().statusCode(200)
.body(instance + ".size()", equalTo(1))
.body(instance + ".LaunchTemplate.Version", equalTo("1"));
} finally {
if (groupCreated) {
given().header("Authorization", AUTH)
.formParam("Action", "DeleteAutoScalingGroup")
.formParam("AutoScalingGroupName", name)
.formParam("ForceDelete", "true")
.post("/").then().statusCode(200);
}
given().header("Authorization", EC2_AUTH)
.formParam("Action", "DeleteLaunchTemplate")
.formParam("LaunchTemplateName", name)
.post("/").then().statusCode(200);
}
}
}
@@ -318,6 +318,17 @@ class AutoScalingServiceTest {
verifyNoInteractions(ec2Service);
}
@Test
void omittedLaunchTemplateVersionLooksUpDefaultProfile() {
Ec2Service ec2Service = mock(Ec2Service.class);
service.ec2Service = ec2Service;
when(ec2Service.describeLaunchTemplateVersions(REGION, "lt-original", null, List.of("$Default")))
.thenReturn(List.of(withProfile("default-profile")));
assertEquals(Optional.of("default-profile"),
service.launchTemplateInstanceProfile("lt-original", null, null, REGION));
}
private static LaunchTemplate withProfile(String profileName) {
LaunchTemplate version = new LaunchTemplate();
version.getData().setIamInstanceProfile(new LaunchTemplateData.IamInstanceProfile(null, profileName));
@@ -663,6 +674,7 @@ class AutoScalingServiceTest {
void describeInstanceRefreshesPaginatesNewestFirst() {
InstanceRefresh first = service.startInstanceRefresh(REGION, "test-asg", new InstanceRefresh());
InstanceRefresh second = service.startInstanceRefresh(REGION, "test-asg", new InstanceRefresh());
first.setStartTime(Instant.EPOCH);
AutoScalingService.InstanceRefreshPage firstPage =
service.describeInstanceRefreshes(REGION, "test-asg", List.of(), 1, null);