Skip to content

Commit

Permalink
refactored NewRest function to not to break api and modified tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hkiiita committed Aug 3, 2024
1 parent 91b7af0 commit 05c102d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
3 changes: 1 addition & 2 deletions pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package apiserver

import (
"context"
"k8s.io/utils/clock"
"time"

apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -242,7 +241,7 @@ func installAPIGroup(s *APIServer, c completedConfig) error {
statsStorage["antreaclusternetworkpolicystats"] = antreaclusternetworkpolicystats.NewREST(c.extraConfig.statsAggregator)
statsStorage["antreanetworkpolicystats"] = antreanetworkpolicystats.NewREST(c.extraConfig.statsAggregator)
statsStorage["multicastgroups"] = multicastgroup.NewREST(c.extraConfig.statsAggregator)
statsStorage["nodelatencystats"] = nodelatencystats.NewREST(clock.RealClock{})
statsStorage["nodelatencystats"] = nodelatencystats.NewREST()
statsGroup.VersionedResourcesStorageMap["v1alpha1"] = statsStorage

groups := []*genericapiserver.APIGroupInfo{&cpGroup, &systemGroup, &statsGroup}
Expand Down
6 changes: 5 additions & 1 deletion pkg/apiserver/registry/stats/nodelatencystats/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ var (
)

// NewREST returns a REST object that will work against API services.
func NewREST(clock clock.Clock) *REST {
func NewREST() *REST {
return newRESTWithClock(clock.RealClock{})
}

func newRESTWithClock(clock clock.Clock) *REST {
return &REST{
indexer: cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{}),
clock: clock,
Expand Down
17 changes: 9 additions & 8 deletions pkg/apiserver/registry/stats/nodelatencystats/rest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (

func TestREST(t *testing.T) {
fakeClock := clocktesting.NewFakeClock(time.Now())
r := NewREST(fakeClock)
r := newRESTWithClock(fakeClock)
assert.Equal(t, &statsv1alpha1.NodeLatencyStats{}, r.New())
assert.Equal(t, &statsv1alpha1.NodeLatencyStats{}, r.NewList())
assert.False(t, r.NamespaceScoped())
Expand Down Expand Up @@ -70,14 +70,14 @@ func TestRESTCreate(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := NewREST(fakeClock)
r := newRESTWithClock(fakeClock)
// Advance the fakeClock to simulate time advancement.
fakeClock.SetTime(fakeClock.Now().Add(1 * time.Minute))
ctx := context.Background()
obj, err := r.Create(ctx, tt.summary, nil, nil)
require.NoError(t, err)

if tt.name == "create with time not added" {
if tt.summary.CreationTimestamp.IsZero() {
assert.Equal(t, metav1.Time{Time: fakeClock.Now()}, obj.(*statsv1alpha1.NodeLatencyStats).CreationTimestamp)

} else {
Expand Down Expand Up @@ -122,7 +122,7 @@ func TestRESTGet(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := NewREST(fakeClock)
r := newRESTWithClock(fakeClock)
ctx := context.Background()

_, err := r.Create(ctx, tt.summary, nil, nil)
Expand Down Expand Up @@ -174,7 +174,7 @@ func TestRESTDelete(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := NewREST(fakeClock)
r := newRESTWithClock(fakeClock)
ctx := context.Background()

_, err := r.Create(ctx, tt.summary, nil, nil)
Expand Down Expand Up @@ -206,7 +206,7 @@ func TestRESTList(t *testing.T) {
},
}

r := NewREST(fakeClock)
r := newRESTWithClock(fakeClock)
ctx := context.Background()

_, err := r.Create(ctx, summary, nil, nil)
Expand Down Expand Up @@ -248,12 +248,13 @@ func TestRESTConvertToTable(t *testing.T) {
}
expectedCells := []interface{}{"node1", 2, "1.5ms", "2ms"}

r := NewREST(fakeClock)
r := newRESTWithClock(fakeClock)
ctx := context.Background()

_, err := r.Create(ctx, summary, nil, nil)
require.NoError(t, err)
obj, err := r.ConvertToTable(ctx, summary, nil)
require.NoError(t, err)
assert.Equal(t, expectedCells, obj.Rows[0].Cells)
}
}

0 comments on commit 05c102d

Please sign in to comment.