|
19 | 19 | import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; |
20 | 20 | import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; |
21 | 21 | 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; |
22 | 27 |
|
23 | 28 | import com.github.tomakehurst.wiremock.junit.WireMockRule; |
24 | 29 | import io.kubernetes.client.openapi.ApiClient; |
|
33 | 38 | import java.io.IOException; |
34 | 39 | import java.io.InputStream; |
35 | 40 | import java.util.Arrays; |
| 41 | +import okhttp3.Call; |
| 42 | +import okhttp3.Response; |
| 43 | +import okhttp3.ResponseBody; |
36 | 44 | import org.junit.Before; |
37 | 45 | import org.junit.Rule; |
38 | 46 | import org.junit.Test; |
@@ -127,4 +135,38 @@ public void testStream() throws ApiException, IOException { |
127 | 135 | Streams.copy(is, bos); |
128 | 136 | assertEquals(content, bos.toString()); |
129 | 137 | } |
| 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 | + } |
130 | 172 | } |
0 commit comments