Skip to content

Commit 632d6c5

Browse files
authored
Merge pull request #401 from brendandburns/unit2
Improve unit test coverage
2 parents 907c350 + e499c21 commit 632d6c5

File tree

11 files changed

+631
-24
lines changed

11 files changed

+631
-24
lines changed

examples/pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
23
<modelVersion>4.0.0</modelVersion>
34
<groupId>io.kubernetes</groupId>
45
<artifactId>client-java-examples</artifactId>

util/pom.xml

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

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ public void copyDirectoryFromPod(
116116
false,
117117
false);
118118
InputStream is = new Base64InputStream(new BufferedInputStream(proc.getInputStream()));
119-
try (ArchiveInputStream archive =
120-
new TarArchiveInputStream(is)) { // new GzipCompressorInputStream(is))) {
119+
try (ArchiveInputStream archive = new TarArchiveInputStream(is)) {
120+
// TODO Use new GzipCompressorInputStream(is))) here {
121121
for (ArchiveEntry entry = archive.getNextEntry();
122122
entry != null;
123123
entry = archive.getNextEntry()) {

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public Process exec(
199199
throws ApiException, IOException {
200200
String path = makePath(namespace, name, command, container, stdin, tty);
201201

202-
ExecProcess exec = new ExecProcess();
202+
ExecProcess exec = new ExecProcess(apiClient);
203203
WebSocketStreamHandler handler = exec.getHandler();
204204
WebSockets.stream(path, "GET", apiClient, handler);
205205

@@ -245,13 +245,13 @@ static int parseExitCode(ApiClient client, InputStream inputStream) {
245245
return -1;
246246
}
247247

248-
private class ExecProcess extends Process {
248+
protected static class ExecProcess extends Process {
249249
private final WebSocketStreamHandler streamHandler;
250250
private int statusCode = -1;
251251
private boolean isAlive = true;
252252
private final Map<Integer, InputStream> input = new HashMap<>();
253253

254-
public ExecProcess() throws IOException {
254+
public ExecProcess(final ApiClient apiClient) throws IOException {
255255
this.streamHandler =
256256
new WebSocketStreamHandler() {
257257
@Override
@@ -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/main/java/io/kubernetes/client/PodLogs.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ public ApiClient getApiClient() {
4949
}
5050

5151
public InputStream streamNamespacedPodLog(V1Pod pod) throws ApiException, IOException {
52+
if (pod.getSpec() == null) {
53+
throw new ApiException("pod.spec is null and container isn't specified.");
54+
}
55+
if (pod.getSpec().getContainers() == null || pod.getSpec().getContainers().size() < 1) {
56+
throw new ApiException("pod.spec.containers has no containers");
57+
}
5258
return streamNamespacedPodLog(
5359
pod.getMetadata().getNamespace(),
5460
pod.getMetadata().getName(),
@@ -86,7 +92,7 @@ public InputStream streamNamespacedPodLog(
8692
null);
8793
Response response = call.execute();
8894
if (!response.isSuccessful()) {
89-
throw new ApiException("Logs request failed: " + response.code());
95+
throw new ApiException(response.code(), "Logs request failed: " + response.code());
9096
}
9197
return response.body().byteStream();
9298
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package io.kubernetes.client;
14+
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.urlPathEqualTo;
21+
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
22+
23+
import com.github.tomakehurst.wiremock.junit.WireMockRule;
24+
import io.kubernetes.client.Attach.AttachResult;
25+
import io.kubernetes.client.util.ClientBuilder;
26+
import java.io.IOException;
27+
import org.junit.Before;
28+
import org.junit.Rule;
29+
import org.junit.Test;
30+
31+
/** Tests for the Attach helper class */
32+
public class AttachTest {
33+
private String namespace;
34+
private String podName;
35+
private String container;
36+
37+
private ApiClient client;
38+
39+
private static final int PORT = 8089;
40+
@Rule public WireMockRule wireMockRule = new WireMockRule(PORT);
41+
42+
@Before
43+
public void setup() throws IOException {
44+
client = new ClientBuilder().setBasePath("http://localhost:" + PORT).build();
45+
46+
namespace = "default";
47+
podName = "apod";
48+
container = "acontainer";
49+
}
50+
51+
@Test
52+
public void testUrl() throws IOException, ApiException, InterruptedException {
53+
Attach attach = new Attach(client);
54+
55+
stubFor(
56+
get(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/attach"))
57+
.willReturn(
58+
aResponse()
59+
.withStatus(404)
60+
.withHeader("Content-Type", "application/json")
61+
.withBody("{}")));
62+
63+
AttachResult res1 = attach.attach(namespace, podName, container, false, false);
64+
AttachResult res2 = attach.attach(namespace, podName, true);
65+
66+
// TODO: Kill this sleep, the trouble is that the test tries to validate before the connection
67+
// event has happened
68+
Thread.sleep(2000);
69+
70+
res1.close();
71+
res2.close();
72+
73+
verify(
74+
getRequestedFor(
75+
urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/attach"))
76+
.withQueryParam("stdin", equalTo("false"))
77+
.withQueryParam("tty", equalTo("false"))
78+
.withQueryParam("container", equalTo(container)));
79+
80+
verify(
81+
getRequestedFor(
82+
urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/attach"))
83+
.withQueryParam("stdin", equalTo("true"))
84+
.withQueryParam("tty", equalTo("false")));
85+
}
86+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package io.kubernetes.client;
14+
15+
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
16+
import static com.github.tomakehurst.wiremock.client.WireMock.get;
17+
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
18+
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
19+
20+
import com.github.tomakehurst.wiremock.junit.WireMockRule;
21+
import io.kubernetes.client.models.V1ObjectMeta;
22+
import io.kubernetes.client.models.V1Pod;
23+
import io.kubernetes.client.util.ClientBuilder;
24+
import java.io.IOException;
25+
import java.io.InputStream;
26+
import org.junit.Before;
27+
import org.junit.Rule;
28+
import org.junit.Test;
29+
30+
/** Tests for the Copy helper class */
31+
public class CopyTest {
32+
private String namespace;
33+
private String podName;
34+
private String[] cmd;
35+
36+
private ApiClient client;
37+
38+
private static final int PORT = 8089;
39+
@Rule public WireMockRule wireMockRule = new WireMockRule(PORT);
40+
41+
@Before
42+
public void setup() throws IOException {
43+
client = new ClientBuilder().setBasePath("http://localhost:" + PORT).build();
44+
45+
namespace = "default";
46+
podName = "apod";
47+
}
48+
49+
@Test
50+
public void testUrl() throws IOException, ApiException, InterruptedException {
51+
Copy copy = new Copy(client);
52+
53+
V1Pod pod = new V1Pod().metadata(new V1ObjectMeta().name(podName).namespace(namespace));
54+
55+
stubFor(
56+
get(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/exec"))
57+
.willReturn(
58+
aResponse()
59+
.withStatus(404)
60+
.withHeader("Content-Type", "application/json")
61+
.withBody("{}")));
62+
63+
InputStream is = copy.copyFileFromPod(pod, "/some/path/to/file");
64+
65+
// verify(
66+
// getRequestedFor(
67+
// urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods/" + podName +
68+
// "/exec"))
69+
// .withQueryParam("stdin", equalTo("false"))
70+
// .withQueryParam("stdout", equalTo("true"))
71+
// .withQueryParam("stderr", equalTo("true"))
72+
// .withQueryParam("tty", equalTo("false"))
73+
// .withQueryParam("command", new AnythingPattern()));
74+
}
75+
}

0 commit comments

Comments
 (0)