mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(elbv2): answer SetSubnets with one ALB subnet with ValidationError (#6116)
* test(elbv2): expect ValidationError for SetSubnets with one ALB subnet SetSubnets on an Application Load Balancer with one subnet answers InvalidConfigurationRequest with a message of Floci's own, once the subnet has been looked up. The SetSubnets API reference states the rule CreateLoadBalancer states, subnets from at least two Availability Zones, and since #6098 CreateLoadBalancer answers one subnet with ValidationError "At least two subnets in two different Availability Zones must be specified", counted before any subnet is looked up. - ElbV2ServiceTest, ElbV2IntegrationTest (Query protocol) and the compatibility suite's ElbV2Test expect that error for SetSubnets with one subnet, in Subnets or in SubnetMappings, and for one subnet that does not exist, with the balancer left on its subnets. - CloudFormationElbV2SubnetUpdateIntegrationTest: an update that keeps the balancer and names one subnet rolls back with that error. - A Network or Gateway Load Balancer is still set onto one subnet, and a move onto no subnets, which a CloudFormation rollback makes for a balancer an earlier version saved without subnets, is still taken. - setSubnetsWithMissingSubnetReturnsSubnetNotFound sends an existing subnet beside the missing one, so it still reaches the lookup. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * fix(elbv2): answer SetSubnets with one ALB subnet with ValidationError SetSubnets on an Application Load Balancer with one subnet now answers ValidationError (400) "At least two subnets in two different Availability Zones must be specified", the answer CreateLoadBalancer gives since #6098, instead of InvalidConfigurationRequest with a message of Floci's own. setSubnets runs requireSubnets once it has looked the balancer up for its type and before any subnet is looked up, so one subnet that does not exist gets the same answer, as on create. One SubnetMappings entry goes through the same check. The answer follows the SetSubnets API reference, which states the two-zone rule for an Application Load Balancer, and the answer AWS gives CreateLoadBalancer; no public report shows what AWS answers SetSubnets with one subnet. AWS does give a Network Load Balancer's SetSubnets with no subnets CreateLoadBalancer's message. A CloudFormation update that keeps an Application Load Balancer and names one subnet moves it through setSubnets, so it now fails with this error and rolls back. A move onto no subnets is not counted, because a CloudFormation rollback moves a balancer an earlier version saved without subnets back onto none. A Network or Gateway Load Balancer can still be set onto one subnet. resolveSubnetVpcId drops its own one-subnet refusal, which neither caller reaches any more; its zone check still refuses an Application Load Balancer whose subnets are all in one Availability Zone. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * docs(elbv2): say where SetSubnets' one-subnet answer comes from Review follow-ups to the SetSubnets one-subnet fix: - The requireSubnets Javadoc scopes what AWS is known to answer to CreateLoadBalancer, and says SetSubnets gets the create message and order because no public report shows what AWS answers there. - The setSubnets comment says an empty list moves the balancer onto no subnets, rather than that the list is left alone. - docs/services/elb.md says the same about the SetSubnets answer, and instead of saying a Gateway Load Balancer can be set onto one subnet, that Floci does not enforce AWS's rule that SetSubnets keeps every subnet a Gateway Load Balancer already has. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * test(elbv2): keep the SetSubnets tests to answers ELBv2 gives Review follow-ups to the SetSubnets one-subnet fix: - ElbV2CfnProvisionerTest: the update with a blank SubnetId keeps a Network Load Balancer, for which SubnetNotFound is still the answer ELBv2 gives; an Application Load Balancer now gets the subnet count error first. The move-and-move-back test moves the kept balancer onto two subnets, as ELBv2 refuses an Application Load Balancer on one. - ElbV2ServiceTest: the move onto no subnets follows the rollback it stands for, a balancer saved without subnets moved onto subnets and back onto none. SetSubnets onto one subnet for a balancer that does not exist reports LoadBalancerNotFound before any subnet is looked up. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * docs(elbv2): word the SetSubnets count notes as the rules read Second review round on the SetSubnets one-subnet fix: - docs/services/elb.md no longer says Network and Gateway Load Balancers are not counted: they are, and a list that names a subnet cannot fail the count for them, while a SetSubnets with neither form is refused for every type once #6100 lands. It now says a Network Load Balancer can be set onto one subnet, and that AWS requires a Gateway Load Balancer's SetSubnets to list every subnet it is already on, which Floci does not enforce. - The setSubnets comment says "an earlier Floci version", as the tests do. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * test(elbv2): give the blank SubnetId update a typed balancer Second review round on the SetSubnets one-subnet fix: the update with a blank SubnetId in ElbV2CfnProvisionerTest names a Network Load Balancer in its template, and the balancer its describe stub returns is now a network one too, so the template, the stub and the Javadoc agree. The answer the stub gives, SubnetNotFound, is unchanged. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * docs(elbv2): scope the Gateway Load Balancer SetSubnets rule to Subnets Third review round on the SetSubnets one-subnet fix: the SetSubnets API reference states that a Gateway Load Balancer must include every subnet it is already on under Subnets only, not under SubnetMappings, so docs/services/elb.md now says the reference requires it of a Gateway Load Balancer's Subnets, rather than of its SetSubnets as a whole. Floci still does not enforce it. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * chore(elbv2): tidy what #6100 and the SetSubnets count both added With #6100 merged, this branch and it each documented and tested the same SetSubnets paths: - ElbV2ServiceTest keeps aRollbackOntoNoSubnetsIsNotCounted, which models the CloudFormation rollback that needs setSubnets to take an empty list, and drops #6100's aMoveBackOntoNoSubnetsIsStillTaken, which tested the same call on a balancer that still had subnets. The two missing-balancer tests now say which path each pins: the SetSubnets action with neither form, or setSubnets with one subnet. - The requireSubnets Javadoc names setSubnetsFromRequest beside setSubnets, and the setSubnets comment no longer repeats the empty-list reason its Javadoc gives. - docs/services/elb.md has one SetSubnets bullet instead of two. No behaviour changes. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * docs(elbv2): say what requireSubnets checks for SetSubnets Review of the merge with #6100: the requireSubnets Javadoc gave the two-zone rule as the reason setSubnetsFromRequest runs it, but that call refuses a SetSubnets that names no subnets, for every type, and requireSubnets only counts the list. It now says SetSubnets takes the same subnets as CreateLoadBalancer, which of the two methods runs the count on which list, and that the two-zone check comes after the subnets are looked up. Javadoc only. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * docs(elbv2): say what the folded SetSubnets bullet counts like Review of the tidy: once the two SetSubnets bullets in docs/services/elb.md became one, "counted the same way" followed the sentence about a SetSubnets with neither form, so it no longer read as CreateLoadBalancer's count. It now says a SetSubnets that names subnets is counted as CreateLoadBalancer counts them. 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:
1 parent
db76a5e5fd
commit
ab0737798d
7 files changed
+294
-39
No files matched your search
@@ -284,6 +284,30 @@ class ElbV2Test {
|
||||
.isInstanceOf(LoadBalancerNotFoundException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(9)
|
||||
@DisplayName("SetSubnets - an Application Load Balancer moved onto one subnet is a ValidationError")
|
||||
void setApplicationLoadBalancerOntoOneSubnet() {
|
||||
List<String> subnets = TestFixtures.defaultVpcSubnets();
|
||||
String arn = elb.createLoadBalancer(r -> r.name(TestFixtures.uniqueName("sdk-set-one"))
|
||||
.type(LoadBalancerTypeEnum.APPLICATION).subnets(subnets))
|
||||
.loadBalancers().get(0).loadBalancerArn();
|
||||
try {
|
||||
assertThatThrownBy(() -> elb.setSubnets(r -> r.loadBalancerArn(arn).subnets(subnets.get(0))))
|
||||
.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");
|
||||
});
|
||||
assertThat(elb.describeLoadBalancers(r -> r.loadBalancerArns(arn)).loadBalancers().get(0).availabilityZones())
|
||||
.extracting(zone -> zone.subnetId())
|
||||
.containsExactlyInAnyOrderElementsOf(subnets);
|
||||
} finally {
|
||||
elb.deleteLoadBalancer(r -> r.loadBalancerArn(arn));
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Target Groups ───────────────────────────────────────────────────────
|
||||
|
||||
@Test
|
||||
|
||||
@@ -93,8 +93,8 @@ HTTPS listeners require a default certificate. Supply one when creating a listen
|
||||
- Load balancer, target group, listener, rule, and tag state is persisted through Floci storage and rebuilt on service startup.
|
||||
- Load balancers are created in `active` state.
|
||||
- `CreateLoadBalancer` takes `Subnets` or `SubnetMappings` for every load balancer type, and both together are refused with `ValidationError`. An Application Load Balancer takes at least two: with one or none it returns `ValidationError` `At least two subnets in two different Availability Zones must be specified` and creates nothing. The count comes before any subnet is looked up, as on AWS, so one subnet that does not exist gets that error too. A Network or Gateway Load Balancer with neither returns `ValidationError` `At least one subnet must be specified`. CloudFormation refuses such an `AWS::ElasticLoadBalancingV2::LoadBalancer` the same way when it creates or replaces the balancer; an update that keeps the balancer and names no subnets leaves it on its subnets.
|
||||
- `SetSubnets`, like `CreateLoadBalancer`, takes `Subnets` or `SubnetMappings`, and both together are refused with `ValidationError`. With neither it returns the same `ValidationError` as `CreateLoadBalancer` for the balancer's type, `At least two subnets in two different Availability Zones must be specified` for an Application Load Balancer and `At least one subnet must be specified` for a Network or Gateway Load Balancer, and leaves the balancer on its subnets. A `SetSubnets` that names subnets is counted as `CreateLoadBalancer` counts them, following the two-zone rule its API reference states: one subnet for an Application Load Balancer, in `Subnets` or `SubnetMappings`, returns `ValidationError` `At least two subnets in two different Availability Zones must be specified` before any subnet is looked up, and leaves the balancer on its subnets. AWS does not document the error for this case, so Floci gives `CreateLoadBalancer`'s error, in the same order. A CloudFormation update that keeps an Application Load Balancer and names one subnet fails with that error and rolls back. A Network Load Balancer can be set onto one subnet. The `SetSubnets` API reference requires a Gateway Load Balancer's `Subnets` to include every subnet it is already on; Floci does not enforce that.
|
||||
- EC2 refuses deletion of subnets and VPCs still referenced by load balancers with `DependencyViolation`. These account- and region-scoped dependencies follow persisted load balancer state; changing subnets or deleting the load balancer releases its old references. A CloudFormation update that names other subnets in `Subnets` or `SubnetMappings` moves the load balancer the way `SetSubnets` does, and a rolled-back update moves it back. An independent target group does not prevent subnet or VPC deletion.
|
||||
- `SetSubnets`, like `CreateLoadBalancer`, takes `Subnets` or `SubnetMappings`, and both together are refused with `ValidationError`. With neither it returns the same `ValidationError` as `CreateLoadBalancer` for the balancer's type, `At least two subnets in two different Availability Zones must be specified` for an Application Load Balancer and `At least one subnet must be specified` for a Network or Gateway Load Balancer, and leaves the balancer on its subnets.
|
||||
- HTTP listener sockets are preserved when listener actions change and are restarted only when socket-level settings such as port change.
|
||||
- Instance targets are resolved through EC2 instance private addresses so local load balancer traffic can reach containers.
|
||||
- Target health starts in `initial` state with reason `Elb.RegistrationInProgress` and is updated by Floci's health checker when monitoring is active.
|
||||
|
||||
@@ -284,12 +284,17 @@ public class ElbV2Service implements ResourceProvider, ServerCertificateReferenc
|
||||
|
||||
/**
|
||||
* Every load balancer type takes {@code Subnets} or {@code SubnetMappings}, and an Application Load
|
||||
* Balancer takes at least two, so a request with fewer is refused before anything is created, with
|
||||
* the message AWS gives an Application or a Network Load Balancer. A Gateway Load Balancer, which
|
||||
* takes subnets the way a Network Load Balancer does, gets the Network one. AWS counts before it
|
||||
* looks a subnet up, so one subnet that does not exist gets this error too, not SubnetNotFound.
|
||||
* This runs after the duplicate-name check, because a CloudFormation stack update finds the
|
||||
* balancer it keeps through DuplicateLoadBalancerName, whatever subnets its template names.
|
||||
* Balancer takes at least two, so a CreateLoadBalancer with fewer is refused before anything is
|
||||
* created, with the message AWS gives an Application or a Network Load Balancer. A Gateway Load
|
||||
* Balancer, which takes subnets the way a Network Load Balancer does, gets the Network one. AWS
|
||||
* counts before it looks a subnet up, so one subnet that does not exist gets this error too, not
|
||||
* SubnetNotFound. On create this runs after the duplicate-name check, because a CloudFormation stack
|
||||
* update finds the balancer it keeps through DuplicateLoadBalancerName, whatever subnets its
|
||||
* template names. SetSubnets takes the same subnets, so {@link #setSubnetsFromRequest} runs this on
|
||||
* a SetSubnets that names none, and {@link #setSubnets} on a list that names some, once it has the
|
||||
* balancer's type; whether the subnets span two Availability Zones is checked after they are looked
|
||||
* up. No public report shows what AWS answers a SetSubnets with one subnet, so it gets the create
|
||||
* message and order.
|
||||
*/
|
||||
private static void requireSubnets(String lbType, List<String> subnets) {
|
||||
int count = subnets == null ? 0 : subnets.size();
|
||||
@@ -435,6 +440,10 @@ public class ElbV2Service implements ResourceProvider, ServerCertificateReferenc
|
||||
public void setSubnets(String region, String arn, List<String> subnets) {
|
||||
ec2Service.runWithSubnetReferencesHeld(region, () -> {
|
||||
LoadBalancer lb = requireLoadBalancer(region, arn);
|
||||
// Counted as on create, before any subnet is looked up; an empty list is taken, as above.
|
||||
if (subnets != null && !subnets.isEmpty()) {
|
||||
requireSubnets(lb.getType(), subnets);
|
||||
}
|
||||
String vpcId = resolveSubnetVpcId(region, lb.getType(), subnets);
|
||||
if (vpcId != null && lb.getVpcId() != null && !lb.getVpcId().equals(vpcId)) {
|
||||
throw new AwsException("InvalidConfigurationRequest",
|
||||
@@ -1423,11 +1432,6 @@ public class ElbV2Service implements ResourceProvider, ServerCertificateReferenc
|
||||
Map<String, Subnet> subnetsById = resolveSubnetsById(region, subnetIds);
|
||||
|
||||
if ("application".equals(lbType)) {
|
||||
if (subnetIds.size() < 2) {
|
||||
throw new AwsException("InvalidConfigurationRequest",
|
||||
"Application Load Balancers must be attached to subnets in at least two Availability Zones.", 400);
|
||||
}
|
||||
|
||||
long distinctAvailabilityZones = subnetIds.stream()
|
||||
.map(subnetsById::get)
|
||||
.map(Subnet::getAvailabilityZone)
|
||||
|
||||
+28
@@ -219,6 +219,34 @@ class CloudFormationElbV2SubnetUpdateIntegrationTest {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* An update that keeps the Application Load Balancer and names one subnet moves it with SetSubnets,
|
||||
* so the update fails with the ValidationError SetSubnets gives an Application Load Balancer on one
|
||||
* subnet, and the balancer stays on both of its subnets.
|
||||
*/
|
||||
@Test
|
||||
void anUpdateThatMovesTheApplicationLoadBalancerOntoOneSubnetRollsBack() {
|
||||
String stack = "alb-one-subnet-update-" + Long.toString(System.nanoTime(), 36);
|
||||
try {
|
||||
Map<String, String> created = createStack(stack,
|
||||
template(CIDR_B, false, List.of("SubnetA", "SubnetB"), ""));
|
||||
|
||||
Map<String, String> rolledBack = updateStack(stack,
|
||||
template(CIDR_B, false, List.of("SubnetA"), ""), "UPDATE_ROLLBACK_COMPLETE");
|
||||
|
||||
String events = request(CFN_AUTH, "DescribeStackEvents", "StackName", stack).statusCode(200)
|
||||
.extract().asString();
|
||||
List<String> reasons = XmlParser.extractAll(events, "ResourceStatusReason");
|
||||
assertTrue(reasons.stream().anyMatch(
|
||||
r -> r.contains("At least two subnets in two different Availability Zones must be specified")),
|
||||
String.join(" | ", reasons));
|
||||
assertEquals(Set.of(created.get("SubnetA"), created.get("SubnetB")),
|
||||
Set.copyOf(loadBalancerSubnets(rolledBack.get("Alb"))));
|
||||
} finally {
|
||||
deleteStack(stack);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The first rollback's move back fails, which leaves the load balancer on the subnet the update
|
||||
* added, so that subnet cannot be deleted either and the stack ends UPDATE_ROLLBACK_FAILED with
|
||||
|
||||
+13
-9
@@ -488,8 +488,8 @@ class ElbV2CfnProvisionerTest {
|
||||
assertEquals(Set.of("subnet-1", "subnet-2"), usedSubnets(created));
|
||||
|
||||
StackResource moved = keptLoadBalancerUpdatedTo(loadBalancerOn("web", "subnet-1", "subnet-2"),
|
||||
"subnet-3");
|
||||
assertEquals(Set.of("subnet-3"), usedSubnets(moved));
|
||||
"subnet-3", "subnet-4");
|
||||
assertEquals(Set.of("subnet-3", "subnet-4"), usedSubnets(moved));
|
||||
|
||||
assertTrue(provisioner.rollbackUpdate(moved));
|
||||
assertEquals(Set.of("subnet-1", "subnet-2"), usedSubnets(moved));
|
||||
@@ -672,18 +672,22 @@ class ElbV2CfnProvisionerTest {
|
||||
assertEquals("SubnetNotFound", e.getErrorCode());
|
||||
}
|
||||
|
||||
/** On an update of a kept load balancer, a blank {@code SubnetId} reaches SetSubnets for ELBv2 to refuse. */
|
||||
/**
|
||||
* On an update of a kept Network Load Balancer, a blank {@code SubnetId} reaches SetSubnets for ELBv2
|
||||
* to refuse as an unknown subnet. An Application Load Balancer would get the subnet count error first.
|
||||
*/
|
||||
@Test
|
||||
void anUpdateWithABlankSubnetIdIsRefusedByTheMoveAndKeepsNoSnapshot() {
|
||||
when(elb.createLoadBalancer(eq(REGION), eq("web"), isNull(), isNull(), isNull(), anyList(), anyList(), any()))
|
||||
.thenThrow(new AwsException("DuplicateLoadBalancerName", "exists", 400));
|
||||
when(elb.describeLoadBalancers(REGION, null, List.of("web"), null, null))
|
||||
.thenReturn(List.of(loadBalancerOn("web", "subnet-1", "subnet-2")));
|
||||
when(elb.createLoadBalancer(eq(REGION), eq("web"), isNull(), eq("network"), isNull(), anyList(), anyList(),
|
||||
any())).thenThrow(new AwsException("DuplicateLoadBalancerName", "exists", 400));
|
||||
LoadBalancer kept = loadBalancerOn("web", "subnet-1", "subnet-2");
|
||||
kept.setType("network");
|
||||
when(elb.describeLoadBalancers(REGION, null, List.of("web"), null, null)).thenReturn(List.of(kept));
|
||||
doThrow(new AwsException("SubnetNotFound", "The subnet ID ' ' does not exist", 400))
|
||||
.when(elb).setSubnets(REGION, LB_ARN, List.of(" "));
|
||||
ObjectNode props = mapper.createObjectNode().put("Name", "web");
|
||||
ObjectNode props = mapper.createObjectNode().put("Name", "web").put("Type", "network");
|
||||
props.putArray("SubnetMappings").addObject().put("SubnetId", " ");
|
||||
StackResource lb = resource("AWS::ElasticLoadBalancingV2::LoadBalancer", "Alb");
|
||||
StackResource lb = resource("AWS::ElasticLoadBalancingV2::LoadBalancer", "Nlb");
|
||||
lb.getAttributes().put("LoadBalancerName", "web");
|
||||
|
||||
AwsException e = assertThrows(AwsException.class, () -> provisioner.provision(lb, props, ctx(LB_ARN)));
|
||||
|
||||
@@ -1363,10 +1363,12 @@ class ElbV2IntegrationTest {
|
||||
.extract()
|
||||
.path("CreateLoadBalancerResponse.CreateLoadBalancerResult.LoadBalancers.member.LoadBalancerArn");
|
||||
|
||||
// Two subnets, so the Application Load Balancer's subnet count passes and the lookup is reached.
|
||||
given()
|
||||
.formParam("Action", "SetSubnets")
|
||||
.formParam("LoadBalancerArn", setSubnetsLbArn)
|
||||
.formParam("Subnets.member.1", "subnet-does-not-exist")
|
||||
.formParam("Subnets.member.1", Ec2Service.defaultSubnetId("us-east-1", "a"))
|
||||
.formParam("Subnets.member.2", "subnet-does-not-exist")
|
||||
.header("Authorization", AUTH)
|
||||
.when()
|
||||
.post("/")
|
||||
@@ -1375,6 +1377,132 @@ class ElbV2IntegrationTest {
|
||||
.body("ErrorResponse.Error.Code", equalTo("SubnetNotFound"));
|
||||
}
|
||||
|
||||
/**
|
||||
* SetSubnets counts the subnets the way CreateLoadBalancer does, so one subnet for an Application
|
||||
* Load Balancer, in Subnets or in SubnetMappings, is the ValidationError CreateLoadBalancer gives,
|
||||
* and the balancer stays on its subnets and in its VPC. The count comes before the subnet is
|
||||
* looked up, so one that does not exist gets it too.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@CsvSource(delimiter = '|', nullValues = "NONE", value = {
|
||||
"set-one-subnet|Subnets.member.1|a",
|
||||
"set-one-mapping|SubnetMappings.member.1.SubnetId|a",
|
||||
"set-one-missing-subnet|Subnets.member.1|NONE"
|
||||
})
|
||||
@Order(68)
|
||||
void setSubnetsOntoOneSubnetIsAValidationErrorForAnApplicationLoadBalancer(String name, String parameter,
|
||||
String zone) {
|
||||
String subnetA = Ec2Service.defaultSubnetId("us-east-1", "a");
|
||||
String subnetB = Ec2Service.defaultSubnetId("us-east-1", "b");
|
||||
String subnet = zone == null ? "subnet-does-not-exist" : Ec2Service.defaultSubnetId("us-east-1", zone);
|
||||
String arn = given()
|
||||
.formParam("Action", "CreateLoadBalancer")
|
||||
.formParam("Name", name)
|
||||
.formParam("Type", "application")
|
||||
.formParam("Subnets.member.1", subnetA)
|
||||
.formParam("Subnets.member.2", subnetB)
|
||||
.header("Authorization", AUTH)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract()
|
||||
.path("CreateLoadBalancerResponse.CreateLoadBalancerResult.LoadBalancers.member.LoadBalancerArn");
|
||||
|
||||
given()
|
||||
.formParam("Action", "SetSubnets")
|
||||
.formParam("LoadBalancerArn", arn)
|
||||
.formParam(parameter, subnet)
|
||||
.header("Authorization", AUTH)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("ValidationError"))
|
||||
.body("ErrorResponse.Error.Message",
|
||||
equalTo("At least two subnets in two different Availability Zones must be specified"));
|
||||
|
||||
given()
|
||||
.formParam("Action", "DescribeLoadBalancers")
|
||||
.formParam("LoadBalancerArns.member.1", arn)
|
||||
.header("Authorization", AUTH)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeLoadBalancersResponse.DescribeLoadBalancersResult.LoadBalancers.member.AvailabilityZones.member.SubnetId",
|
||||
contains(subnetA, subnetB))
|
||||
.body("DescribeLoadBalancersResponse.DescribeLoadBalancersResult.LoadBalancers.member.VpcId",
|
||||
equalTo(Ec2Service.defaultVpcId("us-east-1")));
|
||||
|
||||
given()
|
||||
.formParam("Action", "DeleteLoadBalancer")
|
||||
.formParam("LoadBalancerArn", arn)
|
||||
.header("Authorization", AUTH)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
/**
|
||||
* A Network or Gateway Load Balancer can be in a single Availability Zone, so SetSubnets onto one
|
||||
* subnet is taken: the Network Load Balancer leaves one of its two subnets, and the Gateway Load
|
||||
* Balancer, which AWS lets add subnets but not drop them, is set onto the one it is on.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@CsvSource({"network,true", "gateway,false"})
|
||||
@Order(69)
|
||||
void setSubnetsOntoOneSubnetIsAllowedForNetworkAndGatewayLoadBalancers(String type, boolean onTwoSubnets) {
|
||||
String subnetA = Ec2Service.defaultSubnetId("us-east-1", "a");
|
||||
RequestSpecification create = given()
|
||||
.formParam("Action", "CreateLoadBalancer")
|
||||
.formParam("Name", "set-one-subnet-" + type)
|
||||
.formParam("Type", type)
|
||||
.formParam("Subnets.member.1", subnetA)
|
||||
.header("Authorization", AUTH);
|
||||
if (onTwoSubnets) {
|
||||
create.formParam("Subnets.member.2", Ec2Service.defaultSubnetId("us-east-1", "b"));
|
||||
}
|
||||
String arn = create
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract()
|
||||
.path("CreateLoadBalancerResponse.CreateLoadBalancerResult.LoadBalancers.member.LoadBalancerArn");
|
||||
|
||||
given()
|
||||
.formParam("Action", "SetSubnets")
|
||||
.formParam("LoadBalancerArn", arn)
|
||||
.formParam("Subnets.member.1", subnetA)
|
||||
.header("Authorization", AUTH)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.formParam("Action", "DescribeLoadBalancers")
|
||||
.formParam("LoadBalancerArns.member.1", arn)
|
||||
.header("Authorization", AUTH)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeLoadBalancersResponse.DescribeLoadBalancersResult.LoadBalancers.member.AvailabilityZones.member.SubnetId",
|
||||
equalTo(subnetA));
|
||||
|
||||
given()
|
||||
.formParam("Action", "DeleteLoadBalancer")
|
||||
.formParam("LoadBalancerArn", arn)
|
||||
.header("Authorization", AUTH)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
/**
|
||||
* Every type takes Subnets or SubnetMappings, so CreateLoadBalancer with neither is a
|
||||
* ValidationError and no load balancer is created. The messages are the ones AWS gives an
|
||||
|
||||
@@ -214,7 +214,10 @@ class ElbV2ServiceTest {
|
||||
verify(ec2Service, never()).describeSubnets(anyString(), anyList(), any());
|
||||
}
|
||||
|
||||
/** The message depends on the balancer's type, so a balancer that does not exist is reported first. */
|
||||
/**
|
||||
* Through the SetSubnets action with neither form: the message depends on the balancer's type, so a
|
||||
* balancer that does not exist is reported first.
|
||||
*/
|
||||
@Test
|
||||
void setSubnetsForAMissingBalancerReportsItBeforeMissingSubnets() {
|
||||
MultivaluedHashMap<String, String> params = new MultivaluedHashMap<>();
|
||||
@@ -228,22 +231,6 @@ class ElbV2ServiceTest {
|
||||
assertTrue(xml.contains("<Code>LoadBalancerNotFound</Code>"), xml);
|
||||
}
|
||||
|
||||
/**
|
||||
* Only the SetSubnets action refuses an empty list. A CloudFormation rollback moves a kept
|
||||
* balancer back onto the subnets it had before the update, and for a balancer an earlier Floci
|
||||
* version saved without subnets that is none.
|
||||
*/
|
||||
@Test
|
||||
void aMoveBackOntoNoSubnetsIsStillTaken() {
|
||||
LoadBalancer lb = service.createLoadBalancer(REGION, "moved-back", "internal", "application", "ipv4",
|
||||
ALB_SUBNETS, List.of(), Map.of());
|
||||
|
||||
service.setSubnets(REGION, lb.getLoadBalancerArn(), List.of());
|
||||
|
||||
assertTrue(service.describeLoadBalancers(REGION, List.of(lb.getLoadBalancerArn()), null, null, null)
|
||||
.getFirst().getAvailabilityZones().isEmpty());
|
||||
}
|
||||
|
||||
/**
|
||||
* A taken name is reported before missing subnets: a CloudFormation stack update finds the
|
||||
* balancer it keeps through DuplicateLoadBalancerName, whatever subnets its template names.
|
||||
@@ -1021,4 +1008,84 @@ class ElbV2ServiceTest {
|
||||
assertEquals(List.of("subnet-c", "subnet-d"),
|
||||
moved.getAvailabilityZones().stream().map(AvailabilityZone::getSubnetId).toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* SetSubnets counts the subnets the way CreateLoadBalancer does: one subnet for an Application Load
|
||||
* Balancer gets the refusal CreateLoadBalancer gives it, before the subnet is looked up, and the
|
||||
* balancer keeps its zones and its VPC.
|
||||
*/
|
||||
@Test
|
||||
void anApplicationLoadBalancerMovedOntoOneSubnetIsRefusedBeforeTheSubnetIsLookedUp() {
|
||||
LoadBalancer lb = service.createLoadBalancer(REGION, "set-one-subnet", "internal", "application", "ipv4",
|
||||
ALB_SUBNETS, List.of(), Map.of());
|
||||
// Stubbed so that a lookup before the count would reach the zone check, not a SubnetNotFound from the mock.
|
||||
lenient().when(ec2Service.describeSubnets(REGION, List.of("subnet-a"), Map.of()))
|
||||
.thenReturn(List.of(subnet("subnet-a", REGION + "a")));
|
||||
clearInvocations(ec2Service);
|
||||
|
||||
AwsException refusal = assertThrows(AwsException.class,
|
||||
() -> service.setSubnets(REGION, lb.getLoadBalancerArn(), List.of("subnet-a")));
|
||||
|
||||
assertEquals("ValidationError", refusal.getErrorCode());
|
||||
assertEquals("At least two subnets in two different Availability Zones must be specified",
|
||||
refusal.getMessage());
|
||||
assertEquals(400, refusal.getHttpStatus());
|
||||
LoadBalancer kept = service.describeLoadBalancers(REGION, List.of(lb.getLoadBalancerArn()), null, null,
|
||||
null).getFirst();
|
||||
assertEquals(ALB_SUBNETS, kept.getAvailabilityZones().stream().map(AvailabilityZone::getSubnetId).toList());
|
||||
assertEquals("vpc-a", kept.getVpcId());
|
||||
verify(ec2Service, never()).describeSubnets(anyString(), anyList(), any());
|
||||
}
|
||||
|
||||
/**
|
||||
* A Network or Gateway Load Balancer can be in a single Availability Zone, so SetSubnets onto one
|
||||
* subnet is taken: the Network Load Balancer leaves one of its two subnets, and the Gateway Load
|
||||
* Balancer, which AWS lets add subnets but not drop them, is set onto the one it is on.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@CsvSource(delimiter = '|', value = {"network|subnet-a,subnet-b", "gateway|subnet-a"})
|
||||
void aNetworkOrGatewayLoadBalancerCanBeSetOntoOneSubnet(String type, String createdOn) {
|
||||
when(ec2Service.describeSubnets(REGION, List.of("subnet-a"), Map.of()))
|
||||
.thenReturn(List.of(subnet("subnet-a", REGION + "a")));
|
||||
LoadBalancer lb = service.createLoadBalancer(REGION, "set-one-" + type, "internal", type, "ipv4",
|
||||
List.of(createdOn.split(",")), List.of(), Map.of());
|
||||
|
||||
service.setSubnets(REGION, lb.getLoadBalancerArn(), List.of("subnet-a"));
|
||||
|
||||
assertEquals(List.of("subnet-a"), service.describeLoadBalancers(REGION, List.of(lb.getLoadBalancerArn()),
|
||||
null, null, null).getFirst().getAvailabilityZones().stream().map(AvailabilityZone::getSubnetId).toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* setSubnets counts only a list that names subnets; the SetSubnets action refuses an empty one before
|
||||
* it gets here. A CloudFormation update moves a balancer an earlier Floci version saved without
|
||||
* subnets onto the template's subnets, and when the update rolls back, the move back onto none is taken.
|
||||
*/
|
||||
@Test
|
||||
void aRollbackOntoNoSubnetsIsNotCounted() {
|
||||
LoadBalancer saved = service.createLoadBalancer(REGION, "saved-without-subnets", "internal", "application",
|
||||
"ipv4", ALB_SUBNETS, List.of(), Map.of());
|
||||
saved.setAvailabilityZones(new ArrayList<>());
|
||||
saved.setVpcId(null);
|
||||
service.setSubnets(REGION, saved.getLoadBalancerArn(), ALB_SUBNETS);
|
||||
|
||||
service.setSubnets(REGION, saved.getLoadBalancerArn(), List.of());
|
||||
|
||||
assertTrue(service.describeLoadBalancers(REGION, List.of(saved.getLoadBalancerArn()), null, null, null)
|
||||
.getFirst().getAvailabilityZones().isEmpty());
|
||||
}
|
||||
|
||||
/**
|
||||
* Through setSubnets with one subnet: the count depends on the balancer's type, so a balancer that
|
||||
* does not exist is reported first.
|
||||
*/
|
||||
@Test
|
||||
void setSubnetsOntoOneSubnetReportsAMissingBalancerFirst() {
|
||||
AwsException refusal = assertThrows(AwsException.class, () -> service.setSubnets(REGION,
|
||||
"arn:aws:elasticloadbalancing:" + REGION + ":000000000000:loadbalancer/app/missing/0123456789abcdef",
|
||||
List.of("subnet-a")));
|
||||
|
||||
assertEquals("LoadBalancerNotFound", refusal.getErrorCode());
|
||||
verify(ec2Service, never()).describeSubnets(anyString(), anyList(), any());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user