Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
137 changes: 114 additions & 23 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,18 +303,27 @@ func TestAddMultipleNetworkPolicies(t *testing.T) {

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

dp.EXPECT().UpdatePolicy(gomock.Any()).Times(2)
if util.IsWindowsDP() {
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(0)
// named ports are not allowed on windows
testCases = []expectedNetPolValues{
{0, 0, netPolPromVals{0, 0, 0, 0}},
}
} else {
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(2)

testCases = []expectedNetPolValues{
{2, 0, netPolPromVals{2, 2, 0, 0}},
}
}
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)
}

Expand All @@ -331,10 +341,46 @@ func TestAddNetworkPolicy(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)
// 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{
{1, 0, netPolPromVals{1, 1, 0, 0}},
}
}
addNetPol(f, netPolObj)

checkNetPolTestResult("TestAddNetPol", f, testCases)
}

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)
stopCh := make(chan struct{})
defer close(stopCh)
ctrl := gomock.NewController(t)
defer ctrl.Finish()

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,13 +449,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)

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

testCases = []expectedNetPolValues{
{0, 0, netPolPromVals{0, 1, 0, 1}},
}
}
deleteNetPol(t, f, netPolObj, DeletedFinalStateknownObject)
checkNetPolTestResult("TestDelNetPol", f, testCases)
}

Expand Down Expand Up @@ -454,13 +511,25 @@ 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)

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

testCases = []expectedNetPolValues{
{0, 0, netPolPromVals{0, 1, 0, 1}},
}
}
deleteNetPol(t, f, netPolObj, DeletedFinalStateUnknownObject)

checkNetPolTestResult("TestDeleteNetworkPolicyWithTombstoneAfterAddingNetworkPolicy", f, testCases)
}

Expand All @@ -484,12 +553,23 @@ 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)

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

testCases = []expectedNetPolValues{
{1, 0, netPolPromVals{1, 1, 0, 0}},
}
}
updateNetPol(t, f, oldNetPolObj, newNetPolObj)

checkNetPolTestResult("TestUpdateNetPol", f, testCases)
}

Expand Down Expand Up @@ -518,12 +598,23 @@ 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)

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

testCases = []expectedNetPolValues{
{1, 0, netPolPromVals{1, 1, 1, 0}},
}
}
updateNetPol(t, f, oldNetPolObj, newNetPolObj)

checkNetPolTestResult("TestUpdateNetPol", f, testCases)
}
4 changes: 2 additions & 2 deletions npm/pkg/dataplane/dataplane_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ func TestMetrics(t *testing.T) {

count, err = metrics.TotalListEndpointsLatencyCalls()
require.Nil(t, err, "failed to get metric")
require.Equal(t, 1, count, "should have listed endpoints once")
require.Equal(t, 2, count, "should have listed endpoints twice")

err = dp.refreshPodEndpoints()
require.Nil(t, err, "failed to refresh pod endpoints")

count, err = metrics.TotalListEndpointsLatencyCalls()
require.Nil(t, err, "failed to get metric")
require.Equal(t, 2, count, "should have listed endpoints twice")
require.Equal(t, 4, count, "should have listed endpoints four times")

count, err = metrics.TotalListEndpointsFailures()
require.Nil(t, err, "failed to get metric")
Expand Down
Loading