Skip to content

Commit

Permalink
modify ut
Browse files Browse the repository at this point in the history
  • Loading branch information
nddq committed May 28, 2024
1 parent 28dab50 commit 19ad3f5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ retina-capture-workload: ## build the Retina capture workload

##@ Containers

IMAGE_REGISTRY ?= acnpublic.azurecr.io
IMAGE_REGISTRY ?= ghcr.io
IMAGE_NAMESPACE ?= $(shell git config --get remote.origin.url | sed -E 's/.*github\.com[\/:]([^\/]+)\/([^\/.]+)(.git)?/\1\/\2/' | tr '[:upper:]' '[:lower:]')

RETINA_BUILDER_IMAGE = $(IMAGE_NAMESPACE)/retina-builder
Expand Down Expand Up @@ -368,7 +368,7 @@ helm-install: manifests
--set logLevel=info \
--set os.windows=true \
--set operator.enabled=false \
--set enabledPlugin_linux="\[linuxutil\]"
--set enabledPlugin_linux="\[dropreason\,packetforward\,linuxutil\,dns\]"

helm-install-with-operator: manifests
helm upgrade --install retina ./deploy/manifests/controller/helm/retina/ \
Expand Down
2 changes: 1 addition & 1 deletion pkg/plugin/linuxutil/netstat_stats_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func (nr *NetstatReader) readSockStats() error {
port := strconv.Itoa(int(addrPort.Port()))
// Check if the remote address is in the new sockStats map
if _, ok := sockStats.socketByRemoteAddr[remoteAddr]; !ok {
nr.l.Info("Removing remote address from metrics", zap.String("remoteAddr", remoteAddr))
nr.l.Debug("Removing remote address from metrics", zap.String("remoteAddr", remoteAddr))
// If not, set the value to 0
metrics.TCPConnectionRemoteGauge.WithLabelValues(addr, port).Set(0)
}
Expand Down
19 changes: 11 additions & 8 deletions pkg/plugin/linuxutil/netstat_stats_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/cakturk/go-netstat/netstat"
"github.com/microsoft/retina/pkg/log"
"github.com/microsoft/retina/pkg/metrics"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -353,24 +352,28 @@ func TestReadSockStatsRemoveClosedConnection(t *testing.T) {

ns := NewMockNetstatInterface(ctrl)
nr := NewNetstatReader(opts, ns)
assert.NotNil(t, nr)
InitalizeMetricsForTesting(ctrl)

testmetric := prometheus.NewGauge(prometheus.GaugeOpts{
Name: "testmetric",
Help: "testmetric",
})
// We are expecting the gauge to be called twice, once for the initial set and once for the removal (set to 0)
MockGaugeVec.EXPECT().WithLabelValues("127.0.0.1", "80").Return(testmetric).Times(2)

// Initial value
metrics.TCPConnectionRemoteGauge.WithLabelValues("127.0.0.1", "80").Set(float64(1))
nr.connStats.TcpSockets.socketByRemoteAddr = map[string]int{"127.0.0.1:80": 1}
nr.opts.PrevTCPSockStats = &SocketStats{
socketByRemoteAddr: map[string]int{
"127.0.0.1:80": 1,
},
}

// Latest read would not contain the IP in PrevTCPSockStats
ns.EXPECT().TCPSocks(gomock.Any()).Return([]netstat.SockTabEntry{}, nil).Times(1)
ns.EXPECT().UDPSocks(gomock.Any()).Return([]netstat.SockTabEntry{}, nil).Times(1)
assert.NotNil(t, nr)

// We are expecting the gauge to be called once for this value as it is removed
MockGaugeVec.EXPECT().WithLabelValues("127.0.0.1", "80").Return(testmetric).Times(1)

err = nr.readSockStats()
require.NoError(t, err)

assert.Empty(t, nr.connStats.TcpSockets.socketByRemoteAddr)
}

0 comments on commit 19ad3f5

Please sign in to comment.