fix(autoscaling): reject launch templates without image ids (#1451)

This commit is contained in:
Jason van Zyl authored and GitHub committed 2026-06-21 09:40:33 -05:00
1 parent 3f314dcb4e
commit e8b54e45e9
10 files changed
+562 -16

No files matched your search

@@ -16,10 +16,16 @@ class AutoScalingIntegrationTest {
private static final String AUTH =
"AWS4-HMAC-SHA256 Credential=test/20260501/us-east-1/autoscaling/aws4_request";
private static final String EC2_AUTH =
"AWS4-HMAC-SHA256 Credential=test/20260501/us-east-1/ec2/aws4_request";
private static String policyArn;
private static String instanceRefreshId;
private static String pagedInstanceRefreshId;
private static String launchTemplateId;
private static String refreshLaunchTemplateId;
private static String mixedLaunchTemplateId;
private static String replacementLaunchTemplateId;
// ── Launch Configurations ─────────────────────────────────────────────────
@@ -433,10 +439,12 @@ class AutoScalingIntegrationTest {
@Test
@Order(23)
void createAutoScalingGroupWithLaunchTemplateId() {
launchTemplateId = createLaunchTemplate("asg-integration-lt");
given()
.formParam("Action", "CreateAutoScalingGroup")
.formParam("AutoScalingGroupName", "my-lt-asg")
.formParam("LaunchTemplate.LaunchTemplateId", "lt-0123456789abcdef0")
.formParam("LaunchTemplate.LaunchTemplateId", launchTemplateId)
.formParam("LaunchTemplate.Version", "$Latest")
.formParam("MinSize", "0")
.formParam("MaxSize", "1")
@@ -477,7 +485,7 @@ class AutoScalingIntegrationTest {
.then()
.statusCode(200)
.body(containsString("my-lt-asg"))
.body(containsString("<LaunchTemplateId>lt-0123456789abcdef0</LaunchTemplateId>"))
.body(containsString("<LaunchTemplateId>" + launchTemplateId + "</LaunchTemplateId>"))
.body(containsString("<Version>$Latest</Version>"))
.body(containsString("<VPCZoneIdentifier>subnet-12345678,subnet-87654321</VPCZoneIdentifier>"))
.body(containsString("owner"))
@@ -509,11 +517,14 @@ class AutoScalingIntegrationTest {
@Test
@Order(25)
void startInstanceRefresh() {
refreshLaunchTemplateId = createLaunchTemplate("asg-integration-refresh-lt");
createLaunchTemplateVersion(refreshLaunchTemplateId, "t3.small");
instanceRefreshId = given()
.formParam("Action", "StartInstanceRefresh")
.formParam("AutoScalingGroupName", "my-lt-asg")
.formParam("Strategy", "Rolling")
.formParam("DesiredConfiguration.LaunchTemplate.LaunchTemplateId", "lt-0fedcba9876543210")
.formParam("DesiredConfiguration.LaunchTemplate.LaunchTemplateId", refreshLaunchTemplateId)
.formParam("DesiredConfiguration.LaunchTemplate.Version", "2")
.formParam("Preferences.MinHealthyPercentage", "90")
.formParam("Preferences.MaxHealthyPercentage", "120")
@@ -556,7 +567,7 @@ class AutoScalingIntegrationTest {
assertThat(body, containsString("<PercentageComplete>100</PercentageComplete>"));
assertThat(body, containsString("<InstancesToUpdate>0</InstancesToUpdate>"));
assertThat(body, containsString("<Strategy>Rolling</Strategy>"));
assertThat(body, containsString("<LaunchTemplateId>lt-0fedcba9876543210</LaunchTemplateId>"));
assertThat(body, containsString("<LaunchTemplateId>" + refreshLaunchTemplateId + "</LaunchTemplateId>"));
assertThat(body, containsString("<Version>2</Version>"));
assertThat(body, containsString("<MinHealthyPercentage>90</MinHealthyPercentage>"));
assertThat(body, containsString("<MaxHealthyPercentage>120</MaxHealthyPercentage>"));
@@ -573,7 +584,7 @@ class AutoScalingIntegrationTest {
.post("/")
.then()
.statusCode(200)
.body(containsString("<LaunchTemplateId>lt-0fedcba9876543210</LaunchTemplateId>"))
.body(containsString("<LaunchTemplateId>" + refreshLaunchTemplateId + "</LaunchTemplateId>"))
.body(containsString("<Version>2</Version>"));
}
@@ -624,11 +635,15 @@ class AutoScalingIntegrationTest {
@Test
@Order(28)
void createAutoScalingGroupWithMixedInstancesPolicy() {
mixedLaunchTemplateId = createLaunchTemplate("asg-integration-mixed-lt");
createLaunchTemplateVersion(mixedLaunchTemplateId, "t3.small");
createLaunchTemplateVersion(mixedLaunchTemplateId, "t3.medium");
given()
.formParam("Action", "CreateAutoScalingGroup")
.formParam("AutoScalingGroupName", "my-mixed-asg")
.formParam("MixedInstancesPolicy.LaunchTemplate.LaunchTemplateSpecification.LaunchTemplateId",
"lt-mixed")
mixedLaunchTemplateId)
.formParam("MixedInstancesPolicy.LaunchTemplate.LaunchTemplateSpecification.Version", "3")
.formParam("MixedInstancesPolicy.LaunchTemplate.Overrides.member.1.InstanceType", "t4g.medium")
.formParam("MixedInstancesPolicy.LaunchTemplate.Overrides.member.2.InstanceType", "m7g.large")
@@ -656,7 +671,7 @@ class AutoScalingIntegrationTest {
.statusCode(200)
.body(containsString("<MixedInstancesPolicy>"))
.body(containsString("<LaunchTemplateSpecification>"))
.body(containsString("<LaunchTemplateId>lt-mixed</LaunchTemplateId>"))
.body(containsString("<LaunchTemplateId>" + mixedLaunchTemplateId + "</LaunchTemplateId>"))
.body(containsString("<Version>3</Version>"))
.body(containsString("<InstanceType>t4g.medium</InstanceType>"))
.body(containsString("<InstanceType>m7g.large</InstanceType>"))
@@ -668,11 +683,16 @@ class AutoScalingIntegrationTest {
@Test
@Order(29)
void updateAutoScalingGroupFromLaunchTemplateToMixedInstancesPolicy() {
replacementLaunchTemplateId = createLaunchTemplate("asg-integration-replacement-lt");
createLaunchTemplateVersion(replacementLaunchTemplateId, "t3.small");
createLaunchTemplateVersion(replacementLaunchTemplateId, "t3.medium");
createLaunchTemplateVersion(replacementLaunchTemplateId, "t3.large");
given()
.formParam("Action", "UpdateAutoScalingGroup")
.formParam("AutoScalingGroupName", "my-lt-asg")
.formParam("MixedInstancesPolicy.LaunchTemplate.LaunchTemplateSpecification.LaunchTemplateId",
"lt-replacement")
replacementLaunchTemplateId)
.formParam("MixedInstancesPolicy.LaunchTemplate.LaunchTemplateSpecification.Version", "4")
.formParam("MixedInstancesPolicy.LaunchTemplate.Overrides.member.1.InstanceType", "c7g.large")
.formParam("MixedInstancesPolicy.InstancesDistribution.OnDemandBaseCapacity", "0")
@@ -694,7 +714,7 @@ class AutoScalingIntegrationTest {
.then()
.statusCode(200)
.body(containsString("<MixedInstancesPolicy>"))
.body(containsString("<LaunchTemplateId>lt-replacement</LaunchTemplateId>"))
.body(containsString("<LaunchTemplateId>" + replacementLaunchTemplateId + "</LaunchTemplateId>"))
.body(containsString("<Version>4</Version>"))
.body(containsString("<InstanceType>c7g.large</InstanceType>"))
.body(containsString("<OnDemandBaseCapacity>0</OnDemandBaseCapacity>"))
@@ -705,11 +725,14 @@ class AutoScalingIntegrationTest {
@Test
@Order(30)
void updateMixedInstancesPolicyOverridesAndDistribution() {
createLaunchTemplateVersion(mixedLaunchTemplateId, "t3.large");
createLaunchTemplateVersion(mixedLaunchTemplateId, "t3.xlarge");
given()
.formParam("Action", "UpdateAutoScalingGroup")
.formParam("AutoScalingGroupName", "my-mixed-asg")
.formParam("MixedInstancesPolicy.LaunchTemplate.LaunchTemplateSpecification.LaunchTemplateId",
"lt-mixed")
mixedLaunchTemplateId)
.formParam("MixedInstancesPolicy.LaunchTemplate.LaunchTemplateSpecification.Version", "5")
.formParam("MixedInstancesPolicy.LaunchTemplate.Overrides.member.1.InstanceType", "r7g.large")
.formParam("MixedInstancesPolicy.InstancesDistribution.OnDemandBaseCapacity", "2")
@@ -812,4 +835,31 @@ class AutoScalingIntegrationTest {
.statusCode(200)
.body(not(containsString("my-asg")));
}
private static String createLaunchTemplate(String name) {
return given()
.formParam("Action", "CreateLaunchTemplate")
.formParam("LaunchTemplateName", name)
.formParam("LaunchTemplateData.ImageId", "ami-12345678")
.formParam("LaunchTemplateData.InstanceType", "t3.micro")
.header("Authorization", EC2_AUTH)
.when()
.post("/")
.then()
.statusCode(200)
.extract().path("CreateLaunchTemplateResponse.launchTemplate.launchTemplateId");
}
private static void createLaunchTemplateVersion(String id, String instanceType) {
given()
.formParam("Action", "CreateLaunchTemplateVersion")
.formParam("LaunchTemplateId", id)
.formParam("LaunchTemplateData.ImageId", "ami-12345678")
.formParam("LaunchTemplateData.InstanceType", instanceType)
.header("Authorization", EC2_AUTH)
.when()
.post("/")
.then()
.statusCode(200);
}
}
@@ -6,6 +6,10 @@ import io.github.hectorvent.floci.services.autoscaling.model.AsgInstance;
import io.github.hectorvent.floci.services.autoscaling.model.InstanceRefresh;
import io.github.hectorvent.floci.services.autoscaling.model.MixedInstancesPolicy;
import io.github.hectorvent.floci.services.ec2.Ec2Service;
import io.github.hectorvent.floci.services.ec2.model.GroupIdentifier;
import io.github.hectorvent.floci.services.ec2.model.Instance;
import io.github.hectorvent.floci.services.ec2.model.LaunchTemplate;
import io.github.hectorvent.floci.services.ec2.model.Reservation;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -15,6 +19,7 @@ import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
class AutoScalingServiceTest {
@@ -88,6 +93,184 @@ class AutoScalingServiceTest {
assertNull(group.getLaunchConfigurationName());
}
@Test
void createAutoScalingGroupRejectsResolvedLaunchTemplateWithoutImageId() {
Ec2Service ec2Service = mock(Ec2Service.class);
service.ec2Service = ec2Service;
LaunchTemplate version = new LaunchTemplate();
version.setImageId(null);
when(ec2Service.describeLaunchTemplateVersions(REGION, "lt-no-image", null, List.of("1")))
.thenReturn(List.of(version));
AwsException error = assertThrows(AwsException.class, () -> service.createAutoScalingGroup(REGION,
"missing-image-asg",
null,
"lt-no-image",
null,
"1",
null,
0,
1,
1,
300,
List.of("us-east-1a"),
List.of(),
List.of(),
List.of(),
"EC2",
0,
List.of("Default"),
java.util.Map.of()));
assertEquals("ValidationError", error.getErrorCode());
assertEquals(AutoScalingService.MISSING_LAUNCH_TEMPLATE_IMAGE_ID_MESSAGE, error.getMessage());
assertEquals(400, error.getHttpStatus());
assertTrue(service.describeAutoScalingGroups(REGION, List.of("missing-image-asg")).isEmpty());
}
@Test
void createLaunchConfigurationRejectsMissingImageOrInstanceTypeWithoutInstanceId() {
AwsException missingImage = assertThrows(AwsException.class, () -> service.createLaunchConfiguration(REGION,
"lc-no-image",
null,
"",
"t3.micro",
null,
List.of(),
null,
null,
false));
assertEquals("ValidationError", missingImage.getErrorCode());
assertEquals(AutoScalingService.INVALID_LAUNCH_CONFIGURATION_PARAMETERS_MESSAGE, missingImage.getMessage());
assertEquals(400, missingImage.getHttpStatus());
assertTrue(service.describeLaunchConfigurations(REGION, List.of("lc-no-image")).isEmpty());
AwsException missingInstanceType = assertThrows(AwsException.class,
() -> service.createLaunchConfiguration(REGION,
"lc-no-instance-type",
null,
"ami-12345678",
null,
null,
List.of(),
null,
null,
false));
assertEquals("ValidationError", missingInstanceType.getErrorCode());
assertEquals(AutoScalingService.INVALID_LAUNCH_CONFIGURATION_PARAMETERS_MESSAGE,
missingInstanceType.getMessage());
assertEquals(400, missingInstanceType.getHttpStatus());
assertTrue(service.describeLaunchConfigurations(REGION, List.of("lc-no-instance-type")).isEmpty());
}
@Test
void createLaunchConfigurationWithInstanceIdCopiesSourceInstanceLaunchAttributes() {
Ec2Service ec2Service = mock(Ec2Service.class);
service.ec2Service = ec2Service;
Instance source = new Instance();
source.setInstanceId("i-1234567890abcdef0");
source.setImageId("ami-from-instance");
source.setInstanceType("m7g.large");
source.setKeyName("source-key");
source.setSecurityGroups(List.of(new GroupIdentifier("sg-source", "default")));
source.setUserData("source-user-data");
source.setIamInstanceProfileArn("arn:aws:iam::000000000000:instance-profile/source");
Reservation reservation = new Reservation();
reservation.getInstances().add(source);
when(ec2Service.describeInstances(REGION, List.of("i-1234567890abcdef0"), java.util.Map.of()))
.thenReturn(List.of(reservation));
var launchConfiguration = service.createLaunchConfiguration(REGION,
"lc-from-instance",
"i-1234567890abcdef0",
null,
null,
null,
List.of(),
null,
null,
false);
assertEquals("ami-from-instance", launchConfiguration.getImageId());
assertEquals("m7g.large", launchConfiguration.getInstanceType());
assertEquals("source-key", launchConfiguration.getKeyName());
assertEquals(List.of("sg-source"), launchConfiguration.getSecurityGroups());
assertEquals("source-user-data", launchConfiguration.getUserData());
assertEquals("arn:aws:iam::000000000000:instance-profile/source",
launchConfiguration.getIamInstanceProfile());
assertEquals(launchConfiguration,
service.describeLaunchConfigurations(REGION, List.of("lc-from-instance")).getFirst());
}
@Test
void createAutoScalingGroupRejectsUnresolvedLaunchTemplateBeforeMutation() {
Ec2Service ec2Service = mock(Ec2Service.class);
service.ec2Service = ec2Service;
when(ec2Service.describeLaunchTemplateVersions(REGION, "lt-missing", null, List.of("1")))
.thenReturn(List.of());
AwsException error = assertThrows(AwsException.class, () -> service.createAutoScalingGroup(REGION,
"missing-template-asg",
null,
"lt-missing",
null,
"1",
null,
0,
1,
1,
300,
List.of("us-east-1a"),
List.of(),
List.of(),
List.of(),
"EC2",
0,
List.of("Default"),
java.util.Map.of()));
assertEquals("ValidationError", error.getErrorCode());
assertEquals(AutoScalingService.INVALID_LAUNCH_TEMPLATE_MESSAGE, error.getMessage());
assertEquals(400, error.getHttpStatus());
assertTrue(service.describeAutoScalingGroups(REGION, List.of("missing-template-asg")).isEmpty());
}
@Test
void updateAutoScalingGroupRejectsResolvedLaunchTemplateWithoutImageIdBeforeMutation() {
Ec2Service ec2Service = mock(Ec2Service.class);
service.ec2Service = ec2Service;
LaunchTemplate version = new LaunchTemplate();
version.setImageId("");
when(ec2Service.describeLaunchTemplateVersions(REGION, "lt-no-image", null, List.of("2")))
.thenReturn(List.of(version));
AwsException error = assertThrows(AwsException.class, () -> service.updateAutoScalingGroup(
REGION,
"test-asg",
null,
"lt-no-image",
null,
"2",
null,
null,
5,
null,
null,
null,
null,
null,
null,
null));
assertEquals("ValidationError", error.getErrorCode());
assertEquals(AutoScalingService.MISSING_LAUNCH_TEMPLATE_IMAGE_ID_MESSAGE, error.getMessage());
var group = service.describeAutoScalingGroups(REGION, List.of("test-asg")).getFirst();
assertEquals("lt-original", group.getLaunchTemplateId());
assertEquals(3, group.getMaxSize());
}
@Test
void describeInstanceRefreshesPaginatesNewestFirst() {
InstanceRefresh first = service.startInstanceRefresh(REGION, "test-asg", new InstanceRefresh());
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.ec2;
import com.fasterxml.jackson.core.type.TypeReference;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
@@ -15,6 +16,7 @@ import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verifyNoInteractions;
@@ -37,6 +39,20 @@ class Ec2ServiceTest {
verifyNoInteractions(containerManager);
}
@Test
void runInstancesRequiresImageIdInsteadOfDefaulting() {
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new InMemoryStorageFactory());
AwsException error = assertThrows(AwsException.class, () -> service.runInstances(
"us-east-1", null, "t3.micro", 1, 1, null, List.of(), null, null,
List.of(), null, null));
assertEquals("MissingParameter", error.getErrorCode());
assertEquals("The request must contain the parameter ImageId", error.getMessage());
assertEquals(400, error.getHttpStatus());
}
@Test
void launchTemplateVersionInheritsOmittedFieldsFromRequestedSourceVersion() {
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),