Skip to content

Commit e52632b

Browse files
test: Make multiple update calls to eBPF coll in unit tests
* Making a single update might not cover most of the code as maps population can be runtime dependent. This influences the coverage and depending on run, we get different coverages. So, we make multiple update calls so that coverage is expected to be more reproducible. However, there is no guarantee! * Create HTML coverage reports with coverage target in makefile. Useful for comparisons
1 parent 1056b90 commit e52632b

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
coverage.txt
1414

1515
# Output of the go coverage tool, specifically when used with LiteIDE
16-
*.out
17-
*.out.tmp
16+
coverage*
1817

1918
# Dependency directories (remove the comment below to include it)
2019
vendor/

Makefile

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,13 @@ all:: vet common-all $(checkbpf) $(cross-test) $(test-docker) $(checkmetrics) $(
121121

122122
.PHONY: coverage
123123
coverage:
124-
@echo ">> getting coverage report"
125-
tail -n +2 coverage-cgo.out > coverage-cgo.tmp.out && mv coverage-cgo.tmp.out coverage-cgo.out
126-
cat coverage-go.out coverage-cgo.out > coverage.out
127-
$(GO) tool cover -func=coverage.out -o=coverage.out
124+
@echo ">> getting coverage reports"
125+
$(GO) tool cover -html=coverage-go.out -o coverage-go.html
126+
$(GO) tool cover -html=coverage-cgo.out -o coverage-cgo.html
127+
cp coverage-go.out coverage-raw.out
128+
tail -n +2 coverage-cgo.out >> coverage-raw.out
129+
$(GO) tool cover -html=coverage-raw.out -o coverage.html
130+
$(GO) tool cover -func=coverage-raw.out -o=coverage.out
128131

129132
.PHONY: test
130133
test: pkg/collector/testdata/sys/.unpacked pkg/collector/testdata/proc/.unpacked bpf

pkg/collector/ebpf_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os/user"
66
"slices"
77
"testing"
8+
"time"
89

910
"github.com/prometheus/client_golang/prometheus"
1011
"github.com/stretchr/testify/assert"
@@ -183,8 +184,15 @@ func TestNewEbpfCollector(t *testing.T) {
183184
}
184185
}()
185186

186-
err = collector.Update(metrics, nil, "")
187-
require.NoError(t, err)
187+
// Make multiple update calls to make sure
188+
// we cover most of the code as eBPF maps
189+
// might not be populated with just one call
190+
for range 5 {
191+
err = collector.Update(metrics, nil, "")
192+
require.NoError(t, err)
193+
194+
time.Sleep(2 * time.Second)
195+
}
188196

189197
err = collector.Stop(t.Context())
190198
require.NoError(t, err)

0 commit comments

Comments
 (0)