Skip to content

Commit

Permalink
feat: [sc-106927] Allow kernelConfig analyser to check kernel capabil…
Browse files Browse the repository at this point in the history
…ity is either built in or loaded for EC host preflights (#1572)

* allow multiple value in kernel config check

* update unit test
  • Loading branch information
nvanthao authored Jul 8, 2024
1 parent f5f02f5 commit 8173759
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
8 changes: 4 additions & 4 deletions pkg/analyze/host_kernel_configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (a *AnalyzeHostKernelConfigs) Analyze(
}

var configsNotFound []string
kConfigRegex := regexp.MustCompile("^(CONFIG_[A-Z0-9_]+)=([ymn])$")
kConfigRegex := regexp.MustCompile("^(CONFIG_[A-Z0-9_]+)=([ymn]+)$")
for _, config := range hostAnalyzer.SelectedConfigs {
matches := kConfigRegex.FindStringSubmatch(config)
// zero tolerance for invalid kernel config
Expand All @@ -49,16 +49,16 @@ func (a *AnalyzeHostKernelConfigs) Analyze(
}

key := matches[1]
value := matches[2]
values := matches[2] // values can contain multiple values in any order y, m, n

// check if the kernel config exists
if _, ok := kConfigs[key]; !ok {
configsNotFound = append(configsNotFound, config)
continue
}
// check if the kernel config value matches
if kConfigs[key] != value {
klog.V(2).Infof("collected kernel config %s=%s does not match expected value %s", key, kConfigs[key], value)
if !strings.Contains(values, kConfigs[key]) {
klog.V(2).Infof("collected kernel config %s=%s does not in expected values %s", key, kConfigs[key], values)
configsNotFound = append(configsNotFound, config)
}
}
Expand Down
35 changes: 22 additions & 13 deletions pkg/analyze/host_kernel_configs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,20 @@ import (
"testing"

troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
"github.com/replicatedhq/troubleshoot/pkg/collect"
"github.com/stretchr/testify/assert"
)

func TestAnalyzeKernelConfigs(t *testing.T) {
kConfigs := collect.KConfigs{
"CONFIG_CGROUP_FREEZER": "y",
"CONFIG_NETFILTER_XTABLES": "m",
}

tests := []struct {
name string
kConfigs collect.KConfigs
selectedConfigs []string
outcomes []*troubleshootv1beta2.Outcome
results []*AnalyzeResult
expectErr bool
}{
{
name: "all pass",
kConfigs: kConfigs,
selectedConfigs: []string{"CONFIG_CGROUP_FREEZER=y", "CONFIG_NETFILTER_XTABLES=m"},
outcomes: []*troubleshootv1beta2.Outcome{
{
Expand All @@ -44,7 +37,6 @@ func TestAnalyzeKernelConfigs(t *testing.T) {
},
{
name: "has fail",
kConfigs: kConfigs,
selectedConfigs: []string{"CONFIG_UTS_NS=y"},
outcomes: []*troubleshootv1beta2.Outcome{
{
Expand All @@ -64,7 +56,6 @@ func TestAnalyzeKernelConfigs(t *testing.T) {
},
{
name: "kernel config disabled",
kConfigs: kConfigs,
selectedConfigs: []string{"CONFIG_CGROUP_FREEZER=n"},
outcomes: []*troubleshootv1beta2.Outcome{
{
Expand All @@ -84,17 +75,35 @@ func TestAnalyzeKernelConfigs(t *testing.T) {
},
{
name: "invalid kernel config",
kConfigs: kConfigs,
selectedConfigs: []string{"foobar=n"},
expectErr: true,
},
{
name: "select multiple kernel config values",
selectedConfigs: []string{"CONFIG_BRIDGE=my"},
outcomes: []*troubleshootv1beta2.Outcome{
{
Pass: &troubleshootv1beta2.SingleOutcome{
Message: "required kernel configs are available",
},
},
},
results: []*AnalyzeResult{
{
Title: "Kernel Configs",
IsPass: true,
Message: "required kernel configs are available",
},
},
expectErr: false,
},
}

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

fn := func(_ string) ([]byte, error) {
return []byte(`{"CONFIG_CGROUP_FREEZER": "y", "CONFIG_NETFILTER_XTABLES": "m"}`), nil
mockKernelFile := func(_ string) ([]byte, error) {
return []byte(`{"CONFIG_CGROUP_FREEZER": "y", "CONFIG_NETFILTER_XTABLES": "m", "CONFIG_BRIDGE": "y"}`), nil
}

analyzer := AnalyzeHostKernelConfigs{
Expand All @@ -107,7 +116,7 @@ func TestAnalyzeKernelConfigs(t *testing.T) {
},
}

results, err := analyzer.Analyze(fn, nil)
results, err := analyzer.Analyze(mockKernelFile, nil)

if tt.expectErr {
assert.Error(t, err)
Expand Down

0 comments on commit 8173759

Please sign in to comment.