|
| 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 static com.github.tomakehurst.wiremock.client.WireMock.*; |
| 16 | +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; |
| 17 | +import static org.junit.Assert.assertEquals; |
| 18 | +import static org.junit.Assert.fail; |
| 19 | + |
| 20 | +import com.github.tomakehurst.wiremock.junit.WireMockRule; |
| 21 | +import io.kubernetes.client.openapi.ApiClient; |
| 22 | +import io.kubernetes.client.openapi.ApiException; |
| 23 | +import io.kubernetes.client.util.ClientBuilder; |
| 24 | +import java.io.IOException; |
| 25 | +import org.junit.Before; |
| 26 | +import org.junit.Rule; |
| 27 | +import org.junit.Test; |
| 28 | + |
| 29 | +public class MetricsTest { |
| 30 | + |
| 31 | + private ApiClient client; |
| 32 | + |
| 33 | + @Rule public WireMockRule wireMockRule = new WireMockRule(options().dynamicPort()); |
| 34 | + |
| 35 | + @Before |
| 36 | + public void setup() throws IOException { |
| 37 | + client = new ClientBuilder().setBasePath("http://localhost:" + wireMockRule.port()).build(); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void getPodMetricsThrowsAPIExceptionWhenServerReturnsError() { |
| 42 | + String namespace = "default"; |
| 43 | + Metrics metrics = new Metrics(client); |
| 44 | + wireMockRule.stubFor( |
| 45 | + get(urlPathMatching("^/apis/metrics.k8s.io/v1beta1/namespaces/" + namespace + "/pods.*")) |
| 46 | + .willReturn( |
| 47 | + aResponse() |
| 48 | + .withStatus(503) |
| 49 | + .withHeader("Content-Type", "text/plain") |
| 50 | + .withBody("Service Unavailable"))); |
| 51 | + try { |
| 52 | + metrics.getPodMetrics(namespace); |
| 53 | + fail("Expected ApiException to be thrown"); |
| 54 | + } catch (ApiException ex) { |
| 55 | + assertEquals(503, ex.getCode()); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void getNodeMetricsThrowsAPIExceptionWhenServerReturnsError() { |
| 61 | + Metrics metrics = new Metrics(client); |
| 62 | + wireMockRule.stubFor( |
| 63 | + get(urlPathMatching("^/apis/metrics.k8s.io/v1beta1/nodes.*")) |
| 64 | + .willReturn( |
| 65 | + aResponse() |
| 66 | + .withStatus(503) |
| 67 | + .withHeader("Content-Type", "text/plain") |
| 68 | + .withBody("Service Unavailable"))); |
| 69 | + try { |
| 70 | + metrics.getNodeMetrics(); |
| 71 | + fail("Expected ApiException to be thrown"); |
| 72 | + } catch (ApiException ex) { |
| 73 | + assertEquals(503, ex.getCode()); |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments