-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
PodIT.java
364 lines (303 loc) · 13 KB
/
PodIT.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.kubernetes;
import io.fabric8.junit.jupiter.api.LoadKubernetesManifests;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.Namespace;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodBuilder;
import io.fabric8.kubernetes.api.model.PodList;
import io.fabric8.kubernetes.api.model.Status;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.dsl.ExecListener;
import io.fabric8.kubernetes.client.dsl.ExecWatch;
import io.fabric8.kubernetes.client.dsl.LogWatch;
import io.fabric8.kubernetes.client.dsl.PodResource;
import io.fabric8.kubernetes.client.utils.InputStreamPumper;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
@LoadKubernetesManifests("/pod-it.yml")
class PodIT {
private static final int POD_READY_WAIT_IN_SECONDS = 60;
private static final Logger logger = LoggerFactory.getLogger(PodIT.class);
KubernetesClient client;
Namespace namespace;
@Test
void load() {
Pod aPod = client.pods().load(getClass().getResourceAsStream("/test-pod.yml")).get();
assertThat(aPod).isNotNull();
assertEquals("nginx", aPod.getMetadata().getName());
}
@Test
void get() {
Pod pod1 = client.pods().withName("pod-standard").get();
assertNotNull(pod1);
}
@Test
void list() {
PodList podList = client.pods().list();
assertThat(podList).isNotNull();
assertTrue(podList.getItems().size() >= 1);
}
@Test
void update() {
Pod pod1 = client.pods().withName("pod-standard").edit(p -> new PodBuilder(p)
.editMetadata().addToLabels("foo", "bar").endMetadata().build());
assertEquals("bar", pod1.getMetadata().getLabels().get("foo"));
}
@Test
void delete() {
assertTrue(client.pods().withName("pod-delete").delete().size() == 1);
}
@Test
void log() {
client.pods().withName("pod-standard").waitUntilReady(POD_READY_WAIT_IN_SECONDS, TimeUnit.SECONDS);
String log = client.pods().withName("pod-standard").getLog();
assertNotNull(log);
}
@Test
void exec() throws Exception {
client.pods().withName("pod-standard").waitUntilReady(POD_READY_WAIT_IN_SECONDS, TimeUnit.SECONDS);
final CountDownLatch execLatch = new CountDownLatch(1);
ByteArrayOutputStream out = new ByteArrayOutputStream();
final AtomicBoolean closed = new AtomicBoolean();
final AtomicBoolean failed = new AtomicBoolean();
final CompletableFuture<Status> exitStatus = new CompletableFuture<>();
int[] exitCode = new int[] { Integer.MAX_VALUE };
client.pods().withName("pod-standard")
.writingOutput(out)
.usingListener(new ExecListener() {
@Override
public void onOpen() {
logger.info("Shell was opened");
}
@Override
public void onFailure(Throwable t, Response failureResponse) {
logger.info("Shell barfed");
failed.set(true);
execLatch.countDown();
}
@Override
public void onClose(int i, String s) {
logger.info("Shell closed");
closed.set(true);
execLatch.countDown();
}
@Override
public void onExit(int code, Status status) {
exitCode[0] = code;
exitStatus.complete(status);
}
}).exec("sh", "-c", "echo 'hello world!'");
assertThat(exitStatus.get(5, TimeUnit.SECONDS))
.hasFieldOrPropertyWithValue("status", "Success");
assertTrue(execLatch.await(5, TimeUnit.SECONDS));
assertEquals(0, exitCode[0]);
assertTrue(closed.get());
assertFalse(failed.get());
assertNotNull(out.toString());
}
@Test
void execExitCode() throws Exception {
client.pods().withName("pod-standard").waitUntilReady(POD_READY_WAIT_IN_SECONDS, TimeUnit.SECONDS);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ExecWatch watch = client.pods().withName("pod-standard")
.writingOutput(out)
.exec("sh", "-c", "echo 'hello world!'");
assertEquals(0, watch.exitCode().join());
assertNotNull("hello world!", out.toString());
}
@Test
void execInteractiveShell() throws Exception {
client.pods().withName("pod-standard").waitUntilReady(POD_READY_WAIT_IN_SECONDS, TimeUnit.SECONDS);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ExecWatch watch = client.pods().withName("pod-standard")
.redirectingInput()
.redirectingOutput()
.redirectingError()
.withTTY()
.exec("sh", "-i");
InputStreamPumper.pump(watch.getOutput(), baos::write, Executors.newSingleThreadExecutor());
watch.getInput().write("whoami\n".getBytes(StandardCharsets.UTF_8));
watch.getInput().flush();
Awaitility.await().atMost(30, TimeUnit.SECONDS).until(() -> {
return new String(baos.toByteArray(), StandardCharsets.UTF_8).contains("root");
});
watch.close();
// no error is expected
assertEquals(-1, watch.getError().read());
}
@Test
void attach() throws Exception {
client.pods().withName("pod-interactive").waitUntilReady(POD_READY_WAIT_IN_SECONDS, TimeUnit.SECONDS);
CountDownLatch latch = new CountDownLatch(1);
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
AtomicBoolean closed = new AtomicBoolean();
AtomicBoolean failed = new AtomicBoolean();
ExecWatch watch = client.pods().withName("pod-interactive")
.redirectingInput()
.redirectingOutput()
.redirectingError()
.withTTY()
.usingListener(new ExecListener() {
@Override
public void onFailure(Throwable t, Response failureResponse) {
failed.set(true);
latch.countDown();
}
@Override
public void onClose(int i, String s) {
closed.set(true);
latch.countDown();
}
})
.attach();
watch.getInput().write("whoami\n".getBytes(StandardCharsets.UTF_8));
watch.getInput().flush();
InputStreamPumper.pump(watch.getOutput(), stdout::write, Executors.newSingleThreadExecutor());
Awaitility.await().atMost(30, TimeUnit.SECONDS)
.until(() -> stdout.toString().contains("root"));
watch.close();
assertTrue(latch.await(5, TimeUnit.SECONDS));
assertTrue(closed.get());
assertFalse(failed.get());
// make sure we can get log output before the pod terminates
ByteArrayOutputStream result = new ByteArrayOutputStream();
LogWatch log = client.pods().withName("pod-interactive").tailingLines(10).watchLog(result);
Awaitility.await().atMost(30, TimeUnit.SECONDS).until(() -> result.size() > 0);
log.close();
}
@Test
void readFile() throws IOException {
client.pods().withName("pod-standard").waitUntilReady(POD_READY_WAIT_IN_SECONDS, TimeUnit.SECONDS);
try (
ExecWatch ignore = client.pods().withName("pod-standard").writingOutput(System.out).exec("sh", "-c",
"echo 'hello' > /msg");
InputStream is = client.pods().withName("pod-standard").file("/msg").read()) {
String result = new BufferedReader(new InputStreamReader(is)).lines().collect(Collectors.joining("\n"));
assertEquals("hello", result);
}
}
@Test
void readFileEscapedParams() throws IOException {
client.pods().withName("pod-standard").waitUntilReady(POD_READY_WAIT_IN_SECONDS, TimeUnit.SECONDS);
try (
ExecWatch watch = client.pods().withName("pod-standard").writingOutput(System.out).exec("sh", "-c",
"echo 'H$ll* (W&RLD}' > /msg");
InputStream is = client.pods().withName("pod-standard").file("/msg").read()) {
String result = new BufferedReader(new InputStreamReader(is)).lines().collect(Collectors.joining("\n"));
assertEquals("H$ll* (W&RLD}", result);
}
}
@Test
void uploadFile() throws IOException {
client.pods().withName("pod-standard").waitUntilReady(POD_READY_WAIT_IN_SECONDS, TimeUnit.SECONDS);
// Wait for resources to get ready
final Path tmpFile = Files.createTempFile("PodIT", "toBeUploaded");
Files.write(tmpFile, Collections.singletonList("I'm uploaded"));
assertUploaded("pod-standard", tmpFile, "/tmp/toBeUploaded");
assertUploaded("pod-standard", tmpFile, "/tmp/001_special_!@#\\$^&(.mp4");
}
private void assertUploaded(String podName, final Path tmpFile, String filename) throws IOException {
PodResource podResource = client.pods().withName(podName);
podResource.file(filename).upload(tmpFile);
try (InputStream checkIs = podResource.file(filename).read();
BufferedReader br = new BufferedReader(new InputStreamReader(checkIs, StandardCharsets.UTF_8))) {
String result = br.lines().collect(Collectors.joining(System.lineSeparator()));
assertEquals("I'm uploaded", result);
}
}
@Test
void uploadDir() throws IOException {
client.pods().withName("pod-standard").waitUntilReady(POD_READY_WAIT_IN_SECONDS, TimeUnit.SECONDS);
final String[] files = new String[] { "1", "2", "a", "b", "c" };
final Path tmpDir = Files.createTempDirectory("uploadDir");
for (String fileName : files) {
Path file = tmpDir.resolve(fileName);
Files.write(Files.createFile(file), Arrays.asList("I'm uploaded", fileName));
}
PodResource podResource = client.pods().withName("pod-standard");
podResource.dir("/tmp/uploadDir").upload(tmpDir);
for (String fileName : files) {
try (InputStream checkIs = podResource.file("/tmp/uploadDir/" + fileName).read();
BufferedReader br = new BufferedReader(new InputStreamReader(checkIs, StandardCharsets.UTF_8))) {
String result = br.lines().collect(Collectors.joining(System.lineSeparator()));
assertEquals("I'm uploaded" + System.lineSeparator() + fileName, result);
}
}
}
@Test
void copyDir() throws IOException {
client.pods().withName("pod-standard").waitUntilReady(POD_READY_WAIT_IN_SECONDS, TimeUnit.SECONDS);
final Path tmpDir = Files.createTempDirectory("copyFile");
PodResource podResource = client.pods().withName("pod-standard");
podResource.dir("/etc").copy(tmpDir);
Path msg = tmpDir.resolve("/etc/hosts");
assertTrue(Files.exists(msg));
}
@Test
void copyFile() throws IOException {
client.pods().withName("pod-standard").waitUntilReady(POD_READY_WAIT_IN_SECONDS, TimeUnit.SECONDS);
final Path tmpDir = Files.createTempDirectory("copyFile");
PodResource podResource = client.pods().withName("pod-standard");
podResource.writingOutput(System.out).exec("sh", "-c", "echo 'hello' > /msg.txt");
podResource.file("/msg.txt").copy(tmpDir);
Path msg = tmpDir.resolve("msg.txt");
assertTrue(Files.exists(msg));
try (InputStream is = Files.newInputStream(msg);
BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
String result = br.lines().collect(Collectors.joining(System.lineSeparator()));
assertEquals("hello", result);
}
}
@Test
void listFromServer() {
client.pods().withName("pod-standard").waitUntilReady(POD_READY_WAIT_IN_SECONDS, TimeUnit.SECONDS);
final Pod pod1 = client.pods().withName("pod-standard").get();
List<HasMetadata> resources = client.resourceList(pod1).fromServer().get();
assertNotNull(resources);
assertEquals(1, resources.size());
assertNotNull(resources.get(0));
HasMetadata fromServerPod = resources.get(0);
assertEquals(pod1.getKind(), fromServerPod.getKind());
assertEquals(namespace.getMetadata().getName(), fromServerPod.getMetadata().getNamespace());
assertEquals(pod1.getMetadata().getName(), fromServerPod.getMetadata().getName());
}
}