-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
TypedCustomResourceApiTest.java
164 lines (133 loc) · 7.22 KB
/
TypedCustomResourceApiTest.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
/**
* 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.ObjectMetaBuilder;
import io.fabric8.kubernetes.api.model.apiextensions.CustomResourceDefinition;
import io.fabric8.kubernetes.api.model.apiextensions.CustomResourceDefinitionBuilder;
import io.fabric8.kubernetes.client.dsl.MixedOperation;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.fabric8.kubernetes.client.mock.crd.DoneablePodSet;
import io.fabric8.kubernetes.client.mock.crd.PodSet;
import io.fabric8.kubernetes.client.mock.crd.PodSetList;
import io.fabric8.kubernetes.client.mock.crd.PodSetSpec;
import io.fabric8.kubernetes.client.mock.crd.PodSetStatus;
import io.fabric8.kubernetes.client.server.mock.KubernetesServer;
import okhttp3.mockwebserver.RecordedRequest;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.migrationsupport.rules.EnableRuleMigrationSupport;
import java.util.Collections;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
@EnableRuleMigrationSupport
public class TypedCustomResourceApiTest {
@Rule
public KubernetesServer server = new KubernetesServer();
private MixedOperation<PodSet, PodSetList, DoneablePodSet, Resource<PodSet, DoneablePodSet>> podSetClient;
private CustomResourceDefinition podSetCrd;
@Before
public void setupCrd() {
podSetCrd = new CustomResourceDefinitionBuilder()
.withNewMetadata().withName("podsets.demo.k8s.io").endMetadata()
.withNewSpec()
.withGroup("demo.k8s.io")
.withVersion("v1alpha1")
.withNewNames().withKind("PodSet").withPlural("podsets").endNames()
.withScope("Namespaced")
.endSpec()
.build();
}
@Test
public void create() {
server.expect().post().withPath("/apis/demo.k8s.io/v1alpha1/namespaces/test/podsets").andReturn(200, getPodSet()).once();
podSetClient = server.getClient().customResources(podSetCrd, PodSet.class, PodSetList.class, DoneablePodSet.class);
PodSet returnedPodSet = podSetClient.inNamespace("test").create(getPodSet());
assertNotNull(returnedPodSet);
assertEquals("example-podset", returnedPodSet.getMetadata().getName());
}
@Test
public void list() {
PodSetList podSetList = new PodSetList();
podSetList.setItems(Collections.singletonList(getPodSet()));
server.expect().get().withPath("/apis/demo.k8s.io/v1alpha1/namespaces/test/podsets").andReturn(200, podSetList).once();
podSetClient = server.getClient().customResources(podSetCrd, PodSet.class, PodSetList.class, DoneablePodSet.class);
podSetList = podSetClient.inNamespace("test").list();
assertNotNull(podSetList);
assertEquals(1, podSetList.getItems().size());
assertEquals("example-podset", podSetList.getItems().get(0).getMetadata().getName());
}
@Test
public void createOrReplace() {
server.expect().get().withPath("/apis/demo.k8s.io/v1alpha1/namespaces/test/podsets/example-podset").andReturn(200, getPodSet()).times(2);
server.expect().put().withPath("/apis/demo.k8s.io/v1alpha1/namespaces/test/podsets/example-podset").andReturn(200, getPodSet()).once();
podSetClient = server.getClient().customResources(podSetCrd, PodSet.class, PodSetList.class, DoneablePodSet.class);
PodSet returnedPodSet = podSetClient.inNamespace("test").createOrReplace(getPodSet());
assertNotNull(returnedPodSet);
assertEquals("example-podset", returnedPodSet.getMetadata().getName());
}
@Test
public void delete() {
server.expect().delete().withPath("/apis/demo.k8s.io/v1alpha1/namespaces/test/podsets/example-podset").andReturn(200, getPodSet()).once();
podSetClient = server.getClient().customResources(podSetCrd, PodSet.class, PodSetList.class, DoneablePodSet.class);
boolean isDeleted = podSetClient.inNamespace("test").withName("example-podset").delete();
assertTrue(isDeleted);
}
@Test
public void testCascadingDeletion() throws InterruptedException {
server.expect().delete().withPath("/apis/demo.k8s.io/v1alpha1/namespaces/test/podsets/example-podset").andReturn(200, getPodSet()).once();
podSetClient = server.getClient().customResources(podSetCrd, PodSet.class, PodSetList.class, DoneablePodSet.class);
boolean isDeleted = podSetClient.inNamespace("test").withName("example-podset").cascading(true).delete();
assertTrue(isDeleted);
RecordedRequest recordedRequest = server.getLastRequest();
assertEquals("DELETE", recordedRequest.getMethod());
assertEquals("{\"apiVersion\":\"v1\",\"kind\":\"DeleteOptions\",\"orphanDependents\":false}", recordedRequest.getBody().readUtf8());
}
@Test
public void testPropagationPolicyDeletion() throws InterruptedException {
server.expect().delete().withPath("/apis/demo.k8s.io/v1alpha1/namespaces/test/podsets/example-podset").andReturn(200, getPodSet()).once();
podSetClient = server.getClient().customResources(podSetCrd, PodSet.class, PodSetList.class, DoneablePodSet.class);
boolean isDeleted = podSetClient.inNamespace("test").withName("example-podset").withPropagationPolicy("Orphan").delete();
assertTrue(isDeleted);
RecordedRequest recordedRequest = server.getLastRequest();
assertEquals("DELETE", recordedRequest.getMethod());
assertEquals("{\"apiVersion\":\"v1\",\"kind\":\"DeleteOptions\",\"propagationPolicy\":\"Orphan\"}", recordedRequest.getBody().readUtf8());
}
@Test
public void testStatusUpdation() throws InterruptedException {
PodSet updatedPodSet = getPodSet();
PodSetStatus podSetStatus = new PodSetStatus();
podSetStatus.setAvailableReplicas(4);
updatedPodSet.setStatus(podSetStatus);
server.expect().put().withPath("/apis/demo.k8s.io/v1alpha1/namespaces/test/podsets/example-podset/status").andReturn(200, updatedPodSet).once();
podSetClient = server.getClient().customResources(podSetCrd, PodSet.class, PodSetList.class, DoneablePodSet.class);
podSetClient.inNamespace("test").updateStatus(updatedPodSet);
RecordedRequest recordedRequest = server.getLastRequest();
assertEquals("PUT", recordedRequest.getMethod());
assertEquals("{\"kind\":\"PodSet\",\"apiVersion\":\"demo.k8s.io/v1alpha1\",\"metadata\":{\"name\":\"example-podset\"},\"spec\":{\"replicas\":5},\"status\":{\"availableReplicas\":4}}", recordedRequest.getBody().readUtf8());
System.out.println(recordedRequest.getBody().readUtf8());
}
private PodSet getPodSet() {
PodSetSpec podSetSpec = new PodSetSpec();
podSetSpec.setReplicas(5);
PodSet podSet = new PodSet();
podSet.setApiVersion("demo.k8s.io/v1alpha1");
podSet.setMetadata(new ObjectMetaBuilder().withName("example-podset").build());
podSet.setSpec(podSetSpec);
return podSet;
}
}