KubeOS/0006-KubeOS-fix-a-bug-that-failed-to-parse-key-with.patch
Yuhang Wei 1d36b74685 KubeOS:sync code from source master branch
Signed-off-by: Yuhang Wei <weiyuhang3@huawei.com>
2024-02-26 09:54:27 +08:00

157 lines
4.4 KiB
Diff

From 911fbd04ba55a2560e8da8595ff379a742e5fa30 Mon Sep 17 00:00:00 2001
From: Yuhang Wei <weiyuhang3@huawei.com>
Date: Thu, 10 Aug 2023 17:47:54 +0800
Subject: [PATCH 06/17] KubeOS: fix a bug that failed to parse key with =
Signed-off-by: Yuhang Wei <weiyuhang3@huawei.com>
---
cmd/agent/server/config.go | 6 ++--
cmd/agent/server/config_test.go | 55 +++++++++++++++++++++++++++++----
2 files changed, 52 insertions(+), 9 deletions(-)
diff --git a/cmd/agent/server/config.go b/cmd/agent/server/config.go
index 653f913..bcd9fde 100644
--- a/cmd/agent/server/config.go
+++ b/cmd/agent/server/config.go
@@ -259,7 +259,7 @@ func getAndSetConfigsFromFile(expectConfigs map[string]*agent.KeyInfo, path stri
configsWrite = append(configsWrite, line)
continue
}
- configKV := strings.Split(line, "=")
+ configKV := strings.SplitN(line, "=", kvPair)
if len(configKV) != kvPair {
logrus.Errorf("could not parse systctl config %s", line)
return nil, fmt.Errorf("could not parse systctl config %s", line)
@@ -374,8 +374,8 @@ func handleUpdateKey(config []string, configInfo *agent.KeyInfo, isFound bool) s
func handleAddKey(m map[string]*agent.KeyInfo, isOnlyKeyValid bool) []string {
var configs []string
for key, keyInfo := range m {
- if key == "" {
- logrus.Warnln("Failed to add nil key")
+ if key == "" || strings.Contains(key, "=") {
+ logrus.Warnf("Failed to add nil key or key containing =, key: %s", key)
continue
}
if keyInfo.Operation == "delete" {
diff --git a/cmd/agent/server/config_test.go b/cmd/agent/server/config_test.go
index 2deb15f..ae2a2cf 100644
--- a/cmd/agent/server/config_test.go
+++ b/cmd/agent/server/config_test.go
@@ -23,7 +23,6 @@ import (
"testing"
"github.com/agiledragon/gomonkey/v2"
-
agent "openeuler.org/KubeOS/cmd/agent/api"
)
@@ -105,17 +104,27 @@ func TestKerSysctlPersist_SetConfig(t *testing.T) {
wantErr bool
}{
{name: "create file", args: args{config: &agent.SysConfig{ConfigPath: persistPath}}, want: []string{comment}, wantErr: false},
+ {
+ name: "nil path",
+ args: args{
+ config: &agent.SysConfig{},
+ },
+ want: []string{},
+ wantErr: false,
+ },
{
name: "add configs",
args: args{
config: &agent.SysConfig{
ConfigPath: persistPath,
Contents: map[string]*agent.KeyInfo{
- "a": {Value: "1"},
- "b": {Value: "2"},
- "c": {Value: ""},
- "": {Value: "4"},
- "e": {Value: "5"},
+ "a": {Value: "1"},
+ "b": {Value: "2"},
+ "c": {Value: ""},
+ "": {Value: "4"},
+ "e": {Value: "5"},
+ "y=1": {Value: "26"},
+ "z": {Value: "x=1"},
},
},
},
@@ -123,6 +132,7 @@ func TestKerSysctlPersist_SetConfig(t *testing.T) {
"a=1",
"b=2",
"e=5",
+ "z=x=1",
},
wantErr: false,
},
@@ -134,6 +144,7 @@ func TestKerSysctlPersist_SetConfig(t *testing.T) {
Contents: map[string]*agent.KeyInfo{
"a": {Value: "2"},
"b": {Value: ""},
+ "z": {Value: "x=2"},
},
},
},
@@ -141,6 +152,7 @@ func TestKerSysctlPersist_SetConfig(t *testing.T) {
"a=2",
"b=2",
"e=5",
+ "z=x=2",
},
wantErr: false,
},
@@ -155,6 +167,7 @@ func TestKerSysctlPersist_SetConfig(t *testing.T) {
"c": {Value: "3", Operation: "delete"},
"e": {Value: "5", Operation: "remove"},
"f": {Value: "6", Operation: "remove"},
+ "z": {Value: "x=2", Operation: "delete"},
},
},
},
@@ -166,6 +179,8 @@ func TestKerSysctlPersist_SetConfig(t *testing.T) {
wantErr: false,
},
}
+ patchGetKernelConPath := gomonkey.ApplyFuncReturn(getKernelConPath, persistPath)
+ defer patchGetKernelConPath.Reset()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
k := KerSysctlPersist{}
@@ -467,3 +482,31 @@ func Test_getConfigPartition(t *testing.T) {
})
}
}
+
+func Test_ConfigFactoryTemplate(t *testing.T) {
+ type args struct {
+ configType string
+ config *agent.SysConfig
+ }
+ tests := []struct {
+ name string
+ args args
+ wantErr bool
+ }{
+ {
+ name: "error",
+ args: args{
+ configType: "test",
+ config: &agent.SysConfig{},
+ },
+ wantErr: true,
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ if err := ConfigFactoryTemplate(tt.args.configType, tt.args.config); (err != nil) != tt.wantErr {
+ t.Errorf("ConfigFactoryTemplate() error = %v, wantErr %v", err, tt.wantErr)
+ }
+ })
+ }
+}
--
2.39.0