fix(elbv2): answer a single-subnet ALB with ValidationError (#6098)

* test(elbv2): expect ValidationError for a single-subnet ALB

AWS answers a CreateLoadBalancer for an Application Load Balancer with
one subnet with ValidationError (400) "At least two subnets in two
different Availability Zones must be specified", the answer it gives
one with none, and it counts the subnets before it looks them up.
Floci answers InvalidConfigurationRequest with a message of its own.

- ElbV2ServiceTest, ElbV2IntegrationTest (Query protocol),
  CloudFormationElbV2SubnetMappingsIntegrationTest and the
  compatibility suite's ElbV2Test expect that error, and no balancer,
  for one subnet in Subnets or SubnetMappings, with or without Type,
  and for one subnet that does not exist. A Network or Gateway Load
  Balancer on one subnet is still created.
- The tests that sent an Application Load Balancer one missing subnet
  to get SubnetNotFound now send it an existing subnet as well, or ask
  for a Network Load Balancer, so they still reach the subnet lookup.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* fix(elbv2): answer a single-subnet ALB with ValidationError

CreateLoadBalancer for an Application Load Balancer with one subnet
now answers ValidationError (400) "At least two subnets in two
different Availability Zones must be specified", as AWS does, instead
of InvalidConfigurationRequest with a message of Floci's own.
requireSubnets, which already refused a balancer with no subnets this
way, now refuses an Application Load Balancer with fewer than two. It
runs before any subnet is looked up, so one subnet that does not exist
gets the same answer, as on AWS. One SubnetMappings entry goes through
the same check, following the rule the API reference states for both
fields. CloudFormation creates and replacements go through it too.

A Network or Gateway Load Balancer on one subnet is still created.
SetSubnets with one subnet and two subnets in one Availability Zone
keep their InvalidConfigurationRequest answers.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* test(elbv2): cover a one-subnet ALB replacement

Review follow-ups:
- CloudFormationElbV2ReplacementIntegrationTest renames an Application
  Load Balancer onto one subnet: the update rolls back with
  CreateLoadBalancer's ValidationError, no balancer gets the new name,
  and the one it was to replace keeps both subnets.
- ElbV2CfnProvisioner's Javadoc no longer says a blank SubnetId is
  refused as an unknown subnet, since an Application Load Balancer
  with only that mapping now gets the subnet count error first.
- ElbV2ServiceTest says why its one-subnet test stubs a lookup that it
  then expects never to happen.
- Ec2DescribeSubnetsNotFoundTest says the missing subnet is sent
  alongside an existing one, rather than that its position matters.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* test(elbv2): give the one-subnet NLB and GWLB test its own order

Review follow-up: the test
createLoadBalancerOnOneSubnetIsAllowedForNetworkAndGatewayLoadBalancers
shared @Order(12) with
createLoadBalancerWithOneSubnetIsAValidationError, so JUnit ran the two
in no defined order. It now runs at @Order(78), after the other load
balancer subnet tests. Neither test depends on the other: each uses
its own load balancer names, and the network and gateway one deletes
the balancers it creates.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* test(elbv2): move the one-subnet NLB and GWLB test to order 79

The draft #6100 adds
setSubnetsWithNeitherSubnetsNorSubnetMappingsIsRefused at @Order(78)
in the same class, and the two branches merge without a conflict, so
whichever lands second would bring back a shared order. 79 is unused
on main and on both branches; no other order changes.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

---------

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>
This commit is contained in:
Jackson Price authored and GitHub committed 2026-10-20 17:19:07 -04:00
1 parent 73e890dbc7
commit 61a42591a9
9 files changed
+219 -24

No files matched your search

@@ -50,17 +50,19 @@ class Ec2DescribeSubnetsNotFoundTest {
/**
* describeSubnets is shared with the services that build on it, and each reports a missing
* subnet in its own words. ELBv2 owes its callers SubnetNotFound, so the EC2 error must not
* reach through.
* reach through. An Application Load Balancer takes at least two subnets, and one alone is refused
* before it is looked up, so the missing subnet is sent alongside an existing one.
*/
@Test
@DisplayName("ELBv2 still reports its own SubnetNotFound for a missing subnet")
void elbV2KeepsItsOwnSubnetError() {
String existing = TestFixtures.defaultVpcSubnets().get(0);
try (ElasticLoadBalancingV2Client elb = TestFixtures.elbV2Client()) {
ElasticLoadBalancingV2Exception error = assertThrows(
ElasticLoadBalancingV2Exception.class,
() -> elb.createLoadBalancer(r -> r.name("lb-" + UUID.randomUUID().toString().substring(0, 8))
.type("application")
.subnets(MISSING)));
.subnets(existing, MISSING)));
assertThat(error.awsErrorDetails().errorCode()).isEqualTo("SubnetNotFound");
}
}
@@ -242,6 +242,24 @@ class ElbV2Test {
.isInstanceOf(LoadBalancerNotFoundException.class);
}
@Test
@Order(7)
@DisplayName("CreateLoadBalancer - an Application Load Balancer on one subnet is a ValidationError")
void createApplicationLoadBalancerOnOneSubnet() {
String name = TestFixtures.uniqueName("sdk-one-subnet");
String subnet = TestFixtures.defaultVpcSubnets().get(0);
assertThatThrownBy(() -> elb.createLoadBalancer(r -> r.name(name).type(LoadBalancerTypeEnum.APPLICATION)
.subnets(subnet)))
.isInstanceOfSatisfying(ElasticLoadBalancingV2Exception.class, e -> {
assertThat(e.statusCode()).isEqualTo(400);
assertThat(e.awsErrorDetails().errorCode()).isEqualTo("ValidationError");
assertThat(e.awsErrorDetails().errorMessage())
.isEqualTo("At least two subnets in two different Availability Zones must be specified");
});
assertThatThrownBy(() -> elb.describeLoadBalancers(r -> r.names(name)))
.isInstanceOf(LoadBalancerNotFoundException.class);
}
// ─── Target Groups ───────────────────────────────────────────────────────
@Test