1212*/
1313package io .kubernetes .client ;
1414
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 ;
1624
25+ import com .github .tomakehurst .wiremock .junit .WireMockRule ;
26+
27+ import io .kubernetes .client .ApiException ;
1728import io .kubernetes .client .util .ClientBuilder ;
29+
1830import java .io .ByteArrayInputStream ;
1931import java .io .IOException ;
2032import java .io .InputStream ;
2133import java .nio .charset .StandardCharsets ;
2234import org .junit .Before ;
35+ import org .junit .Rule ;
2336import org .junit .Test ;
2437
2538/** Tests for the Exec helper class */
@@ -33,11 +46,49 @@ public class ExecTest {
3346 private static final String BAD_OUTPUT_INCOMPLETE_MSG1 =
3447 "{\" metadata\" :{},\" status\" :\" Failure\" ,\" message\" :\" command terminated with non-zero exit code: Error executing in Docker Container: 1\" ,\" reas" ;
3548
49+ private String namespace ;
50+ private String podName ;
51+ private String [] cmd ;
52+
3653 private ApiClient client ;
3754
55+ private static final int PORT = 8089 ;
56+ @ Rule public WireMockRule wireMockRule = new WireMockRule (PORT );
57+
3858 @ Before
3959 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 ());
4192 }
4293
4394 @ Test
0 commit comments