From 8173759e526821423ed5484bd615e970b4490692 Mon Sep 17 00:00:00 2001 From: Gerard Nguyen Date: Tue, 9 Jul 2024 09:45:42 +1000 Subject: [PATCH] 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 --- pkg/analyze/host_kernel_configs.go | 8 +++--- pkg/analyze/host_kernel_configs_test.go | 35 ++++++++++++++++--------- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/pkg/analyze/host_kernel_configs.go b/pkg/analyze/host_kernel_configs.go index 90154663c..d7b3455d2 100644 --- a/pkg/analyze/host_kernel_configs.go +++ b/pkg/analyze/host_kernel_configs.go @@ -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) } } diff --git a/pkg/analyze/host_kernel_configs_test.go b/pkg/analyze/host_kernel_configs_test.go index b7d737cae..2dfecc917 100644 --- a/pkg/analyze/host_kernel_configs_test.go +++ b/pkg/analyze/host_kernel_configs_test.go @@ -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)