Skip to content

Commit

Permalink
validation_test.go : move TestSpecs to genericTestSpecs for easier use
Browse files Browse the repository at this point in the history
With this commit, it will be more trivial to add different BTF files for
testing

Signed-off-by: Tristan d'Audibert <[email protected]>
  • Loading branch information
ScriptSathi committed Feb 19, 2025
1 parent f118af7 commit c91f4d3
Showing 1 changed file with 31 additions and 24 deletions.
55 changes: 31 additions & 24 deletions pkg/btf/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,36 @@ var testFiles = []struct {
/* {specFname: "specs/wrongrettype.yaml", checkFn: expectError}, */
}

func genericTestSpecs(ks *ksyms.Ksyms, btfFName string, testdataPath string) func(*testing.T) {
return func(t *testing.T) {
if _, err := os.Stat(btfFName); err != nil {
t.Skipf("%q not found", btfFName)
}
btf, err := btf.LoadSpec(btfFName)
if err != nil {
t.Fatalf("failed to initialize BTF: %s", err)
}

for fi := range testFiles {
specFname := testFiles[fi].specFname
t.Run(specFname, func(t *testing.T) {
specFname := filepath.Join(testdataPath, specFname)
tp, err := tracingpolicy.FromFile(specFname)
if err != nil {
t.Fatal(err)
}
spec := tp.TpSpec()
for ki := range spec.KProbes {
err = ValidateKprobeSpec(btf, spec.KProbes[ki].Call, &spec.KProbes[ki], ks)
if checkErr := testFiles[fi].checkFn(t, err); checkErr != nil {
t.Fatal(checkErr)
}
}
})
}
}
}

func TestSpecs(t *testing.T) {
_, testFname, _, _ := runtime.Caller(0)
testdataPath := filepath.Join(filepath.Dir(testFname), "..", "..", "testdata")
Expand All @@ -61,31 +91,8 @@ func TestSpecs(t *testing.T) {

// NB: for now we check against a single BTF file.
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)
}

for fi := range testFiles {
specFname := testFiles[fi].specFname
t.Run(specFname, func(t *testing.T) {
specFname := filepath.Join(testdataPath, specFname)
tp, err := tracingpolicy.FromFile(specFname)
if err != nil {
t.Fatal(err)
}
spec := tp.TpSpec()
for ki := range spec.KProbes {
err = ValidateKprobeSpec(btf, spec.KProbes[ki].Call, &spec.KProbes[ki], ks)
if checkErr := testFiles[fi].checkFn(t, err); checkErr != nil {
t.Fatal(checkErr)
}
}
})
}
t.Run(btfFname, genericTestSpecs(ks, btfFname, testdataPath))
}

func TestEnum(t *testing.T) {
Expand Down

0 comments on commit c91f4d3

Please sign in to comment.