From b924de37eb830994d48ef74c7545df38c6528bef Mon Sep 17 00:00:00 2001 From: Yu-Lin Chen Date: Fri, 15 Sep 2023 19:08:34 +0000 Subject: [PATCH] Add test case if another annotation patch exists --- pkg/admission/admission_controller_test.go | 36 +++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/pkg/admission/admission_controller_test.go b/pkg/admission/admission_controller_test.go index aab9a83b9..f38d37200 100644 --- a/pkg/admission/admission_controller_test.go +++ b/pkg/admission/admission_controller_test.go @@ -51,7 +51,7 @@ const ( ) // nolint: funlen -func TestUpdateLabels(t *testing.T) { +func TestUpdateApplicationInfo(t *testing.T) { // verify when appId/queue are not given, // we patch it correctly var patch []common.PatchOperation @@ -248,6 +248,40 @@ func TestUpdateLabels(t *testing.T) { } else { t.Fatal("patch info content is not as expected") } + + // should combine with the existing patch, which has the 'add' operation and the '/metadata/annotations' path. + patch = make([]common.PatchOperation, 0) + patch = append(patch, common.PatchOperation{ + Op: "add", + Path: "/metadata/annotations", + Value: map[string]string{ + "existingAnnotationKey": "existingAnnotationValue", + }, + }) + pod = &v1.Pod{ + TypeMeta: metav1.TypeMeta{ + Kind: "Pod", + APIVersion: "v1", + }, + ObjectMeta: metav1.ObjectMeta{}, + Spec: v1.PodSpec{}, + Status: v1.PodStatus{}, + } + + patch = c.updateApplicationInfo("default", pod, patch) + + assert.Equal(t, len(patch), 1) + assert.Equal(t, patch[0].Op, "add") //nolint:gosec + assert.Equal(t, patch[0].Path, "/metadata/annotations") //nolint:gosec + if updatedMap, ok := patch[0].Value.(map[string]string); ok { //nolint:gosec + assert.Equal(t, len(updatedMap), 4) + assert.Equal(t, updatedMap["existingAnnotationKey"], "existingAnnotationValue") + assert.Equal(t, updatedMap["yunikorn.apache.org/queue"], "root.default") + assert.Equal(t, updatedMap["yunikorn.apache.org/disable-state-aware"], "true") + assert.Equal(t, strings.HasPrefix(updatedMap["yunikorn.apache.org/app-id"], constants.AutoGenAppPrefix), true) + } else { + t.Fatal("patch info content is not as expected") + } } func TestUpdateSchedulerName(t *testing.T) {