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