Skip to content

Commit 8173759

Browse files
authored
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
1 parent f5f02f5 commit 8173759

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

pkg/analyze/host_kernel_configs.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (a *AnalyzeHostKernelConfigs) Analyze(
4040
}
4141

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

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

5454
// check if the kernel config exists
5555
if _, ok := kConfigs[key]; !ok {
5656
configsNotFound = append(configsNotFound, config)
5757
continue
5858
}
5959
// check if the kernel config value matches
60-
if kConfigs[key] != value {
61-
klog.V(2).Infof("collected kernel config %s=%s does not match expected value %s", key, kConfigs[key], value)
60+
if !strings.Contains(values, kConfigs[key]) {
61+
klog.V(2).Infof("collected kernel config %s=%s does not in expected values %s", key, kConfigs[key], values)
6262
configsNotFound = append(configsNotFound, config)
6363
}
6464
}

pkg/analyze/host_kernel_configs_test.go

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,20 @@ import (
44
"testing"
55

66
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
7-
"github.com/replicatedhq/troubleshoot/pkg/collect"
87
"github.com/stretchr/testify/assert"
98
)
109

1110
func TestAnalyzeKernelConfigs(t *testing.T) {
12-
kConfigs := collect.KConfigs{
13-
"CONFIG_CGROUP_FREEZER": "y",
14-
"CONFIG_NETFILTER_XTABLES": "m",
15-
}
1611

1712
tests := []struct {
1813
name string
19-
kConfigs collect.KConfigs
2014
selectedConfigs []string
2115
outcomes []*troubleshootv1beta2.Outcome
2216
results []*AnalyzeResult
2317
expectErr bool
2418
}{
2519
{
2620
name: "all pass",
27-
kConfigs: kConfigs,
2821
selectedConfigs: []string{"CONFIG_CGROUP_FREEZER=y", "CONFIG_NETFILTER_XTABLES=m"},
2922
outcomes: []*troubleshootv1beta2.Outcome{
3023
{
@@ -44,7 +37,6 @@ func TestAnalyzeKernelConfigs(t *testing.T) {
4437
},
4538
{
4639
name: "has fail",
47-
kConfigs: kConfigs,
4840
selectedConfigs: []string{"CONFIG_UTS_NS=y"},
4941
outcomes: []*troubleshootv1beta2.Outcome{
5042
{
@@ -64,7 +56,6 @@ func TestAnalyzeKernelConfigs(t *testing.T) {
6456
},
6557
{
6658
name: "kernel config disabled",
67-
kConfigs: kConfigs,
6859
selectedConfigs: []string{"CONFIG_CGROUP_FREEZER=n"},
6960
outcomes: []*troubleshootv1beta2.Outcome{
7061
{
@@ -84,17 +75,35 @@ func TestAnalyzeKernelConfigs(t *testing.T) {
8475
},
8576
{
8677
name: "invalid kernel config",
87-
kConfigs: kConfigs,
8878
selectedConfigs: []string{"foobar=n"},
8979
expectErr: true,
9080
},
81+
{
82+
name: "select multiple kernel config values",
83+
selectedConfigs: []string{"CONFIG_BRIDGE=my"},
84+
outcomes: []*troubleshootv1beta2.Outcome{
85+
{
86+
Pass: &troubleshootv1beta2.SingleOutcome{
87+
Message: "required kernel configs are available",
88+
},
89+
},
90+
},
91+
results: []*AnalyzeResult{
92+
{
93+
Title: "Kernel Configs",
94+
IsPass: true,
95+
Message: "required kernel configs are available",
96+
},
97+
},
98+
expectErr: false,
99+
},
91100
}
92101

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

96-
fn := func(_ string) ([]byte, error) {
97-
return []byte(`{"CONFIG_CGROUP_FREEZER": "y", "CONFIG_NETFILTER_XTABLES": "m"}`), nil
105+
mockKernelFile := func(_ string) ([]byte, error) {
106+
return []byte(`{"CONFIG_CGROUP_FREEZER": "y", "CONFIG_NETFILTER_XTABLES": "m", "CONFIG_BRIDGE": "y"}`), nil
98107
}
99108

100109
analyzer := AnalyzeHostKernelConfigs{
@@ -107,7 +116,7 @@ func TestAnalyzeKernelConfigs(t *testing.T) {
107116
},
108117
}
109118

110-
results, err := analyzer.Analyze(fn, nil)
119+
results, err := analyzer.Analyze(mockKernelFile, nil)
111120

112121
if tt.expectErr {
113122
assert.Error(t, err)

0 commit comments

Comments
 (0)