fix(autoscaling): validate MixedInstancesPolicy launch template (#1439)

Reject create/update requests whose MixedInstancesPolicy lacks a usable
LaunchTemplate (no LaunchTemplateId or LaunchTemplateName) with a
ValidationError, instead of persisting a group the reconciler can never
scale out. Also update the missing-launch-source message to mention all
valid sources (LaunchTemplate, LaunchConfigurationName, InstanceId,
MixedInstancesPolicy), matching AWS wording.

Follow-up to #1437.
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-06-19 00:07:27 -05:00
1 parent 9236b40073
commit dbad78991d
2 files changed
+99 -1

No files matched your search

@@ -4,6 +4,7 @@ import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
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 org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -317,6 +318,79 @@ class AutoScalingServiceTest {
assertTrue(service.describeAutoScalingGroups(REGION, List.of("test-asg")).isEmpty());
}
@Test
void createAutoScalingGroupRejectsMixedInstancesPolicyWithoutLaunchTemplate() {
MixedInstancesPolicy policy = new MixedInstancesPolicy();
MixedInstancesPolicy.InstancesDistribution distribution =
new MixedInstancesPolicy.InstancesDistribution();
distribution.setOnDemandBaseCapacity(1);
policy.setInstancesDistribution(distribution);
AwsException error = assertThrows(AwsException.class,
() -> createWithMixedInstancesPolicy("mixed-no-lt", policy));
assertEquals("ValidationError", error.getErrorCode());
assertEquals(400, error.getHttpStatus());
}
@Test
void createAutoScalingGroupRejectsMixedInstancesPolicyWithBlankLaunchTemplateIdentifiers() {
MixedInstancesPolicy policy = new MixedInstancesPolicy();
MixedInstancesPolicy.LaunchTemplate launchTemplate = new MixedInstancesPolicy.LaunchTemplate();
MixedInstancesPolicy.LaunchTemplateSpecification specification =
new MixedInstancesPolicy.LaunchTemplateSpecification();
specification.setLaunchTemplateId("");
specification.setLaunchTemplateName(" ");
launchTemplate.setLaunchTemplateSpecification(specification);
policy.setLaunchTemplate(launchTemplate);
AwsException error = assertThrows(AwsException.class,
() -> createWithMixedInstancesPolicy("mixed-blank-lt", policy));
assertEquals("ValidationError", error.getErrorCode());
assertEquals(400, error.getHttpStatus());
}
@Test
void createAutoScalingGroupAcceptsMixedInstancesPolicyWithLaunchTemplate() {
MixedInstancesPolicy policy = new MixedInstancesPolicy();
MixedInstancesPolicy.LaunchTemplate launchTemplate = new MixedInstancesPolicy.LaunchTemplate();
MixedInstancesPolicy.LaunchTemplateSpecification specification =
new MixedInstancesPolicy.LaunchTemplateSpecification();
specification.setLaunchTemplateId("lt-mixed");
launchTemplate.setLaunchTemplateSpecification(specification);
policy.setLaunchTemplate(launchTemplate);
createWithMixedInstancesPolicy("mixed-with-lt", policy);
var group = service.describeAutoScalingGroups(REGION, List.of("mixed-with-lt")).getFirst();
assertEquals("lt-mixed",
group.getMixedInstancesPolicy().getLaunchTemplate()
.getLaunchTemplateSpecification().getLaunchTemplateId());
}
private void createWithMixedInstancesPolicy(String name, MixedInstancesPolicy policy) {
service.createAutoScalingGroup(REGION,
name,
null,
null,
null,
null,
policy,
0,
3,
1,
300,
List.of("us-east-1a"),
List.of(),
List.of(),
List.of(),
"EC2",
0,
List.of("Default"),
java.util.Map.of());
}
private static final class AutoScalingGroupFixture {
private static void addInstance(AutoScalingService service, String region, String name, String instanceId,
String lifecycleState, String launchTemplateId, String launchTemplateVersion) {