Skip to content

Commit

Permalink
kconfig: merge kconfig tests into single testdata file
Browse files Browse the repository at this point in the history
The Go counterparts of these tests were overly-specific, checking if each
individual value was set correctly. This arguably tests at the wrong level
of abstraction. The data source of the values needs to be tested individually,
with the C code simply checking if a non-zero value was populated.

Signed-off-by: Timo Beckers <[email protected]>
  • Loading branch information
ti-mo committed Dec 9, 2024
1 parent 724b704 commit 99e9ca6
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 115 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ TARGETS := \
testdata/subprog_reloc \
testdata/fwd_decl \
testdata/kconfig \
testdata/kconfig_config \
testdata/ksym \
testdata/kfunc \
testdata/invalid-kfunc \
Expand Down
88 changes: 3 additions & 85 deletions elf_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/cilium/ebpf/btf"
"github.com/cilium/ebpf/internal"
"github.com/cilium/ebpf/internal/kallsyms"
"github.com/cilium/ebpf/internal/linux"
"github.com/cilium/ebpf/internal/sys"
"github.com/cilium/ebpf/internal/testutils"

Expand Down Expand Up @@ -643,51 +642,15 @@ func TestTailCall(t *testing.T) {
}
}

func TestKconfigKernelVersion(t *testing.T) {
func TestKconfig(t *testing.T) {
file := testutils.NativeFile(t, "testdata/kconfig-%s.elf")
spec, err := LoadCollectionSpec(file)
if err != nil {
t.Fatal(err)
}

var obj struct {
Main *Program `ebpf:"kernel_version"`
}

testutils.SkipOnOldKernel(t, "5.2", "readonly maps")

err = spec.LoadAndAssign(&obj, nil)
if err != nil {
t.Fatal(err)
}
defer obj.Main.Close()

ret, _, err := obj.Main.Test(internal.EmptyBPFContext)
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal(err)
}

v, err := linux.KernelVersion()
if err != nil {
t.Fatalf("getting kernel version: %s", err)
}

version := v.Kernel()
if ret != version {
t.Fatalf("Expected eBPF to return value %d, got %d", version, ret)
}
}

func TestKconfigSyscallWrapper(t *testing.T) {
file := testutils.NativeFile(t, "testdata/kconfig-%s.elf")
spec, err := LoadCollectionSpec(file)
if err != nil {
t.Fatal(err)
}

var obj struct {
Main *Program `ebpf:"syscall_wrapper"`
Main *Program `ebpf:"kconfig"`
}

err = spec.LoadAndAssign(&obj, nil)
Expand All @@ -703,52 +666,7 @@ func TestKconfigSyscallWrapper(t *testing.T) {
t.Fatal(err)
}

var expected uint32
if testutils.IsKernelLessThan(t, "4.17") {
expected = 0
} else {
expected = 1
}

if ret != expected {
t.Fatalf("Expected eBPF to return value %d, got %d", expected, ret)
}
}

func TestKconfigConfig(t *testing.T) {
file := testutils.NativeFile(t, "testdata/kconfig_config-%s.elf")
spec, err := LoadCollectionSpec(file)
if err != nil {
t.Fatal(err)
}

var obj struct {
Main *Program `ebpf:"kconfig"`
ArrayMap *Map `ebpf:"array_map"`
}

err = spec.LoadAndAssign(&obj, nil)
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal(err)
}
defer obj.Main.Close()
defer obj.ArrayMap.Close()

_, _, err = obj.Main.Test(internal.EmptyBPFContext)
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal(err)
}

var value uint64
err = obj.ArrayMap.Lookup(uint32(0), &value)
if err != nil {
t.Fatal(err)
}

// CONFIG_HZ must have a value.
qt.Assert(t, qt.Not(qt.Equals(value, 0)))
qt.Assert(t, qt.Equals(ret, 0), qt.Commentf("Failed assertion at line %d in testdata/kconfig.c", ret))
}

func TestKsym(t *testing.T) {
Expand Down
Binary file modified testdata/kconfig-eb.elf
Binary file not shown.
Binary file modified testdata/kconfig-el.elf
Binary file not shown.
29 changes: 23 additions & 6 deletions testdata/kconfig.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
#include "common.h"

char __license[] __section("license") = "MIT";
char __license[] __section("license") = "GPL-2.0";

/* Special cases requiring feature testing or vDSO magic. */
extern int LINUX_KERNEL_VERSION __kconfig;
extern _Bool LINUX_HAS_SYSCALL_WRAPPER __kconfig;

__section("socket") int kernel_version() {
return LINUX_KERNEL_VERSION;
}
/* Values pulled from /proc/kconfig. */
extern int CONFIG_HZ __kconfig;
extern enum libbpf_tristate CONFIG_BPF_SYSCALL __kconfig;
extern char CONFIG_DEFAULT_HOSTNAME[1] __kconfig;

__section("socket") int kconfig() {
if (LINUX_KERNEL_VERSION == 0)
return __LINE__;

if (LINUX_HAS_SYSCALL_WRAPPER == 0)
return __LINE__;

if (CONFIG_HZ == 0)
return __LINE__;

if (CONFIG_BPF_SYSCALL == TRI_NO)
return __LINE__;

if (CONFIG_DEFAULT_HOSTNAME[0] == 0)
return __LINE__;

__section("socket") int syscall_wrapper() {
return LINUX_HAS_SYSCALL_WRAPPER;
return 0;
}
Binary file removed testdata/kconfig_config-eb.elf
Binary file not shown.
Binary file removed testdata/kconfig_config-el.elf
Binary file not shown.
23 changes: 0 additions & 23 deletions testdata/kconfig_config.c

This file was deleted.

0 comments on commit 99e9ca6

Please sign in to comment.