Skip to content

Commit

Permalink
Add unit test for pkg/apiserver/registry (antrea-io#4304)
Browse files Browse the repository at this point in the history
Signed-off-by: Quan Tian <qtian@vmware.com>
  • Loading branch information
tnqn authored and hongliangl committed Oct 22, 2022
1 parent 6b51f29 commit f4cd88f
Show file tree
Hide file tree
Showing 20 changed files with 1,282 additions and 30 deletions.
122 changes: 122 additions & 0 deletions pkg/apiserver/registry/controlplane/egressgroup/rest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,77 @@ package egressgroup
import (
"context"
"testing"
"time"

"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/internalversion"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/watch"

"antrea.io/antrea/pkg/apis/controlplane"
"antrea.io/antrea/pkg/controller/egress/store"
"antrea.io/antrea/pkg/controller/types"
)

func TestREST(t *testing.T) {
r := NewREST(nil)
assert.Equal(t, &controlplane.EgressGroup{}, r.New())
assert.Equal(t, &controlplane.EgressGroupList{}, r.NewList())
assert.False(t, r.NamespaceScoped())
}

func TestRESTGet(t *testing.T) {
tests := []struct {
name string
egressGroups []*types.EgressGroup
objName string
expectedObj runtime.Object
expectedErr error
}{
{
name: "get existing object",
egressGroups: []*types.EgressGroup{
{
Name: "foo",
},
},
objName: "foo",
expectedObj: &controlplane.EgressGroup{
ObjectMeta: v1.ObjectMeta{
Name: "foo",
},
},
},
{
name: "get non-existing object",
egressGroups: []*types.EgressGroup{
{
Name: "foo",
},
},
objName: "bar",
expectedErr: errors.NewNotFound(controlplane.Resource("egressgroup"), "bar"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
storage := store.NewEgressGroupStore()
for _, obj := range tt.egressGroups {
storage.Create(obj)
}
r := NewREST(storage)
actualObj, err := r.Get(context.TODO(), tt.objName, &v1.GetOptions{})
assert.Equal(t, tt.expectedErr, err)
assert.Equal(t, tt.expectedObj, actualObj)
})
}
}

func TestRESTList(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -78,3 +137,66 @@ func TestRESTList(t *testing.T) {
})
}
}

func TestRESTWatch(t *testing.T) {
egressGroups := []*types.EgressGroup{
{
Name: "egress1",
SpanMeta: types.SpanMeta{NodeNames: sets.NewString("node1")},
},
}
tests := []struct {
name string
fieldSelector fields.Selector
expectedEvents []watch.Event
}{
{
name: "nodeName selecting nothing",
fieldSelector: fields.OneTermEqualSelector("nodeName", "foo"),
expectedEvents: []watch.Event{
{Type: watch.Bookmark, Object: &controlplane.EgressGroup{}},
},
},
{
name: "nodeName provided",
fieldSelector: fields.OneTermEqualSelector("nodeName", "node1"),
expectedEvents: []watch.Event{
{Type: watch.Added, Object: &controlplane.EgressGroup{ObjectMeta: v1.ObjectMeta{Name: "egress1"}}},
{Type: watch.Bookmark, Object: &controlplane.EgressGroup{}},
},
},
{
name: "nodeName not provided",
fieldSelector: nil,
expectedEvents: []watch.Event{
{Type: watch.Added, Object: &controlplane.EgressGroup{ObjectMeta: v1.ObjectMeta{Name: "egress1"}}},
{Type: watch.Bookmark, Object: &controlplane.EgressGroup{}},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
storage := store.NewEgressGroupStore()
for _, obj := range egressGroups {
storage.Create(obj)
}
r := NewREST(storage)
watcher, err := r.Watch(context.TODO(), &internalversion.ListOptions{FieldSelector: tt.fieldSelector})
assert.NoError(t, err)
defer watcher.Stop()
for _, expectedObj := range tt.expectedEvents {
select {
case gotObj := <-watcher.ResultChan():
assert.Equal(t, expectedObj, gotObj)
case <-time.NewTimer(time.Second).C:
t.Errorf("Failed to get expected object %v from watcher in time", expectedObj)
}
}
select {
case gotObj := <-watcher.ResultChan():
t.Errorf("Got unexpected object %v from watcher", gotObj)
case <-time.NewTimer(time.Millisecond * 100).C:
}
})
}
}
62 changes: 62 additions & 0 deletions pkg/apiserver/registry/controlplane/nodestatssummary/rest_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2022 Antrea Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package nodestatssummary

import (
"context"
"testing"

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

"antrea.io/antrea/pkg/apis/controlplane"
statsv1alpha1 "antrea.io/antrea/pkg/apis/stats/v1alpha1"
)

func TestREST(t *testing.T) {
r := NewREST(nil)
assert.Equal(t, &controlplane.NodeStatsSummary{}, r.New())
assert.False(t, r.NamespaceScoped())
}

type fakeCollector struct {
gotSummary *controlplane.NodeStatsSummary
}

func (f *fakeCollector) Collect(summary *controlplane.NodeStatsSummary) {
f.gotSummary = summary
}

func TestRESTCreate(t *testing.T) {
collector := &fakeCollector{}
r := NewREST(collector)

summary := &controlplane.NodeStatsSummary{
ObjectMeta: v1.ObjectMeta{
Name: "foo",
},
NetworkPolicies: []controlplane.NetworkPolicyStats{
{
NetworkPolicy: controlplane.NetworkPolicyReference{Type: controlplane.K8sNetworkPolicy, Namespace: "default", Name: "policy1"},
TrafficStats: statsv1alpha1.TrafficStats{Packets: 10, Sessions: 2, Bytes: 10000},
},
},
}
actualObj, err := r.Create(context.TODO(), summary, nil, &v1.CreateOptions{})
assert.NoError(t, err)
// Empty struct is returned on success.
assert.Equal(t, &controlplane.NodeStatsSummary{}, actualObj)
assert.Equal(t, summary, collector.gotSummary)
}
122 changes: 122 additions & 0 deletions pkg/apiserver/registry/networkpolicy/addressgroup/rest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,77 @@ package addressgroup
import (
"context"
"testing"
"time"

"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/internalversion"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/watch"

"antrea.io/antrea/pkg/apis/controlplane"
"antrea.io/antrea/pkg/controller/networkpolicy/store"
"antrea.io/antrea/pkg/controller/types"
)

func TestREST(t *testing.T) {
r := NewREST(nil)
assert.Equal(t, &controlplane.AddressGroup{}, r.New())
assert.Equal(t, &controlplane.AddressGroupList{}, r.NewList())
assert.False(t, r.NamespaceScoped())
}

func TestRESTGet(t *testing.T) {
tests := []struct {
name string
addressGroups []*types.AddressGroup
objName string
expectedObj runtime.Object
expectedErr error
}{
{
name: "get existing object",
addressGroups: []*types.AddressGroup{
{
Name: "foo",
},
},
objName: "foo",
expectedObj: &controlplane.AddressGroup{
ObjectMeta: v1.ObjectMeta{
Name: "foo",
},
},
},
{
name: "get non-existing object",
addressGroups: []*types.AddressGroup{
{
Name: "foo",
},
},
objName: "bar",
expectedErr: errors.NewNotFound(controlplane.Resource("addressgroup"), "bar"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
storage := store.NewAddressGroupStore()
for _, obj := range tt.addressGroups {
storage.Create(obj)
}
r := NewREST(storage)
actualObj, err := r.Get(context.TODO(), tt.objName, &v1.GetOptions{})
assert.Equal(t, tt.expectedErr, err)
assert.Equal(t, tt.expectedObj, actualObj)
})
}
}

func TestRESTList(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -78,3 +137,66 @@ func TestRESTList(t *testing.T) {
})
}
}

func TestRESTWatch(t *testing.T) {
addressGroups := []*types.AddressGroup{
{
Name: "addressGroup1",
SpanMeta: types.SpanMeta{NodeNames: sets.NewString("node1")},
},
}
tests := []struct {
name string
fieldSelector fields.Selector
expectedEvents []watch.Event
}{
{
name: "nodeName selecting nothing",
fieldSelector: fields.OneTermEqualSelector("nodeName", "foo"),
expectedEvents: []watch.Event{
{Type: watch.Bookmark, Object: &controlplane.AddressGroup{}},
},
},
{
name: "nodeName provided",
fieldSelector: fields.OneTermEqualSelector("nodeName", "node1"),
expectedEvents: []watch.Event{
{Type: watch.Added, Object: &controlplane.AddressGroup{ObjectMeta: v1.ObjectMeta{Name: "addressGroup1"}}},
{Type: watch.Bookmark, Object: &controlplane.AddressGroup{}},
},
},
{
name: "nodeName not provided",
fieldSelector: nil,
expectedEvents: []watch.Event{
{Type: watch.Added, Object: &controlplane.AddressGroup{ObjectMeta: v1.ObjectMeta{Name: "addressGroup1"}}},
{Type: watch.Bookmark, Object: &controlplane.AddressGroup{}},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
storage := store.NewAddressGroupStore()
for _, obj := range addressGroups {
storage.Create(obj)
}
r := NewREST(storage)
watcher, err := r.Watch(context.TODO(), &internalversion.ListOptions{FieldSelector: tt.fieldSelector})
assert.NoError(t, err)
defer watcher.Stop()
for _, expectedObj := range tt.expectedEvents {
select {
case gotObj := <-watcher.ResultChan():
assert.Equal(t, expectedObj, gotObj)
case <-time.NewTimer(time.Second).C:
t.Errorf("Failed to get expected object %v from watcher in time", expectedObj)
}
}
select {
case gotObj := <-watcher.ResultChan():
t.Errorf("Got unexpected object %v from watcher", gotObj)
case <-time.NewTimer(time.Millisecond * 100).C:
}
})
}
}
Loading

0 comments on commit f4cd88f

Please sign in to comment.