Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NPM] Modified NPM Unit Tests #3160

Merged
merged 4 commits into from
Jan 8, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 127 additions & 29 deletions npm/pkg/controlplane/controllers/v2/networkPolicyController_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/Azure/azure-container-networking/npm/metrics/promutil"
"github.com/Azure/azure-container-networking/npm/pkg/dataplane"
dpmocks "github.com/Azure/azure-container-networking/npm/pkg/dataplane/mocks"
"github.com/Azure/azure-container-networking/npm/util"
gomock "github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -302,24 +303,71 @@ func TestAddMultipleNetworkPolicies(t *testing.T) {

dp := dpmocks.NewMockGenericDataplane(ctrl)
f.newNetPolController(stopCh, dp, false)
var testCases []expectedNetPolValues

if util.IsWindowsDP() {
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(0)
addNetPol(f, netPolObj1)
rejain456 marked this conversation as resolved.
Show resolved Hide resolved
addNetPol(f, netPolObj2)

// already exists (will be a no-op)
addNetPol(f, netPolObj1)
// named ports are not allowed on windows
testCases = []expectedNetPolValues{
{0, 0, netPolPromVals{0, 0, 0, 0}},
}
} else {
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(2)
addNetPol(f, netPolObj1)
addNetPol(f, netPolObj2)

// already exists (will be a no-op)
addNetPol(f, netPolObj1)
testCases = []expectedNetPolValues{
{2, 0, netPolPromVals{2, 2, 0, 0}},
}
}

checkNetPolTestResult("TestAddMultipleNetPols", f, testCases)
}

func TestAddNetworkPolicy(t *testing.T) {
netPolObj := createNetPol()

f := newNetPolFixture(t)
f.netPolLister = append(f.netPolLister, netPolObj)
f.kubeobjects = append(f.kubeobjects, netPolObj)
stopCh := make(chan struct{})
defer close(stopCh)
ctrl := gomock.NewController(t)
defer ctrl.Finish()

dp.EXPECT().UpdatePolicy(gomock.Any()).Times(2)
dp := dpmocks.NewMockGenericDataplane(ctrl)
f.newNetPolController(stopCh, dp, false)

addNetPol(f, netPolObj1)
addNetPol(f, netPolObj2)
var testCases []expectedNetPolValues

// already exists (will be a no-op)
addNetPol(f, netPolObj1)
if util.IsWindowsDP() {
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(0)
addNetPol(f, netPolObj)
// named ports are not allowed on windows
testCases = []expectedNetPolValues{
{0, 0, netPolPromVals{0, 0, 0, 0}},
}
} else {
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(1)

testCases := []expectedNetPolValues{
{2, 0, netPolPromVals{2, 2, 0, 0}},
addNetPol(f, netPolObj)
testCases = []expectedNetPolValues{
{1, 0, netPolPromVals{1, 1, 0, 0}},
}
}
checkNetPolTestResult("TestAddMultipleNetPols", f, testCases)
checkNetPolTestResult("TestAddNetPol", f, testCases)
}

func TestAddNetworkPolicy(t *testing.T) {
func TestAddNetworkPolicyWithNumericPort(t *testing.T) {
netPolObj := createNetPol()

netPolObj.Spec.Egress[0].Ports[0].Port = &intstr.IntOrString{IntVal: 8000}
f := newNetPolFixture(t)
f.netPolLister = append(f.netPolLister, netPolObj)
f.kubeobjects = append(f.kubeobjects, netPolObj)
Expand All @@ -331,10 +379,12 @@ func TestAddNetworkPolicy(t *testing.T) {
dp := dpmocks.NewMockGenericDataplane(ctrl)
f.newNetPolController(stopCh, dp, false)

var testCases []expectedNetPolValues

dp.EXPECT().UpdatePolicy(gomock.Any()).Times(1)

addNetPol(f, netPolObj)
testCases := []expectedNetPolValues{
testCases = []expectedNetPolValues{
{1, 0, netPolPromVals{1, 1, 0, 0}},
}

Expand Down Expand Up @@ -403,12 +453,24 @@ func TestDeleteNetworkPolicy(t *testing.T) {
dp := dpmocks.NewMockGenericDataplane(ctrl)
f.newNetPolController(stopCh, dp, false)

dp.EXPECT().UpdatePolicy(gomock.Any()).Times(1)
dp.EXPECT().RemovePolicy(gomock.Any()).Times(1)
var testCases []expectedNetPolValues

deleteNetPol(t, f, netPolObj, DeletedFinalStateknownObject)
testCases := []expectedNetPolValues{
{0, 0, netPolPromVals{0, 1, 0, 1}},
if util.IsWindowsDP() {
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(0)
dp.EXPECT().RemovePolicy(gomock.Any()).Times(0)

deleteNetPol(t, f, netPolObj, DeletedFinalStateknownObject)
testCases = []expectedNetPolValues{
{0, 0, netPolPromVals{0, 0, 0, 0}},
}
} else {
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(1)
dp.EXPECT().RemovePolicy(gomock.Any()).Times(1)

deleteNetPol(t, f, netPolObj, DeletedFinalStateknownObject)
testCases = []expectedNetPolValues{
{0, 0, netPolPromVals{0, 1, 0, 1}},
}
}
checkNetPolTestResult("TestDelNetPol", f, testCases)
}
Expand Down Expand Up @@ -454,12 +516,24 @@ func TestDeleteNetworkPolicyWithTombstoneAfterAddingNetworkPolicy(t *testing.T)
dp := dpmocks.NewMockGenericDataplane(ctrl)
f.newNetPolController(stopCh, dp, false)

dp.EXPECT().UpdatePolicy(gomock.Any()).Times(1)
dp.EXPECT().RemovePolicy(gomock.Any()).Times(1)
var testCases []expectedNetPolValues

deleteNetPol(t, f, netPolObj, DeletedFinalStateUnknownObject)
testCases := []expectedNetPolValues{
{0, 0, netPolPromVals{0, 1, 0, 1}},
if util.IsWindowsDP() {
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(0)
dp.EXPECT().RemovePolicy(gomock.Any()).Times(0)

deleteNetPol(t, f, netPolObj, DeletedFinalStateUnknownObject)
testCases = []expectedNetPolValues{
{0, 0, netPolPromVals{0, 0, 0, 0}},
}
} else {
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(1)
dp.EXPECT().RemovePolicy(gomock.Any()).Times(1)

deleteNetPol(t, f, netPolObj, DeletedFinalStateUnknownObject)
testCases = []expectedNetPolValues{
{0, 0, netPolPromVals{0, 1, 0, 1}},
}
}
checkNetPolTestResult("TestDeleteNetworkPolicyWithTombstoneAfterAddingNetworkPolicy", f, testCases)
}
Expand All @@ -484,11 +558,22 @@ func TestUpdateNetworkPolicy(t *testing.T) {
// oldNetPolObj.ResourceVersion value is "0"
newRV, _ := strconv.Atoi(oldNetPolObj.ResourceVersion)
newNetPolObj.ResourceVersion = fmt.Sprintf("%d", newRV+1)
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(1)
var testCases []expectedNetPolValues

updateNetPol(t, f, oldNetPolObj, newNetPolObj)
testCases := []expectedNetPolValues{
{1, 0, netPolPromVals{1, 1, 0, 0}},
if util.IsWindowsDP() {
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(0)

updateNetPol(t, f, oldNetPolObj, newNetPolObj)
testCases = []expectedNetPolValues{
{0, 0, netPolPromVals{0, 0, 0, 0}},
}
} else {
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(1)

updateNetPol(t, f, oldNetPolObj, newNetPolObj)
testCases = []expectedNetPolValues{
{1, 0, netPolPromVals{1, 1, 0, 0}},
}
}
checkNetPolTestResult("TestUpdateNetPol", f, testCases)
}
Expand Down Expand Up @@ -518,12 +603,25 @@ func TestLabelUpdateNetworkPolicy(t *testing.T) {
// oldNetPolObj.ResourceVersion value is "0"
newRV, _ := strconv.Atoi(oldNetPolObj.ResourceVersion)
newNetPolObj.ResourceVersion = fmt.Sprintf("%d", newRV+1)
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(2)

updateNetPol(t, f, oldNetPolObj, newNetPolObj)
var testCases []expectedNetPolValues

testCases := []expectedNetPolValues{
{1, 0, netPolPromVals{1, 1, 1, 0}},
if util.IsWindowsDP() {
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(0)

updateNetPol(t, f, oldNetPolObj, newNetPolObj)

testCases = []expectedNetPolValues{
{0, 0, netPolPromVals{0, 0, 0, 0}},
}
} else {
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(2)

updateNetPol(t, f, oldNetPolObj, newNetPolObj)

testCases = []expectedNetPolValues{
{1, 0, netPolPromVals{1, 1, 1, 0}},
}
}
checkNetPolTestResult("TestUpdateNetPol", f, testCases)
}
Loading