Skip to content

Commit bed5d59

Browse files
committed
modified unit tests
1 parent bf2b3d2 commit bed5d59

File tree

1 file changed

+127
-29
lines changed

1 file changed

+127
-29
lines changed

npm/pkg/controlplane/controllers/v2/networkPolicyController_test.go

Lines changed: 127 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/Azure/azure-container-networking/npm/metrics/promutil"
1212
"github.com/Azure/azure-container-networking/npm/pkg/dataplane"
1313
dpmocks "github.com/Azure/azure-container-networking/npm/pkg/dataplane/mocks"
14+
"github.com/Azure/azure-container-networking/npm/util"
1415
gomock "github.com/golang/mock/gomock"
1516
"github.com/stretchr/testify/require"
1617
corev1 "k8s.io/api/core/v1"
@@ -302,24 +303,71 @@ func TestAddMultipleNetworkPolicies(t *testing.T) {
302303

303304
dp := dpmocks.NewMockGenericDataplane(ctrl)
304305
f.newNetPolController(stopCh, dp, false)
306+
var testCases []expectedNetPolValues
307+
308+
if util.IsWindowsDP() {
309+
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(0)
310+
addNetPol(f, netPolObj1)
311+
addNetPol(f, netPolObj2)
312+
313+
// already exists (will be a no-op)
314+
addNetPol(f, netPolObj1)
315+
// named ports are not allowed on windows
316+
testCases = []expectedNetPolValues{
317+
{0, 0, netPolPromVals{0, 0, 0, 0}},
318+
}
319+
} else {
320+
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(2)
321+
addNetPol(f, netPolObj1)
322+
addNetPol(f, netPolObj2)
323+
324+
// already exists (will be a no-op)
325+
addNetPol(f, netPolObj1)
326+
testCases = []expectedNetPolValues{
327+
{2, 0, netPolPromVals{2, 2, 0, 0}},
328+
}
329+
}
330+
331+
checkNetPolTestResult("TestAddMultipleNetPols", f, testCases)
332+
}
333+
334+
func TestAddNetworkPolicy(t *testing.T) {
335+
netPolObj := createNetPol()
336+
337+
f := newNetPolFixture(t)
338+
f.netPolLister = append(f.netPolLister, netPolObj)
339+
f.kubeobjects = append(f.kubeobjects, netPolObj)
340+
stopCh := make(chan struct{})
341+
defer close(stopCh)
342+
ctrl := gomock.NewController(t)
343+
defer ctrl.Finish()
305344

306-
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(2)
345+
dp := dpmocks.NewMockGenericDataplane(ctrl)
346+
f.newNetPolController(stopCh, dp, false)
307347

308-
addNetPol(f, netPolObj1)
309-
addNetPol(f, netPolObj2)
348+
var testCases []expectedNetPolValues
310349

311-
// already exists (will be a no-op)
312-
addNetPol(f, netPolObj1)
350+
if util.IsWindowsDP() {
351+
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(0)
352+
addNetPol(f, netPolObj)
353+
// named ports are not allowed on windows
354+
testCases = []expectedNetPolValues{
355+
{0, 0, netPolPromVals{0, 0, 0, 0}},
356+
}
357+
} else {
358+
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(1)
313359

314-
testCases := []expectedNetPolValues{
315-
{2, 0, netPolPromVals{2, 2, 0, 0}},
360+
addNetPol(f, netPolObj)
361+
testCases = []expectedNetPolValues{
362+
{1, 0, netPolPromVals{1, 1, 0, 0}},
363+
}
316364
}
317-
checkNetPolTestResult("TestAddMultipleNetPols", f, testCases)
365+
checkNetPolTestResult("TestAddNetPol", f, testCases)
318366
}
319367

320-
func TestAddNetworkPolicy(t *testing.T) {
368+
func TestAddNetworkPolicyWithNumericPort(t *testing.T) {
321369
netPolObj := createNetPol()
322-
370+
netPolObj.Spec.Egress[0].Ports[0].Port = &intstr.IntOrString{IntVal: 8000}
323371
f := newNetPolFixture(t)
324372
f.netPolLister = append(f.netPolLister, netPolObj)
325373
f.kubeobjects = append(f.kubeobjects, netPolObj)
@@ -331,10 +379,12 @@ func TestAddNetworkPolicy(t *testing.T) {
331379
dp := dpmocks.NewMockGenericDataplane(ctrl)
332380
f.newNetPolController(stopCh, dp, false)
333381

382+
var testCases []expectedNetPolValues
383+
334384
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(1)
335385

336386
addNetPol(f, netPolObj)
337-
testCases := []expectedNetPolValues{
387+
testCases = []expectedNetPolValues{
338388
{1, 0, netPolPromVals{1, 1, 0, 0}},
339389
}
340390

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

406-
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(1)
407-
dp.EXPECT().RemovePolicy(gomock.Any()).Times(1)
456+
var testCases []expectedNetPolValues
408457

409-
deleteNetPol(t, f, netPolObj, DeletedFinalStateknownObject)
410-
testCases := []expectedNetPolValues{
411-
{0, 0, netPolPromVals{0, 1, 0, 1}},
458+
if util.IsWindowsDP() {
459+
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(0)
460+
dp.EXPECT().RemovePolicy(gomock.Any()).Times(0)
461+
462+
deleteNetPol(t, f, netPolObj, DeletedFinalStateknownObject)
463+
testCases = []expectedNetPolValues{
464+
{0, 0, netPolPromVals{0, 0, 0, 0}},
465+
}
466+
} else {
467+
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(1)
468+
dp.EXPECT().RemovePolicy(gomock.Any()).Times(1)
469+
470+
deleteNetPol(t, f, netPolObj, DeletedFinalStateknownObject)
471+
testCases = []expectedNetPolValues{
472+
{0, 0, netPolPromVals{0, 1, 0, 1}},
473+
}
412474
}
413475
checkNetPolTestResult("TestDelNetPol", f, testCases)
414476
}
@@ -454,12 +516,24 @@ func TestDeleteNetworkPolicyWithTombstoneAfterAddingNetworkPolicy(t *testing.T)
454516
dp := dpmocks.NewMockGenericDataplane(ctrl)
455517
f.newNetPolController(stopCh, dp, false)
456518

457-
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(1)
458-
dp.EXPECT().RemovePolicy(gomock.Any()).Times(1)
519+
var testCases []expectedNetPolValues
459520

460-
deleteNetPol(t, f, netPolObj, DeletedFinalStateUnknownObject)
461-
testCases := []expectedNetPolValues{
462-
{0, 0, netPolPromVals{0, 1, 0, 1}},
521+
if util.IsWindowsDP() {
522+
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(0)
523+
dp.EXPECT().RemovePolicy(gomock.Any()).Times(0)
524+
525+
deleteNetPol(t, f, netPolObj, DeletedFinalStateUnknownObject)
526+
testCases = []expectedNetPolValues{
527+
{0, 0, netPolPromVals{0, 0, 0, 0}},
528+
}
529+
} else {
530+
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(1)
531+
dp.EXPECT().RemovePolicy(gomock.Any()).Times(1)
532+
533+
deleteNetPol(t, f, netPolObj, DeletedFinalStateUnknownObject)
534+
testCases = []expectedNetPolValues{
535+
{0, 0, netPolPromVals{0, 1, 0, 1}},
536+
}
463537
}
464538
checkNetPolTestResult("TestDeleteNetworkPolicyWithTombstoneAfterAddingNetworkPolicy", f, testCases)
465539
}
@@ -484,11 +558,22 @@ func TestUpdateNetworkPolicy(t *testing.T) {
484558
// oldNetPolObj.ResourceVersion value is "0"
485559
newRV, _ := strconv.Atoi(oldNetPolObj.ResourceVersion)
486560
newNetPolObj.ResourceVersion = fmt.Sprintf("%d", newRV+1)
487-
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(1)
561+
var testCases []expectedNetPolValues
488562

489-
updateNetPol(t, f, oldNetPolObj, newNetPolObj)
490-
testCases := []expectedNetPolValues{
491-
{1, 0, netPolPromVals{1, 1, 0, 0}},
563+
if util.IsWindowsDP() {
564+
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(0)
565+
566+
updateNetPol(t, f, oldNetPolObj, newNetPolObj)
567+
testCases = []expectedNetPolValues{
568+
{0, 0, netPolPromVals{0, 0, 0, 0}},
569+
}
570+
} else {
571+
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(1)
572+
573+
updateNetPol(t, f, oldNetPolObj, newNetPolObj)
574+
testCases = []expectedNetPolValues{
575+
{1, 0, netPolPromVals{1, 1, 0, 0}},
576+
}
492577
}
493578
checkNetPolTestResult("TestUpdateNetPol", f, testCases)
494579
}
@@ -518,12 +603,25 @@ func TestLabelUpdateNetworkPolicy(t *testing.T) {
518603
// oldNetPolObj.ResourceVersion value is "0"
519604
newRV, _ := strconv.Atoi(oldNetPolObj.ResourceVersion)
520605
newNetPolObj.ResourceVersion = fmt.Sprintf("%d", newRV+1)
521-
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(2)
522606

523-
updateNetPol(t, f, oldNetPolObj, newNetPolObj)
607+
var testCases []expectedNetPolValues
524608

525-
testCases := []expectedNetPolValues{
526-
{1, 0, netPolPromVals{1, 1, 1, 0}},
609+
if util.IsWindowsDP() {
610+
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(0)
611+
612+
updateNetPol(t, f, oldNetPolObj, newNetPolObj)
613+
614+
testCases = []expectedNetPolValues{
615+
{0, 0, netPolPromVals{0, 0, 0, 0}},
616+
}
617+
} else {
618+
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(2)
619+
620+
updateNetPol(t, f, oldNetPolObj, newNetPolObj)
621+
622+
testCases = []expectedNetPolValues{
623+
{1, 0, netPolPromVals{1, 1, 1, 0}},
624+
}
527625
}
528626
checkNetPolTestResult("TestUpdateNetPol", f, testCases)
529627
}

0 commit comments

Comments
 (0)