Skip to content

Commit 6fb1018

Browse files
committed
Handle more negative cases
1 parent 360634a commit 6fb1018

File tree

3 files changed

+36
-15
lines changed

3 files changed

+36
-15
lines changed

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -185,27 +185,29 @@ public Process exec(
185185
}
186186

187187
static int parseExitCode(InputStream inputStream) {
188-
int exitCode = 0;
189188
try {
190189
int available = inputStream.available();
191-
if (available > 0) {
192-
byte[] b = new byte[available];
193-
inputStream.read(b);
194-
String result = new String(b, "UTF-8");
195-
int idx = result.lastIndexOf(':');
196-
if (idx > 0) {
197-
try {
198-
exitCode = Integer.parseInt(result.substring(idx + 1).trim());
199-
} catch (NumberFormatException nfe) {
200-
log.error("Error parsing exit code from status channel response", nfe);
201-
}
190+
191+
// Kubernetes returns no content when the exit code is 0
192+
if (available == 0) return 0;
193+
194+
byte[] b = new byte[available];
195+
inputStream.read(b);
196+
String result = new String(b, "UTF-8");
197+
int idx = result.lastIndexOf(':');
198+
if (idx > 0) {
199+
try {
200+
return Integer.parseInt(result.substring(idx + 1).trim());
201+
} catch (NumberFormatException nfe) {
202+
log.error("Error parsing exit code from status channel response", nfe);
202203
}
203204
}
204205
} catch (IOException io) {
205206
log.error("Error parsing exit code from status channel response", io);
206207
}
207208

208-
return exitCode;
209+
// Unable to parse the exit code from the content
210+
return -1;
209211
}
210212

211213
private static class ExecProcess extends Process {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2017 The Kubernetes Authors.
2+
Copyright 2017, 2018 The Kubernetes Authors.
33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.
55
You may obtain a copy of the License at
@@ -51,7 +51,7 @@ public interface SocketListener {
5151
public void open(String protocol, WebSocket socket);
5252

5353
/**
54-
* Callled when a binary media type message is received
54+
* Called when a binary media type message is received
5555
*
5656
* @param in The input stream containing the binary data
5757
*/

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ public class ExecTest {
2525
"command terminated with non-zero exit code: Error executing in Docker Container: 1";
2626
private static final String OUTPUT_EXIT126 =
2727
"command terminated with non-zero exit code: Error executing in Docker Container: 126";
28+
private static final String BAD_OUTPUT_INCOMPLETE_MSG1 =
29+
"command terminated with non-zero exit code: Error ";
30+
private static final String BAD_OUTPUT_INCOMPLETE_MSG2 = "command terminated with non-zero";
2831

2932
@Test
3033
public void testExit0() {
@@ -48,4 +51,20 @@ public void testExit126() {
4851
int exitCode = Exec.parseExitCode(inputStream);
4952
assertEquals(126, exitCode);
5053
}
54+
55+
@Test
56+
public void testIncompleteData1() {
57+
InputStream inputStream =
58+
new ByteArrayInputStream(BAD_OUTPUT_INCOMPLETE_MSG1.getBytes(StandardCharsets.UTF_8));
59+
int exitCode = Exec.parseExitCode(inputStream);
60+
assertEquals(-1, exitCode);
61+
}
62+
63+
@Test
64+
public void testIncompleteData2() {
65+
InputStream inputStream =
66+
new ByteArrayInputStream(BAD_OUTPUT_INCOMPLETE_MSG2.getBytes(StandardCharsets.UTF_8));
67+
int exitCode = Exec.parseExitCode(inputStream);
68+
assertEquals(-1, exitCode);
69+
}
5170
}

0 commit comments

Comments
 (0)