Skip to content

Commit

Permalink
fix: fix paged list unit test.
Browse files Browse the repository at this point in the history
Signed-off-by: Asklv <[email protected]>
  • Loading branch information
IRONICBo committed Jul 2, 2024
1 parent 3e54c05 commit 3e38af0
Showing 1 changed file with 46 additions and 30 deletions.
76 changes: 46 additions & 30 deletions pkg/apiserver/registry/stats/nodelatencystats/rest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/apis/meta/internalversion"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"

Expand Down Expand Up @@ -159,6 +160,7 @@ func TestRESTList(t *testing.T) {
tests := []struct {
name string
summary *statsv1alpha1.NodeLatencyStats
options *internalversion.ListOptions
expectedObj runtime.Object
expectedErr bool
}{
Expand All @@ -168,6 +170,10 @@ func TestRESTList(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{Name: "node1"},
PeerNodeLatencyStats: nil,
},
options: &internalversion.ListOptions{
Limit: 10,
Continue: "",
},
expectedObj: &statsv1alpha1.NodeLatencyStatsList{
Items: []statsv1alpha1.NodeLatencyStats{
{
Expand All @@ -178,39 +184,23 @@ func TestRESTList(t *testing.T) {
},
expectedErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := NewREST()
_, err := r.Create(context.TODO(), tt.summary, nil, nil)
assert.Nil(t, err)
obj, err := r.List(context.TODO(), nil)
if tt.expectedErr {
assert.NotNil(t, err)
} else {
assert.Nil(t, err)
assert.Equal(t, tt.expectedObj, obj)
}
})
}
}

func TestRESTConvertToTable(t *testing.T) {
tests := []struct {
name string
summary *statsv1alpha1.NodeLatencyStats
expectedObj runtime.Object
expectedErr bool
}{
{
name: "convert to table",
name: "list summary",
summary: &statsv1alpha1.NodeLatencyStats{
ObjectMeta: metav1.ObjectMeta{Name: "node1"},
PeerNodeLatencyStats: nil,
},
expectedObj: &statsv1alpha1.NodeLatencyStats{
ObjectMeta: metav1.ObjectMeta{Name: "node1"},
PeerNodeLatencyStats: nil,
options: &internalversion.ListOptions{
Limit: 0,
Continue: "",
},
expectedObj: &statsv1alpha1.NodeLatencyStatsList{
Items: []statsv1alpha1.NodeLatencyStats{
{
ObjectMeta: metav1.ObjectMeta{Name: "node1"},
PeerNodeLatencyStats: nil,
},
},
},
expectedErr: false,
},
Expand All @@ -220,13 +210,39 @@ func TestRESTConvertToTable(t *testing.T) {
r := NewREST()
_, err := r.Create(context.TODO(), tt.summary, nil, nil)
assert.Nil(t, err)
obj, err := r.ConvertToTable(context.TODO(), tt.summary, nil)
obj, err := r.List(context.TODO(), tt.options)
if tt.expectedErr {
assert.NotNil(t, err)
} else {
assert.Nil(t, err)
assert.Equal(t, tt.expectedObj, obj.Rows[0].Object.Object)
assert.Equal(t, tt.expectedObj, obj)
}
})
}
}

func TestRESTConvertToTable(t *testing.T) {
name := "convert to table"
summary := &statsv1alpha1.NodeLatencyStats{
ObjectMeta: metav1.ObjectMeta{Name: "node1"},
PeerNodeLatencyStats: nil,
}
expectedObj := &statsv1alpha1.NodeLatencyStats{
ObjectMeta: metav1.ObjectMeta{Name: "node1"},
PeerNodeLatencyStats: nil,
}
expectedErr := false

t.Run(name, func(t *testing.T) {
r := NewREST()
_, err := r.Create(context.TODO(), summary, nil, nil)
assert.Nil(t, err)
obj, err := r.ConvertToTable(context.TODO(), summary, nil)
if expectedErr {
assert.NotNil(t, err)
} else {
assert.Nil(t, err)
assert.Equal(t, expectedObj, obj.Rows[0].Object.Object)
}
})
}

0 comments on commit 3e38af0

Please sign in to comment.