Skip to content

Commit 5a52f34

Browse files
test: Fix unit tests to run independent of current env
* Save all intermediate coverage files in `/tmp` folder to minimize unwanted files in root directory
1 parent 206f5b3 commit 5a52f34

File tree

4 files changed

+18
-27
lines changed

4 files changed

+18
-27
lines changed

Makefile

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ ifeq ($(CGO_APPS), 1)
6666
checkbpf := skip-checkbpf
6767

6868
# go test flags
69-
coverage-file := coverage-cgo.out
69+
coverage-file := /tmp/coverage-cgo.out
7070
else
7171
PROMU_CONF ?= .promu/.promu-go.yml
7272
pkgs := ./pkg/collector ./pkg/emissions ./pkg/tsdb ./pkg/grafana ./pkg/k8s \
@@ -78,7 +78,7 @@ else
7878
checkbpf := checkbpf
7979

8080
# go test flags
81-
coverage-file := coverage-go.out
81+
coverage-file := /tmp/coverage-go.out
8282

8383
# If running in CI add -exec sudo flags to run tests that require privileges
8484
ifeq ($(CI), true)
@@ -122,12 +122,12 @@ all:: vet common-all $(checkbpf) $(cross-test) $(test-docker) $(checkmetrics) $(
122122
.PHONY: coverage
123123
coverage:
124124
@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
125+
$(GO) tool cover -html=/tmp/coverage-go.out -o coverage-go.html
126+
$(GO) tool cover -html=/tmp/coverage-cgo.out -o coverage-cgo.html
127+
cp /tmp/coverage-go.out /tmp/coverage-raw.out
128+
tail -n +2 /tmp/coverage-cgo.out >> /tmp/coverage-raw.out
129+
$(GO) tool cover -html=/tmp/coverage-raw.out -o=coverage.html
130+
$(GO) tool cover -func=/tmp/coverage-raw.out -o=coverage.out
131131

132132
.PHONY: test
133133
test: pkg/collector/testdata/sys/.unpacked pkg/collector/testdata/proc/.unpacked bpf

cmd/ceems_api_server/main_test.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,6 @@ func TestBatchjobStatsExecutable(t *testing.T) {
2323
t.Skipf("ceems_api_server binary not available, try to run `make build` first: %s", err)
2424
}
2525

26-
tmpDir := t.TempDir()
27-
tmpSacctPath := tmpDir + "/sacct"
28-
29-
sacctPath, err := filepath.Abs("../../pkg/api/testdata/sacct")
30-
require.NoError(t, err)
31-
32-
err = os.Link(sacctPath, tmpSacctPath)
33-
require.NoError(t, err)
34-
3526
usagestats := exec.CommandContext(
3627
t.Context(),
3728
binary,

pkg/collector/ipmi_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package collector
55

66
import (
77
"errors"
8-
"fmt"
98
"os"
109
"path/filepath"
1110
"testing"
@@ -238,15 +237,12 @@ func TestIpmiClientFinder(t *testing.T) {
238237
},
239238
}
240239

241-
// Get PATH
242-
basePath := os.Getenv("PATH")
243-
244240
for _, test := range tests {
245241
ipmiClientPath, err := filepath.Abs(filepath.Join("testdata/ipmi", test.path))
246242
require.NoError(t, err)
247243

248244
// Set path
249-
t.Setenv("PATH", fmt.Sprintf("%s:%s", ipmiClientPath, basePath))
245+
t.Setenv("PATH", ipmiClientPath)
250246

251247
ipmiClientSlice, err := findIPMICmd()
252248
require.NoError(t, err, test.name)
@@ -259,7 +255,7 @@ func TestCachedPowerReadings(t *testing.T) {
259255
tmpIPMIPath := tmpDir + "/ipmiutil"
260256

261257
// Set path
262-
t.Setenv("PATH", fmt.Sprintf("%s:%s", tmpDir, os.Getenv("PATH")))
258+
t.Setenv("PATH", tmpDir)
263259

264260
// Expected values
265261
expected := map[string]float64{"dcmi_avg": 49, "dcmi_current": 304, "dcmi_max": 304, "dcmi_min": 6}

scripts/e2e-test.sh

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ then
570570

571571
elif [ "${scenario}" = "exporter-cgroups-v2-nvidia-ipmiutil" ]
572572
then
573-
REDFISH_HOST=localhost REDFISH_PORT=5000 PATH="${PWD}/pkg/collector/testdata/ipmi/ipmiutils:${PATH}" ./bin/ceems_exporter \
573+
REDFISH_HOST=localhost REDFISH_PORT=5000 ./bin/ceems_exporter \
574574
--path.sysfs="pkg/collector/testdata/sys" \
575575
--path.cgroupfs="pkg/collector/testdata/sys/fs/cgroup" \
576576
--path.procfs="pkg/collector/testdata/proc" \
@@ -584,6 +584,7 @@ then
584584
--collector.empty-hostname-label \
585585
--collector.ipmi \
586586
--collector.ipmi.test-mode \
587+
--collector.ipmi.dcmi.cmd="pkg/collector/testdata/ipmi/ipmiutils/ipmiutil" \
587588
--collector.redfish \
588589
--collector.redfish.config.file="pkg/collector/testdata/redfish/config.yml" \
589590
--collector.redfish.config.file.expand-env-vars \
@@ -596,7 +597,7 @@ then
596597

597598
elif [ "${scenario}" = "exporter-cgroups-v2-nvidia-gpu-reordering" ]
598599
then
599-
PATH="${PWD}/pkg/collector/testdata/ipmi/ipmiutils:${PATH}" ./bin/ceems_exporter \
600+
./bin/ceems_exporter \
600601
--path.sysfs="pkg/collector/testdata/sys" \
601602
--path.cgroupfs="pkg/collector/testdata/sys/fs/cgroup" \
602603
--path.procfs="pkg/collector/testdata/proc" \
@@ -610,6 +611,7 @@ then
610611
--collector.empty-hostname-label \
611612
--collector.ipmi \
612613
--collector.ipmi.test-mode \
614+
--collector.ipmi.dcmi.cmd="pkg/collector/testdata/ipmi/ipmiutils/ipmiutil" \
613615
--collector.cray_pm_counters \
614616
--collector.rapl \
615617
--web.listen-address "127.0.0.1:${port}" \
@@ -618,7 +620,7 @@ then
618620

619621
elif [ "${scenario}" = "exporter-cgroups-v2-amd-ipmitool" ]
620622
then
621-
REDFISH_HOST=localhost REDFISH_PORT=5000 PATH="${PWD}/pkg/collector/testdata/ipmi/openipmi:${PATH}" ./bin/ceems_exporter \
623+
REDFISH_HOST=localhost REDFISH_PORT=5000 ./bin/ceems_exporter \
622624
--path.sysfs="pkg/collector/testdata/sys" \
623625
--path.cgroupfs="pkg/collector/testdata/sys/fs/cgroup" \
624626
--path.procfs="pkg/collector/testdata/proc" \
@@ -630,6 +632,7 @@ then
630632
--collector.hwmon \
631633
--collector.ipmi \
632634
--collector.ipmi.test-mode \
635+
--collector.ipmi.dcmi.cmd="pkg/collector/testdata/ipmi/openipmi/ipmitool" \
633636
--collector.redfish \
634637
--collector.redfish.config.file="pkg/collector/testdata/redfish/config.yml" \
635638
--collector.redfish.config.file.expand-env-vars \
@@ -640,7 +643,7 @@ then
640643

641644
elif [ "${scenario}" = "exporter-cgroups-v2-nogpu" ]
642645
then
643-
PATH="${PWD}/pkg/collector/testdata/ipmi/capmc:${PATH}" ./bin/ceems_exporter \
646+
./bin/ceems_exporter \
644647
--path.sysfs="pkg/collector/testdata/sys" \
645648
--path.cgroupfs="pkg/collector/testdata/sys/fs/cgroup" \
646649
--path.procfs="pkg/collector/testdata/proc" \
@@ -650,6 +653,7 @@ then
650653
--collector.empty-hostname-label \
651654
--collector.ipmi \
652655
--collector.ipmi.test-mode \
656+
--collector.ipmi.dcmi.cmd="pkg/collector/testdata/ipmi/capmc/capmc" \
653657
--collector.infiniband \
654658
--collector.rapl \
655659
--collector.emissions \

0 commit comments

Comments
 (0)