mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(ec2): serialize unique IPv6 allocation through VPC publication (#8536)
* fix(ec2): serialize unique IPv6 allocation through VPC publication
This commit is contained in:
1 parent
53ef87af1c
commit
0e86263ac0
3 files changed
+126
-10
No files matched your search
@@ -162,6 +162,49 @@ import static org.mockito.Mockito.withSettings;
|
||||
|
||||
class Ec2ServiceTest {
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"false, us-east-1", "true, us-east-1", "false, eu-west-1", "true, eu-west-1"})
|
||||
void ipv6AllocationWaitsForThePreviousVpcPublication(boolean associate, String secondRegion) throws Exception {
|
||||
AccountAwareStorageBackend<Vpc> vpcStore = spy(AccountAwareStorageBackend.<Vpc>inMemory("000000000000"));
|
||||
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
|
||||
mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class),
|
||||
new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory(Map.of("ec2-vpcs.json", vpcStore)));
|
||||
Vpc existing = service.createVpc(secondRegion, "10.112.0.0/16", false);
|
||||
CountDownLatch publishing = new CountDownLatch(1);
|
||||
CountDownLatch resume = new CountDownLatch(1);
|
||||
doAnswer(call -> {
|
||||
Vpc publishingVpc = call.getArgument(1);
|
||||
if ("10.111.0.0/16".equals(publishingVpc.getCidrBlock())) {
|
||||
publishing.countDown();
|
||||
assertTrue(resume.await(15, TimeUnit.SECONDS));
|
||||
}
|
||||
return call.callRealMethod();
|
||||
}).when(vpcStore).put(anyString(), any(Vpc.class));
|
||||
AtomicReference<Thread> allocating = new AtomicReference<>();
|
||||
ExecutorService workers = Executors.newFixedThreadPool(2);
|
||||
try {
|
||||
Future<Vpc> first = workers.submit(() -> service.createVpc("us-east-1", "10.111.0.0/16", false, true));
|
||||
assertTrue(publishing.await(5, TimeUnit.SECONDS));
|
||||
Future<String> second = workers.submit(() -> {
|
||||
allocating.set(Thread.currentThread());
|
||||
return associate
|
||||
? service.associateAmazonProvidedIpv6CidrBlock(secondRegion, existing.getVpcId()).getIpv6CidrBlock()
|
||||
: service.createVpc(secondRegion, "10.113.0.0/16", false, true)
|
||||
.getIpv6CidrBlockAssociationSet().getFirst().getIpv6CidrBlock();
|
||||
});
|
||||
awaitBlockedOrDone(allocating, second);
|
||||
assertFalse(second.isDone(), "A new allocation must wait until the previous block is visible in storage");
|
||||
resume.countDown();
|
||||
String firstBlock = first.get(5, TimeUnit.SECONDS).getIpv6CidrBlockAssociationSet().getFirst()
|
||||
.getIpv6CidrBlock();
|
||||
assertNotEquals(firstBlock, second.get(5, TimeUnit.SECONDS));
|
||||
} finally {
|
||||
resume.countDown();
|
||||
workers.shutdownNow();
|
||||
assertTrue(workers.awaitTermination(5, TimeUnit.SECONDS));
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"false, aws:autoscaling:groupName", "true, aws:autoscaling:groupName",
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package io.floci.aws.services.ec2;
|
||||
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -7,6 +8,10 @@ import org.junit.jupiter.api.Test;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
@@ -15,6 +20,7 @@ import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.matchesRegex;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
/**
|
||||
* CreateVpc ignored AmazonProvidedIpv6CidrBlock and no response carried an
|
||||
@@ -195,4 +201,36 @@ class Ec2VpcIpv6CidrBlockIntegrationTest {
|
||||
}
|
||||
return String.join(":", Arrays.copyOf(groups, kept)) + "::/56";
|
||||
}
|
||||
|
||||
/** AWS gives each VPC its own Amazon-provided IPv6 block; no two VPCs here share one either. */
|
||||
@Test
|
||||
void amazonProvidedBlocksAreDistinctAcrossVpcs() {
|
||||
Set<String> blocks = new HashSet<>();
|
||||
for (int i = 0; i < 40; i++) {
|
||||
String block = given()
|
||||
.formParam("Action", "CreateVpc")
|
||||
.formParam("CidrBlock", "10.120." + i + ".0/24")
|
||||
.formParam("AmazonProvidedIpv6CidrBlock", "true")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().path("CreateVpcResponse.vpc.ipv6CidrBlockAssociationSet.item.ipv6CidrBlock");
|
||||
assertEquals(true, blocks.add(block), "block handed out twice: " + block);
|
||||
}
|
||||
}
|
||||
|
||||
/** A drawn block another VPC holds is drawn again; a run of held draws is refused, never handed out twice. */
|
||||
@Test
|
||||
void aHeldBlockIsDrawnAgainAndExhaustionIsRefused() {
|
||||
Set<String> held = Set.of("2600:1f18:1:100::/56", "2600:1f18:2:200::/56");
|
||||
Iterator<String> draws = List.of("2600:1f18:1:100::/56", "2600:1f18:2:200::/56",
|
||||
"2600:1f18:3:300::/56").iterator();
|
||||
assertEquals("2600:1f18:3:300::/56", Ec2Service.unusedAmazonProvidedIpv6Block(held, draws::next));
|
||||
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> Ec2Service.unusedAmazonProvidedIpv6Block(held, () -> "2600:1f18:1:100::/56"));
|
||||
assertEquals("InsufficientCidrBlocks", refused.getErrorCode());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user