From 030d468fd3807f40408258dcd36fb404430cedbc Mon Sep 17 00:00:00 2001 From: Hector Ventura Date: Fri, 9 Oct 2026 22:11:00 -0500 Subject: [PATCH] test: add an opt-in integration suite against the real emulator images (#44) mvn test -Pintegration drives start, wait, status, env and stop for all four products through the CLI's own command tree, plus two AWS instances side by side, each container uniquely named on a free port and removed afterwards. Tests tagged integration are excluded from the default build. A new Integration workflow runs the suite on main, weekly and on demand. --- .github/workflows/integration.yml | 38 +++++ AGENTS.md | 1 + pom.xml | 15 ++ .../EmulatorLifecycleIntegrationTest.java | 139 ++++++++++++++++++ 4 files changed, 193 insertions(+) create mode 100644 .github/workflows/integration.yml create mode 100644 src/test/java/io/floci/cli/integration/EmulatorLifecycleIntegrationTest.java diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml new file mode 100644 index 0000000..a1bafbb --- /dev/null +++ b/.github/workflows/integration.yml @@ -0,0 +1,38 @@ +name: Integration + +# Drives the CLI against the real emulator images (start, wait, status, env, stop for each product, +# and two instances side by side). It pulls four images, so it runs on main and on demand rather +# than on every pull request. + +on: + push: + branches: [main] + schedule: + - cron: '0 6 * * 1' + workflow_dispatch: + +permissions: + contents: read + +jobs: + integration: + name: CLI against the emulator images + runs-on: ubuntu-latest + timeout-minutes: 30 + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-java@v4 + with: + java-version: '25' + distribution: 'temurin' + cache: maven + + - name: Pull emulator images + run: | + for image in floci/floci floci/floci-gcp floci/floci-az floci/floci-oci; do + docker pull "$image:latest" + done + + - name: Run integration tests + run: mvn test -Pintegration -B diff --git a/AGENTS.md b/AGENTS.md index dc15159..eafc09d 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -23,6 +23,7 @@ mvn compile # compile only mvn test # run all unit tests mvn test -Dtest=WaitCommandParseTest # run a single test class mvn test -Dtest=DockerVersionCheckTest#testVersionParsing # run a single test method +mvn test -Pintegration # integration suite: the CLI against real emulator images (needs Docker) mvn package # compile + test + produce target/floci.jar (fat JAR) mvn package -DskipTests # skip tests mvn package -Pnative -DskipTests # build native binary → target/floci (requires GraalVM) diff --git a/pom.xml b/pom.xml index 219e748..4fee441 100644 --- a/pom.xml +++ b/pom.xml @@ -27,6 +27,8 @@ 2.18.3 5.11.4 io.floci.cli.FlociCli + + integration @@ -96,6 +98,12 @@ org.apache.maven.plugins maven-surefire-plugin 3.5.5 + + + ${floci.test.groups} + ${floci.test.excludedGroups} + @@ -126,6 +134,13 @@ + + integration + + integration + none + + native diff --git a/src/test/java/io/floci/cli/integration/EmulatorLifecycleIntegrationTest.java b/src/test/java/io/floci/cli/integration/EmulatorLifecycleIntegrationTest.java new file mode 100644 index 0000000..4de6539 --- /dev/null +++ b/src/test/java/io/floci/cli/integration/EmulatorLifecycleIntegrationTest.java @@ -0,0 +1,139 @@ +package io.floci.cli.integration; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import io.floci.cli.FlociCli; +import io.floci.cli.config.ProfileStore; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.net.ServerSocket; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * BL-075: the CLI against the real emulator images, through the same command tree a user runs. + * Opt-in (needs Docker and the images): {@code mvn test -Pintegration}. + * + *

Every container gets a unique name and a free host port, so the suite never touches a + * developer's own instances, and each test removes what it started. + */ +@Tag("integration") +class EmulatorLifecycleIntegrationTest { + + private static final ObjectMapper JSON = new ObjectMapper(); + + @TempDir + Path tempDir; + + private final List started = new ArrayList<>(); + + private record Run(int exit, String out, String err) { + JsonNode json() { + try { + return JSON.readTree(out); + } catch (IOException e) { + throw new AssertionError("not JSON: " + out + "\nstderr: " + err, e); + } + } + } + + @AfterEach + void removeWhatWasStarted() throws Exception { + for (String container : started) { + new ProcessBuilder("docker", "rm", "-f", "--", container) + .redirectErrorStream(true) + .redirectOutput(ProcessBuilder.Redirect.DISCARD) + .start() + .waitFor(); + } + } + + private Run floci(String... args) { + PrintStream out = System.out; + PrintStream err = System.err; + ByteArrayOutputStream outBuf = new ByteArrayOutputStream(); + ByteArrayOutputStream errBuf = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outBuf)); + System.setErr(new PrintStream(errBuf)); + try { + int exit = FlociCli.buildCommandLine(new ProfileStore(tempDir)).execute(args); + return new Run(exit, outBuf.toString(), errBuf.toString()); + } finally { + System.setOut(out); + System.setErr(err); + } + } + + // The product's tree as args: AWS is the root tree. + private static String[] cmd(String product, String... rest) { + List all = new ArrayList<>(); + if (!product.equals("aws")) all.add(product); + all.addAll(List.of(rest)); + return all.toArray(String[]::new); + } + + private static int freePort() throws IOException { + try (ServerSocket socket = new ServerSocket(0)) { + return socket.getLocalPort(); + } + } + + private String startInstance(String product, String defaultContainer, int port) { + String container = defaultContainer + "-it" + UUID.randomUUID().toString().substring(0, 6); + started.add(container); + Run start = floci(cmd(product, "start", "--container", container, "--port", String.valueOf(port), "--detach")); + assertEquals(0, start.exit(), "start " + container + ": " + start.err()); + Run wait = floci(cmd(product, "wait", "--container", container, "--timeout", "120s")); + assertEquals(0, wait.exit(), "wait " + container + ": " + wait.err()); + return container; + } + + @ParameterizedTest(name = "{0}") + @CsvSource({"aws, floci", "gcp, floci-gcp", "az, floci-az", "oci, floci-oci"}) + void startWaitStatusEnvStop(String product, String defaultContainer) throws Exception { + int port = freePort(); + String container = startInstance(product, defaultContainer, port); + + JsonNode status = floci(cmd(product, "status", "--container", container, "-o", "json")).json(); + assertEquals("running", status.path("state").asText(), status.toString()); + assertTrue(status.path("reachable").asBoolean(), status.toString()); + assertTrue(status.path("endpoint").asText().endsWith(":" + port), status.toString()); + + Run env = floci(cmd(product, "env", "--container", container, "-o", "json")); + assertEquals(0, env.exit(), env.err()); + assertTrue(env.out().contains(String.valueOf(port)), "env names the instance's port: " + env.out()); + + Run stop = floci(cmd(product, "stop", "--container", container, "--remove")); + assertEquals(0, stop.exit(), stop.err()); + JsonNode after = floci(cmd(product, "status", "--container", container, "-o", "json")).json(); + assertNotEquals("running", after.path("state").asText(), after.toString()); + } + + /** Two instances of one product side by side: each is found by its container, on its own port. */ + @Test + void twoInstancesRunSideBySide() throws Exception { + int portA = freePort(); + String a = startInstance("aws", "floci", portA); + int portB = freePort(); + String b = startInstance("aws", "floci", portB); + + assertTrue(floci("status", "--container", a, "-o", "json").json().path("endpoint").asText().endsWith(":" + portA)); + assertTrue(floci("status", "--container", b, "-o", "json").json().path("endpoint").asText().endsWith(":" + portB)); + + assertEquals(0, floci("stop", "--container", a, "--remove").exit()); + JsonNode still = floci("status", "--container", b, "-o", "json").json(); + assertTrue(still.path("reachable").asBoolean(), "stopping one instance leaves the other up: " + still); + } +}