Skip to content

Commit

Permalink
fix: add some tests, fix error string
Browse files Browse the repository at this point in the history
Signed-off-by: grantseltzer <grantseltzer@gmail.com>
  • Loading branch information
grantseltzer committed Mar 31, 2021
1 parent 6acdf8c commit 2d62a69
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 14 deletions.
30 changes: 16 additions & 14 deletions libbpfgo/helpers/kernel_features.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"compress/gzip"
"errors"
"fmt"
"io"
"os"
"strings"

Expand Down Expand Up @@ -70,12 +71,12 @@ func InitKernelConfig(k KernelConfig) (KernelConfig, error) {

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

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

err2 := k.getProcGZConfig("/proc/config.gz")
err2 := k.getProcGZConfigByPath("/proc/config.gz")
if err != nil {
return nil, fmt.Errorf("%v %v", err, err2)
}
Expand All @@ -88,45 +89,46 @@ func InitKernelConfig(k KernelConfig) (KernelConfig, error) {
func (k KernelConfig) GetKernelConfigValue(key string) (string, error) {
v, exists := k[key]
if !exists {
return "", errors.New("kernel config value does not exist, it is could not be known by your kernel version")
return "", errors.New("kernel config value does not exist, it's possible this option is not present in your kernel version or the KernelConfig has not been initialized")
}
return v, nil
}

func (k KernelConfig) getBootConfig(bootConfigPath string) error {
func (k KernelConfig) getBootConfigByPath(bootConfigPath string) error {

configFile, err := os.Open(bootConfigPath)
if err != nil {
return fmt.Errorf("could not open %s: %v", bootConfigPath, err)
}

scanner := bufio.NewScanner(configFile)
k.readConfigFromScanner(scanner)
configFile.Close()
k.readConfigFromScanner(configFile)

return nil
}

func (k KernelConfig) getProcGZConfig(procConfigPath string) error {
func (k KernelConfig) getProcGZConfigByPath(procConfigPath string) error {

configFile, err := os.Open(procConfigPath)
if err != nil {
return fmt.Errorf("could not open %s: %v", procConfigPath, err)
}

zreader, err := gzip.NewReader(configFile)
return k.getProcGZConfig(configFile)
}

func (k KernelConfig) getProcGZConfig(reader io.Reader) error {
zreader, err := gzip.NewReader(reader)
if err != nil {
return err
}

scanner := bufio.NewScanner(zreader)
k.readConfigFromScanner(scanner)
configFile.Close()

k.readConfigFromScanner(zreader)
return nil
}

func (k KernelConfig) readConfigFromScanner(scanner *bufio.Scanner) {
func (k KernelConfig) readConfigFromScanner(reader io.Reader) {
scanner := bufio.NewScanner(reader)

for scanner.Scan() {
kv := strings.Split(scanner.Text(), "=")
if len(kv) != 2 {
Expand Down
47 changes: 47 additions & 0 deletions libbpfgo/helpers/kernel_features_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,50 @@ func TestGetProcGZConfig(t *testing.T) {
})
}
}

func TestGetKernelConfigValue(t *testing.T) {
testCases := []struct {
name string
key string
conf KernelConfig
expectedError error
expectedValue string
}{
{
name: "Value present",
key: CONFIG_BPF,
conf: KernelConfig{CONFIG_BPF: "y"},
expectedError: nil,
expectedValue: "y",
},
{
name: "Value present",
key: CONFIG_BPF,
conf: KernelConfig{CONFIG_BPFILTER: "foo", CONFIG_BPF: "y"},
expectedError: nil,
expectedValue: "y",
},
{
name: "nil conf",
key: CONFIG_BPF,
conf: nil,
expectedError: errors.New("kernel config value does not exist, it's possible this option is not present in your kernel version or the KernelConfig has not been initialized"),
expectedValue: "",
},
{
name: "Value not present",
key: CONFIG_BPF_JIT,
conf: KernelConfig{CONFIG_BPF: "y"},
expectedError: errors.New("kernel config value does not exist, it's possible this option is not present in your kernel version or the KernelConfig has not been initialized"),
expectedValue: "",
},
}

for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
v, err := tt.conf.GetKernelConfigValue(tt.key)
assert.Equal(t, tt.expectedError, err)
assert.Equal(t, tt.expectedValue, v)
})
}
}

0 comments on commit 2d62a69

Please sign in to comment.