Skip to content

Commit 261a604

Browse files
authored
Introduce customizable control-plane timeout for ApiServerContainer (#294)
1 parent 4ec19ec commit 261a604

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

src/main/java/com/dajudge/kindcontainer/ApiServerContainer.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.testcontainers.shaded.org.bouncycastle.asn1.x509.GeneralName;
1414
import org.testcontainers.utility.DockerImageName;
1515

16+
import java.time.Duration;
1617
import java.util.*;
1718
import java.util.stream.Collectors;
1819

@@ -51,6 +52,7 @@ public class ApiServerContainer<T extends ApiServerContainer<T>> extends Kuberne
5152
new GeneralName(GeneralName.iPAddress, "127.0.0.1")
5253
));
5354
private EtcdContainer etcd;
55+
private Duration controlPlaneReadyTimeout = Duration.ofMinutes(5);
5456

5557
/**
5658
* Constructs a new <code>ApiServerContainer</code> with the latest supported Kubernetes version.
@@ -160,7 +162,7 @@ private void waitForApiServer() {
160162
.pollInterval(ofMillis(100))
161163
.pollDelay(ZERO)
162164
.ignoreExceptions()
163-
.forever()
165+
.timeout(controlPlaneReadyTimeout)
164166
.until(() -> null != TinyK8sClient.fromKubeconfig(getKubeconfig()).v1().nodes().list());
165167
}
166168

@@ -204,4 +206,15 @@ public void stop() {
204206
super.stop();
205207
etcd.stop();
206208
}
209+
210+
/**
211+
* Sets the timeout applied when waiting for the Kubernetes control plane to become ready.
212+
*
213+
* @param timeout the timeout
214+
* @return <code>this</code>
215+
*/
216+
public T withControlPlaneReadyTimeout(final Duration timeout) {
217+
this.controlPlaneReadyTimeout = timeout;
218+
return self();
219+
}
207220
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.dajudge.kindcontainer;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.concurrent.atomic.AtomicBoolean;
6+
import java.util.concurrent.atomic.AtomicReference;
7+
8+
import static com.dajudge.kindcontainer.KubernetesVersionEnum.latest;
9+
import static java.time.Duration.ofSeconds;
10+
import static java.util.concurrent.TimeUnit.MINUTES;
11+
import static org.awaitility.Awaitility.await;
12+
import static org.junit.jupiter.api.Assertions.assertNotNull;
13+
14+
public class ApiServerTest {
15+
@Test
16+
public void configurableTimeout() {
17+
final AtomicBoolean containerCompleted = new AtomicBoolean();
18+
final AtomicReference<Exception> containerFailed = new AtomicReference<>();
19+
final ApiServerContainer<?> apiServer = new ApiServerContainer<>(latest(ApiServerContainerVersion.class).withImage("nginx"))
20+
.withCreateContainerCmdModifier(cmd -> cmd.withEntrypoint().withCmd())
21+
.withCommand("nginx")
22+
.withControlPlaneReadyTimeout(ofSeconds(1));
23+
final Thread containerThread = new Thread(() -> {
24+
try {
25+
apiServer.start();
26+
apiServer.stop();
27+
} catch (final Exception e) {
28+
containerFailed.set(e);
29+
} finally {
30+
containerCompleted.set(true);
31+
}
32+
});
33+
try {
34+
containerThread.start();
35+
36+
await().timeout(5, MINUTES).until(containerCompleted::get);
37+
assertNotNull(containerFailed.get());
38+
} finally {
39+
apiServer.stop();
40+
}
41+
}
42+
43+
}

0 commit comments

Comments
 (0)