Merge pull request #1073 from iawia002/fix-log

Print pointer variables correctly in the scheduler log
This commit is contained in:
karmada-bot 2021-12-08 09:24:14 +08:00 committed by GitHub
commit a1f88d1bbf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 0 deletions

View File

@ -0,0 +1,6 @@
package v1alpha1
// String returns a well-formatted string for the Cluster object.
func (c *Cluster) String() string {
return c.Name
}

View File

@ -0,0 +1,57 @@
package v1alpha1
import (
"fmt"
"testing"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func TestString(t *testing.T) {
clusterName := "cluster1"
cluster1 := &Cluster{
ObjectMeta: metav1.ObjectMeta{Name: clusterName},
}
tests := []struct {
name string
fmtFunc func() string
want string
}{
{
name: "%s pointer test",
fmtFunc: func() string {
return fmt.Sprintf("%s", cluster1) // nolint
},
want: clusterName,
},
{
name: "%v variable test",
fmtFunc: func() string {
return fmt.Sprintf("%v", *cluster1)
},
want: "{{ } {cluster1 0 0001-01-01 00:00:00 +0000 UTC <nil> <nil> map[] map[] [] [] []} { <nil> false []} { [] [] <nil> <nil>}}",
},
{
name: "%v pointer test",
fmtFunc: func() string {
return fmt.Sprintf("%v", cluster1)
},
want: clusterName,
},
{
name: "%v pointer array test",
fmtFunc: func() string {
return fmt.Sprintf("%v", []*Cluster{cluster1})
},
want: fmt.Sprintf("[%s]", clusterName),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.fmtFunc(); got != tt.want {
t.Errorf("%s String() = %v, want %v", tt.name, got, tt.want)
}
})
}
}