feat: [sc-106927] Allow kernelConfig analyser to check kernel capability is either built in or loaded for EC host preflights (#1572)

* allow multiple value in kernel config check

* update unit test
This commit is contained in:
Gerard Nguyen
2024-07-09 09:45:42 +10:00
committed by GitHub
parent f5f02f5a80
commit 84d5506ded
2 changed files with 26 additions and 17 deletions
+4 -4
View File
@@ -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
@@ -49,7 +49,7 @@ 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 {
@@ -57,8 +57,8 @@ func (a *AnalyzeHostKernelConfigs) Analyze(
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)
}
}
+22 -13
View File
@@ -4,19 +4,13 @@ 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
@@ -24,7 +18,6 @@ func TestAnalyzeKernelConfigs(t *testing.T) {
}{
{
name: "all pass",
kConfigs: kConfigs,
selectedConfigs: []string{"CONFIG_CGROUP_FREEZER=y", "CONFIG_NETFILTER_XTABLES=m"},
outcomes: []*troubleshootv1beta2.Outcome{
{
@@ -44,7 +37,6 @@ func TestAnalyzeKernelConfigs(t *testing.T) {
},
{
name: "has fail",
kConfigs: kConfigs,
selectedConfigs: []string{"CONFIG_UTS_NS=y"},
outcomes: []*troubleshootv1beta2.Outcome{
{
@@ -64,7 +56,6 @@ func TestAnalyzeKernelConfigs(t *testing.T) {
},
{
name: "kernel config disabled",
kConfigs: kConfigs,
selectedConfigs: []string{"CONFIG_CGROUP_FREEZER=n"},
outcomes: []*troubleshootv1beta2.Outcome{
{
@@ -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{
@@ -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)