Merge pull request #5302 from NishantBansal2003/add-unit-tests-util
Add 100% unit test coverage for annotation and label in pkg/util
This commit is contained in:
commit
da74c2de65
|
@ -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",
|
||||
|
@ -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",
|
||||
|
@ -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) {
|
||||
|
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -229,14 +229,46 @@ func TestDedupeAndMergeLabels(t *testing.T) {
|
|||
|
||||
func TestRemoveLabel(t *testing.T) {
|
||||
type args struct {
|
||||
obj *unstructured.Unstructured
|
||||
labelKey string
|
||||
obj *unstructured.Unstructured
|
||||
labelKeys []string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
expected *unstructured.Unstructured
|
||||
}{
|
||||
{
|
||||
name: "empty labelKeys",
|
||||
args: args{
|
||||
obj: &unstructured.Unstructured{
|
||||
Object: map[string]interface{}{
|
||||
"apiVersion": "apps/v1",
|
||||
"kind": "Deployment",
|
||||
"metadata": map[string]interface{}{
|
||||
"name": "demo-deployment",
|
||||
"labels": map[string]interface{}{"foo": "bar"},
|
||||
},
|
||||
"spec": map[string]interface{}{
|
||||
"replicas": 2,
|
||||
},
|
||||
},
|
||||
},
|
||||
labelKeys: []string{},
|
||||
},
|
||||
expected: &unstructured.Unstructured{
|
||||
Object: map[string]interface{}{
|
||||
"apiVersion": "apps/v1",
|
||||
"kind": "Deployment",
|
||||
"metadata": map[string]interface{}{
|
||||
"name": "demo-deployment",
|
||||
"labels": map[string]interface{}{"foo": "bar"},
|
||||
},
|
||||
"spec": map[string]interface{}{
|
||||
"replicas": 2,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "nil object labels",
|
||||
args: args{
|
||||
|
@ -249,8 +281,10 @@ func TestRemoveLabel(t *testing.T) {
|
|||
},
|
||||
"spec": map[string]interface{}{
|
||||
"replicas": 2,
|
||||
}}},
|
||||
labelKey: "foo",
|
||||
},
|
||||
},
|
||||
},
|
||||
labelKeys: []string{"foo"},
|
||||
},
|
||||
expected: &unstructured.Unstructured{
|
||||
Object: map[string]interface{}{
|
||||
|
@ -261,10 +295,12 @@ func TestRemoveLabel(t *testing.T) {
|
|||
},
|
||||
"spec": map[string]interface{}{
|
||||
"replicas": 2,
|
||||
}}},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "same labelKey",
|
||||
name: "same labelKeys",
|
||||
args: args{
|
||||
obj: &unstructured.Unstructured{
|
||||
Object: map[string]interface{}{
|
||||
|
@ -276,8 +312,10 @@ func TestRemoveLabel(t *testing.T) {
|
|||
},
|
||||
"spec": map[string]interface{}{
|
||||
"replicas": 2,
|
||||
}}},
|
||||
labelKey: "foo",
|
||||
},
|
||||
},
|
||||
},
|
||||
labelKeys: []string{"foo"},
|
||||
},
|
||||
expected: &unstructured.Unstructured{
|
||||
Object: map[string]interface{}{
|
||||
|
@ -289,10 +327,12 @@ func TestRemoveLabel(t *testing.T) {
|
|||
},
|
||||
"spec": map[string]interface{}{
|
||||
"replicas": 2,
|
||||
}}},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "different labelKey",
|
||||
name: "different labelKeys",
|
||||
args: args{
|
||||
obj: &unstructured.Unstructured{
|
||||
Object: map[string]interface{}{
|
||||
|
@ -304,8 +344,10 @@ func TestRemoveLabel(t *testing.T) {
|
|||
},
|
||||
"spec": map[string]interface{}{
|
||||
"replicas": 2,
|
||||
}}},
|
||||
labelKey: "foo1",
|
||||
},
|
||||
},
|
||||
},
|
||||
labelKeys: []string{"foo1"},
|
||||
},
|
||||
expected: &unstructured.Unstructured{
|
||||
Object: map[string]interface{}{
|
||||
|
@ -317,12 +359,78 @@ func TestRemoveLabel(t *testing.T) {
|
|||
},
|
||||
"spec": map[string]interface{}{
|
||||
"replicas": 2,
|
||||
}}},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "same labelKeys of different length",
|
||||
args: args{
|
||||
obj: &unstructured.Unstructured{
|
||||
Object: map[string]interface{}{
|
||||
"apiVersion": "apps/v1",
|
||||
"kind": "Deployment",
|
||||
"metadata": map[string]interface{}{
|
||||
"name": "demo-deployment",
|
||||
"labels": map[string]interface{}{"foo": "bar", "foo1": "bar1"},
|
||||
},
|
||||
"spec": map[string]interface{}{
|
||||
"replicas": 2,
|
||||
},
|
||||
},
|
||||
},
|
||||
labelKeys: []string{"foo", "foo1"},
|
||||
},
|
||||
expected: &unstructured.Unstructured{
|
||||
Object: map[string]interface{}{
|
||||
"apiVersion": "apps/v1",
|
||||
"kind": "Deployment",
|
||||
"metadata": map[string]interface{}{
|
||||
"name": "demo-deployment",
|
||||
"labels": map[string]interface{}{},
|
||||
},
|
||||
"spec": map[string]interface{}{
|
||||
"replicas": 2,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "different labelKeys of different length",
|
||||
args: args{
|
||||
obj: &unstructured.Unstructured{
|
||||
Object: map[string]interface{}{
|
||||
"apiVersion": "apps/v1",
|
||||
"kind": "Deployment",
|
||||
"metadata": map[string]interface{}{
|
||||
"name": "demo-deployment",
|
||||
"labels": map[string]interface{}{"foo": "bar", "foo1": "bar1"},
|
||||
},
|
||||
"spec": map[string]interface{}{
|
||||
"replicas": 2,
|
||||
},
|
||||
},
|
||||
},
|
||||
labelKeys: []string{"foo2", "foo3"},
|
||||
},
|
||||
expected: &unstructured.Unstructured{
|
||||
Object: map[string]interface{}{
|
||||
"apiVersion": "apps/v1",
|
||||
"kind": "Deployment",
|
||||
"metadata": map[string]interface{}{
|
||||
"name": "demo-deployment",
|
||||
"labels": 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) {
|
||||
RemoveLabels(tt.args.obj, tt.args.labelKey)
|
||||
RemoveLabels(tt.args.obj, tt.args.labelKeys...)
|
||||
if !reflect.DeepEqual(tt.args.obj, tt.expected) {
|
||||
t.Errorf("RemoveLabel() = %v, want %v", tt.args.obj, tt.expected)
|
||||
}
|
||||
|
@ -560,7 +668,7 @@ func TestRecordManagedLabels(t *testing.T) {
|
|||
},
|
||||
},
|
||||
{
|
||||
name: "object has has labels",
|
||||
name: "object has labels",
|
||||
object: &unstructured.Unstructured{
|
||||
Object: map[string]interface{}{
|
||||
"apiVersion": "apps/v1",
|
||||
|
|
Loading…
Reference in New Issue