Skip to content

Commit 6662736

Browse files
rejain456sivakami
authored andcommitted
[NPM] Modified NPM Unit Tests (#3160)
* modified unit tests * refactored code * fixed unit tests
1 parent a7279e6 commit 6662736

File tree

2 files changed

+116
-25
lines changed

2 files changed

+116
-25
lines changed

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

Lines changed: 114 additions & 23 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,18 +303,27 @@ func TestAddMultipleNetworkPolicies(t *testing.T) {
302303

303304
dp := dpmocks.NewMockGenericDataplane(ctrl)
304305
f.newNetPolController(stopCh, dp, false)
306+
var testCases []expectedNetPolValues
305307

306-
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(2)
308+
if util.IsWindowsDP() {
309+
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(0)
310+
// named ports are not allowed on windows
311+
testCases = []expectedNetPolValues{
312+
{0, 0, netPolPromVals{0, 0, 0, 0}},
313+
}
314+
} else {
315+
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(2)
307316

317+
testCases = []expectedNetPolValues{
318+
{2, 0, netPolPromVals{2, 2, 0, 0}},
319+
}
320+
}
308321
addNetPol(f, netPolObj1)
309322
addNetPol(f, netPolObj2)
310323

311324
// already exists (will be a no-op)
312325
addNetPol(f, netPolObj1)
313326

314-
testCases := []expectedNetPolValues{
315-
{2, 0, netPolPromVals{2, 2, 0, 0}},
316-
}
317327
checkNetPolTestResult("TestAddMultipleNetPols", f, testCases)
318328
}
319329

@@ -331,10 +341,46 @@ func TestAddNetworkPolicy(t *testing.T) {
331341
dp := dpmocks.NewMockGenericDataplane(ctrl)
332342
f.newNetPolController(stopCh, dp, false)
333343

344+
var testCases []expectedNetPolValues
345+
346+
if util.IsWindowsDP() {
347+
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(0)
348+
// named ports are not allowed on windows
349+
testCases = []expectedNetPolValues{
350+
{0, 0, netPolPromVals{0, 0, 0, 0}},
351+
}
352+
} else {
353+
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(1)
354+
355+
testCases = []expectedNetPolValues{
356+
{1, 0, netPolPromVals{1, 1, 0, 0}},
357+
}
358+
}
359+
addNetPol(f, netPolObj)
360+
361+
checkNetPolTestResult("TestAddNetPol", f, testCases)
362+
}
363+
364+
func TestAddNetworkPolicyWithNumericPort(t *testing.T) {
365+
netPolObj := createNetPol()
366+
netPolObj.Spec.Egress[0].Ports[0].Port = &intstr.IntOrString{IntVal: 8000}
367+
f := newNetPolFixture(t)
368+
f.netPolLister = append(f.netPolLister, netPolObj)
369+
f.kubeobjects = append(f.kubeobjects, netPolObj)
370+
stopCh := make(chan struct{})
371+
defer close(stopCh)
372+
ctrl := gomock.NewController(t)
373+
defer ctrl.Finish()
374+
375+
dp := dpmocks.NewMockGenericDataplane(ctrl)
376+
f.newNetPolController(stopCh, dp, false)
377+
378+
var testCases []expectedNetPolValues
379+
334380
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(1)
335381

336382
addNetPol(f, netPolObj)
337-
testCases := []expectedNetPolValues{
383+
testCases = []expectedNetPolValues{
338384
{1, 0, netPolPromVals{1, 1, 0, 0}},
339385
}
340386

@@ -403,13 +449,24 @@ func TestDeleteNetworkPolicy(t *testing.T) {
403449
dp := dpmocks.NewMockGenericDataplane(ctrl)
404450
f.newNetPolController(stopCh, dp, false)
405451

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

409-
deleteNetPol(t, f, netPolObj, DeletedFinalStateknownObject)
410-
testCases := []expectedNetPolValues{
411-
{0, 0, netPolPromVals{0, 1, 0, 1}},
454+
if util.IsWindowsDP() {
455+
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(0)
456+
dp.EXPECT().RemovePolicy(gomock.Any()).Times(0)
457+
458+
testCases = []expectedNetPolValues{
459+
{0, 0, netPolPromVals{0, 0, 0, 0}},
460+
}
461+
} else {
462+
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(1)
463+
dp.EXPECT().RemovePolicy(gomock.Any()).Times(1)
464+
465+
testCases = []expectedNetPolValues{
466+
{0, 0, netPolPromVals{0, 1, 0, 1}},
467+
}
412468
}
469+
deleteNetPol(t, f, netPolObj, DeletedFinalStateknownObject)
413470
checkNetPolTestResult("TestDelNetPol", f, testCases)
414471
}
415472

@@ -454,13 +511,25 @@ func TestDeleteNetworkPolicyWithTombstoneAfterAddingNetworkPolicy(t *testing.T)
454511
dp := dpmocks.NewMockGenericDataplane(ctrl)
455512
f.newNetPolController(stopCh, dp, false)
456513

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

460-
deleteNetPol(t, f, netPolObj, DeletedFinalStateUnknownObject)
461-
testCases := []expectedNetPolValues{
462-
{0, 0, netPolPromVals{0, 1, 0, 1}},
516+
if util.IsWindowsDP() {
517+
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(0)
518+
dp.EXPECT().RemovePolicy(gomock.Any()).Times(0)
519+
520+
testCases = []expectedNetPolValues{
521+
{0, 0, netPolPromVals{0, 0, 0, 0}},
522+
}
523+
} else {
524+
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(1)
525+
dp.EXPECT().RemovePolicy(gomock.Any()).Times(1)
526+
527+
testCases = []expectedNetPolValues{
528+
{0, 0, netPolPromVals{0, 1, 0, 1}},
529+
}
463530
}
531+
deleteNetPol(t, f, netPolObj, DeletedFinalStateUnknownObject)
532+
464533
checkNetPolTestResult("TestDeleteNetworkPolicyWithTombstoneAfterAddingNetworkPolicy", f, testCases)
465534
}
466535

@@ -484,12 +553,23 @@ func TestUpdateNetworkPolicy(t *testing.T) {
484553
// oldNetPolObj.ResourceVersion value is "0"
485554
newRV, _ := strconv.Atoi(oldNetPolObj.ResourceVersion)
486555
newNetPolObj.ResourceVersion = fmt.Sprintf("%d", newRV+1)
487-
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(1)
556+
var testCases []expectedNetPolValues
488557

489-
updateNetPol(t, f, oldNetPolObj, newNetPolObj)
490-
testCases := []expectedNetPolValues{
491-
{1, 0, netPolPromVals{1, 1, 0, 0}},
558+
if util.IsWindowsDP() {
559+
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(0)
560+
561+
testCases = []expectedNetPolValues{
562+
{0, 0, netPolPromVals{0, 0, 0, 0}},
563+
}
564+
} else {
565+
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(1)
566+
567+
testCases = []expectedNetPolValues{
568+
{1, 0, netPolPromVals{1, 1, 0, 0}},
569+
}
492570
}
571+
updateNetPol(t, f, oldNetPolObj, newNetPolObj)
572+
493573
checkNetPolTestResult("TestUpdateNetPol", f, testCases)
494574
}
495575

@@ -518,12 +598,23 @@ func TestLabelUpdateNetworkPolicy(t *testing.T) {
518598
// oldNetPolObj.ResourceVersion value is "0"
519599
newRV, _ := strconv.Atoi(oldNetPolObj.ResourceVersion)
520600
newNetPolObj.ResourceVersion = fmt.Sprintf("%d", newRV+1)
521-
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(2)
522601

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

525-
testCases := []expectedNetPolValues{
526-
{1, 0, netPolPromVals{1, 1, 1, 0}},
604+
if util.IsWindowsDP() {
605+
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(0)
606+
607+
testCases = []expectedNetPolValues{
608+
{0, 0, netPolPromVals{0, 0, 0, 0}},
609+
}
610+
} else {
611+
dp.EXPECT().UpdatePolicy(gomock.Any()).Times(2)
612+
613+
testCases = []expectedNetPolValues{
614+
{1, 0, netPolPromVals{1, 1, 1, 0}},
615+
}
527616
}
617+
updateNetPol(t, f, oldNetPolObj, newNetPolObj)
618+
528619
checkNetPolTestResult("TestUpdateNetPol", f, testCases)
529620
}

npm/pkg/dataplane/dataplane_windows_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ func TestMetrics(t *testing.T) {
4242

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

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

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

5454
count, err = metrics.TotalListEndpointsFailures()
5555
require.Nil(t, err, "failed to get metric")

0 commit comments

Comments
 (0)