Skip to content

Commit f31afbb

Browse files
committed
Add marker struct test case
1 parent 5db9eff commit f31afbb

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed

test/integration/apiserver/apply/apply_test.go

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,125 @@ func TestNoOpApplyWithEmptyMap(t *testing.T) {
356356
}
357357
}
358358

359+
// TestApplyEmptyMarkerStructDifferentFromNil
360+
func TestApplyEmptyMarkerStructDifferentFromNil(t *testing.T) {
361+
client, closeFn := setup(t)
362+
defer closeFn()
363+
364+
podName := "pod-with-empty-dir"
365+
podsResource := "pods"
366+
podBytesWithEmptyDir := []byte(`{
367+
"apiVersion": "v1",
368+
"kind": "Pod",
369+
"metadata": {
370+
"name": "` + podName + `"
371+
},
372+
"spec": {
373+
"containers": [{
374+
"name": "test-container-a",
375+
"image": "test-image-one",
376+
"volumeMounts": [{
377+
"mountPath": "/cache",
378+
"name": "cache-volume"
379+
}],
380+
}],
381+
"volumes": [{
382+
"name": "cache-volume",
383+
"emptyDir": {}
384+
}]
385+
}
386+
}`)
387+
388+
_, err := client.CoreV1().RESTClient().Patch(types.ApplyPatchType).
389+
Namespace("default").
390+
Param("fieldManager", "apply_test").
391+
Resource(podsResource).
392+
Name(podName).
393+
Body(podBytesWithEmptyDir).
394+
Do(context.TODO()).
395+
Get()
396+
if err != nil {
397+
t.Fatalf("Failed to create object: %v", err)
398+
}
399+
400+
// This sleep is necessary to consistently produce different timestamps because the time field in managedFields has
401+
// 1 second granularity and if both apply requests happen during the same second, this test would flake.
402+
time.Sleep(1 * time.Second)
403+
404+
createdObject, err := client.CoreV1().RESTClient().Get().Namespace("default").Resource(podsResource).Name(podName).Do(context.TODO()).Get()
405+
if err != nil {
406+
t.Fatalf("Failed to retrieve created object: %v", err)
407+
}
408+
409+
createdAccessor, err := meta.Accessor(createdObject)
410+
if err != nil {
411+
t.Fatalf("Failed to get meta accessor for created object: %v", err)
412+
}
413+
414+
createdBytes, err := json.MarshalIndent(createdObject, "\t", "\t")
415+
if err != nil {
416+
t.Fatalf("Failed to marshal created object: %v", err)
417+
}
418+
419+
podBytesNoEmptyDir := []byte(`{
420+
"apiVersion": "v1",
421+
"kind": "Pod",
422+
"metadata": {
423+
"name": "` + podName + `"
424+
},
425+
"spec": {
426+
"containers": [{
427+
"name": "test-container-a",
428+
"image": "test-image-one",
429+
"volumeMounts": [{
430+
"mountPath": "/cache",
431+
"name": "cache-volume"
432+
}],
433+
}],
434+
"volumes": [{
435+
"name": "cache-volume"
436+
}]
437+
}
438+
}`)
439+
440+
// Test that an apply with no emptyDir is recognized as distinct from an empty marker struct emptyDir.
441+
_, err = client.CoreV1().RESTClient().Patch(types.ApplyPatchType).
442+
Namespace("default").
443+
Param("fieldManager", "apply_test").
444+
Resource(podsResource).
445+
Name(podName).
446+
Body(podBytesNoEmptyDir).
447+
Do(context.TODO()).
448+
Get()
449+
if err != nil {
450+
t.Fatalf("Failed to create object: %v", err)
451+
}
452+
453+
updatedObject, err := client.CoreV1().RESTClient().Get().Namespace("default").Resource(podsResource).Name(podName).Do(context.TODO()).Get()
454+
if err != nil {
455+
t.Fatalf("Failed to retrieve updated object: %v", err)
456+
}
457+
458+
updatedAccessor, err := meta.Accessor(updatedObject)
459+
if err != nil {
460+
t.Fatalf("Failed to get meta accessor for updated object: %v", err)
461+
}
462+
463+
updatedBytes, err := json.MarshalIndent(updatedObject, "\t", "\t")
464+
if err != nil {
465+
t.Fatalf("Failed to marshal updated object: %v", err)
466+
}
467+
468+
if createdAccessor.GetResourceVersion() == updatedAccessor.GetResourceVersion() {
469+
t.Fatalf("Expected different resource version to be %v but got: %v\nold object:\n%v\nnew object:\n%v",
470+
createdAccessor.GetResourceVersion(),
471+
updatedAccessor.GetResourceVersion(),
472+
string(createdBytes),
473+
string(updatedBytes),
474+
)
475+
}
476+
}
477+
359478
func getRV(obj runtime.Object) (string, error) {
360479
acc, err := meta.Accessor(obj)
361480
if err != nil {

0 commit comments

Comments
 (0)