Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

test: share one real EKS profile across the two k3s cluster classes (#6038)

Priamo Germosen39a164cd19fb
diff --git a/src/test/java/io/github/hectorvent/floci/services/eks/EksRegistryHostsDockerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/eks/EksRegistryHostsDockerIntegrationTest.java
index e50e98b042..9216adef09 100644
--- a/src/test/java/io/github/hectorvent/floci/services/eks/EksRegistryHostsDockerIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/eks/EksRegistryHostsDockerIntegrationTest.java
@@ -12,19 +12,20 @@ import io.github.hectorvent.floci.services.ec2.model.LaunchTemplate;
import io.github.hectorvent.floci.services.ec2.model.LaunchTemplateData;
import io.github.hectorvent.floci.services.eks.model.Cluster;
import io.github.hectorvent.floci.services.eks.model.ClusterStatus;
+import io.github.hectorvent.floci.testing.PartitionCleanup;
+import io.github.hectorvent.floci.testing.RealEksProfile;
import io.quarkus.test.junit.QuarkusTest;
-import io.quarkus.test.junit.QuarkusTestProfile;
import io.quarkus.test.junit.TestProfile;
import jakarta.inject.Inject;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.jboss.logging.Logger;
-import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
+import org.junit.jupiter.api.extension.RegisterExtension;
import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
@@ -34,7 +35,6 @@ import java.time.Instant;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
-import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.zip.GZIPOutputStream;
@@ -52,9 +52,13 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
* directory (/var/lib/rancher/k3s/agent/etc/containerd/certs.d) before container start, so standard
* user data works unchanged. A pod scheduled in the cluster pulls its image through the configured
* host, with the custom header that registries.yaml could not express.
+ *
+ * <p>The real cluster comes from {@link RealEksProfile}, which this class shares with the other
+ * classes that run a k3s cluster, so it removes the cluster, node group and launch template it
+ * creates.
*/
@QuarkusTest
-@TestProfile(EksRegistryHostsDockerIntegrationTest.Profile.class)
+@TestProfile(RealEksProfile.class)
// Bounded cluster/workload waits total 285 s; allow Docker setup and cleanup as well.
@Timeout(value = 6, unit = TimeUnit.MINUTES)
class EksRegistryHostsDockerIntegrationTest {
@@ -62,12 +66,8 @@ class EksRegistryHostsDockerIntegrationTest {
private static final Logger LOG = Logger.getLogger(EksRegistryHostsDockerIntegrationTest.class);
private static final String JSON = "application/json";
- public static final class Profile implements QuarkusTestProfile {
- @Override
- public Map<String, String> getConfigOverrides() {
- return Map.of("floci.services.eks.mock", "false");
- }
- }
+ @RegisterExtension
+ final PartitionCleanup cleanup = new PartitionCleanup();
@ConfigProperty(name = "quarkus.http.test-port", defaultValue = "8081")
int httpPort;
@@ -87,8 +87,6 @@ class EksRegistryHostsDockerIntegrationTest {
@Inject
DockerHostResolver dockerHostResolver;
- private String clusterName;
-
@BeforeEach
void requireDocker() {
boolean dockerAvailable = isDockerAvailable();
@@ -98,21 +96,11 @@ class EksRegistryHostsDockerIntegrationTest {
Assumptions.assumeTrue(dockerAvailable, "Docker daemon must be available for the registry-hosts integration test");
}
- @AfterEach
- void tearDown() {
- if (clusterName != null) {
- try {
- eksService.deleteCluster(clusterName);
- } catch (Exception e) {
- LOG.warnv("Failed to delete test cluster {0}: {1}", clusterName, e.getMessage());
- }
- }
- }
-
@Test
void podPullsThroughRegistryHostConfiguredViaLaunchTemplateUserData() throws Exception {
String suffix = UUID.randomUUID().toString().substring(0, 8);
- clusterName = "reg-hosts-dock-" + suffix;
+ String clusterName = "reg-hosts-dock-" + suffix;
+ String nodegroupName = "ng-" + suffix;
String repo = "reg-hosts-it-" + suffix;
// Not "000000000000": the generated ECR mirror only covers the default account, so this
// hostname is only reachable at all because of the registry-hosts configuration below.
@@ -126,6 +114,7 @@ class EksRegistryHostsDockerIntegrationTest {
+ "\"arn:aws:iam::000000000000:role/eks-service-role\"}")
.when().post("/clusters")
.then().statusCode(200);
+ cleanup.register(() -> eksService.deleteCluster(clusterName));
Cluster cluster = eksService.describeCluster(clusterName);
assertNotNull(cluster.getContainerId(), "Container ID must not be null");
@@ -165,11 +154,12 @@ class EksRegistryHostsDockerIntegrationTest {
LaunchTemplateData ltData = new LaunchTemplateData();
ltData.setUserData(Base64.getEncoder().encodeToString(userDataScript.getBytes(StandardCharsets.UTF_8)));
LaunchTemplate lt = ec2Service.createLaunchTemplate("us-east-1", "eks-hosts-lt-" + suffix, ltData, List.of());
+ cleanup.register(() -> ec2Service.deleteLaunchTemplate("us-east-1", lt.getLaunchTemplateId(), null));
given().contentType(JSON)
.body("""
{
- "nodegroupName": "ng-%s",
+ "nodegroupName": "%s",
"subnets": ["subnet-12345678"],
"nodeRole": "arn:aws:iam::000000000000:role/eks-node-role",
"launchTemplate": {
@@ -177,9 +167,10 @@ class EksRegistryHostsDockerIntegrationTest {
"version": "1"
}
}
- """.formatted(suffix, lt.getLaunchTemplateId()))
+ """.formatted(nodegroupName, lt.getLaunchTemplateId()))
.when().post("/clusters/" + clusterName + "/node-groups")
.then().statusCode(200);
+ cleanup.register(() -> eksService.deleteNodeGroup(clusterName, nodegroupName));
// Re-read cluster and container ID: applying the first node group replaces the k3s container
// to apply node capacity limits, invalidating the previous container ID.
diff --git a/src/test/java/io/github/hectorvent/floci/services/eks/EksStorageClassDockerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/eks/EksStorageClassDockerIntegrationTest.java
index 581d8c9b0b..68ca0c205d 100644
--- a/src/test/java/io/github/hectorvent/floci/services/eks/EksStorageClassDockerIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/eks/EksStorageClassDockerIntegrationTest.java
@@ -3,8 +3,8 @@ package io.github.hectorvent.floci.services.eks;
import com.github.dockerjava.api.DockerClient;
import io.github.hectorvent.floci.core.common.docker.ContainerExec;
import io.github.hectorvent.floci.services.eks.model.Cluster;
+import io.github.hectorvent.floci.testing.RealEksProfile;
import io.quarkus.test.junit.QuarkusTest;
-import io.quarkus.test.junit.QuarkusTestProfile;
import io.quarkus.test.junit.TestProfile;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
@@ -14,7 +14,6 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.time.Instant;
-import java.util.Map;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -25,23 +24,18 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Docker integration test verifying that freshly created clusters start with no StorageClass,
* no bundled local-path provisioner, and that CoreDNS remains functional.
+ *
+ * <p>The cluster is started through {@link EksClusterManager} directly, which runs a k3s container
+ * whether or not EKS is mocked. The class runs on {@link RealEksProfile} with the other classes
+ * that run a real cluster.
*/
@QuarkusTest
-@TestProfile(EksStorageClassDockerIntegrationTest.Profile.class)
+@TestProfile(RealEksProfile.class)
class EksStorageClassDockerIntegrationTest {
private static final Logger LOG = Logger.getLogger(EksStorageClassDockerIntegrationTest.class);
private static final String ACCOUNT = "000000000000";
- public static final class Profile implements QuarkusTestProfile {
- @Override
- public Map<String, String> getConfigOverrides() {
- return Map.of(
- "floci.services.eks.mock", "false"
- );
- }
- }
-
@Inject
DockerClient dockerClient;
diff --git a/src/test/java/io/github/hectorvent/floci/testing/RealEksProfile.java b/src/test/java/io/github/hectorvent/floci/testing/RealEksProfile.java
new file mode 100644
index 0000000000..05a321c9de
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/testing/RealEksProfile.java
@@ -0,0 +1,29 @@
+package io.github.hectorvent.floci.testing;
+
+import io.quarkus.test.junit.QuarkusTestProfile;
+
+import java.util.Map;
+
+/**
+ * Runs EKS clusters as real k3s containers, which the test defaults mock, for the classes that test
+ * what happens inside one. Quarkus builds the application once per distinct profile class, so they
+ * share this one rather than each declaring their own.
+ *
+ * <p>{@code floci.services.eks.mock=false} is the only override.
+ * {@code EksRegistryHostsDockerIntegrationTest} relies on it: it creates its cluster and node group
+ * through the EKS API, which starts no container while EKS is mocked.
+ * {@code EksStorageClassDockerIntegrationTest} passes with it off, since it starts its cluster
+ * through {@code EksClusterManager} directly, which runs a k3s container either way. It shares
+ * the profile because it runs a real cluster too. Each class removes the clusters it starts, so the
+ * next one finds no container left behind.
+ *
+ * <p>{@code EksIrsaDockerIntegrationTest} keeps a profile of its own: it also turns on IAM
+ * enforcement, which would decide every request the other classes send.
+ */
+public class RealEksProfile implements QuarkusTestProfile {
+
+ @Override
+ public Map<String, String> getConfigOverrides() {
+ return Map.of("floci.services.eks.mock", "false");
+ }
+}