forked from fabric8io/kubernetes-client
/
V1HorizontalPodAutoscalerTest.java
215 lines (178 loc) · 10.4 KB
/
V1HorizontalPodAutoscalerTest.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**
* 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.HasMetadata;
import io.fabric8.kubernetes.api.model.autoscaling.v1.HorizontalPodAutoscaler;
import io.fabric8.kubernetes.api.model.autoscaling.v1.HorizontalPodAutoscalerList;
import io.fabric8.kubernetes.api.model.autoscaling.v1.HorizontalPodAutoscalerBuilder;
import io.fabric8.kubernetes.api.model.autoscaling.v1.HorizontalPodAutoscalerListBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.server.mock.KubernetesServer;
import io.fabric8.kubernetes.client.utils.Utils;
import org.junit.Rule;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.migrationsupport.rules.EnableRuleMigrationSupport;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;
@EnableRuleMigrationSupport
public class V1HorizontalPodAutoscalerTest {
@Rule
public KubernetesServer server = new KubernetesServer();
@Test
void testLoadAsPlainList() {
KubernetesClient client = server.getClient();
List<HasMetadata> itemsList = client.load(getClass().getResourceAsStream("/test-horizontalpodautoscaler.yml")).get();
assertNotNull(itemsList);
assertEquals(1, itemsList.size());
HorizontalPodAutoscaler horizontalPodAutoscaler = (HorizontalPodAutoscaler) itemsList.get(0);
assertEquals("php-apache", horizontalPodAutoscaler.getMetadata().getName());
assertEquals(10, horizontalPodAutoscaler.getSpec().getMaxReplicas());
assertEquals(1, horizontalPodAutoscaler.getSpec().getMinReplicas());
}
@Test
void testList() {
server.expect().withPath("/apis/autoscaling/v1/namespaces/test/horizontalpodautoscalers").andReturn(200, new HorizontalPodAutoscalerListBuilder().build()).once();
server.expect().withPath("/apis/autoscaling/v1/namespaces/ns1/horizontalpodautoscalers").andReturn(200, new HorizontalPodAutoscalerListBuilder()
.addNewItem().and()
.addNewItem().and()
.build()).once();
KubernetesClient client = server.getClient();
HorizontalPodAutoscalerList horizontalPodAutoscalerList = client.autoscaling().v1().horizontalPodAutoscalers().inNamespace("test").list();
assertNotNull(horizontalPodAutoscalerList);
assertEquals(0, horizontalPodAutoscalerList.getItems().size());
horizontalPodAutoscalerList = client.autoscaling().v1().horizontalPodAutoscalers().inNamespace("ns1").list();
assertNotNull(horizontalPodAutoscalerList);
assertEquals(2, horizontalPodAutoscalerList.getItems().size());
}
@Test
void testListWithLabels() {
server.expect().withPath("/apis/autoscaling/v1/namespaces/test/horizontalpodautoscalers?labelSelector=" + Utils.toUrlEncoded("key1=value1,key2=value2,key3=value3")).andReturn(200, new HorizontalPodAutoscalerListBuilder().build()).once();
server.expect().withPath("/apis/autoscaling/v1/namespaces/ns1/horizontalpodautoscalers?labelSelector=" + Utils.toUrlEncoded("key1=value1,key2=value2")).andReturn(200, new HorizontalPodAutoscalerListBuilder()
.addNewItem().and()
.addNewItem().and()
.addNewItem().and()
.build()).once();
KubernetesClient client = server.getClient();
HorizontalPodAutoscalerList horizontalPodAutoscalerList = client.autoscaling().v1().horizontalPodAutoscalers()
.withLabel("key1", "value1")
.withLabel("key2","value2")
.withLabel("key3","value3")
.list();
assertNotNull(horizontalPodAutoscalerList);
assertEquals(0, horizontalPodAutoscalerList.getItems().size());
horizontalPodAutoscalerList = client.autoscaling().v1().horizontalPodAutoscalers().inNamespace("ns1")
.withLabel("key1", "value1")
.withLabel("key2","value2")
.list();
assertNotNull(horizontalPodAutoscalerList);
assertEquals(3, horizontalPodAutoscalerList.getItems().size());
}
@Test
void testGet() {
server.expect().withPath("/apis/autoscaling/v1/namespaces/test/horizontalpodautoscalers/horizontalpodautoscaler1").andReturn(200, new HorizontalPodAutoscalerBuilder().build()).once();
server.expect().withPath("/apis/autoscaling/v1/namespaces/ns1/horizontalpodautoscalers/horizontalpodautoscaler2").andReturn(200, new HorizontalPodAutoscalerBuilder().build()).once();
KubernetesClient client = server.getClient();
HorizontalPodAutoscaler horizontalPodAutoscaler = client.autoscaling().v1().horizontalPodAutoscalers().inNamespace("test").withName("horizontalpodautoscaler1").get();
assertNotNull(horizontalPodAutoscaler);
horizontalPodAutoscaler = client.autoscaling().v1().horizontalPodAutoscalers().withName("horizontalpodautoscaler2").get();
assertNull(horizontalPodAutoscaler);
horizontalPodAutoscaler = client.autoscaling().v1().horizontalPodAutoscalers().inNamespace("ns1").withName("horizontalpodautoscaler2").get();
assertNotNull(horizontalPodAutoscaler);
}
@Test
void testEditMissing() {
Assertions.assertThrows(KubernetesClientException.class, () -> {
server.expect().withPath("/apis/autoscaling/v1/namespaces/test/horizontalpodautoscalers/horizontalpodautoscaler").andReturn(404, "error message from kubernetes").always();
KubernetesClient client = server.getClient();
client.autoscaling().v1().horizontalPodAutoscalers().inNamespace("test").withName("horizontalpodautoscaler").edit();
});
}
@Test
void testDelete() {
server.expect().withPath("/apis/autoscaling/v1/namespaces/test/horizontalpodautoscalers/horizontalpodautoscaler1").andReturn(200, new HorizontalPodAutoscalerBuilder().build()).once();
server.expect().withPath("/apis/autoscaling/v1/namespaces/ns1/horizontalpodautoscalers/horizontalpodautoscaler2").andReturn(200, new HorizontalPodAutoscalerBuilder().build()).once();
KubernetesClient client = server.getClient();
Boolean deleted = client.autoscaling().v1().horizontalPodAutoscalers().inNamespace("test").withName("horizontalpodautoscaler1").delete();
assertTrue(deleted);
deleted = client.autoscaling().v1().horizontalPodAutoscalers().withName("horizontalpodautoscaler2").delete();
assertFalse(deleted);
deleted = client.autoscaling().v1().horizontalPodAutoscalers().inNamespace("ns1").withName("horizontalpodautoscaler2").delete();
assertTrue(deleted);
}
@Test
void testDeleteMulti() {
HorizontalPodAutoscaler horizontalPodAutoscaler1 = new HorizontalPodAutoscalerBuilder().withNewMetadata().withName("horizontalpodautoscaler1").withNamespace("test").endMetadata().build();
HorizontalPodAutoscaler horizontalPodAutoscaler2 = new HorizontalPodAutoscalerBuilder().withNewMetadata().withName("horizontalpodautoscaler2").withNamespace("ns1").endMetadata().build();
HorizontalPodAutoscaler horizontalPodAutoscaler3 = new HorizontalPodAutoscalerBuilder().withNewMetadata().withName("horizontalpodautoscaler3").withNamespace("any").endMetadata().build();
server.expect().withPath("/apis/autoscaling/v1/namespaces/test/horizontalpodautoscalers/horizontalpodautoscaler1").andReturn(200, horizontalPodAutoscaler1).once();
server.expect().withPath("/apis/autoscaling/v1/namespaces/ns1/horizontalpodautoscalers/horizontalpodautoscaler2").andReturn(200, horizontalPodAutoscaler2).once();
KubernetesClient client = server.getClient();
Boolean deleted = client.autoscaling().v1().horizontalPodAutoscalers().inAnyNamespace().delete(horizontalPodAutoscaler1, horizontalPodAutoscaler2);
assertTrue(deleted);
deleted = client.autoscaling().v1().horizontalPodAutoscalers().inAnyNamespace().delete(horizontalPodAutoscaler3);
assertFalse(deleted);
}
@Test
void testCreateWithNameMismatch() {
Assertions.assertThrows(KubernetesClientException.class, () -> {
HorizontalPodAutoscaler horizontalPodAutoscaler1 = new HorizontalPodAutoscalerBuilder().withNewMetadata().withName("horizontalpodautoscaler1").withNamespace("test").endMetadata().build();
KubernetesClient client = server.getClient();
client.autoscaling().v1().horizontalPodAutoscalers().inNamespace("test").withName("horizontalpodautoscaler1").create(horizontalPodAutoscaler1);
});
}
@Test
void testLoadFromFile() {
KubernetesClient client = server.getClient();
HorizontalPodAutoscaler horizontalPodAutoscaler = client.autoscaling().v1().horizontalPodAutoscalers().load(getClass().getResourceAsStream("/test-horizontalpodautoscaler.yml")).get();
assertEquals("php-apache", horizontalPodAutoscaler.getMetadata().getName());
}
@Test
void testBuild() {
HorizontalPodAutoscaler horizontalPodAutoscaler = new HorizontalPodAutoscalerBuilder()
.withNewMetadata().withName("test-hpa").withNamespace("test").endMetadata()
.withNewSpec()
.withNewScaleTargetRef()
.withApiVersion("apps/v1")
.withKind("Deployment")
.withName("the-deployment")
.endScaleTargetRef()
.withMinReplicas(1)
.withMaxReplicas(10)
.endSpec()
.build();
server.expect().withPath("/apis/autoscaling/v1/namespaces/test/horizontalpodautoscalers/test-hpa").andReturn(200, horizontalPodAutoscaler).once();
KubernetesClient client = server.getClient();
horizontalPodAutoscaler = client.autoscaling().v1().horizontalPodAutoscalers().inNamespace("test").withName("test-hpa").get();
assertNotNull(horizontalPodAutoscaler);
}
@Test
void testHorizontalPodAutoscalerLoadWithNoApiVersion() {
// Given
KubernetesClient client = server.getClient();
// When
List<HasMetadata> items = client.load(getClass().getResourceAsStream("/test-hpa-no-apiversion.yml")).get();
// Then
assertNotNull(items);
assertEquals(1, items.size());
assertTrue(items.get(0) instanceof HorizontalPodAutoscaler);
}
}