@@ -28,6 +28,7 @@ import io.fabric8.kubernetes.api.model.APIResource
28
28
import io.fabric8.kubernetes.api.model.GenericKubernetesResource
29
29
import io.fabric8.kubernetes.api.model.GenericKubernetesResourceList
30
30
import io.fabric8.kubernetes.api.model.HasMetadata
31
+ import io.fabric8.kubernetes.api.model.ManagedFieldsEntry
31
32
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder
32
33
import io.fabric8.kubernetes.api.model.PodBuilder
33
34
import io.fabric8.kubernetes.client.KubernetesClientException
@@ -185,6 +186,63 @@ class NonCachingSingleResourceOperatorTest {
185
186
// then
186
187
}
187
188
189
+ @Test
190
+ fun `#create should call #patch(SERVER_SIDE_APPLY) if resource has a name and NO managed fields` () {
191
+ // given
192
+ val metadata = ObjectMetaBuilder ().build().apply {
193
+ managedFields = null
194
+ }
195
+ val hasName = PodBuilder (namespacedCoreResource)
196
+ .withMetadata(metadata)
197
+ .build()
198
+ hasName.metadata.name = " yoda"
199
+ hasName.metadata.generateName = null
200
+ val apiResource = namespacedApiResource(namespacedCoreResource)
201
+ val operator = NonCachingSingleResourceOperator (clientAdapter, createAPIResources(apiResource))
202
+ // when
203
+ operator .create(hasName)
204
+ // then
205
+ verify(resourceOp)
206
+ .patch(argThat(ArgumentMatcher <PatchContext > { context ->
207
+ context.patchType == PatchType .SERVER_SIDE_APPLY
208
+ }))
209
+ }
210
+
211
+ @Test
212
+ fun `#create should call #create if resource has no name` () {
213
+ // given
214
+ val hasNoName = PodBuilder (namespacedCoreResource)
215
+ .withNewMetadata()
216
+ .withManagedFields(ManagedFieldsEntry ())
217
+ .endMetadata()
218
+ .build()
219
+ val apiResource = namespacedApiResource(namespacedCoreResource)
220
+ val operator = NonCachingSingleResourceOperator (clientAdapter, createAPIResources(apiResource))
221
+ // when
222
+ operator .create(hasNoName)
223
+ // then
224
+ verify(resourceOp)
225
+ .create()
226
+ }
227
+
228
+ @Test
229
+ fun `#create should call #create if resource has a name but managed fields` () {
230
+ // given
231
+ val hasNameAndManagedFields = PodBuilder (namespacedCoreResource)
232
+ .withNewMetadata()
233
+ .withManagedFields(ManagedFieldsEntry ())
234
+ .withName(" obiwan" )
235
+ .endMetadata()
236
+ .build()
237
+ val apiResource = namespacedApiResource(namespacedCoreResource)
238
+ val operator = NonCachingSingleResourceOperator (clientAdapter, createAPIResources(apiResource))
239
+ // when
240
+ operator .create(hasNameAndManagedFields)
241
+ // then
242
+ verify(resourceOp)
243
+ .create()
244
+ }
245
+
188
246
@Test
189
247
fun `#replace should call #inNamespace for namespaced resource` () {
190
248
// given
@@ -210,11 +268,40 @@ class NonCachingSingleResourceOperatorTest {
210
268
}
211
269
212
270
@Test
213
- fun `#replace should call #patch() if resource has a name` () {
271
+ fun `#replace should call #patch(JSON_MERGE ) if resource has a name and managed fields ` () {
214
272
// given
215
- val hasName = PodBuilder (namespacedCoreResource).build()
216
- hasName.metadata.name = " yoda"
217
- hasName.metadata.generateName = null
273
+ val metadata = ObjectMetaBuilder ()
274
+ .withManagedFields(ManagedFieldsEntry ())
275
+ .build().apply {
276
+ name = " yoda"
277
+ generateName = null
278
+ }
279
+ val hasName = PodBuilder (namespacedCoreResource)
280
+ .withMetadata(metadata)
281
+ .build()
282
+ val apiResource = namespacedApiResource(namespacedCoreResource)
283
+ val operator = NonCachingSingleResourceOperator (clientAdapter, createAPIResources(apiResource))
284
+ // when
285
+ operator .replace(hasName)
286
+ // then
287
+ verify(resourceOp)
288
+ .patch(argThat(ArgumentMatcher <PatchContext > { context ->
289
+ context.patchType == PatchType .JSON_MERGE
290
+ }))
291
+ }
292
+
293
+ @Test
294
+ fun `#replace should call #patch(SERVER_SIDE_APPLY) if resource has a name and NO managed fields` () {
295
+ // given
296
+ val metadata = ObjectMetaBuilder ()
297
+ .build().apply {
298
+ name = " yoda"
299
+ generateName = null
300
+ managedFields = null
301
+ }
302
+ val hasName = PodBuilder (namespacedCoreResource)
303
+ .withMetadata(metadata)
304
+ .build()
218
305
val apiResource = namespacedApiResource(namespacedCoreResource)
219
306
val operator = NonCachingSingleResourceOperator (clientAdapter, createAPIResources(apiResource))
220
307
// when
@@ -229,9 +316,10 @@ class NonCachingSingleResourceOperatorTest {
229
316
@Test
230
317
fun `#replace should call #create() if resource has NO name but has generateName` () {
231
318
// given
232
- val hasGeneratedName = PodBuilder (namespacedCoreResource).build()
233
- hasGeneratedName.metadata.name = null
234
- hasGeneratedName.metadata.generateName = " storm trooper clone"
319
+ val hasGeneratedName = PodBuilder (namespacedCoreResource).build().apply {
320
+ metadata.name = null
321
+ metadata.generateName = " storm trooper clone"
322
+ }
235
323
val operator = NonCachingSingleResourceOperator (
236
324
clientAdapter,
237
325
createAPIResources(namespacedApiResource(hasGeneratedName))
@@ -246,9 +334,10 @@ class NonCachingSingleResourceOperatorTest {
246
334
@Test(expected = ResourceException ::class )
247
335
fun `#replace should throw if resource has NO name NOR generateName` () {
248
336
// given
249
- val generatedName = PodBuilder (namespacedCoreResource).build()
250
- generatedName.metadata.name = null
251
- generatedName.metadata.generateName = null
337
+ val generatedName = PodBuilder (namespacedCoreResource).build().apply {
338
+ metadata.name = null
339
+ metadata.generateName = null
340
+ }
252
341
val apiResource = namespacedApiResource(namespacedCustomResource)
253
342
val operator = NonCachingSingleResourceOperator (clientAdapter, createAPIResources(apiResource))
254
343
// when
@@ -306,8 +395,10 @@ class NonCachingSingleResourceOperatorTest {
306
395
@Test
307
396
fun `#watch should return NULL if resource has NO name` () {
308
397
// given
309
- val unnamed = PodBuilder (namespacedCoreResource).build()
310
- unnamed.metadata.name = null
398
+ val unnamed = PodBuilder (namespacedCoreResource).build().apply {
399
+ metadata.name = null
400
+ }
401
+
311
402
val apiResource = namespacedApiResource(unnamed)
312
403
val operator = NonCachingSingleResourceOperator (clientAdapter, createAPIResources(apiResource))
313
404
// when
0 commit comments