Skip to content

Commit 438ad98

Browse files
committed
bpf2go: add test for handling multiple source files
Signed-off-by: Jonathan Perry <yonch@yonch.com>
1 parent 0104501 commit 438ad98

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

cmd/bpf2go/main_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,91 @@ func TestParseArgs(t *testing.T) {
406406
})
407407
}
408408

409+
func TestMultipleSourceFiles(t *testing.T) {
410+
modRoot, err := filepath.Abs("../..")
411+
if err != nil {
412+
t.Fatal("Can't get module root:", err)
413+
}
414+
415+
if _, err := os.Stat(filepath.Join(modRoot, "go.mod")); os.IsNotExist(err) {
416+
t.Fatal("No go.mod file in", modRoot)
417+
}
418+
419+
dir := t.TempDir()
420+
421+
// Create two source files with different functions
422+
mustWriteFile(t, dir, "func1.c", `__attribute__((section("socket"), used)) int func1() { return 1; }`)
423+
mustWriteFile(t, dir, "func2.c", `__attribute__((section("socket"), used)) int func2() { return 2; }`)
424+
425+
// Set up module directory
426+
modDir := t.TempDir()
427+
execInModule := func(name string, args ...string) {
428+
t.Helper()
429+
cmd := exec.Command(name, args...)
430+
cmd.Dir = modDir
431+
if out, err := cmd.CombinedOutput(); err != nil {
432+
if out := string(out); out != "" {
433+
t.Log(out)
434+
}
435+
t.Fatalf("Can't execute %s: %v", name, args)
436+
}
437+
}
438+
439+
// Initialize module
440+
execInModule("go", "mod", "init", "bpf2go-test")
441+
execInModule("go", "mod", "edit",
442+
fmt.Sprintf("-require=%s@v0.0.0", internal.CurrentModule),
443+
fmt.Sprintf("-replace=%s=%s", internal.CurrentModule, modRoot),
444+
)
445+
446+
// Run bpf2go with both source files
447+
err = run(io.Discard, []string{
448+
"-go-package", "main",
449+
"-output-dir", modDir,
450+
"-cc", testutils.ClangBin(t),
451+
"-target", "bpfel,bpfeb",
452+
"bar",
453+
filepath.Join(dir, "func1.c"),
454+
filepath.Join(dir, "func2.c"),
455+
})
456+
457+
if err != nil {
458+
t.Fatal("Can't run bpf2go with multiple source files:", err)
459+
}
460+
461+
// Create a main.go that uses both functions
462+
mustWriteFile(t, modDir, "main.go",
463+
`
464+
package main
465+
466+
func main() {
467+
var obj barObjects
468+
println(obj.Func1)
469+
println(obj.Func2)
470+
}`)
471+
472+
// Test compilation for different architectures
473+
for _, arch := range []string{"amd64", "arm64", "s390x"} {
474+
t.Run(arch, func(t *testing.T) {
475+
goBuild := exec.Command("go", "build", "-mod=mod", "-o", "/dev/null")
476+
goBuild.Dir = modDir
477+
goBuild.Env = append(os.Environ(),
478+
"GOOS=linux",
479+
"GOARCH="+arch,
480+
"GOPROXY=off",
481+
"GOSUMDB=off",
482+
)
483+
out, err := goBuild.CombinedOutput()
484+
if err != nil {
485+
if out := string(out); out != "" {
486+
t.Log(out)
487+
}
488+
t.Error("Can't compile package:", err)
489+
}
490+
})
491+
}
492+
}
493+
409494
func mustWriteFile(tb testing.TB, dir, name, contents string) {
410495
tb.Helper()
411496
tmpFile := filepath.Join(dir, name)

0 commit comments

Comments
 (0)