Skip to content

Commit

Permalink
feat: add tests for proc gz config
Browse files Browse the repository at this point in the history
Signed-off-by: grantseltzer <grantseltzer@gmail.com>
  • Loading branch information
grantseltzer committed Mar 26, 2021
1 parent cf01331 commit 7ce5943
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 14 deletions.
26 changes: 12 additions & 14 deletions libbpfgo/helpers/kernel_features.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,20 @@ type KernelConfig map[string]string
// /boot/config.gz
func InitKernelConfig(k KernelConfig) (KernelConfig, error) {

err := k.getBootConfig()
x := unix.Utsname{}
err := unix.Uname(&x)
if err != nil {
return nil, fmt.Errorf("could not determine uname release: %v", err)
}

bootConfigPath := fmt.Sprintf("/boot/config-%s", bytes.Trim(x.Release[:], "\x00"))

err = k.getBootConfig(bootConfigPath)
if err == nil {
return k, nil
}

err2 := k.getProcGZConfig()
err2 := k.getProcGZConfig("/proc/config.gz")
if err != nil {
return nil, fmt.Errorf("%v %v", err, err2)
}
Expand All @@ -44,15 +52,7 @@ func (k KernelConfig) GetKernelConfigValue(key string) (string, error) {
return v, nil
}

func (k KernelConfig) getBootConfig() error {

x := unix.Utsname{}
err := unix.Uname(&x)
if err != nil {
return fmt.Errorf("could not determine uname release: %v", err)
}

bootConfigPath := fmt.Sprintf("/boot/config-%s", bytes.Trim(x.Release[:], "\x00"))
func (k KernelConfig) getBootConfig(bootConfigPath string) error {

configFile, err := os.Open(bootConfigPath)
if err != nil {
Expand All @@ -66,9 +66,7 @@ func (k KernelConfig) getBootConfig() error {
return nil
}

func (k KernelConfig) getProcGZConfig() error {

procConfigPath := "/proc/config.gz"
func (k KernelConfig) getProcGZConfig(procConfigPath string) error {

configFile, err := os.Open(procConfigPath)
if err != nil {
Expand Down
52 changes: 52 additions & 0 deletions libbpfgo/helpers/kernel_features_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package helpers

import (
"errors"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetProcGZConfig(t *testing.T) {
testCases := []struct {
name string
goldenFilePath string
expectedMap KernelConfig
expectedError error
}{
{
name: "non-existant",
goldenFilePath: "foobarblahblahblah",
expectedMap: KernelConfig{},
expectedError: errors.New("could not open foobarblahblahblah: open foobarblahblahblah: no such file or directory"),
},
{
name: "invalid zip format",
goldenFilePath: "testdata/tarred_config.tar",
expectedMap: KernelConfig{},
expectedError: errors.New("gzip: invalid header"),
},
{
name: "standard config",
goldenFilePath: "testdata/config_standard.gz",
expectedMap: KernelConfig{"CONFIG_ARCH_WANT_DEFAULT_BPF_JIT": "y", "CONFIG_BPF": "y", "CONFIG_BPF_JIT_ALWAYS_ON": "y", "CONFIG_BPF_JIT_DEFAULT_ON": "y", "CONFIG_BPF_LSM": "y", "CONFIG_BPF_PRELOAD": "y", "CONFIG_BPF_PRELOAD_UMD": "m", "CONFIG_BPF_SYSCALL": "y", "CONFIG_IPV6_SEG6_BPF": "y", "CONFIG_NETFILTER_XT_MATCH_BPF": "m"},
expectedError: nil,
},
{
name: "config with comments in it",
goldenFilePath: "testdata/comments_config.gz",
expectedMap: KernelConfig{"CONFIG_BPF": "y", "CONFIG_BPF_PRELOAD_UMD": "m", "CONFIG_BPF_SYSCALL": "y"},
expectedError: nil,
},
}

for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {

var kconfig KernelConfig = make(map[string]string)
err := kconfig.getProcGZConfig(tt.goldenFilePath)
assert.Equal(t, tt.expectedError, err)
assert.Equal(t, tt.expectedMap, kconfig)
})
}
}
Binary file added libbpfgo/helpers/testdata/comments_config.gz
Binary file not shown.
Binary file added libbpfgo/helpers/testdata/config_standard.gz
Binary file not shown.
Binary file added libbpfgo/helpers/testdata/tarred_config.tar
Binary file not shown.
Binary file removed libbpfgo/helpers/testdata/test_config.gz
Binary file not shown.

0 comments on commit 7ce5943

Please sign in to comment.