Skip to content

Commit 333482a

Browse files
authored
Merge pull request #2314 from adamjshook/master
Close ResponseBody in PodLogs on error
2 parents 36f0b4c + 25dd1e3 commit 333482a

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ public InputStream streamNamespacedPodLog(
9595
null);
9696
Response response = call.execute();
9797
if (!response.isSuccessful()) {
98+
if (response.body() != null) {
99+
response.close();
100+
}
98101
throw new ApiException(response.code(), "Logs request failed: " + response.code());
99102
}
100103
return response.body().byteStream();

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
2020
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
2121
import static org.junit.Assert.assertEquals;
22+
import static org.junit.Assert.assertTrue;
23+
import static org.mockito.ArgumentMatchers.any;
24+
import static org.mockito.Mockito.mock;
25+
import static org.mockito.Mockito.verify;
26+
import static org.mockito.Mockito.when;
2227

2328
import com.github.tomakehurst.wiremock.junit.WireMockRule;
2429
import io.kubernetes.client.openapi.ApiClient;
@@ -33,6 +38,9 @@
3338
import java.io.IOException;
3439
import java.io.InputStream;
3540
import java.util.Arrays;
41+
import okhttp3.Call;
42+
import okhttp3.Response;
43+
import okhttp3.ResponseBody;
3644
import org.junit.Before;
3745
import org.junit.Rule;
3846
import org.junit.Test;
@@ -127,4 +135,38 @@ public void testStream() throws ApiException, IOException {
127135
Streams.copy(is, bos);
128136
assertEquals(content, bos.toString());
129137
}
138+
139+
@Test
140+
public void testResponseClosedOnError() throws ApiException, IOException {
141+
V1Pod pod =
142+
new V1Pod()
143+
.metadata(new V1ObjectMeta().name(podName).namespace(namespace))
144+
.spec(
145+
new V1PodSpec()
146+
.containers(Arrays.asList(new V1Container().name(container).image("nginx"))));
147+
148+
ApiClient mockClient = mock(ApiClient.class);
149+
Call mockCall = mock(Call.class);
150+
Response mockResponse = mock(Response.class);
151+
152+
when(mockClient.escapeString(any())).thenReturn("foo");
153+
when(mockClient.buildCall(any(), any(), any(), any(), any(), any(), any(), any(), any(), any()))
154+
.thenReturn(mockCall);
155+
when(mockCall.execute()).thenReturn(mockResponse);
156+
when(mockResponse.isSuccessful()).thenReturn(false);
157+
when(mockResponse.code()).thenReturn(404);
158+
when(mockResponse.body()).thenReturn(mock(ResponseBody.class));
159+
160+
PodLogs logs = new PodLogs(mockClient);
161+
boolean thrown;
162+
try (InputStream ignored = logs.streamNamespacedPodLog(pod)) {
163+
thrown = false;
164+
} catch (ApiException ex) {
165+
assertEquals(404, ex.getCode());
166+
thrown = true;
167+
}
168+
169+
assertTrue(thrown);
170+
verify(mockResponse).close();
171+
}
130172
}

0 commit comments

Comments
 (0)