Skip to content

Commit

Permalink
add test function
Browse files Browse the repository at this point in the history
Signed-off-by: zach593 <[email protected]>
  • Loading branch information
zach593 committed Jan 6, 2025
1 parent 822c028 commit 560f2e8
Showing 1 changed file with 106 additions and 50 deletions.
156 changes: 106 additions & 50 deletions pkg/util/equality_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package util

import (
"encoding/json"
"fmt"
"reflect"
"testing"

"k8s.io/apimachinery/pkg/api/equality"
Expand All @@ -28,66 +30,67 @@ import (
workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2"
)

func TestRegisterEqualityCheckFunctions(t *testing.T) {
obj := &unstructured.Unstructured{
Object: map[string]any{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]any{
"labels": map[string]any{
"app": "nginx",
},
"name": "nginx",
"namespace": "default",
var unstructuredDeployment = &unstructured.Unstructured{
Object: map[string]any{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]any{
"labels": map[string]any{
"app": "nginx",
},
"spec": map[string]any{
"selector": map[string]any{
"app": "nginx",
},
"template": map[string]any{
"metadata": map[string]any{
"labels": map[string]any{
"app": "nginx",
},
"name": "nginx",
"namespace": "default",
},
"spec": map[string]any{
"selector": map[string]any{
"app": "nginx",
},
"template": map[string]any{
"metadata": map[string]any{
"labels": map[string]any{
"app": "nginx",
},
"spec": map[string]any{
"containers": []any{
map[string]any{
"name": "nginx",
"image": "nginx:latest",
},
},
"spec": map[string]any{
"containers": []any{
map[string]any{
"name": "nginx",
"image": "nginx:latest",
},
},
},
},
"status": map[string]any{
"conditions": []any{
map[string]any{
"lastTransitionTime": "2024-10-21T08:24:46Z",
"lastUpdateTime": "2024-10-21T08:24:46Z",
"message": `ReplicaSet "nginx-649577f8c7" has successfully progressed.`,
"reason": "NewReplicaSetAvailable",
"status": "True",
"type": "Progressing",
},
map[string]any{
"lastTransitionTime": "2024-10-24T02:55:32Z",
"lastUpdateTime": "2024-10-24T02:55:32Z",
"message": "Deployment has minimum availability.",
"reason": "MinimumReplicasAvailable",
"status": "True",
"type": "Available",
},
},
"status": map[string]any{
"conditions": []any{
map[string]any{
"lastTransitionTime": "2024-10-21T08:24:46Z",
"lastUpdateTime": "2024-10-21T08:24:46Z",
"message": `ReplicaSet "nginx-649577f8c7" has successfully progressed.`,
"reason": "NewReplicaSetAvailable",
"status": "True",
"type": "Progressing",
},
map[string]any{
"lastTransitionTime": "2024-10-24T02:55:32Z",
"lastUpdateTime": "2024-10-24T02:55:32Z",
"message": "Deployment has minimum availability.",
"reason": "MinimumReplicasAvailable",
"status": "True",
"type": "Available",
},
"availableReplicas": int64(1),
"observedGeneration": int64(1),
"readyReplicas": int64(1),
"replicas": int64(1),
"updatedReplicas": int64(1),
},
"availableReplicas": int64(1),
"observedGeneration": int64(1),
"readyReplicas": int64(1),
"replicas": int64(1),
"updatedReplicas": int64(1),
},
}
},
}

func TestRegisterEqualityCheckFunctions(t *testing.T) {
obj := unstructuredDeployment.DeepCopy()
tests := []struct {
name string
objFn1 func() (runtime.Object, error)
Expand Down Expand Up @@ -326,3 +329,56 @@ func TestRegisterEqualityCheckFunctions(t *testing.T) {
})
}
}

func Test_parseRawExtension(t *testing.T) {
obj := unstructuredDeployment.DeepCopy()

tests := []struct {
name string
argsFunc func() (param runtime.RawExtension, want map[string]any, err error)
wantErr bool
}{
{
name: "should able to parse status",
argsFunc: func() (runtime.RawExtension, map[string]any, error) {
status, ok := obj.Object["status"].(map[string]any)
if !ok {
return runtime.RawExtension{}, nil, fmt.Errorf("failed to convert status to map[string]any")
}
j, err := json.Marshal(status)
return runtime.RawExtension{Raw: j}, status, err
},
wantErr: false,
},
{
name: "should able to parse normal object",
argsFunc: func() (runtime.RawExtension, map[string]any, error) {
j, err := json.Marshal(obj)
return runtime.RawExtension{Raw: j}, obj.Object, err
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
extension, want, err := tt.argsFunc()
if err != nil {
t.Fatalf("parseRawExtension() get args fail, error = %v,", err)
}
got, err := parseRawExtension(extension)
if err != nil != tt.wantErr {
t.Fatalf("parseRawExtension() error = %v, wantErr %v", err, tt.wantErr)
}
// unmarshalled JSON won't equal to the original object because the number type will be different
// so we marshal them to JSON and compare
gotJ, gotErr := json.Marshal(got)
wantJ, wantErr := json.Marshal(want)
if gotErr != nil || wantErr != nil {
t.Fatalf("parseRawExtension() marshal JSON fail, gotErr = %v, wantErr %v", wantJ, wantErr)
}
if !reflect.DeepEqual(gotJ, wantJ) {
t.Errorf("parseRawExtension() = %v, want %v", gotJ, wantJ)
}
})
}
}

0 comments on commit 560f2e8

Please sign in to comment.