forked from fabric8io/kubernetes-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MetricsTest.java
140 lines (115 loc) · 5.28 KB
/
MetricsTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.kubernetes.client.mock;
import io.fabric8.kubernetes.api.model.Duration;
import io.fabric8.kubernetes.api.model.Quantity;
import io.fabric8.kubernetes.api.model.metrics.v1beta1.NodeMetrics;
import io.fabric8.kubernetes.api.model.metrics.v1beta1.NodeMetricsBuilder;
import io.fabric8.kubernetes.api.model.metrics.v1beta1.NodeMetricsList;
import io.fabric8.kubernetes.api.model.metrics.v1beta1.NodeMetricsListBuilder;
import io.fabric8.kubernetes.api.model.metrics.v1beta1.PodMetrics;
import io.fabric8.kubernetes.api.model.metrics.v1beta1.PodMetricsBuilder;
import io.fabric8.kubernetes.api.model.metrics.v1beta1.PodMetricsList;
import io.fabric8.kubernetes.api.model.metrics.v1beta1.PodMetricsListBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.server.mock.KubernetesServer;
import io.fabric8.kubernetes.client.utils.Utils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.migrationsupport.rules.EnableRuleMigrationSupport;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.assertEquals;
@EnableRuleMigrationSupport
public class MetricsTest {
@Rule
public KubernetesServer server = new KubernetesServer();
@Test
public void testPodMetricsAllNamespace() throws Exception {
server.expect().get().withPath("/apis/metrics.k8s.io/v1beta1/pods")
.andReturn(200, new PodMetricsListBuilder().withItems(getPodMetric()).build()).once();
KubernetesClient client = server.getClient();
PodMetricsList podMetricsList = client.top().pods().metrics();
assertEquals(1, podMetricsList.getItems().size());
assertEquals("foo", podMetricsList.getItems().get(0).getMetadata().getName());
}
@Test
public void testPodMetricsNamespace() throws Exception {
server.expect().get().withPath("/apis/metrics.k8s.io/v1beta1/namespaces/test/pods")
.andReturn(200, new PodMetricsListBuilder().withItems(getPodMetric()).build()).once();
KubernetesClient client = server.getClient();
PodMetricsList podMetricsList = client.top().pods().metrics("test");
assertEquals(1, podMetricsList.getItems().size());
assertEquals("foo", podMetricsList.getItems().get(0).getMetadata().getName());
}
@Test
public void testPodMetricsNamespaceWithName() throws Exception {
server.expect().get().withPath("/apis/metrics.k8s.io/v1beta1/namespaces/test/pods/test-pod")
.andReturn(200, getPodMetric()).once();
KubernetesClient client = server.getClient();
PodMetrics podMetrics = client.top().pods().metrics("test", "test-pod");
assertEquals("foo", podMetrics.getMetadata().getName());
}
@Test
public void testAllNodeMetrics() {
server.expect().get().withPath("/apis/metrics.k8s.io/v1beta1/nodes")
.andReturn(200, new NodeMetricsListBuilder().withItems(getNodeMetric()).build()).once();
KubernetesClient client = server.getClient();
NodeMetricsList nodeMetricsList = client.top().nodes().metrics();
assertEquals(1, nodeMetricsList.getItems().size());
assertEquals("foo", nodeMetricsList.getItems().get(0).getMetadata().getName());
}
@Test
public void testNodeMetric() {
server.expect().get().withPath("/apis/metrics.k8s.io/v1beta1/nodes/test-node")
.andReturn(200, getNodeMetric()).once();
KubernetesClient client = server.getClient();
NodeMetrics nodeMetrics = client.top().nodes().metrics("test-node");
assertEquals("foo", nodeMetrics.getMetadata().getName());
}
@Test
public void testNodeMetricWithLabels() {
// Given
server.expect().get().withPath("/apis/metrics.k8s.io/v1beta1/nodes?labelSelector=" + Utils.toUrlEncoded("ss=true,cs=true"))
.andReturn(200, new NodeMetricsListBuilder().withItems(getNodeMetric()).build()).once();
KubernetesClient client = server.getClient();
Map<String,Object> lablesMap = new HashMap();
lablesMap.put("ss", "true");
lablesMap.put("cs", "true");
// When
NodeMetricsList nodeMetricList = client.top().nodes().metrics(lablesMap);
// Then
assertEquals(1, nodeMetricList.getItems().size());
}
private PodMetrics getPodMetric() throws Exception {
return new PodMetricsBuilder()
.withNewMetadata().withName("foo").endMetadata()
.withWindow(Duration.parse("1m0s"))
.addNewContainer()
.withName("test-container")
.withUsage(Collections.singletonMap("cpu", new Quantity("38m")))
.endContainer()
.build();
}
private NodeMetrics getNodeMetric() {
return new NodeMetricsBuilder()
.withNewMetadata().withName("foo").endMetadata()
.withWindow(new Duration(java.time.Duration.ofMinutes(1L)))
.withUsage(Collections.singletonMap("cpu", new Quantity("38m")))
.build();
}
}