Light, fluffy, and always free - The AWS Local Emulator alternative
test(core): migrate remaining tests to free-port helpers (floci-io#4840) (#5547)
Victor Marquesfea7d7b0b13c
diff --git a/src/test/java/io/github/hectorvent/floci/config/IotMqttWebSocketProxyIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/config/IotMqttWebSocketProxyIntegrationTest.javaindex 7707e08e92..182e53c138 100644--- a/src/test/java/io/github/hectorvent/floci/config/IotMqttWebSocketProxyIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/config/IotMqttWebSocketProxyIntegrationTest.java@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.config;import io.github.hectorvent.floci.services.iot.IotMqttWebSocketIntegrationTest;import io.github.hectorvent.floci.testing.IotMqttTlsProfile;+import io.github.hectorvent.floci.testutil.FreePorts;import io.quarkus.test.junit.QuarkusTest;import io.quarkus.test.junit.TestProfile;import io.vertx.core.Vertx;@@ -20,13 +21,13 @@ import javax.net.ssl.SNIHostName;import javax.net.ssl.SSLHandshakeException;import javax.net.ssl.SSLParameters;import javax.net.ssl.SSLSocket;+import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.net.InetSocketAddress;-import java.net.ServerSocket;import java.net.Socket;import java.nio.charset.StandardCharsets;import java.util.List;@@ -107,10 +108,7 @@ class IotMqttWebSocketProxyIntegrationTest {}private int startProxy() throws Exception {- int publicPort;- try (ServerSocket probe = new ServerSocket(0)) {- publicPort = probe.getLocalPort();- }+ int publicPort = FreePorts.anyInterfacePort();EmulatorConfig config = mock(EmulatorConfig.class);EmulatorConfig.TlsConfig tls = mock(EmulatorConfig.TlsConfig.class);when(config.port()).thenReturn(publicPort);diff --git a/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayHttpProxyIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayHttpProxyIntegrationTest.javaindex 3a3158e6fd..5f8975fd43 100644--- a/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayHttpProxyIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayHttpProxyIntegrationTest.java@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.apigateway;import com.sun.net.httpserver.HttpExchange;import com.sun.net.httpserver.HttpServer;import io.github.hectorvent.floci.testutil.ExecuteApiRequestSigner;+import io.github.hectorvent.floci.testutil.FreePorts;import io.quarkus.test.junit.QuarkusTest;import io.restassured.RestAssured;import io.restassured.http.ContentType;@@ -15,7 +16,6 @@ import org.junit.jupiter.params.provider.CsvSource;import java.io.IOException;import java.net.InetSocketAddress;-import java.net.ServerSocket;import java.nio.charset.StandardCharsets;import java.time.Instant;import java.util.ArrayList;@@ -69,9 +69,7 @@ class ApiGatewayHttpProxyIntegrationTest {backendServer.start();backendPort = backendServer.getAddress().getPort();- try (ServerSocket probe = new ServerSocket(0)) {- deadPort = probe.getLocalPort();- }+ deadPort = FreePorts.anyInterfacePort();}@AfterAlldiff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/backend/DynamoDbLocalClientTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/backend/DynamoDbLocalClientTest.javaindex 65b403024d..64ccf3a024 100644--- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/backend/DynamoDbLocalClientTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/backend/DynamoDbLocalClientTest.java@@ -11,15 +11,14 @@ import io.github.hectorvent.floci.core.common.auth.SigV4RequestValidator;import io.github.hectorvent.floci.services.dynamodb.backend.DynamoDbOperations.Api;import io.github.hectorvent.floci.services.dynamodb.backend.DynamoDbOperations.Reply;import io.github.hectorvent.floci.services.dynamodb.backend.DynamoDbOperations.Scope;+import io.github.hectorvent.floci.testutil.FreePorts;import org.junit.jupiter.api.AfterEach;import org.junit.jupiter.api.Test;import java.io.IOException;import java.io.OutputStream;import java.net.ConnectException;-import java.net.InetAddress;import java.net.InetSocketAddress;-import java.net.ServerSocket;import java.net.URI;import java.net.http.HttpTimeoutException;import java.nio.charset.StandardCharsets;@@ -151,10 +150,7 @@ class DynamoDbLocalClientTest {@Testvoid connectionRefused() throws Exception {- int port;- try (ServerSocket socket = new ServerSocket(0, 1, InetAddress.getByName("127.0.0.1"))) {- port = socket.getLocalPort();- }+ int port = FreePorts.loopbackIpv4Port();client = new DynamoDbLocalClient(URI.create("http://127.0.0.1:" + port),Duration.ofSeconds(2), Duration.ofSeconds(2), mapper);diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2MetadataTestFixture.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2MetadataTestFixture.javaindex ecc9a23e1f..677f44640c 100644--- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2MetadataTestFixture.java+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2MetadataTestFixture.java@@ -4,9 +4,9 @@ import io.github.hectorvent.floci.config.EmulatorConfig;import io.github.hectorvent.floci.services.ec2.model.Instance;import io.github.hectorvent.floci.services.iam.IamService;import io.github.hectorvent.floci.testing.MutableClock;+import io.github.hectorvent.floci.testutil.FreePorts;import io.vertx.core.Vertx;-import java.net.ServerSocket;import java.net.URI;import java.net.http.HttpClient;import java.net.http.HttpRequest;@@ -46,10 +46,7 @@ final class Ec2MetadataTestFixture implements AutoCloseable {private void start(EmulatorConfig config, IamService iam, Instance registeredInstance,Clock serverClock) throws Exception {- int port;- try (ServerSocket socket = new ServerSocket(0)) {- port = socket.getLocalPort();- }+ int port = FreePorts.anyInterfacePort();when(config.services().ec2().imdsPort()).thenReturn(port);vertx = Vertx.vertx();server = new Ec2MetadataServer(vertx, config, iam, serverClock);diff --git a/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsCredentialsProxyDockerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsCredentialsProxyDockerIntegrationTest.javaindex 28be6829a0..fb1b7490c0 100644--- a/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsCredentialsProxyDockerIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsCredentialsProxyDockerIntegrationTest.java@@ -14,6 +14,7 @@ import io.github.hectorvent.floci.core.common.docker.ContainerStorageHelper;import io.github.hectorvent.floci.core.common.docker.DockerHostResolver;import io.github.hectorvent.floci.services.iam.IamService;import io.github.hectorvent.floci.testing.TestImages;+import io.github.hectorvent.floci.testutil.FreePorts;import io.quarkus.test.junit.QuarkusTest;import io.vertx.core.Vertx;import jakarta.inject.Inject;@@ -22,7 +23,6 @@ import org.junit.jupiter.api.Assumptions;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;-import java.net.ServerSocket;import java.nio.charset.StandardCharsets;import java.time.Instant;import java.util.List;@@ -125,10 +125,7 @@ class EcsCredentialsProxyDockerIntegrationTest {.post("/").then().statusCode(200);String roleArn = "arn:aws:iam::" + account + ":role/" + role;- int port;- try (ServerSocket socket = new ServerSocket(0)) {- port = socket.getLocalPort();- }+ int port = FreePorts.anyInterfacePort();EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);when(config.services().ecs().taskRoleCredentials().port()).thenReturn(port);when(config.services().ecs().taskRoleCredentials().ttlSeconds()).thenReturn(21600L);@@ -193,10 +190,7 @@ class EcsCredentialsProxyDockerIntegrationTest {CreateNetworkResponse created = dockerClient.createNetworkCmd().withName(network).exec();networkId = created.getId();- int port;- try (ServerSocket socket = new ServerSocket(0)) {- port = socket.getLocalPort();- }+ int port = FreePorts.anyInterfacePort();EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);when(config.services().ecs().taskRoleCredentials().proxyImage()).thenReturn("floci/network-helper:local");when(config.services().ecs().taskRoleCredentials().port()).thenReturn(port);diff --git a/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsTaskRoleCredentialsServerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsTaskRoleCredentialsServerIntegrationTest.javaindex fdba082ad0..84161f96c4 100644--- a/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsTaskRoleCredentialsServerIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsTaskRoleCredentialsServerIntegrationTest.java@@ -2,13 +2,13 @@ package io.github.hectorvent.floci.services.ecs.container;import io.github.hectorvent.floci.config.EmulatorConfig;import io.github.hectorvent.floci.services.iam.IamService;+import io.github.hectorvent.floci.testutil.FreePorts;import io.quarkus.test.junit.QuarkusTest;import io.vertx.core.Vertx;import io.vertx.core.json.JsonObject;import jakarta.inject.Inject;import org.junit.jupiter.api.Test;-import java.net.ServerSocket;import java.net.URI;import java.net.http.HttpClient;import java.net.http.HttpRequest;@@ -55,10 +55,7 @@ class EcsTaskRoleCredentialsServerIntegrationTest {String roleArn = "arn:aws:iam::" + account + ":role/" + role;EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);- int port;- try (ServerSocket socket = new ServerSocket(0)) {- port = socket.getLocalPort();- }+ int port = FreePorts.anyInterfacePort();when(config.services().ecs().taskRoleCredentials().port()).thenReturn(port);when(config.services().ecs().taskRoleCredentials().ttlSeconds()).thenReturn(21600L);diff --git a/src/test/java/io/github/hectorvent/floci/services/elasticache/proxy/AbstractRedisAuthProxyHandshakeTest.java b/src/test/java/io/github/hectorvent/floci/services/elasticache/proxy/AbstractRedisAuthProxyHandshakeTest.javaindex e18b96afbd..18eabce400 100644--- a/src/test/java/io/github/hectorvent/floci/services/elasticache/proxy/AbstractRedisAuthProxyHandshakeTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/elasticache/proxy/AbstractRedisAuthProxyHandshakeTest.java@@ -1,5 +1,6 @@package io.github.hectorvent.floci.services.elasticache.proxy;+import io.github.hectorvent.floci.testutil.FreePorts;import org.jboss.logging.Logger;import org.junit.jupiter.api.Test;@@ -293,10 +294,7 @@ class AbstractRedisAuthProxyHandshakeTest {private static TestProxy startProxy(ServerSocket backendServer, boolean authRequired,String username, String password,int handshakeTimeoutMillis) throws IOException {- int proxyPort;- try (ServerSocket freePort = new ServerSocket(0)) {- proxyPort = freePort.getLocalPort();- }+ int proxyPort = FreePorts.anyInterfacePort();TestProxy proxy = new TestProxy("127.0.0.1", backendServer.getLocalPort(), proxyPort,authRequired, username, password, handshakeTimeoutMillis);proxy.start(proxyPort);diff --git a/src/test/java/io/github/hectorvent/floci/services/elasticache/proxy/AbstractRedisAuthProxyRelayTest.java b/src/test/java/io/github/hectorvent/floci/services/elasticache/proxy/AbstractRedisAuthProxyRelayTest.javaindex 45ce14540c..198d498bf1 100644--- a/src/test/java/io/github/hectorvent/floci/services/elasticache/proxy/AbstractRedisAuthProxyRelayTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/elasticache/proxy/AbstractRedisAuthProxyRelayTest.java@@ -1,5 +1,6 @@package io.github.hectorvent.floci.services.elasticache.proxy;+import io.github.hectorvent.floci.testutil.FreePorts;import org.junit.jupiter.api.Test;import java.io.IOException;@@ -13,11 +14,9 @@ class AbstractRedisAuthProxyRelayTest {@Testvoid closingBackendReleasesClientConnection() throws Exception {- try (ServerSocket backendServer = new ServerSocket(0);- ServerSocket freePort = new ServerSocket(0)) {- int proxyPort = freePort.getLocalPort();+ try (ServerSocket backendServer = new ServerSocket(0)) {+ int proxyPort = FreePorts.anyInterfacePort();TestProxy proxy = new TestProxy("127.0.0.1", backendServer.getLocalPort());- freePort.close();proxy.start(proxyPort);try (Socket client = new Socket("127.0.0.1", proxyPort)) {@@ -38,11 +37,9 @@ class AbstractRedisAuthProxyRelayTest {@Testvoid clientHalfCloseStillAllowsBackendResponse() throws Exception {- try (ServerSocket backendServer = new ServerSocket(0);- ServerSocket freePort = new ServerSocket(0)) {- int proxyPort = freePort.getLocalPort();+ try (ServerSocket backendServer = new ServerSocket(0)) {+ int proxyPort = FreePorts.anyInterfacePort();TestProxy proxy = new TestProxy("127.0.0.1", backendServer.getLocalPort());- freePort.close();proxy.start(proxyPort);try (Socket client = new Socket("127.0.0.1", proxyPort)) {@@ -70,11 +67,9 @@ class AbstractRedisAuthProxyRelayTest {@Testvoid clientHalfCloseClosesRelayWhenBackendStaysOpenAfterResponse() throws Exception {- try (ServerSocket backendServer = new ServerSocket(0);- ServerSocket freePort = new ServerSocket(0)) {- int proxyPort = freePort.getLocalPort();+ try (ServerSocket backendServer = new ServerSocket(0)) {+ int proxyPort = FreePorts.anyInterfacePort();TestProxy proxy = new TestProxy("127.0.0.1", backendServer.getLocalPort());- freePort.close();proxy.start(proxyPort);try (Socket client = new Socket("127.0.0.1", proxyPort)) {@@ -101,11 +96,9 @@ class AbstractRedisAuthProxyRelayTest {@Testvoid keepsHealthyIdleConnectionOpenBeyondRelayShutdownDeadline() throws Exception {- try (ServerSocket backendServer = new ServerSocket(0);- ServerSocket freePort = new ServerSocket(0)) {- int proxyPort = freePort.getLocalPort();+ try (ServerSocket backendServer = new ServerSocket(0)) {+ int proxyPort = FreePorts.anyInterfacePort();TestProxy proxy = new TestProxy("127.0.0.1", backendServer.getLocalPort());- freePort.close();proxy.start(proxyPort);try (Socket client = new Socket("127.0.0.1", proxyPort)) {diff --git a/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2PreserveHostHeaderIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2PreserveHostHeaderIntegrationTest.javaindex 7d52a4a0cb..043d87aef0 100644--- a/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2PreserveHostHeaderIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2PreserveHostHeaderIntegrationTest.java@@ -1,6 +1,7 @@package io.github.hectorvent.floci.services.elbv2;import io.github.hectorvent.floci.testing.RealElbV2DataPlaneProfile;+import io.github.hectorvent.floci.testutil.FreePorts;import io.quarkus.test.junit.QuarkusTest;import io.quarkus.test.junit.TestProfile;import io.restassured.response.Response;@@ -20,7 +21,6 @@ import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStream;-import java.net.ServerSocket;import java.net.Socket;import java.nio.charset.StandardCharsets;import java.time.Duration;@@ -140,10 +140,7 @@ class ElbV2PreserveHostHeaderIntegrationTest {String loadBalancerArn = null;String targetGroupArn = null;String listenerArn = null;- int failedPort;- try (ServerSocket reservation = new ServerSocket(0)) {- failedPort = reservation.getLocalPort();- }+ int failedPort = FreePorts.anyInterfacePort();try {loadBalancerArn = createLoadBalancer();targetGroupArn = createTargetGroup(backend.actualPort());diff --git a/src/test/java/io/github/hectorvent/floci/services/mwaa/proxy/MwaaWebProxyCliOffloadTest.java b/src/test/java/io/github/hectorvent/floci/services/mwaa/proxy/MwaaWebProxyCliOffloadTest.javaindex 271586f8d0..84b406801c 100644--- a/src/test/java/io/github/hectorvent/floci/services/mwaa/proxy/MwaaWebProxyCliOffloadTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/mwaa/proxy/MwaaWebProxyCliOffloadTest.java@@ -1,6 +1,7 @@package io.github.hectorvent.floci.services.mwaa.proxy;import io.github.hectorvent.floci.core.common.docker.ContainerExec;+import io.github.hectorvent.floci.testutil.FreePorts;import io.vertx.core.Vertx;import io.vertx.core.VertxOptions;import io.vertx.core.buffer.Buffer;@@ -12,7 +13,6 @@ import io.vertx.core.json.JsonObject;import org.junit.jupiter.api.AfterEach;import org.junit.jupiter.api.Test;-import java.net.ServerSocket;import java.nio.charset.StandardCharsets;import java.util.ArrayList;import java.util.Base64;@@ -67,10 +67,7 @@ class MwaaWebProxyCliOffloadTest {MwaaWebProxy proxy = new MwaaWebProxy("env1", vertx, "127.0.0.1", backend.actualPort(),(environmentName, token) -> true, slowExecutor);- int proxyPort;- try (ServerSocket freePort = new ServerSocket(0)) {- proxyPort = freePort.getLocalPort();- }+ int proxyPort = FreePorts.anyInterfacePort();proxy.start(proxyPort);try {@@ -142,10 +139,7 @@ class MwaaWebProxyCliOffloadTest {};MwaaWebProxy proxy = new MwaaWebProxy("env1", vertx, "127.0.0.1", 1,(environmentName, token) -> true, slowExecutor);- int proxyPort;- try (ServerSocket freePort = new ServerSocket(0)) {- proxyPort = freePort.getLocalPort();- }+ int proxyPort = FreePorts.anyInterfacePort();proxy.start(proxyPort);try {@@ -192,10 +186,7 @@ class MwaaWebProxyCliOffloadTest {};MwaaWebProxy proxy = new MwaaWebProxy("env1", vertx, "127.0.0.1", 1,(environmentName, token) -> true, failingExecutor);- int proxyPort;- try (ServerSocket freePort = new ServerSocket(0)) {- proxyPort = freePort.getLocalPort();- }+ int proxyPort = FreePorts.anyInterfacePort();proxy.start(proxyPort);try {