forked from fabric8io/kubernetes-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PersistentVolumeClaimTest.java
278 lines (243 loc) · 11.6 KB
/
PersistentVolumeClaimTest.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/**
* 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.PersistentVolumeClaim;
import io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder;
import io.fabric8.kubernetes.api.model.PersistentVolumeClaimList;
import io.fabric8.kubernetes.api.model.PersistentVolumeClaimListBuilder;
import io.fabric8.kubernetes.api.model.Quantity;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient;
import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer;
import io.fabric8.kubernetes.client.utils.Utils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.net.HttpURLConnection;
import java.util.Collections;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
@EnableKubernetesMockClient
class PersistentVolumeClaimTest {
KubernetesMockServer server;
KubernetesClient client;
@Test
void testList() {
server.expect().withPath("/api/v1/namespaces/test/persistentvolumeclaims")
.andReturn(200, new PersistentVolumeClaimListBuilder().build()).once();
server.expect().withPath("/api/v1/namespaces/ns1/persistentvolumeclaims")
.andReturn(200, new PersistentVolumeClaimListBuilder()
.addNewItem().and()
.addNewItem().and()
.build())
.once();
PersistentVolumeClaimList persistentVolumeClaimList = client.persistentVolumeClaims().inNamespace("test").list();
assertNotNull(persistentVolumeClaimList);
assertEquals(0, persistentVolumeClaimList.getItems().size());
persistentVolumeClaimList = client.persistentVolumeClaims().inNamespace("ns1").list();
assertNotNull(persistentVolumeClaimList);
assertEquals(2, persistentVolumeClaimList.getItems().size());
}
@Test
void testListWithlabels() {
server.expect()
.withPath("/api/v1/namespaces/test/persistentvolumeclaims?labelSelector="
+ Utils.toUrlEncoded("key1=value1,key2=value2,key3=value3"))
.andReturn(200, new PersistentVolumeClaimListBuilder().build()).once();
server.expect()
.withPath(
"/api/v1/namespaces/test/persistentvolumeclaims?labelSelector=" + Utils.toUrlEncoded("key1=value1,key2=value2"))
.andReturn(200, new PersistentVolumeClaimListBuilder()
.addNewItem().and()
.addNewItem().and()
.addNewItem().and()
.build())
.once();
PersistentVolumeClaimList persistentVolumeClaimList = client.persistentVolumeClaims().inNamespace("test")
.withLabel("key1", "value1")
.withLabel("key2", "value2")
.withLabel("key3", "value3")
.list();
assertNotNull(persistentVolumeClaimList);
assertEquals(0, persistentVolumeClaimList.getItems().size());
persistentVolumeClaimList = client.persistentVolumeClaims().inNamespace("test")
.withLabel("key1", "value1")
.withLabel("key2", "value2")
.list();
assertNotNull(persistentVolumeClaimList);
assertEquals(3, persistentVolumeClaimList.getItems().size());
}
@Test
void testGet() {
server.expect().withPath("/api/v1/namespaces/test/persistentvolumeclaims/persistentvolumeclaim1")
.andReturn(200, new PersistentVolumeClaimBuilder().build()).once();
server.expect().withPath("/api/v1/namespaces/ns1/persistentvolumeclaims/persistentvolumeclaim2")
.andReturn(200, new PersistentVolumeClaimBuilder().build()).once();
PersistentVolumeClaim persistentVolumeClaim = client.persistentVolumeClaims().inNamespace("test")
.withName("persistentvolumeclaim1").get();
assertNotNull(persistentVolumeClaim);
persistentVolumeClaim = client.persistentVolumeClaims().withName("persistentvolumeclaim2").get();
assertNull(persistentVolumeClaim);
persistentVolumeClaim = client.persistentVolumeClaims().inNamespace("ns1").withName("persistentvolumeclaim2").get();
assertNotNull(persistentVolumeClaim);
}
@Test
void testEditMissing() {
server.expect().withPath("/api/v1/namespaces/test/persistentvolumeclaims/persistentvolumeclaim")
.andReturn(404, "error message from kubernetes").always();
Resource<PersistentVolumeClaim> pvcResource = client.persistentVolumeClaims().inNamespace("test")
.withName("persistentvolumeclaim");
Assertions.assertThrows(KubernetesClientException.class, () -> pvcResource.edit(r -> r));
}
@Test
void testDelete() {
server.expect().withPath("/api/v1/namespaces/test/persistentvolumeclaims/persistentvolumeclaim1")
.andReturn(200, new PersistentVolumeClaimBuilder().build()).once();
server.expect().withPath("/api/v1/namespaces/ns1/persistentvolumeclaims/persistentvolumeclaim2")
.andReturn(200, new PersistentVolumeClaimBuilder().build()).once();
boolean deleted = client.persistentVolumeClaims().inNamespace("test").withName("persistentvolumeclaim1").delete()
.size() == 1;
assertTrue(deleted);
deleted = client.persistentVolumeClaims().withName("persistentvolumeclaim2").delete().size() == 1;
assertFalse(deleted);
deleted = client.persistentVolumeClaims().inNamespace("ns1").withName("persistentvolumeclaim2").delete().size() == 1;
assertTrue(deleted);
}
@Test
void testDeleteMulti() {
PersistentVolumeClaim persistentVolumeClaim1 = new PersistentVolumeClaimBuilder().withNewMetadata()
.withName("persistentvolumeclaim1").withNamespace("test").endMetadata().build();
PersistentVolumeClaim persistentVolumeClaim2 = new PersistentVolumeClaimBuilder().withNewMetadata()
.withName("persistentvolumeclaim2").withNamespace("ns1").endMetadata().build();
PersistentVolumeClaim persistentVolumeClaim3 = new PersistentVolumeClaimBuilder().withNewMetadata()
.withName("persistentvolumeclaim3").withNamespace("any").endMetadata().build();
server.expect().withPath("/api/v1/namespaces/test/persistentvolumeclaims/persistentvolumeclaim1")
.andReturn(200, persistentVolumeClaim1).once();
server.expect().withPath("/api/v1/namespaces/ns1/persistentvolumeclaims/persistentvolumeclaim2")
.andReturn(200, persistentVolumeClaim2).once();
Boolean deleted = client.persistentVolumeClaims().inAnyNamespace().delete(persistentVolumeClaim1, persistentVolumeClaim2);
assertTrue(deleted);
deleted = client.persistentVolumeClaims().inAnyNamespace().delete(persistentVolumeClaim3).size() == 1;
assertFalse(deleted);
}
@Test
void testLoadFromFile() {
PersistentVolumeClaim persistentVolumeClaim = client.persistentVolumeClaims()
.load(getClass().getResourceAsStream("/test-persistentvolumeclaim.yml")).item();
assertEquals("task-pv-claim", persistentVolumeClaim.getMetadata().getName());
}
@Test
void testBuild() {
PersistentVolumeClaim persistentVolumeClaim = new PersistentVolumeClaimBuilder()
.withNewMetadata().withName("test-pv-claim").withNamespace("test").endMetadata()
.withNewSpec()
.withStorageClassName("my-local-storage")
.withAccessModes("ReadWriteOnce")
.withNewResources()
.addToRequests("storage", new Quantity("500Gi"))
.endResources()
.endSpec()
.build();
server.expect().withPath("/api/v1/namespaces/test/persistentvolumeclaims/test-pv-claim")
.andReturn(200, persistentVolumeClaim).once();
persistentVolumeClaim = client.persistentVolumeClaims().inNamespace("test").withName("test-pv-claim").get();
assertNotNull(persistentVolumeClaim);
}
@Test
@DisplayName("createOrReplace, resource is new, should successfully POST to server")
void testCreateOrReplaceWhenNotExists() {
// Given
server.expect().post().withPath("/api/v1/namespaces/ns1/persistentvolumeclaims")
.andReturn(HttpURLConnection.HTTP_CREATED, mockPVCBuilder().build()).once();
// @formatter:off
final PersistentVolumeClaim toCreate = mockPVCBuilder()
.editMetadata()
.withResourceVersion(null)
.withCreationTimestamp(null)
.endMetadata()
.build();
// @formatter:on
// When
final PersistentVolumeClaim result = client.persistentVolumeClaims().inNamespace("ns1")
.createOrReplace(toCreate);
// Then
assertThat(result)
.isNotNull()
.extracting("metadata")
.hasFieldOrPropertyWithValue("resourceVersion", "1")
.extracting("creationTimestamp").asString().isNotBlank();
}
@Test
@DisplayName("createOrReplace, resource exists on server, should PUT updated resource")
void testCreateOrReplaceWhenExists() {
// Given
final PersistentVolumeClaim existent = mockPVCBuilder().build();
server.expect().post().withPath("/api/v1/namespaces/ns1/persistentvolumeclaims")
.andReturn(HttpURLConnection.HTTP_CONFLICT, "").once();
server.expect().get().withPath("/api/v1/namespaces/ns1/persistentvolumeclaims/my-pvc")
.andReturn(HttpURLConnection.HTTP_OK, existent).times(2);
server.expect().put().withPath("/api/v1/namespaces/ns1/persistentvolumeclaims/my-pvc")
.andReturn(HttpURLConnection.HTTP_OK, mockPVCBuilder()
.editMetadata().withResourceVersion("2").endMetadata().build())
.once();
// @formatter:off
final PersistentVolumeClaim toReplace = mockPVCBuilder()
.editMetadata()
.withResourceVersion(null)
.withCreationTimestamp(null)
.endMetadata()
.build();
// @formatter:on
// When
final PersistentVolumeClaim result = client.persistentVolumeClaims().inNamespace("ns1")
.createOrReplace(toReplace);
// Then
assertThat(result)
.isNotNull()
.extracting("metadata")
.hasFieldOrPropertyWithValue("resourceVersion", "2")
.hasFieldOrPropertyWithValue("creationTimestamp", existent.getMetadata().getCreationTimestamp());
}
private static PersistentVolumeClaimBuilder mockPVCBuilder() {
// @formatter:off
return new PersistentVolumeClaimBuilder()
.withNewMetadata()
.withName("my-pvc")
.withCreationTimestamp("2020-07-27T11:02:15Z")
.addToFinalizers("kubernetes.io/pvc-protection")
.withNamespace("ns1")
.withResourceVersion("1")
.withSelfLink("/api/v1/namespaces/default/persistentvolumeclaims/my-pvc")
.withUid("60817eaa-19d8-41ba-adb4-3ea75860e1f8")
.endMetadata()
.withNewSpec()
.withAccessModes("ReadWriteOnce")
.withNewResources()
.withRequests(Collections.singletonMap("storage", new Quantity("20Gi")))
.endResources()
.withStorageClassName("standard")
.withVolumeMode("Filesystem")
.withVolumeName("pvc-60817eaa-19d8-41ba-adb4-3ea75860e1f8")
.endSpec();
// @formatter:on
}
}