12
12
*/
13
13
package io .kubernetes .client ;
14
14
15
- import static org .junit .Assert .*;
15
+ import static com .github .tomakehurst .wiremock .client .WireMock .aResponse ;
16
+ import static com .github .tomakehurst .wiremock .client .WireMock .equalTo ;
17
+ import static com .github .tomakehurst .wiremock .client .WireMock .get ;
18
+ import static com .github .tomakehurst .wiremock .client .WireMock .getRequestedFor ;
19
+ import static com .github .tomakehurst .wiremock .client .WireMock .stubFor ;
20
+ import static com .github .tomakehurst .wiremock .client .WireMock .urlEqualTo ;
21
+ import static com .github .tomakehurst .wiremock .client .WireMock .urlPathEqualTo ;
22
+ import static com .github .tomakehurst .wiremock .client .WireMock .verify ;
23
+ import static org .junit .Assert .assertEquals ;
16
24
25
+ import com .github .tomakehurst .wiremock .junit .WireMockRule ;
26
+
27
+ import io .kubernetes .client .ApiException ;
17
28
import io .kubernetes .client .util .ClientBuilder ;
29
+
18
30
import java .io .ByteArrayInputStream ;
19
31
import java .io .IOException ;
20
32
import java .io .InputStream ;
21
33
import java .nio .charset .StandardCharsets ;
22
34
import org .junit .Before ;
35
+ import org .junit .Rule ;
23
36
import org .junit .Test ;
24
37
25
38
/** Tests for the Exec helper class */
@@ -33,11 +46,49 @@ public class ExecTest {
33
46
private static final String BAD_OUTPUT_INCOMPLETE_MSG1 =
34
47
"{\" metadata\" :{},\" status\" :\" Failure\" ,\" message\" :\" command terminated with non-zero exit code: Error executing in Docker Container: 1\" ,\" reas" ;
35
48
49
+ private String namespace ;
50
+ private String podName ;
51
+ private String [] cmd ;
52
+
36
53
private ApiClient client ;
37
54
55
+ private static final int PORT = 8089 ;
56
+ @ Rule public WireMockRule wireMockRule = new WireMockRule (PORT );
57
+
38
58
@ Before
39
59
public void setup () throws IOException {
40
- client = new ClientBuilder ().setBasePath ("http://localhost:9999" ).build ();
60
+ client = new ClientBuilder ().setBasePath ("http://localhost:" + PORT ).build ();
61
+
62
+ namespace = "default" ;
63
+ podName = "apod" ;
64
+ // TODO: When WireMock supports multiple query params with the same name expand this
65
+ // See: https://github.com/tomakehurst/wiremock/issues/398
66
+ cmd = new String [] {"cmd" };
67
+ }
68
+
69
+ @ Test
70
+ public void testUrl () throws IOException , ApiException , InterruptedException {
71
+ Exec exec = new Exec (client );
72
+
73
+ stubFor (
74
+ get (urlPathEqualTo ("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/exec" ))
75
+ .willReturn (
76
+ aResponse ()
77
+ .withStatus (404 )
78
+ .withHeader ("Content-Type" , "application/json" )
79
+ .withBody ("{}" )));
80
+
81
+ Process p = exec .exec (namespace , podName , cmd , false );
82
+ p .waitFor ();
83
+
84
+ verify (getRequestedFor (urlPathEqualTo ("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/exec" ))
85
+ .withQueryParam ("stdin" , equalTo ("false" ))
86
+ .withQueryParam ("stdout" , equalTo ("true" ))
87
+ .withQueryParam ("stderr" , equalTo ("true" ))
88
+ .withQueryParam ("tty" , equalTo ("false" ))
89
+ .withQueryParam ("command" , equalTo ("cmd" )));
90
+
91
+ assertEquals (-1 , p .exitValue ());
41
92
}
42
93
43
94
@ Test
0 commit comments