File tree 2 files changed +57
-1
lines changed
main/java/com/dajudge/kindcontainer
test/java/com/dajudge/kindcontainer
2 files changed +57
-1
lines changed Original file line number Diff line number Diff line change 13
13
import org .testcontainers .shaded .org .bouncycastle .asn1 .x509 .GeneralName ;
14
14
import org .testcontainers .utility .DockerImageName ;
15
15
16
+ import java .time .Duration ;
16
17
import java .util .*;
17
18
import java .util .stream .Collectors ;
18
19
@@ -51,6 +52,7 @@ public class ApiServerContainer<T extends ApiServerContainer<T>> extends Kuberne
51
52
new GeneralName (GeneralName .iPAddress , "127.0.0.1" )
52
53
));
53
54
private EtcdContainer etcd ;
55
+ private Duration controlPlaneReadyTimeout = Duration .ofMinutes (5 );
54
56
55
57
/**
56
58
* Constructs a new <code>ApiServerContainer</code> with the latest supported Kubernetes version.
@@ -160,7 +162,7 @@ private void waitForApiServer() {
160
162
.pollInterval (ofMillis (100 ))
161
163
.pollDelay (ZERO )
162
164
.ignoreExceptions ()
163
- .forever ( )
165
+ .timeout ( controlPlaneReadyTimeout )
164
166
.until (() -> null != TinyK8sClient .fromKubeconfig (getKubeconfig ()).v1 ().nodes ().list ());
165
167
}
166
168
@@ -204,4 +206,15 @@ public void stop() {
204
206
super .stop ();
205
207
etcd .stop ();
206
208
}
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
+ }
207
220
}
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments