Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove LoadBTF and allow multiple btf files for testing #3411

Open
ScriptSathi opened this issue Feb 17, 2025 · 1 comment · May be fixed by #3414
Open

Remove LoadBTF and allow multiple btf files for testing #3411

ScriptSathi opened this issue Feb 17, 2025 · 1 comment · May be fixed by #3414
Labels
good first issue Good for newcomers

Comments

@ScriptSathi
Copy link
Contributor

This issue is the follow-up to this conversation #3143 (comment)

The idea is to make a more simple use of the btfFile global variable in case we want to test multiple BTF files in a single test.

I think what we can do here is using a callback to set the btfFile, then perform the test, and global variable like this.

func RunTestOnSingleBtf(t *testing.T, btfFname string, test func(t *testing.T)) {
	if err := btfFileExists(btfFname); err != nil {
		t.Fatal(err)
	}
	btfFile = btfFname
        t.Run(btfFname, test)
	btfFile = ""
}

func TestFindBtfFuncParamFromHook(t *testing.T) {
	RunTestOnSingleBtf(t, defaults.DefaultBTFFile, testFindBtfFuncParamFromHook)
        RunTestOnSingleBtf(t, "/path/to/my/btf/2", testFindBtfFuncParamFromHook)
        RunTestOnSingleBtf(t, "/path/to/my/btf/3", testFindBtfFuncParamFromHook)
}

In FindBtfFuncParamFromHook we could also remove the use LoadBtf() by simply using NewBtf()

tetragon/pkg/btf/btf.go

Lines 118 to 120 in fc759c4

func FindBtfFuncParamFromHook(hook string, argIndex int) (*btf.FuncParam, error) {
var hookFn *btf.Func
spec, err := LoadBTF()

Similarly we could refactor this below code by using such a function

btfFname := filepath.Join(testdataPath, "btf", "vmlinux-5.4.104+")
if _, err := os.Stat(btfFname); err != nil {
t.Skipf("%s not found", btfFname)
}
btf, err := btf.LoadSpec(btfFname)
if err != nil {
t.Fatalf("failed to initialize BTF: %s", err)
}

@kkourt, do you think it's close enough to what you were thinking of ?

@kkourt
Copy link
Contributor

kkourt commented Feb 18, 2025

@kkourt, do you think it's close enough to what you were thinking of?
Yes, thanks for writing up!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
Status: No status
Development

Successfully merging a pull request may close this issue.

2 participants