Skip to content

Commit

Permalink
Add unit test for pkg/monitor
Browse files Browse the repository at this point in the history
Signed-off-by: Naman Agarwal <[email protected]>
  • Loading branch information
NamanAg30 committed Dec 7, 2022
1 parent 0da7785 commit fd53cb9
Show file tree
Hide file tree
Showing 2 changed files with 845 additions and 0 deletions.
190 changes: 190 additions & 0 deletions pkg/monitor/agent_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
// 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 monitor

import (
"context"
"errors"
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/fake"
cgtesting "k8s.io/client-go/testing"

"antrea.io/antrea/pkg/agent/config"
interfacestoretest "antrea.io/antrea/pkg/agent/interfacestore/testing"
openflowtest "antrea.io/antrea/pkg/agent/openflow/testing"
"antrea.io/antrea/pkg/agent/querier"
"antrea.io/antrea/pkg/apis/crd/v1beta1"
fakeclientset "antrea.io/antrea/pkg/client/clientset/versioned/fake"
binding "antrea.io/antrea/pkg/ovs/openflow"
ovsconfigtest "antrea.io/antrea/pkg/ovs/ovsconfig/testing"
queriertest "antrea.io/antrea/pkg/querier/testing"
)

const ovsVersion = "2.10.0"

func TestSyncAgentCRD(t *testing.T) {
TestCRD := &v1beta1.AntreaAgentInfo{
ObjectMeta: metav1.ObjectMeta{
Name: "TestAgentCRD",
},
Version: "UNKNOWN",
APIPort: 10349,
}
tc := []struct {
name string
agentCRD string
prepareReactor func(clientset *fakeclientset.Clientset)
existingCRD *v1beta1.AntreaAgentInfo
expectedAgentCRD *v1beta1.AntreaAgentInfo
expectedError string
}{
{
name: "Agent CRD exists-updated Agent CRD partially successfully",
agentCRD: "TestAgentCRD",
prepareReactor: func(clientset *fakeclientset.Clientset) {},
existingCRD: TestCRD,
expectedAgentCRD: TestCRD,
},
{
name: "Agent CRD exists-failed to update Controller CRD partially",
agentCRD: "TestAgentCRD",
prepareReactor: func(clientset *fakeclientset.Clientset) {
clientset.PrependReactor("update", "antreaagentinfos", func(action cgtesting.Action) (handled bool, ret runtime.Object, err error) {
return true, &v1beta1.AntreaAgentInfo{}, errors.New("error updating agent crd")
})
clientset.PrependReactor("get", "antreaagentinfos", func(action cgtesting.Action) (handled bool, ret runtime.Object, err error) {
return true, &v1beta1.AntreaAgentInfo{}, errors.New("error getting agent crd")
})
},
existingCRD: TestCRD,
expectedError: "error getting agent crd",
},
{
name: "Agent CRD does not exist",
prepareReactor: func(clientset *fakeclientset.Clientset) {
clientset.PrependReactor("get", "antreaagentinfos", func(action cgtesting.Action) (handled bool, ret runtime.Object, err error) {
return true, &v1beta1.AntreaAgentInfo{}, errors.New("error getting agent crd")
})
},
expectedError: "error getting agent crd",
},
{
name: "Agent CRD does not exist-updated TestCRD successfully",
prepareReactor: func(clientset *fakeclientset.Clientset) {},
expectedAgentCRD: TestCRD,
},
{
name: "Agent CRD does not exist-failed to update TestCRD",
prepareReactor: func(clientset *fakeclientset.Clientset) {
clientset.PrependReactor("update", "antreaagentinfos", func(action cgtesting.Action) (handled bool, ret runtime.Object, err error) {
return true, &v1beta1.AntreaAgentInfo{}, errors.New("error updating controller crd")
})
},
},
}
for _, tt := range tc {
t.Run(tt.name, func(t *testing.T) {
switch tt.name {
case "Agent CRD exists-updated Agent CRD partially successfully":
clientset := fakeclientset.NewSimpleClientset(tt.existingCRD)
monitor := newAgentMonitor(clientset, t)
tt.prepareReactor(clientset)
monitor.agentCRD = tt.existingCRD
monitor.syncAgentCRD()
crd, err := monitor.client.CrdV1beta1().AntreaAgentInfos().Get(context.TODO(), tt.agentCRD, metav1.GetOptions{})
assert.NoError(t, err)
assert.Equal(t, tt.expectedAgentCRD, crd)
case "Agent CRD exists-failed to update Controller CRD partially":
clientset := fakeclientset.NewSimpleClientset(tt.existingCRD)
monitor := newAgentMonitor(clientset, t)
tt.prepareReactor(clientset)
monitor.agentCRD = tt.existingCRD
monitor.syncAgentCRD()
_, err := monitor.client.CrdV1beta1().AntreaAgentInfos().Get(context.TODO(), tt.agentCRD, metav1.GetOptions{})
assert.Equal(t, tt.expectedError, err.Error())
assert.Nil(t, monitor.agentCRD)
case "Agent CRD does not exist":
clientset := fakeclientset.NewSimpleClientset()
monitor := newAgentMonitor(clientset, t)
tt.prepareReactor(clientset)
monitor.syncAgentCRD()
_, err := monitor.client.CrdV1beta1().AntreaAgentInfos().Get(context.TODO(), crdName, metav1.GetOptions{})
assert.Equal(t, tt.expectedError, err.Error())
assert.Nil(t, monitor.agentCRD)
case "Agent CRD does not exist-updated TestCRD successfully":
clientset := fakeclientset.NewSimpleClientset(TestCRD)
monitor := newAgentMonitor(clientset, t)
tt.prepareReactor(clientset)
monitor.syncAgentCRD()
crd, err := monitor.client.CrdV1beta1().AntreaAgentInfos().Get(context.TODO(), "TestAgentCRD", metav1.GetOptions{})
assert.Equal(t, tt.expectedAgentCRD.Name, crd.Name)
assert.Equal(t, tt.expectedAgentCRD.APIPort, crd.APIPort)
assert.Equal(t, tt.expectedAgentCRD.Version, crd.Version)
assert.NoError(t, err)
case "Agent CRD does not exist-failed to update TestCRD":
clientset := fakeclientset.NewSimpleClientset(TestCRD)
monitor := newAgentMonitor(clientset, t)
tt.prepareReactor(clientset)
monitor.syncAgentCRD()
_, err := monitor.client.CrdV1beta1().AntreaAgentInfos().Get(context.TODO(), "TestAgentCRD", metav1.GetOptions{})
assert.NoError(t, err)
assert.Nil(t, monitor.agentCRD)
}
})
}

}

func newAgentMonitor(clientset *fakeclientset.Clientset, t *testing.T) *agentMonitor {
client := fake.NewSimpleClientset()
ctrl := gomock.NewController(t)
defer ctrl.Finish()

nodeConfig := &config.NodeConfig{
Name: "TestAgentCRD",
}

interfaceStore := interfacestoretest.NewMockInterfaceStore(ctrl)
interfaceStore.EXPECT().GetContainerInterfaceNum().Return(2).AnyTimes()

ofClient := openflowtest.NewMockClient(ctrl)
ofClient.EXPECT().GetFlowTableStatus().Return([]binding.TableStatus{
{
ID: 1,
Name: "1",
FlowCount: 2,
},
}).AnyTimes()
ofClient.EXPECT().IsConnected().Return(true).AnyTimes()

ovsBridgeClient := ovsconfigtest.NewMockOVSBridgeClient(ctrl)
ovsBridgeClient.EXPECT().GetOVSVersion().Return(ovsVersion, nil).AnyTimes()

networkPolicyInfoQuerier := queriertest.NewMockAgentNetworkPolicyInfoQuerier(ctrl)
networkPolicyInfoQuerier.EXPECT().GetNetworkPolicyNum().Return(10).AnyTimes()
networkPolicyInfoQuerier.EXPECT().GetAppliedToGroupNum().Return(20).AnyTimes()
networkPolicyInfoQuerier.EXPECT().GetAddressGroupNum().Return(30).AnyTimes()
networkPolicyInfoQuerier.EXPECT().GetControllerConnectionStatus().Return(true).AnyTimes()

querier := querier.NewAgentQuerier(nodeConfig, nil, interfaceStore, client, ofClient, ovsBridgeClient, nil, networkPolicyInfoQuerier, 10349)

monitor := NewAgentMonitor(clientset, querier)
return monitor
}
Loading

0 comments on commit fd53cb9

Please sign in to comment.