From d3709b0e98ae9ed1b7a37ed8b601023876e660fd Mon Sep 17 00:00:00 2001 From: Hunter Gregory Date: Thu, 16 Sep 2021 13:23:32 -0700 Subject: [PATCH 1/6] fixed bug in NumIPSetsIsPositive() --- npm/metrics/ipsets.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/npm/metrics/ipsets.go b/npm/metrics/ipsets.go index 26c3622e12..c2bde70f51 100644 --- a/npm/metrics/ipsets.go +++ b/npm/metrics/ipsets.go @@ -32,7 +32,7 @@ func ResetNumIPSets() { // TODO might be more efficient to keep track of the count func NumIPSetsIsPositive() bool { val, err := GetNumIPSets() - return val > 0 && err != nil + return val > 0 && err == nil } // RecordIPSetExecTime adds an observation of execution time for adding an IPSet. @@ -42,6 +42,7 @@ func RecordIPSetExecTime(timer *Timer) { } // AddEntryToIPSet increments the number of entries for IPSet setName. +// It doesn't ever update the number of IPSets. func AddEntryToIPSet(setName string) { numIPSetEntries.Inc() ipsetInventoryMap[setName]++ // adds the setName with value 1 if it doesn't exist @@ -63,6 +64,7 @@ func RemoveEntryFromIPSet(setName string) { } // RemoveAllEntriesFromIPSet sets the number of entries for ipset setName to 0. +// It doesn't ever update the number of IPSets. func RemoveAllEntriesFromIPSet(setName string) { numIPSetEntries.Add(-getEntryCountForIPSet(setName)) delete(ipsetInventoryMap, setName) @@ -76,6 +78,7 @@ func DeleteIPSet(setName string) { } // ResetIPSetEntries sets the number of entries to 0 for all IPSets. +// It doesn't ever update the number of IPSets. func ResetIPSetEntries() { numIPSetEntries.Set(0) ipsetInventoryMap = make(map[string]int) @@ -95,6 +98,7 @@ func GetNumIPSetEntries() (int, error) { // GetNumEntriesForIPSet returns the number entries for IPSet setName. // This function is slow. +// TODO could use the map if this function needs to be faster. func GetNumEntriesForIPSet(setName string) (int, error) { labels := getIPSetInventoryLabels(setName) return getVecValue(ipsetInventory, labels) From f01721fd35ad0ecec5763c5483180771add614ef Mon Sep 17 00:00:00 2001 From: Hunter Gregory Date: Thu, 16 Sep 2021 13:33:05 -0700 Subject: [PATCH 2/6] moved code for getting metric values to a new file --- npm/metrics/prometheus-values.go | 46 +++++++++++++++++++++++++++++++ npm/metrics/prometheus_metrics.go | 39 -------------------------- 2 files changed, 46 insertions(+), 39 deletions(-) create mode 100644 npm/metrics/prometheus-values.go diff --git a/npm/metrics/prometheus-values.go b/npm/metrics/prometheus-values.go new file mode 100644 index 0000000000..12e0c9017e --- /dev/null +++ b/npm/metrics/prometheus-values.go @@ -0,0 +1,46 @@ +package metrics + +import ( + "fmt" + + "github.com/prometheus/client_golang/prometheus" + dto "github.com/prometheus/client_model/go" +) + +// getValue returns a Gauge metric's value. +// This function is slow. +func getValue(gaugeMetric prometheus.Gauge) (int, error) { + dtoMetric, err := getDTOMetric(gaugeMetric) + if err != nil { + return 0, err + } + return int(dtoMetric.Gauge.GetValue()), nil +} + +// getVecValue returns a Gauge Vec metric's value, or 0 if the label doesn't exist for the metric. +// This function is slow. +func getVecValue(gaugeVecMetric *prometheus.GaugeVec, labels prometheus.Labels) (int, error) { + return getValue(gaugeVecMetric.With(labels)) +} + +// getCountValue returns the number of times a Summary metric has recorded an observation. +// This function is slow. +func getCountValue(summaryMetric prometheus.Summary) (int, error) { + dtoMetric, err := getDTOMetric(summaryMetric) + if err != nil { + return 0, err + } + return int(dtoMetric.Summary.GetSampleCount()), nil +} + +// This function is slow. +func getDTOMetric(collector prometheus.Collector) (*dto.Metric, error) { + channel := make(chan prometheus.Metric, 1) + collector.Collect(channel) + metric := &dto.Metric{} + err := (<-channel).Write(metric) + if err != nil { + err = fmt.Errorf("error while extracting Prometheus metric value: %w", err) + } + return metric, err +} diff --git a/npm/metrics/prometheus_metrics.go b/npm/metrics/prometheus_metrics.go index 33776d9a5b..cca3ea165d 100644 --- a/npm/metrics/prometheus_metrics.go +++ b/npm/metrics/prometheus_metrics.go @@ -1,13 +1,11 @@ package metrics import ( - "fmt" "net/http" "github.com/Azure/azure-container-networking/log" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" - dto "github.com/prometheus/client_model/go" ) const namespace = "npm" @@ -137,40 +135,3 @@ func createSummary(name string, helpMessage string, isNodeLevel bool) prometheus register(summary, name, isNodeLevel) return summary } - -// getValue returns a Gauge metric's value. -// This function is slow. -func getValue(gaugeMetric prometheus.Gauge) (int, error) { - dtoMetric, err := getDTOMetric(gaugeMetric) - if err != nil { - return 0, err - } - return int(dtoMetric.Gauge.GetValue()), nil -} - -// getVecValue Gauge Vec metric's value, or 0 if the label doesn't exist for the metric. -// This function is slow. -func getVecValue(gaugeVecMetric *prometheus.GaugeVec, labels prometheus.Labels) (int, error) { - return getValue(gaugeVecMetric.With(labels)) -} - -// getCountValue the number of times a Summary metric has recorded an observation. -// This function is slow. -func getCountValue(summaryMetric prometheus.Summary) (int, error) { - dtoMetric, err := getDTOMetric(summaryMetric) - if err != nil { - return 0, err - } - return int(dtoMetric.Summary.GetSampleCount()), nil -} - -func getDTOMetric(collector prometheus.Collector) (*dto.Metric, error) { - channel := make(chan prometheus.Metric, 1) - collector.Collect(channel) - metric := &dto.Metric{} - err := (<-channel).Write(metric) - if err != nil { - err = fmt.Errorf("error while extracting Prometheus metric value: %w", err) - } - return metric, err -} From 154b9985fe68972238285eb01a002271c3b94115 Mon Sep 17 00:00:00 2001 From: Hunter Gregory Date: Thu, 16 Sep 2021 13:33:59 -0700 Subject: [PATCH 3/6] renamed file --- npm/metrics/{prometheus_metrics.go => prometheus-metrics.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename npm/metrics/{prometheus_metrics.go => prometheus-metrics.go} (100%) diff --git a/npm/metrics/prometheus_metrics.go b/npm/metrics/prometheus-metrics.go similarity index 100% rename from npm/metrics/prometheus_metrics.go rename to npm/metrics/prometheus-metrics.go From 23dcbe754e2dc34658221a561410899ad97aaac4 Mon Sep 17 00:00:00 2001 From: Hunter Gregory Date: Thu, 16 Sep 2021 13:34:40 -0700 Subject: [PATCH 4/6] unit tests for prometheus metrics --- npm/metrics/acl_rules_test.go | 22 ++++ npm/metrics/ipsets.go | 1 + npm/metrics/ipsets_test.go | 194 +++++++++++++++++++++++++++++++ npm/metrics/policies_test.go | 22 ++++ npm/metrics/prometheus-values.go | 9 ++ npm/metrics/prometheus_test.go | 89 ++++++++++++++ npm/metrics/timer_test.go | 21 ++++ 7 files changed, 358 insertions(+) create mode 100644 npm/metrics/acl_rules_test.go create mode 100644 npm/metrics/ipsets_test.go create mode 100644 npm/metrics/policies_test.go create mode 100644 npm/metrics/prometheus_test.go create mode 100644 npm/metrics/timer_test.go diff --git a/npm/metrics/acl_rules_test.go b/npm/metrics/acl_rules_test.go new file mode 100644 index 0000000000..d5eeebc5f1 --- /dev/null +++ b/npm/metrics/acl_rules_test.go @@ -0,0 +1,22 @@ +package metrics + +import "testing" + +var numRulesMetric = &basicMetric{ResetNumACLRules, IncNumACLRules, DecNumACLRules, GetNumACLRules} +var ruleExecMetric = &recordingMetric{RecordACLRuleExecTime, GetACLRuleExecCount} + +func TestRecordACLRuleExecTime(t *testing.T) { + testStopAndRecord(t, ruleExecMetric) +} + +func TestIncNumACLRules(t *testing.T) { + testIncMetric(t, numRulesMetric) +} + +func TestDecNumACLRules(t *testing.T) { + testDecMetric(t, numRulesMetric) +} + +func TestResetNumACLRules(t *testing.T) { + testResetMetric(t, numRulesMetric) +} diff --git a/npm/metrics/ipsets.go b/npm/metrics/ipsets.go index c2bde70f51..503388f110 100644 --- a/npm/metrics/ipsets.go +++ b/npm/metrics/ipsets.go @@ -99,6 +99,7 @@ func GetNumIPSetEntries() (int, error) { // GetNumEntriesForIPSet returns the number entries for IPSet setName. // This function is slow. // TODO could use the map if this function needs to be faster. +// If updated, replace GetNumEntriesForIPSet() with getVecValue() in assertEqualMapAndMetricElements() in ipsets_test.go func GetNumEntriesForIPSet(setName string) (int, error) { labels := getIPSetInventoryLabels(setName) return getVecValue(ipsetInventory, labels) diff --git a/npm/metrics/ipsets_test.go b/npm/metrics/ipsets_test.go new file mode 100644 index 0000000000..4a9f30445f --- /dev/null +++ b/npm/metrics/ipsets_test.go @@ -0,0 +1,194 @@ +package metrics + +import ( + "testing" + + "github.com/Azure/azure-container-networking/npm/metrics/promutil" + "github.com/stretchr/testify/require" +) + +const testName1 = "test-set1" +const testName2 = "test-set2" + +var numSetsMetric = &basicMetric{ResetNumIPSets, IncNumIPSets, DecNumIPSets, GetNumIPSets} +var setExecMetric = &recordingMetric{RecordIPSetExecTime, GetIPSetExecCount} + +type testSet struct { + name string + entryCount int +} + +func TestRecordIPSetExecTime(t *testing.T) { + testStopAndRecord(t, setExecMetric) +} + +func TestIncNumIPSets(t *testing.T) { + testIncMetric(t, numSetsMetric) +} + +func TestDecNumIPSets(t *testing.T) { + testDecMetric(t, numSetsMetric) +} + +func TestResetNumIPSets(t *testing.T) { + testResetMetric(t, numSetsMetric) +} + +func TestSetNumIPSets(t *testing.T) { + ResetIPSetEntries() + SetNumIPSets(10) + assertMetricVal(t, numSetsMetric, 10) + SetNumIPSets(0) + assertMetricVal(t, numSetsMetric, 0) +} + +func TestNumIPSetsIsPositive(t *testing.T) { + ResetIPSetEntries() + assertNotPositive(t) + + IncNumIPSets() + DecNumIPSets() + assertNotPositive(t) + + IncNumIPSets() + assertPositive(t) + + SetNumIPSets(5) + assertPositive(t) + + SetNumIPSets(0) + assertNotPositive(t) + + DecNumIPSets() + assertNotPositive(t) +} + +func assertNotPositive(t *testing.T) { + if NumIPSetsIsPositive() { + numSets, _ := GetNumIPSets() + require.FailNowf(t, "", "expected num IPSets not to be positive. Current number: %d", numSets) + } +} + +func assertPositive(t *testing.T) { + if !NumIPSetsIsPositive() { + numSets, _ := GetNumIPSets() + require.FailNowf(t, "", "expected num IPSets to be positive. Current number: %d", numSets) + } +} + +func TestAddEntryToIPSet(t *testing.T) { + ResetIPSetEntries() + AddEntryToIPSet(testName1) + AddEntryToIPSet(testName1) + AddEntryToIPSet(testName2) + assertNumEntriesAndCounts(t, &testSet{testName1, 2}, &testSet{testName2, 1}) + assertMapIsGood(t) +} + +func assertNumEntriesAndCounts(t *testing.T, sets ...*testSet) { + expectedTotal := 0 + for _, set := range sets { + val, exists := ipsetInventoryMap[set.name] + if !exists { + require.FailNowf(t, "", "expected set %s to exist in map for ipset entries", set.name) + } + if set.entryCount != val { + require.FailNowf(t, "", "set %s has incorrect number of entries. Expected %d, got %d", set.name, set.entryCount, val) + } + expectedTotal += val + } + + numEntries, err := GetNumIPSetEntries() + promutil.NotifyIfErrors(t, err) + if numEntries != expectedTotal { + require.FailNowf(t, "", "incorrect numver of entries. Expected %d, got %d", expectedTotal, numEntries) + } +} + +func assertNotInMap(t *testing.T, setNames ...string) { + for _, setName := range setNames { + _, exists := ipsetInventoryMap[setName] + if exists { + require.FailNowf(t, "", "expected set %s to not exist in map for ipset entries", setName) + } + } +} + +func assertMapIsGood(t *testing.T) { + assertEqualMapAndMetricElements(t) + assertNoZeroEntriesInMap(t) +} + +// can't get all of a GaugeVec's labels, so need to trust code to delete labels in GaugeVec when done +func assertEqualMapAndMetricElements(t *testing.T) { + for setName, mapVal := range ipsetInventoryMap { + val, err := GetNumEntriesForIPSet(setName) + promutil.NotifyIfErrors(t, err) + promutil.NotifyIfErrors(t, err) + if mapVal != val { + require.FailNowf(t, "", "set %s has incorrect number of entries. Metric has %d, map has %d", setName, val, mapVal) + } + } +} + +func assertNoZeroEntriesInMap(t *testing.T) { + for setname, mapVal := range ipsetInventoryMap { + if mapVal <= 0 { + require.FailNowf(t, "", "expected all ipset entry counts to be positive, but got %d for set %s", mapVal, setname) + } + } +} + +func TestRemoveEntryFromIPSet(t *testing.T) { + ResetIPSetEntries() + AddEntryToIPSet(testName1) + AddEntryToIPSet(testName1) + AddEntryToIPSet(testName2) + RemoveEntryFromIPSet(testName1) + assertNumEntriesAndCounts(t, &testSet{testName1, 1}, &testSet{testName2, 1}) + assertMapIsGood(t) +} + +func TestRemoveAllEntriesFromIPSet(t *testing.T) { + ResetIPSetEntries() + AddEntryToIPSet(testName1) + AddEntryToIPSet(testName1) + AddEntryToIPSet(testName2) + RemoveAllEntriesFromIPSet(testName1) + assertNotInMap(t, testName1) + assertNumEntriesAndCounts(t, &testSet{testName2, 1}) + assertMapIsGood(t) +} + +func TestDeleteIPSet(t *testing.T) { + ResetNumIPSets() + ResetIPSetEntries() + IncNumIPSets() + AddEntryToIPSet(testName1) + AddEntryToIPSet(testName1) + IncNumIPSets() + AddEntryToIPSet(testName2) + DeleteIPSet(testName1) + assertNumSets(t, 1) + assertNotInMap(t, testName1) + assertNumEntriesAndCounts(t, &testSet{testName2, 1}) + assertMapIsGood(t) +} + +func assertNumSets(t *testing.T, exectedVal int) { + numSets, err := GetNumIPSets() + promutil.NotifyIfErrors(t, err) + if numSets != exectedVal { + require.FailNowf(t, "", "incorrect number of ipsets. Expected %d, got %d", exectedVal, numSets) + } +} + +func TestResetIPSetEntries(t *testing.T) { + AddEntryToIPSet(testName1) + AddEntryToIPSet(testName1) + AddEntryToIPSet(testName2) + ResetIPSetEntries() + assertNotInMap(t, testName1, testName2) + assertMapIsGood(t) +} diff --git a/npm/metrics/policies_test.go b/npm/metrics/policies_test.go new file mode 100644 index 0000000000..e722e73a7c --- /dev/null +++ b/npm/metrics/policies_test.go @@ -0,0 +1,22 @@ +package metrics + +import "testing" + +var numPoliciesMetric = &basicMetric{ResetNumPolicies, IncNumPolicies, DecNumPolicies, GetNumPolicies} +var policyExecMetric = &recordingMetric{RecordPolicyExecTime, GetPolicyExecCount} + +func TestRecordPolicyExecTime(t *testing.T) { + testStopAndRecord(t, policyExecMetric) +} + +func TestIncNumPolicies(t *testing.T) { + testIncMetric(t, numPoliciesMetric) +} + +func TestDecNumPolicies(t *testing.T) { + testDecMetric(t, numPoliciesMetric) +} + +func TestResetNumPolicies(t *testing.T) { + testResetMetric(t, numPoliciesMetric) +} diff --git a/npm/metrics/prometheus-values.go b/npm/metrics/prometheus-values.go index 12e0c9017e..dd9bc7efb8 100644 --- a/npm/metrics/prometheus-values.go +++ b/npm/metrics/prometheus-values.go @@ -33,6 +33,15 @@ func getCountValue(summaryMetric prometheus.Summary) (int, error) { return int(dtoMetric.Summary.GetSampleCount()), nil } +// This function is slow. +func getQuantiles(summaryMetric prometheus.Summary) ([]*dto.Quantile, error) { + dtoMetric, err := getDTOMetric(summaryMetric) + if err != nil { + return nil, err + } + return dtoMetric.Summary.GetQuantile(), nil +} + // This function is slow. func getDTOMetric(collector prometheus.Collector) (*dto.Metric, error) { channel := make(chan prometheus.Metric, 1) diff --git a/npm/metrics/prometheus_test.go b/npm/metrics/prometheus_test.go new file mode 100644 index 0000000000..57eda0baa7 --- /dev/null +++ b/npm/metrics/prometheus_test.go @@ -0,0 +1,89 @@ +package metrics + +import ( + "math" + "os" + "testing" + "time" + + "github.com/Azure/azure-container-networking/npm/metrics/promutil" + "github.com/stretchr/testify/require" +) + +func TestMain(m *testing.M) { + InitializeAll() + exitCode := m.Run() + os.Exit(exitCode) +} + +type basicMetric struct { + reset func() + inc func() + dec func() + get func() (int, error) +} + +type recordingMetric struct { + record func(timer *Timer) + getCount func() (int, error) +} + +func assertMetricVal(t *testing.T, metric *basicMetric, expectedVal int) { + val, err := metric.get() + promutil.NotifyIfErrors(t, err) + if val != expectedVal { + require.FailNowf(t, "", "expected metric count to be %d but got %d", expectedVal, val) + } +} + +func testIncMetric(t *testing.T, metric *basicMetric) { + metric.reset() + metric.inc() + metric.inc() + assertMetricVal(t, metric, 2) +} + +func testDecMetric(t *testing.T, metric *basicMetric) { + metric.reset() + metric.inc() + metric.inc() + metric.dec() + assertMetricVal(t, metric, 1) +} + +func testResetMetric(t *testing.T, metric *basicMetric) { + metric.inc() + metric.inc() + metric.reset() + assertMetricVal(t, metric, 0) +} + +func testStopAndRecord(t *testing.T, metric *recordingMetric) { + InitializeAll() + durations := []int{100, 100, 100, 100, 100, 100, 100, 100, 200, 300} + quantileMap := map[float64]float64{0.5: 100, 0.9: 200, 0.99: 300} + for _, duration := range durations { + timer := StartNewTimer() + time.Sleep(time.Duration(duration) * time.Millisecond) + timer.stop() + metric.record(timer) + } + + quantiles, err := getQuantiles(addACLRuleExecTime) + require.NoError(t, err) + for _, quantile := range quantiles { + percent := quantile.GetQuantile() + duration := math.Floor(quantile.GetValue()) + expectedDuration := quantileMap[percent] + if duration > expectedDuration+millisecondForgiveness { + require.FailNowf(t, "", "expected quantile %f for timer to be %f but got %f", percent, expectedDuration, duration) + } + } + + val, err := metric.getCount() + require.NoError(t, err) + expectedVal := len(durations) + if val != expectedVal { + require.FailNowf(t, "", "expected exec count to be %d but got %d", expectedVal, val) + } +} diff --git a/npm/metrics/timer_test.go b/npm/metrics/timer_test.go new file mode 100644 index 0000000000..00e4e52346 --- /dev/null +++ b/npm/metrics/timer_test.go @@ -0,0 +1,21 @@ +package metrics + +import ( + "math" + "testing" + "time" + + "github.com/stretchr/testify/require" +) + +const millisecondForgiveness = 25 + +func TestTimeElapsed(t *testing.T) { + expectedDuration := 100.0 + timer := StartNewTimer() + time.Sleep(time.Millisecond * time.Duration(expectedDuration)) + duration := math.Floor(timer.timeElapsed()) + if duration > expectedDuration+millisecondForgiveness { + require.FailNowf(t, "", "expected elapsed time for timer to be %f but got %f", expectedDuration, duration) + } +} From 644fe40b97ab7eaf51d8c1ffe7ff1737d37f99ec Mon Sep 17 00:00:00 2001 From: Hunter Gregory Date: Thu, 16 Sep 2021 14:31:11 -0700 Subject: [PATCH 5/6] fix go lints --- npm/metrics/acl_rules_test.go | 6 ++++-- npm/metrics/ipsets_test.go | 12 ++++++++---- npm/metrics/policies_test.go | 6 ++++-- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/npm/metrics/acl_rules_test.go b/npm/metrics/acl_rules_test.go index d5eeebc5f1..2995e6abca 100644 --- a/npm/metrics/acl_rules_test.go +++ b/npm/metrics/acl_rules_test.go @@ -2,8 +2,10 @@ package metrics import "testing" -var numRulesMetric = &basicMetric{ResetNumACLRules, IncNumACLRules, DecNumACLRules, GetNumACLRules} -var ruleExecMetric = &recordingMetric{RecordACLRuleExecTime, GetACLRuleExecCount} +var ( + numRulesMetric = &basicMetric{ResetNumACLRules, IncNumACLRules, DecNumACLRules, GetNumACLRules} + ruleExecMetric = &recordingMetric{RecordACLRuleExecTime, GetACLRuleExecCount} +) func TestRecordACLRuleExecTime(t *testing.T) { testStopAndRecord(t, ruleExecMetric) diff --git a/npm/metrics/ipsets_test.go b/npm/metrics/ipsets_test.go index 4a9f30445f..6300ee545d 100644 --- a/npm/metrics/ipsets_test.go +++ b/npm/metrics/ipsets_test.go @@ -7,11 +7,15 @@ import ( "github.com/stretchr/testify/require" ) -const testName1 = "test-set1" -const testName2 = "test-set2" +const ( + testName1 = "test-set1" + testName2 = "test-set2" +) -var numSetsMetric = &basicMetric{ResetNumIPSets, IncNumIPSets, DecNumIPSets, GetNumIPSets} -var setExecMetric = &recordingMetric{RecordIPSetExecTime, GetIPSetExecCount} +var ( + numSetsMetric = &basicMetric{ResetNumIPSets, IncNumIPSets, DecNumIPSets, GetNumIPSets} + setExecMetric = &recordingMetric{RecordIPSetExecTime, GetIPSetExecCount} +) type testSet struct { name string diff --git a/npm/metrics/policies_test.go b/npm/metrics/policies_test.go index e722e73a7c..9d0582db19 100644 --- a/npm/metrics/policies_test.go +++ b/npm/metrics/policies_test.go @@ -2,8 +2,10 @@ package metrics import "testing" -var numPoliciesMetric = &basicMetric{ResetNumPolicies, IncNumPolicies, DecNumPolicies, GetNumPolicies} -var policyExecMetric = &recordingMetric{RecordPolicyExecTime, GetPolicyExecCount} +var ( + numPoliciesMetric = &basicMetric{ResetNumPolicies, IncNumPolicies, DecNumPolicies, GetNumPolicies} + policyExecMetric = &recordingMetric{RecordPolicyExecTime, GetPolicyExecCount} +) func TestRecordPolicyExecTime(t *testing.T) { testStopAndRecord(t, policyExecMetric) From 3d846ca115f09751e8fdce0935379a70456c1b56 Mon Sep 17 00:00:00 2001 From: Hunter Gregory Date: Fri, 17 Sep 2021 16:23:49 -0700 Subject: [PATCH 6/6] use fexec for TestDestroyNpmIpsets() --- npm/ipsm/ipsm_test.go | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/npm/ipsm/ipsm_test.go b/npm/ipsm/ipsm_test.go index e99ad5faaf..68153bd44e 100644 --- a/npm/ipsm/ipsm_test.go +++ b/npm/ipsm/ipsm_test.go @@ -12,7 +12,6 @@ import ( "github.com/Azure/azure-container-networking/npm/util" testutils "github.com/Azure/azure-container-networking/test/utils" "github.com/stretchr/testify/require" - "k8s.io/utils/exec" ) const ( @@ -506,7 +505,38 @@ func TestDestroyNpmIpsets(t *testing.T) { testSet1Name := util.AzureNpmPrefix + "123456" testSet2Name := util.AzureNpmPrefix + "56543" - ipsMgr := NewIpsetManager(exec.New()) + ipsetListFormat := `Name: %s + Type: hash:net + Revision: 6 + Header: family inet hashsize 1024 maxelem 65536 + Size in memory: 448 + References: 0 + Number of entries: 0 + Members: + + Name: %s + Type: hash:net + Revision: 6 + Header: family inet hashsize 1024 maxelem 65536 + Size in memory: 448 + References: 0 + Number of entries: 0 + Members:` + ipsetListStdout := fmt.Sprintf(ipsetListFormat, testSet1Name, testSet2Name) + + calls := []testutils.TestCmd{ + {Cmd: []string{"ipset", "-N", "-exist", util.GetHashedName(testSet1Name), "nethash"}}, + {Cmd: []string{"ipset", "-N", "-exist", util.GetHashedName(testSet2Name), "nethash"}}, + {Cmd: []string{"ipset", "list"}, Stdout: ipsetListStdout}, + {Cmd: []string{"ipset", "-F", "-exist", testSet1Name}}, + {Cmd: []string{"ipset", "-X", "-exist", testSet1Name}}, + {Cmd: []string{"ipset", "-F", "-exist", testSet2Name}}, + {Cmd: []string{"ipset", "-X", "-exist", testSet2Name}}, + } + + fexec := testutils.GetFakeExecWithScripts(calls) + ipsMgr := NewIpsetManager(fexec) + defer testutils.VerifyCalls(t, fexec, calls) execCount := resetPrometheusAndGetExecCount(t) expectedSets := []expectedSetInfo{{0, testSet1Name}, {0, testSet1Name}}