Skip to content

Commit 0cb2474

Browse files
committed
fix again
1 parent 0d38d98 commit 0cb2474

File tree

2 files changed

+84
-2
lines changed

2 files changed

+84
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ public NodeMetricsList getNodeMetrics() throws ApiException {
6565
"v1beta1",
6666
"nodes",
6767
apiClient);
68-
return metricsClient.list().getObject();
68+
return metricsClient.list().throwsApiException().getObject();
6969
}
7070

7171
public PodMetricsList getPodMetrics(String namespace) throws ApiException {
7272
GenericKubernetesApi<PodMetrics, PodMetricsList> metricsClient =
7373
new GenericKubernetesApi<>(
7474
PodMetrics.class, PodMetricsList.class, "metrics.k8s.io", "v1beta1", "pods", apiClient);
75-
return metricsClient.list(namespace).getObject();
75+
return metricsClient.list(namespace).throwsApiException().getObject();
7676
}
7777
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package io.kubernetes.client;
14+
15+
import com.github.tomakehurst.wiremock.junit.WireMockRule;
16+
import io.kubernetes.client.openapi.ApiClient;
17+
import io.kubernetes.client.openapi.ApiException;
18+
import io.kubernetes.client.util.ClientBuilder;
19+
import org.junit.Before;
20+
import org.junit.Rule;
21+
import org.junit.Test;
22+
23+
import java.io.IOException;
24+
25+
import static com.github.tomakehurst.wiremock.client.WireMock.*;
26+
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
27+
import static org.junit.Assert.assertEquals;
28+
import static org.junit.Assert.fail;
29+
30+
public class MetricsTest {
31+
32+
private ApiClient client;
33+
34+
@Rule
35+
public WireMockRule wireMockRule = new WireMockRule(options().dynamicPort());
36+
37+
@Before
38+
public void setup() throws IOException {
39+
client = new ClientBuilder().setBasePath("http://localhost:" + wireMockRule.port()).build();
40+
}
41+
42+
@Test
43+
public void getPodMetricsThrowsAPIExceptionWhenServerReturnsError() {
44+
String namespace = "default";
45+
Metrics metrics = new Metrics(client);
46+
wireMockRule.stubFor(
47+
get(urlEqualTo("/apis/metrics.k8s.io/v1beta1/namespaces/" + namespace + "/pods?watch=false"))
48+
.willReturn(
49+
aResponse()
50+
.withStatus(503)
51+
.withHeader("Content-Type", "text/plain")
52+
.withBody("Service Unavailable")
53+
)
54+
);
55+
try {
56+
metrics.getPodMetrics(namespace);
57+
fail("Expected ApiException to be thrown");
58+
} catch (ApiException ex) {
59+
assertEquals(503, ex.getCode());
60+
}
61+
}
62+
63+
@Test
64+
public void getNodeMetricsThrowsAPIExceptionWhenServerReturnsError() {
65+
Metrics metrics = new Metrics(client);
66+
wireMockRule.stubFor(
67+
get(urlEqualTo("/apis/metrics.k8s.io/v1beta1/nodes?watch=false"))
68+
.willReturn(
69+
aResponse()
70+
.withStatus(503)
71+
.withHeader("Content-Type", "text/plain")
72+
.withBody("Service Unavailable")
73+
)
74+
);
75+
try {
76+
metrics.getNodeMetrics();
77+
fail("Expected ApiException to be thrown");
78+
} catch (ApiException ex) {
79+
assertEquals(503, ex.getCode());
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)