19
19
import static io .restassured .RestAssured .given ;
20
20
import static org .awaitility .Awaitility .await ;
21
21
import static org .hamcrest .core .Is .is ;
22
- import static org .junit .jupiter .api .Assertions .assertNotNull ;
22
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
23
+ import static org .junit .jupiter .api .Assertions .assertTrue ;
23
24
25
+ import java .io .IOException ;
24
26
import java .util .concurrent .TimeUnit ;
25
27
28
+ import org .apache .commons .lang3 .StringUtils ;
26
29
import org .junit .jupiter .api .Test ;
27
30
28
31
import io .fabric8 .kubernetes .api .model .ConfigMapBuilder ;
32
+ import io .fabric8 .kubernetes .api .model .Pod ;
33
+ import io .fabric8 .kubernetes .api .model .PodList ;
29
34
import io .fabric8 .kubernetes .client .KubernetesClient ;
30
- import io .fabric8 .kubernetes .client .dsl .ScalableResource ;
35
+ import io .fabric8 .kubernetes .client .LocalPortForward ;
36
+ import io .fabric8 .kubernetes .client .dsl .FilterWatchListDeletable ;
37
+ import io .fabric8 .kubernetes .client .internal .readiness .Readiness ;
31
38
32
39
public abstract class AbstractIT {
33
40
@@ -36,42 +43,59 @@ public abstract class AbstractIT {
36
43
protected static final String GREETING_PATH = "api/greeting" ;
37
44
38
45
@ Test
39
- public void testConfigMapLifecycle () {
46
+ public void testConfigMapLifecycle () throws IOException {
40
47
// Endpoint should say Hello at the beginning as ConfigMap must have been loaded before running the test
41
48
verifyEndpoint ("Hello" );
42
49
43
50
// Verify the name parameter is properly replaced in the greetings sentence.
44
- given ().param ("name" , "John" )
45
- .when ()
46
- .get (baseURL () + GREETING_PATH )
47
- .then ()
48
- .statusCode (200 )
49
- .body ("content" , is ("Hello John from a ConfigMap!" ));
51
+ try (LocalPortForward appPort = kubernetesClient ().services ().withName (GREETING_NAME ).portForward (8080 )) {
52
+ given ().param ("name" , "John" )
53
+ .get ("http://localhost:" + appPort .getLocalPort () + "/" + GREETING_PATH )
54
+ .then ()
55
+ .statusCode (200 )
56
+ .body ("content" , is ("Hello John from a ConfigMap!" ));
57
+ }
50
58
51
59
// Verify the app is updated when the config map changes
52
60
updateConfigMap ();
53
- rolloutChanges ();
54
- waitForApp ();
61
+ stopApplication ();
62
+ startApplication ();
55
63
verifyEndpoint ("Bonjour" );
56
64
57
65
// Verify the app is updated when the config map is deleted
58
66
deleteConfigMap ();
59
- rolloutChanges ();
67
+ stopApplication ();
68
+ startApplication ();
60
69
await ().atMost (5 , TimeUnit .MINUTES )
61
- .catchUncaughtExceptions ()
62
- .untilAsserted (() -> given ().get (baseURL () + GREETING_PATH )
63
- .then ().statusCode (500 ));
70
+ .ignoreExceptions ()
71
+ .untilAsserted (() -> {
72
+ try (LocalPortForward appPort = kubernetesClient ().services ().withName (GREETING_NAME ).portForward (8080 )) {
73
+ String message = given ().get ("http://localhost:" + appPort .getLocalPort () + "/" + GREETING_PATH + "/message" )
74
+ .then ().extract ().asString ();
75
+ assertTrue (StringUtils .isEmpty (message ));
76
+ }
77
+ }
78
+ );
64
79
}
65
80
66
- protected abstract String baseURL ();
67
81
protected abstract KubernetesClient kubernetesClient ();
68
- protected abstract ScalableResource <?> deployment ();
82
+
83
+ protected void stopApplication () {
84
+ pods ().delete ();
85
+ }
69
86
70
87
private void verifyEndpoint (final String greeting ) {
71
- given ().get (baseURL () + GREETING_PATH )
72
- .then ()
73
- .statusCode (200 )
74
- .body ("content" , is (String .format ("%s World from a ConfigMap!" , greeting )));
88
+ await ().atMost (5 , TimeUnit .MINUTES )
89
+ .ignoreExceptions ()
90
+ .untilAsserted (() -> {
91
+ try (LocalPortForward appPort = kubernetesClient ().services ().withName (GREETING_NAME ).portForward (8080 )) {
92
+ given ().get ("http://localhost:" + appPort .getLocalPort () + "/" + GREETING_PATH )
93
+ .then ()
94
+ .statusCode (200 )
95
+ .body ("content" , is (String .format ("%s World from a ConfigMap!" , greeting )));
96
+ }
97
+ }
98
+ );
75
99
}
76
100
77
101
private void updateConfigMap () {
@@ -88,31 +112,16 @@ private void deleteConfigMap() {
88
112
.delete ();
89
113
}
90
114
91
- private void rolloutChanges () {
92
- scale (0 );
93
- scale (1 );
94
- }
95
-
96
- private void scale (int replicas ) {
97
- ScalableResource <?> deployment = deployment ();
98
- assertNotNull (deployment , "Deployment with name '" + GREETING_NAME + "' not found!" );
99
- deployment .scale (replicas );
100
-
101
- await ().atMost (5 , TimeUnit .MINUTES )
102
- .until (() -> {
103
- ScalableResource <?> updatedDeployment = deployment ();
104
- return updatedDeployment != null && updatedDeployment .scale ().getStatus ().getReplicas () == replicas ;
105
- });
106
- }
107
-
108
- private void waitForApp () {
115
+ private void startApplication () {
109
116
await ().atMost (5 , TimeUnit .MINUTES )
110
117
.ignoreExceptions ()
111
118
.untilAsserted (
112
- () -> given ()
113
- .get (baseURL () + GREETING_PATH )
114
- .then ().statusCode (200 )
119
+ () -> assertEquals (1 , pods ().list ().getItems ().stream ().filter (Readiness ::isPodReady ).count ())
115
120
);
116
121
}
117
122
123
+ private FilterWatchListDeletable <Pod , PodList > pods () {
124
+ return kubernetesClient ().pods ().withLabel ("app" , "configmap" );
125
+ }
126
+
118
127
}
0 commit comments