Repository navigation
Expand file tree
/
Copy pathExecCallbacksTest.java
More file actions
355 lines (311 loc) · 10.3 KB
/
Copy pathExecCallbacksTest.java
File metadata and controls
355 lines (311 loc) · 10.3 KB
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
/*
Copyright 2020 The Kubernetes Authors.
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.kubernetes.client;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.fail;
import io.kubernetes.client.custom.IOTrio;
import io.kubernetes.client.openapi.ApiException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.BiConsumer;
import org.junit.jupiter.api.Test;
class ExecCallbacksTest {
@Test
void maxTimeout() throws Exception {
CountDownLatch release = new CountDownLatch(1);
try {
Exec exec = getExec((future, io) -> release.await());
long startTime = System.currentTimeMillis();
Future<Integer> promise =
exec.exec("ns-1", "pod1", null, null, null, 1_000L, true, "random-command");
int exitCode = promise.get(10_000, TimeUnit.MILLISECONDS);
long delta = System.currentTimeMillis() - startTime;
assertThat(delta)
.isGreaterThanOrEqualTo(1_000L)
.isLessThanOrEqualTo(10_000L);
assertThat(exitCode).isEqualTo(Integer.MAX_VALUE);
} finally {
release.countDown();
}
}
@Test
void stderrTest() throws Exception {
Exec exec =
getExec(
(future, io) -> {
io.getErrors().write("Some error".getBytes());
future.complete(1);
});
List<String> errors = new ArrayList<>();
Future<Integer> promise =
exec.exec(
"ns-1",
"pod1",
io ->
quietly(
() -> {
byte[] buff = new byte[10];
io.getStderr().read(buff);
errors.add(new String(buff));
}),
null,
null,
100_000L,
true,
"random-command");
assertThat(promise.get()).isEqualTo(1);
assertThat(errors).containsExactly("Some error");
}
@Test
void stdoutTest() throws Exception {
Exec exec =
getExec(
(future, io) -> {
io.getOutput().write("Some stream".getBytes());
future.complete(0);
});
List<String> output = new ArrayList<>();
Future<Integer> promise =
exec.exec(
"ns-1",
"pod1",
io ->
quietly(
() -> {
byte[] buff = new byte[11];
io.getStdout().read(buff);
output.add(new String(buff));
}),
null,
null,
100_000L,
true,
"random-command");
assertThat(promise.get()).isZero();
assertThat(output).containsExactly("Some stream");
}
@Test
void onClosedTriggerTest() throws Exception {
Exec exec = getExec((future, io) -> future.complete(9));
List<Integer> codes = new ArrayList<>(1);
Future<Integer> promise =
exec.exec(
"ns-1",
"pod1",
null,
(code, io) -> codes.add(code),
null,
100_000L,
true,
"random-command");
assertThat(promise.get()).isEqualTo(9);
assertThat(codes).containsExactly(9);
}
@Test
void mockedExecutionWithMixedEventsTest() throws Exception {
Exec exec =
getExec(
(future, io) ->
quietly(
() -> {
io.getErrors()
.write(
("bash: cannot set terminal process group (-1): Inappropriate ioctl for device\n"
+ "bash: no job control in this shell\n")
.getBytes());
read(io.getInput(), "uotime\n");
write(io.getErrors(), "uotime: command not found\n");
read(io.getInput(), "uptime\n");
write(
io.getOutput(),
" 00:44:54 up 30 days, 1:15, 0 users, load average: 0,01, 0,04, 0,00\n");
read(io.getInput(), "exit\n");
write(io.getOutput(), "Bye!\n");
future.complete(5);
}));
List<Integer> codes = new ArrayList<>(1);
AtomicBoolean callbackInvoked = new AtomicBoolean(false);
Future<Integer> promise =
exec.exec(
"ns-1",
"pod1",
io ->
quietly(
() -> {
read(
io.getStderr(),
("bash: cannot set terminal process group (-1): Inappropriate ioctl for device\n"
+ "bash: no job control in this shell\n"));
write(io.getStdin(), "uotime\n");
read(io.getStderr(), "uotime: command not found\n");
write(io.getStdin(), "uptime\n");
read(
io.getStdout(),
" 00:44:54 up 30 days, 1:15, 0 users, load average: 0,01, 0,04, 0,00\n");
write(io.getStdin(), "exit\n");
read(io.getStdout(), "Bye!\n");
callbackInvoked.set(true);
}),
(code, io) -> codes.add(code),
null,
100_000L,
true,
"ssh",
"pc",
"ssh",
"pi",
"bash",
"-i");
assertThat(promise.get()).isEqualTo(5);
assertThat(codes).containsExactly(5);
assertThat(callbackInvoked).isTrue();
}
// helper functions
private Exec getExec(NoisyBiConsumer<CompletableFuture<Integer>, PipedIO> remoteProcess) {
return new MockedExecution((fut, io) -> quietly(() -> remoteProcess.accept(fut, io)));
}
private void quietly(NoisyRunnable task) {
try {
task.run();
} catch (Exception e) {
fail(e);
}
}
private String read(InputStream is, String expectedText) {
String readText = read(is, expectedText.length());
assertThat(readText).isEqualTo(expectedText);
return readText;
}
private void write(OutputStream os, String text) {
quietly(() -> os.write(text.getBytes()));
}
private String read(InputStream is, int count) {
try {
byte[] buff = new byte[count];
is.read(buff);
return new String(buff);
} catch (IOException e) {
fail(e);
}
return null;
}
private interface NoisyBiConsumer<V1, V2> {
void accept(V1 value1, V2 value2) throws Exception;
}
private interface NoisyRunnable {
void run() throws Exception;
}
// mocked EXEC/Process and the IO pipe
private static class MockedExecution extends Exec {
private final BiConsumer<CompletableFuture<Integer>, PipedIO> remoteProcess;
private final IOTrio localIO;
private MockedExecution(BiConsumer<CompletableFuture<Integer>, PipedIO> remoteProcess) {
this.remoteProcess = remoteProcess;
localIO = new IOTrio();
}
@Override
public Process exec(
String namespace,
String name,
String[] command,
String container,
boolean stdin,
boolean tty)
throws ApiException, IOException {
CompletableFuture<Integer> taskPromise = new CompletableFuture<>();
PipedIO pipedIO = new PipedIO(localIO);
runAsync("mocked exec", () -> remoteProcess.accept(taskPromise, pipedIO));
return new Process() {
@Override
public OutputStream getOutputStream() {
return localIO.getStdin();
}
@Override
public InputStream getInputStream() {
return localIO.getStdout();
}
@Override
public InputStream getErrorStream() {
return localIO.getStderr();
}
@Override
public int waitFor() throws InterruptedException {
try {
return taskPromise.get();
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
}
@Override
public int exitValue() {
try {
return taskPromise.get(1, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
throw new RuntimeException(e);
} catch (TimeoutException e) {
throw new IllegalThreadStateException();
}
}
@Override
public void destroy() {
taskPromise.complete(1);
}
};
}
}
private static class PipedIO {
private final PipedOutputStream errors;
private final PipedOutputStream output;
private final PipedInputStream input;
private final IOTrio localIO;
public PipedIO(IOTrio localIO) {
this.localIO = localIO;
try {
output = new PipedOutputStream();
localIO.setStdout(new PipedInputStream(output));
errors = new PipedOutputStream();
localIO.setStderr(new PipedInputStream(errors));
input = new PipedInputStream();
localIO.setStdin(new PipedOutputStream(input));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public IOTrio getLocalIO() {
return localIO;
}
public OutputStream getErrors() {
return errors;
}
public OutputStream getOutput() {
return output;
}
public InputStream getInput() {
return input;
}
}
}