-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
PatchIT.java
169 lines (139 loc) · 6.16 KB
/
PatchIT.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
/**
* 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;
import io.fabric8.commons.ClusterEntity;
import io.fabric8.kubernetes.api.model.ConfigMap;
import io.fabric8.kubernetes.api.model.ConfigMapBuilder;
import io.fabric8.kubernetes.api.model.apps.ReplicaSet;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.dsl.base.PatchContext;
import io.fabric8.kubernetes.client.dsl.base.PatchType;
import org.arquillian.cube.kubernetes.api.Session;
import org.arquillian.cube.kubernetes.impl.requirement.RequiresKubernetes;
import org.arquillian.cube.requirement.ArquillianConditionalRunner;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.Collections;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertThrows;
@RunWith(ArquillianConditionalRunner.class)
@RequiresKubernetes
public class PatchIT {
@ArquillianResource
KubernetesClient client;
@ArquillianResource
Session session;
private String currentNamespace;
@BeforeClass
public static void init() {
ClusterEntity.apply(PatchIT.class.getResourceAsStream("/patch-it.yml"));
}
@Before
public void initNamespace() {
this.currentNamespace = session.getNamespace();
}
@Test
public void testJsonPatch() {
// Given
String name = "patchit-testjsonpatch";
// When
ConfigMap patchedConfigMap = client.configMaps().inNamespace(currentNamespace).withName(name).patch("{\"metadata\":{\"labels\":{\"version\":\"v1\"}}}");
// Then
assertThat(patchedConfigMap).isNotNull();
assertThat(patchedConfigMap.getMetadata().getLabels()).isNotNull()
.hasFieldOrPropertyWithValue("version", "v1");
}
@Test
public void testJsonMergePatch() {
// Given
String name = "patchit-testjsonmergepatch";
PatchContext patchContext = new PatchContext.Builder().withPatchType(PatchType.JSON_MERGE).build();
// When
ConfigMap patchedConfigMap = client.configMaps().inNamespace(currentNamespace).withName(name)
.patch(patchContext, "{\"metadata\":{\"annotations\":{\"foo\":null}}}");
// Then
assertThat(patchedConfigMap).isNotNull();
assertThat(patchedConfigMap.getMetadata().getAnnotations()).isNull();
}
@Test
public void testJsonPatchWithPositionalArrays() {
// Given
String name = "patchit-testjsonpatchpositionalarray";
PatchContext patchContext = new PatchContext.Builder().withPatchType(PatchType.JSON).build();
// When
ReplicaSet patchedReplicaSet = client.apps().replicaSets().inNamespace(currentNamespace).withName(name)
.patch(patchContext
, "[{\"op\": \"replace\", \"path\":\"/spec/template/spec/containers/0/image\", \"value\":\"foo/gb-frontend:v4\"}]");
// Then
assertThat(patchedReplicaSet).isNotNull();
assertThat(patchedReplicaSet.getSpec().getTemplate().getSpec().getContainers().get(0).getImage()).isEqualTo("foo/gb-frontend:v4");
}
@Test
public void testYamlPatch() {
// Given
String name = "patchit-testyamlpatch";
// When
ConfigMap patchedConfigMap = client.configMaps().inNamespace(currentNamespace).withName(name)
.patch("data:\n version: v1\n status: patched");
// Then
assertThat(patchedConfigMap).isNotNull();
assertThat(patchedConfigMap.getData())
.hasFieldOrPropertyWithValue("version", "v1")
.hasFieldOrPropertyWithValue("status", "patched");
}
@Test
public void testFullObjectPatch() {
// Given
String name = "patchit-fullobjectpatch";
// When
ConfigMap configMapFromServer = client.configMaps().inNamespace(currentNamespace).withName(name).get();
configMapFromServer.setData(Collections.singletonMap("foo", "bar"));
ConfigMap patchedConfigMap = client.configMaps().inNamespace(currentNamespace).withName(name).patch(configMapFromServer);
// Then
assertThat(patchedConfigMap).isNotNull();
assertThat(patchedConfigMap.getData()).hasFieldOrPropertyWithValue("foo", "bar");
}
@Test
public void testFullObjectPatchWithConcurrentChange() {
// Given
String name = "patchit-fullobjectpatch";
// When
ConfigMap configMapFromServer = client.configMaps().inNamespace(currentNamespace).withName(name).get();
configMapFromServer.setData(Collections.singletonMap("conflicting", "change"));
ConfigMap base = client.configMaps().inNamespace(currentNamespace).withName(name).patch(configMapFromServer);
// concurrent change to empty
ConfigMap baseCopy = new ConfigMapBuilder(base).build();
baseCopy.setData(Collections.emptyMap());
client.configMaps().inNamespace(currentNamespace).withName(name).patch(baseCopy);
// concurrent change to empty
ConfigMap baseCopy2 = new ConfigMapBuilder(base).build();
baseCopy2.setData(Collections.singletonMap("conflicting", "second"));
// optimistically locking should work because the resource version is set
assertThrows(KubernetesClientException.class, () -> client.configMaps().inNamespace(currentNamespace).withName(name).patch(new PatchContext.Builder().withPatchType(PatchType.JSON_MERGE).build(), baseCopy2));
baseCopy2.getMetadata().setResourceVersion(null);
// will succeed when not locked
client.configMaps().inNamespace(currentNamespace).withName(name).patch(new PatchContext.Builder().withPatchType(PatchType.JSON_MERGE).build(), baseCopy2);
}
@AfterClass
public static void cleanup() {
ClusterEntity.remove(CronJobIT.class.getResourceAsStream("/patch-it.yml"));
}
}