diff --git a/src/main/java/com/dajudge/kindcontainer/ApiServerContainer.java b/src/main/java/com/dajudge/kindcontainer/ApiServerContainer.java index 403a181..d031023 100644 --- a/src/main/java/com/dajudge/kindcontainer/ApiServerContainer.java +++ b/src/main/java/com/dajudge/kindcontainer/ApiServerContainer.java @@ -13,6 +13,7 @@ import org.testcontainers.shaded.org.bouncycastle.asn1.x509.GeneralName; import org.testcontainers.utility.DockerImageName; +import java.time.Duration; import java.util.*; import java.util.stream.Collectors; @@ -51,6 +52,7 @@ public class ApiServerContainer> extends Kuberne new GeneralName(GeneralName.iPAddress, "127.0.0.1") )); private EtcdContainer etcd; + private Duration controlPlaneReadyTimeout = Duration.ofMinutes(5); /** * Constructs a new ApiServerContainer with the latest supported Kubernetes version. @@ -160,7 +162,7 @@ private void waitForApiServer() { .pollInterval(ofMillis(100)) .pollDelay(ZERO) .ignoreExceptions() - .forever() + .timeout(controlPlaneReadyTimeout) .until(() -> null != TinyK8sClient.fromKubeconfig(getKubeconfig()).v1().nodes().list()); } @@ -204,4 +206,15 @@ public void stop() { super.stop(); etcd.stop(); } + + /** + * Sets the timeout applied when waiting for the Kubernetes control plane to become ready. + * + * @param timeout the timeout + * @return this + */ + public T withControlPlaneReadyTimeout(final Duration timeout) { + this.controlPlaneReadyTimeout = timeout; + return self(); + } } diff --git a/src/test/java/com/dajudge/kindcontainer/ApiServerTest.java b/src/test/java/com/dajudge/kindcontainer/ApiServerTest.java new file mode 100644 index 0000000..356244b --- /dev/null +++ b/src/test/java/com/dajudge/kindcontainer/ApiServerTest.java @@ -0,0 +1,43 @@ +package com.dajudge.kindcontainer; + +import org.junit.jupiter.api.Test; + +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; + +import static com.dajudge.kindcontainer.KubernetesVersionEnum.latest; +import static java.time.Duration.ofSeconds; +import static java.util.concurrent.TimeUnit.MINUTES; +import static org.awaitility.Awaitility.await; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class ApiServerTest { + @Test + public void configurableTimeout() { + final AtomicBoolean containerCompleted = new AtomicBoolean(); + final AtomicReference containerFailed = new AtomicReference<>(); + final ApiServerContainer apiServer = new ApiServerContainer<>(latest(ApiServerContainerVersion.class).withImage("nginx")) + .withCreateContainerCmdModifier(cmd -> cmd.withEntrypoint().withCmd()) + .withCommand("nginx") + .withControlPlaneReadyTimeout(ofSeconds(1)); + final Thread containerThread = new Thread(() -> { + try { + apiServer.start(); + apiServer.stop(); + } catch (final Exception e) { + containerFailed.set(e); + } finally { + containerCompleted.set(true); + } + }); + try { + containerThread.start(); + + await().timeout(5, MINUTES).until(containerCompleted::get); + assertNotNull(containerFailed.get()); + } finally { + apiServer.stop(); + } + } + +}