Skip to content

Commit 52f847c

Browse files
committed
Add more tests for Exec.
1 parent e8f1788 commit 52f847c

File tree

3 files changed

+75
-17
lines changed

3 files changed

+75
-17
lines changed

util/pom.xml

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,29 @@
6767
<artifactId>bcpkix-jdk15on</artifactId>
6868
<version>1.59</version>
6969
</dependency>
70-
<!-- test dependencies -->
7170
<dependency>
72-
<groupId>junit</groupId>
73-
<artifactId>junit</artifactId>
74-
<version>4.12</version>
75-
<scope>test</scope>
71+
<groupId>com.microsoft.azure</groupId>
72+
<artifactId>adal4j</artifactId>
73+
<version>1.6.0</version>
7674
</dependency>
7775
<dependency>
7876
<groupId>ch.qos.logback</groupId>
7977
<artifactId>logback-classic</artifactId>
8078
<version>1.2.3</version>
8179
<scope>test</scope>
8280
</dependency>
81+
<dependency>
82+
<groupId>com.google.protobuf</groupId>
83+
<artifactId>protobuf-java</artifactId>
84+
<version>3.4.0</version>
85+
</dependency>
86+
<!-- test dependencies -->
87+
<dependency>
88+
<groupId>junit</groupId>
89+
<artifactId>junit</artifactId>
90+
<version>4.12</version>
91+
<scope>test</scope>
92+
</dependency>
8393
<dependency>
8494
<groupId>org.mockito</groupId>
8595
<artifactId>mockito-core</artifactId>
@@ -93,15 +103,11 @@
93103
<scope>test</scope>
94104
</dependency>
95105
<dependency>
96-
<groupId>com.google.protobuf</groupId>
97-
<artifactId>protobuf-java</artifactId>
98-
<version>3.4.0</version>
99-
</dependency>
100-
<dependency>
101-
<groupId>com.microsoft.azure</groupId>
102-
<artifactId>adal4j</artifactId>
103-
<version>1.6.0</version>
104-
</dependency>
106+
<groupId>com.github.tomakehurst</groupId>
107+
<artifactId>wiremock</artifactId>
108+
<version>2.19.0</version>
109+
<scope>test</scope>
110+
</dependency>
105111
</dependencies>
106112
<build>
107113
<plugins>

util/src/main/java/io/kubernetes/client/Exec.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,8 @@ public void close() {
284284
};
285285
}
286286

287-
private WebSocketStreamHandler getHandler() {
287+
// Protected to facilitate unit testing.
288+
protected WebSocketStreamHandler getHandler() {
288289
return streamHandler;
289290
}
290291

util/src/test/java/io/kubernetes/client/ExecTest.java

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,27 @@
1212
*/
1313
package 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;
1728
import io.kubernetes.client.util.ClientBuilder;
29+
1830
import java.io.ByteArrayInputStream;
1931
import java.io.IOException;
2032
import java.io.InputStream;
2133
import java.nio.charset.StandardCharsets;
2234
import org.junit.Before;
35+
import org.junit.Rule;
2336
import 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

Comments
 (0)