Skip to content

Commit 0d8aad3

Browse files
authored
More robust path handling in tests (#74)
Better handle situations where the working directory is not as expected. This PR specifically covers two scenarios: 1. Building parts of goebpf that use cgo when the working directory does not match the location of the target. We resolve this by removing relative paths anywhere `bpf_helpers.h` is included, instead passing that path as an `-I` argument to the compiler. This is done through a `#cgo` directive in the source but allows this to be overridden through other means such as a larger build system's CFLAGS. 2. Running the itest from a working directory other than `itest`. The tests now search the working directory first (preserving prior behavior) but also check for the image using the absolute path of the executable.
1 parent 1ed8049 commit 0d8aad3

File tree

7 files changed

+50
-8
lines changed

7 files changed

+50
-8
lines changed

goebpf_mock/mock_map.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ package goebpf_mock
66
// eBPF maps golang mock implementation for testing purposes.
77
// The idea behind is make any BPF program unit-testable right from GO
88

9+
// #cgo CFLAGS: -I..
910
/*
1011
#include <stdint.h>
1112
#include <sys/queue.h>
1213
#include <stdlib.h>
1314
14-
#include "../bpf_helpers.h"
15+
#include "bpf_helpers.h"
1516
1617
#define MAX_KEY_SIZE 32
1718

goebpf_mock/wrapper/wrapper.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
// using import "C" from tests... :-(
77
package wrapper
88

9+
// #cgo CFLAGS: -I../..
910
/*
10-
#include "../../bpf_helpers.h"
11+
#include "bpf_helpers.h"
1112
1213
// Since eBPF mock package is optional and have definition of "__maps_head" symbol
1314
// it may cause link error, so defining weak symbol here as well

itest/kprobe_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func (ts *kprobeTestSuite) TestKprobeEvents() {
153153
// Run suite
154154
func TestKprobeSuite(t *testing.T) {
155155
suite.Run(t, &kprobeTestSuite{
156-
programFilename: "ebpf_prog/kprobe1.elf",
156+
programFilename: progPath("kprobe1.elf"),
157157
programsCount: 2,
158158
mapsCount: 1,
159159
})

itest/path.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package itest
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path"
7+
)
8+
9+
const progSubdir = "ebpf_prog"
10+
11+
// Helper to get the absolute path of a test program.
12+
func progPath(imageName string) string {
13+
// within `go test`, the test executable lives in a temporary directory, so
14+
// we check the WD first.
15+
exePath, err := os.Executable()
16+
if err != nil {
17+
panic(err)
18+
}
19+
wd, err := os.Getwd()
20+
if err != nil {
21+
panic(err)
22+
}
23+
24+
relPath := path.Join(progSubdir, imageName)
25+
wdPath := path.Join(wd, relPath)
26+
absPath := path.Join(path.Dir(exePath), relPath)
27+
28+
if _, err = os.Stat(wdPath); err == nil {
29+
return wdPath
30+
}
31+
32+
if _, err = os.Stat(absPath); err == nil {
33+
return absPath
34+
}
35+
36+
panic(
37+
fmt.Sprintf(
38+
"eBPF executable not found, checked paths:\n %q\n %q",
39+
wdPath, absPath))
40+
}

itest/perf_events_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
func TestPerfEvents(t *testing.T) {
1818
// Read ELF, find map, load/attach program
1919
eb := goebpf.NewDefaultEbpfSystem()
20-
err := eb.LoadElf(xdpProgramFilename)
20+
err := eb.LoadElf(progPath(xdpProgramFilename))
2121
require.NoError(t, err)
2222
perfMap := eb.GetMapByName("perf_map")
2323
require.NotNil(t, perfMap)

itest/tc_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
)
1414

1515
const (
16-
tcProgramFilename = "ebpf_prog/tc1.elf"
16+
tcProgramFilename = "tc1.elf"
1717
)
1818

1919
type tcTestSuite struct {
@@ -129,7 +129,7 @@ func (ts *tcTestSuite) TestProgramInfo() {
129129
// Run suite
130130
func TestTcSuite(t *testing.T) {
131131
suite.Run(t, &tcTestSuite{
132-
programFilename: tcProgramFilename,
132+
programFilename: progPath(tcProgramFilename),
133133
programsCount: 3,
134134
})
135135
}

itest/xdp_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
)
1414

1515
const (
16-
xdpProgramFilename = "ebpf_prog/xdp1.elf"
16+
xdpProgramFilename = "xdp1.elf"
1717
)
1818

1919
type xdpTestSuite struct {
@@ -204,7 +204,7 @@ func (ts *xdpTestSuite) TestProgramInfo() {
204204
// Run suite
205205
func TestXdpSuite(t *testing.T) {
206206
suite.Run(t, &xdpTestSuite{
207-
programFilename: xdpProgramFilename,
207+
programFilename: progPath(xdpProgramFilename),
208208
programsCount: 5,
209209
mapsCount: 7,
210210
})

0 commit comments

Comments
 (0)