Skip to content

Commit

Permalink
Add 100% unit test coverage for annotation and label in pkg/util
Browse files Browse the repository at this point in the history
Signed-off-by: Nishant Bansal <nishant.bansal.mec21@iitbhu.ac.in>
  • Loading branch information
NishantBansal2003 committed Aug 7, 2024
1 parent 8b4e006 commit 5643f77
Show file tree
Hide file tree
Showing 2 changed files with 446 additions and 17 deletions.
325 changes: 323 additions & 2 deletions pkg/util/annotation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ func TestRecordManagedAnnotations(t *testing.T) {
},
},
{
name: "object has has annotations",
name: "object has annotations",
object: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "apps/v1",
Expand Down Expand Up @@ -322,7 +322,7 @@ func TestRecordManagedAnnotations(t *testing.T) {
},
},
{
name: "object has has annotations and labels",
name: "object has annotations and labels",
object: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "apps/v1",
Expand Down Expand Up @@ -361,6 +361,47 @@ func TestRecordManagedAnnotations(t *testing.T) {
},
},
},
{
name: "object has recorded annotations and labels",
object: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"name": "demo-deployment-1",
"annotations": map[string]interface{}{
workv1alpha2.ManagedAnnotation: "foo,resourcetemplate.karmada.io/managed-annotations,resourcetemplate.karmada.io/managed-labels",
"foo": "foo",
},
"labels": map[string]interface{}{
"bar": "bar",
},
},
"spec": map[string]interface{}{
"replicas": 2,
},
},
},
expected: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"name": "demo-deployment-1",
"annotations": map[string]interface{}{
workv1alpha2.ManagedAnnotation: "foo,resourcetemplate.karmada.io/managed-annotations,resourcetemplate.karmada.io/managed-labels",
"foo": "foo",
},
"labels": map[string]interface{}{
"bar": "bar",
},
},
"spec": map[string]interface{}{
"replicas": 2,
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -425,3 +466,283 @@ func TestMergeAnnotation(t *testing.T) {
})
}
}

func TestDedupeAndMergeAnnotations(t *testing.T) {
tests := []struct {
name string
existAnnotation map[string]string
newAnnotation map[string]string
expectedAnnotation map[string]string
}{
{
name: "nil both annotation",
existAnnotation: nil,
newAnnotation: nil,
expectedAnnotation: nil,
},
{
name: "nil existing annotation",
existAnnotation: nil,
newAnnotation: map[string]string{
"newAnnotationKey": "newAnnotationValues",
},
expectedAnnotation: map[string]string{
"newAnnotationKey": "newAnnotationValues",
},
},
{
name: "nil new annotation",
existAnnotation: map[string]string{
"existAnnotationKey": "existAnnotationValues",
},
newAnnotation: nil,
expectedAnnotation: map[string]string{
"existAnnotationKey": "existAnnotationValues",
},
},
{
name: "same annotation",
existAnnotation: map[string]string{
"existAnnotationKey": "existAnnotationValues",
},
newAnnotation: map[string]string{
"existAnnotationKey": "existAnnotationValues",
},
expectedAnnotation: map[string]string{
"existAnnotationKey": "existAnnotationValues",
},
},
{
name: "different annotation",
existAnnotation: map[string]string{
"existAnnotationKey": "existAnnotationValues",
},
newAnnotation: map[string]string{
"newAnnotationKey": "newAnnotationValues",
},
expectedAnnotation: map[string]string{
"existAnnotationKey": "existAnnotationValues",
"newAnnotationKey": "newAnnotationValues",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.existAnnotation = DedupeAndMergeAnnotations(tt.existAnnotation, tt.newAnnotation)
if !reflect.DeepEqual(tt.existAnnotation, tt.expectedAnnotation) {
t.Errorf("DedupeAndMergeAnnotations(), existAnnotation = %v, want %v", tt.existAnnotation, tt.expectedAnnotation)
}
})
}
}

func TestRemoveAnnotations(t *testing.T) {
type args struct {
obj *unstructured.Unstructured
keys []string
}
tests := []struct {
name string
args args
expected *unstructured.Unstructured
}{
{
name: "empty keys",
args: args{
obj: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"name": "demo-deployment",
"annotations": map[string]interface{}{"foo": "bar"},
},
"spec": map[string]interface{}{
"replicas": 2,
},
},
},
keys: []string{},
},
expected: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"name": "demo-deployment",
"annotations": map[string]interface{}{"foo": "bar"},
},
"spec": map[string]interface{}{
"replicas": 2,
},
},
},
},
{
name: "nil object annotations",
args: args{
obj: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"name": "demo-deployment",
},
"spec": map[string]interface{}{
"replicas": 2,
},
},
},
keys: []string{"foo"},
},
expected: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"name": "demo-deployment",
},
"spec": map[string]interface{}{
"replicas": 2,
},
},
},
},
{
name: "same keys",
args: args{
obj: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"name": "demo-deployment",
"annotations": map[string]interface{}{"foo": "bar"},
},
"spec": map[string]interface{}{
"replicas": 2,
},
},
},
keys: []string{"foo"},
},
expected: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"name": "demo-deployment",
"annotations": map[string]interface{}{},
},
"spec": map[string]interface{}{
"replicas": 2,
},
},
},
},
{
name: "different keys",
args: args{
obj: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"name": "demo-deployment",
"annotations": map[string]interface{}{"foo": "bar"},
},
"spec": map[string]interface{}{
"replicas": 2,
},
},
},
keys: []string{"foo1"},
},
expected: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"name": "demo-deployment",
"annotations": map[string]interface{}{"foo": "bar"},
},
"spec": map[string]interface{}{
"replicas": 2,
},
},
},
},
{
name: "same keys of different length",
args: args{
obj: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"name": "demo-deployment",
"annotations": map[string]interface{}{"foo": "bar", "foo1": "bar1"},
},
"spec": map[string]interface{}{
"replicas": 2,
},
},
},
keys: []string{"foo", "foo1"},
},
expected: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"name": "demo-deployment",
"annotations": map[string]interface{}{},
},
"spec": map[string]interface{}{
"replicas": 2,
},
},
},
},
{
name: "different keys of different length",
args: args{
obj: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"name": "demo-deployment",
"annotations": map[string]interface{}{"foo": "bar", "foo1": "bar1"},
},
"spec": map[string]interface{}{
"replicas": 2,
},
},
},
keys: []string{"foo2", "foo3"},
},
expected: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"name": "demo-deployment",
"annotations": map[string]interface{}{"foo": "bar", "foo1": "bar1"},
},
"spec": map[string]interface{}{
"replicas": 2,
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
RemoveAnnotations(tt.args.obj, tt.args.keys...)
if !reflect.DeepEqual(tt.args.obj, tt.expected) {
t.Errorf("RemoveAnnotations(), tt.args.obj = %v, want %v", tt.args.obj, tt.expected)
}
})
}
}
Loading

0 comments on commit 5643f77

Please sign in to comment.