Skip to content
Merged
7 changes: 4 additions & 3 deletions .pipelines/npm/npm-conformance-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,10 @@ jobs:
curl -LO https://dl.k8s.io/release/v1.20.0/bin/linux/amd64/kubectl
chmod +x kubectl
npmPodList=`kubectl get pods -n kube-system | grep npm | awk '{print $1}'`
mkdir -p $(System.DefaultWorkingDirectory)/npmLogs_$(PROFILE)
for npm in $npmPodList; do ./kubectl logs -n kube-system $npm --kubeconfig=./kubeconfig > $(System.DefaultWorkingDirectory)/npmLogs/$npm ;done
mv ./kubeconfig $(System.DefaultWorkingDirectory)/npmLogs_$(PROFILE)/kubeconfig
npmLogsFolder=$(System.DefaultWorkingDirectory)/npmLogs_$(PROFILE)
mkdir -p $npmLogsFolder
for npm in $npmPodList; do ./kubectl logs -n kube-system $npm --kubeconfig=./kubeconfig > $npmLogsFolder/$npm ;done
mv ./kubeconfig $npmLogsFolder/kubeconfig
displayName: "Gather NPM Logs"
condition: always()
Expand Down
8 changes: 8 additions & 0 deletions npm/metrics/prometheus-metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ func InitializeAll() {
}
}

// ReinitializeAll creates/replaces Prometheus metrics. This function is intended for UTs.
// Be sure to reset helper variables e.g. ipsetInventoryMap.
func ReinitializeAll() {
haveInitialized = false
InitializeAll()
ipsetInventoryMap = make(map[string]int)
}

// GetHandler returns the HTTP handler for the metrics endpoint
func GetHandler(isNodeLevel bool) http.Handler {
return promhttp.HandlerFor(getRegistry(isNodeLevel), promhttp.HandlerOpts{})
Expand Down
6 changes: 0 additions & 6 deletions npm/pkg/dataplane/dataplane_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/Azure/azure-container-networking/npm/metrics"
"github.com/Azure/azure-container-networking/npm/pkg/dataplane/ipsets"
"github.com/Azure/azure-container-networking/npm/pkg/dataplane/policies"
"github.com/Azure/azure-container-networking/npm/util"
testutils "github.com/Azure/azure-container-networking/test/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -27,11 +26,6 @@ var (
},
}

fakeIPSetRestoreSuccess = testutils.TestCmd{
Cmd: []string{util.Ipset, util.IpsetRestoreFlag},
ExitCode: 0,
}

setPodKey1 = &ipsets.TranslatedIPSet{
Metadata: ipsets.NewIPSetMetadata("setpodkey1", ipsets.KeyLabelOfPod),
}
Expand Down
134 changes: 134 additions & 0 deletions npm/pkg/dataplane/ipsets/ipset_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package ipsets

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestShouldBeInKernelAndCanDelete(t *testing.T) {
s := &IPSetMetadata{"test-set", Namespace}
l := &IPSetMetadata{"test-list", KeyLabelOfNamespace}
tests := []struct {
name string
set *IPSet
wantInKernel bool
wantDeletable bool
}{
{
name: "only has selector reference",
set: &IPSet{
Name: s.GetPrefixName(),
SetProperties: SetProperties{
Type: s.Type,
Kind: s.GetSetKind(),
},
SelectorReference: map[string]struct{}{
"ref-1": {},
},
},
wantInKernel: true,
wantDeletable: false,
},
{
name: "only has netpol reference",
set: &IPSet{
Name: s.GetPrefixName(),
SetProperties: SetProperties{
Type: s.Type,
Kind: s.GetSetKind(),
},
NetPolReference: map[string]struct{}{
"ref-1": {},
},
},
wantInKernel: true,
wantDeletable: false,
},
{
name: "only referenced in list (in kernel)",
set: &IPSet{
Name: s.GetPrefixName(),
SetProperties: SetProperties{
Type: s.Type,
Kind: s.GetSetKind(),
},
ipsetReferCount: 1,
kernelReferCount: 1,
},
wantInKernel: true,
wantDeletable: false,
},
{
name: "only referenced in list (not in kernel)",
set: &IPSet{
Name: s.GetPrefixName(),
SetProperties: SetProperties{
Type: s.Type,
Kind: s.GetSetKind(),
},
ipsetReferCount: 1,
},
wantInKernel: false,
wantDeletable: false,
},
{
name: "only has set members",
set: &IPSet{
Name: l.GetPrefixName(),
SetProperties: SetProperties{
Type: l.Type,
Kind: l.GetSetKind(),
},
MemberIPSets: map[string]*IPSet{
s.GetPrefixName(): NewIPSet(s),
},
},
wantInKernel: false,
wantDeletable: false,
},
{
name: "only has ip members",
set: &IPSet{
Name: s.GetPrefixName(),
SetProperties: SetProperties{
Type: s.Type,
Kind: s.GetSetKind(),
},
IPPodKey: map[string]string{
"1.2.3.4": "pod-a",
},
},
wantInKernel: false,
wantDeletable: false,
},
{
name: "deletable",
set: &IPSet{
Name: s.GetPrefixName(),
SetProperties: SetProperties{
Type: Namespace,
Kind: s.GetSetKind(),
},
},
wantInKernel: false,
wantDeletable: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
if tt.wantInKernel {
require.True(t, tt.set.shouldBeInKernel())
} else {
require.False(t, tt.set.shouldBeInKernel())
}

if tt.wantDeletable {
require.True(t, tt.set.canBeDeleted())
} else {
require.False(t, tt.set.canBeDeleted())
}
})
}
}
Loading