File tree Expand file tree Collapse file tree 3 files changed +36
-15
lines changed
main/java/io/kubernetes/client
test/java/io/kubernetes/client Expand file tree Collapse file tree 3 files changed +36
-15
lines changed Original file line number Diff line number Diff line change @@ -185,27 +185,29 @@ public Process exec(
185
185
}
186
186
187
187
static int parseExitCode (InputStream inputStream ) {
188
- int exitCode = 0 ;
189
188
try {
190
189
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 );
202
203
}
203
204
}
204
205
} catch (IOException io ) {
205
206
log .error ("Error parsing exit code from status channel response" , io );
206
207
}
207
208
208
- return exitCode ;
209
+ // Unable to parse the exit code from the content
210
+ return -1 ;
209
211
}
210
212
211
213
private static class ExecProcess extends Process {
Original file line number Diff line number Diff line change 1
1
/*
2
- Copyright 2017 The Kubernetes Authors.
2
+ Copyright 2017, 2018 The Kubernetes Authors.
3
3
Licensed under the Apache License, Version 2.0 (the "License");
4
4
you may not use this file except in compliance with the License.
5
5
You may obtain a copy of the License at
@@ -51,7 +51,7 @@ public interface SocketListener {
51
51
public void open (String protocol , WebSocket socket );
52
52
53
53
/**
54
- * Callled when a binary media type message is received
54
+ * Called when a binary media type message is received
55
55
*
56
56
* @param in The input stream containing the binary data
57
57
*/
Original file line number Diff line number Diff line change @@ -25,6 +25,9 @@ public class ExecTest {
25
25
"command terminated with non-zero exit code: Error executing in Docker Container: 1" ;
26
26
private static final String OUTPUT_EXIT126 =
27
27
"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" ;
28
31
29
32
@ Test
30
33
public void testExit0 () {
@@ -48,4 +51,20 @@ public void testExit126() {
48
51
int exitCode = Exec .parseExitCode (inputStream );
49
52
assertEquals (126 , exitCode );
50
53
}
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
+ }
51
70
}
You can’t perform that action at this time.
0 commit comments