github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/configuration/core/config_patch_test.go (about) 1 /* 2 Copyright (C) 2022-2023 ApeCloud Co., Ltd 3 4 This file is part of KubeBlocks project 5 6 This program is free software: you can redistribute it and/or modify 7 it under the terms of the GNU Affero General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU Affero General Public License for more details. 15 16 You should have received a copy of the GNU Affero General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package core 21 22 import ( 23 "context" 24 "encoding/json" 25 "testing" 26 27 "github.com/ghodss/yaml" 28 "github.com/stretchr/testify/require" 29 "sigs.k8s.io/controller-runtime/pkg/log" 30 31 appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1" 32 "github.com/1aal/kubeblocks/pkg/configuration/util" 33 ) 34 35 func TestConfigPatch(t *testing.T) { 36 37 cfg, err := NewConfigLoader(CfgOption{ 38 Type: CfgRawType, 39 Log: log.FromContext(context.Background()), 40 CfgType: appsv1alpha1.Ini, 41 RawData: []byte(iniConfig), 42 }) 43 44 if err != nil { 45 t.Fatalf("new config loader failed [%v]", err) 46 } 47 48 ctx := NewCfgOptions("", 49 func(ctx *CfgOpOption) { 50 // filter mysqld 51 ctx.IniContext = &IniContext{ 52 SectionName: "mysqld", 53 } 54 }) 55 56 // ctx := NewCfgOptions("$..slow_query_log_file", "") 57 58 result, err := cfg.Query("$..slow_query_log_file", NewCfgOptions("")) 59 require.Nil(t, err) 60 require.NotNil(t, result) 61 require.Equal(t, "[\"/data/mysql/mysqld-slow.log\"]", string(result)) 62 63 require.Nil(t, 64 cfg.MergeFrom(map[string]interface{}{ 65 "slow_query_log": 1, 66 "server-id": 2, 67 "socket": "xxxxxxxxxxxxxxx", 68 }, ctx)) 69 70 content, err := cfg.ToCfgContent() 71 require.NotNil(t, content) 72 require.Nil(t, err) 73 74 newContent, exist := content[cfg.name] 75 require.True(t, exist) 76 patch, err := CreateMergePatch([]byte(iniConfig), []byte(newContent), cfg.Option) 77 require.Nil(t, err) 78 log.Log.Info("patch : %v", patch) 79 require.True(t, patch.IsModify) 80 require.Equal(t, string(patch.UpdateConfig["raw"]), `{"mysqld":{"server-id":"2","socket":"xxxxxxxxxxxxxxx"}}`) 81 82 { 83 require.Nil(t, 84 cfg.MergeFrom(map[string]interface{}{ 85 "server-id": 1, 86 "socket": "/data/mysql/tmp/mysqld.sock", 87 }, ctx)) 88 content, err := cfg.ToCfgContent() 89 require.Nil(t, err) 90 newContent := content[cfg.name] 91 // CreateMergePatch([]byte(iniConfig), []byte(newContent), cfg.Option) 92 patch, err := CreateMergePatch([]byte(iniConfig), []byte(newContent), cfg.Option) 93 require.Nil(t, err) 94 log.Log.Info("patch : %v", patch) 95 require.False(t, patch.IsModify) 96 } 97 } 98 99 func TestYamlConfigPatch(t *testing.T) { 100 yamlContext := ` 101 net: 102 port: 2000 103 bindIp: 104 type: "string" 105 trim: "whitespace" 106 tls: 107 mode: requireTLS 108 certificateKeyFilePassword: 109 type: "string" 110 digest: b08519162ba332985ac18204851949611ef73835ec99067b85723e10113f5c26 111 digest_key: 6d795365637265744b65795374756666 112 ` 113 114 patchOption := CfgOption{ 115 Type: CfgTplType, 116 CfgType: appsv1alpha1.YAML, 117 } 118 patch, err := CreateMergePatch(&ConfigResource{ConfigData: map[string]string{"test": ""}}, &ConfigResource{ConfigData: map[string]string{"test": yamlContext}}, patchOption) 119 require.Nil(t, err) 120 121 yb, err := yaml.YAMLToJSON([]byte(yamlContext)) 122 require.Nil(t, err) 123 124 require.Nil(t, err) 125 require.Equal(t, yb, patch.UpdateConfig["test"]) 126 } 127 128 func TestTransformConfigPatchFromData(t *testing.T) { 129 configFile := "my.cnf" 130 testData := "[mysqld]\nmax_connections = 2000\ngeneral_log = OFF" 131 132 t.Run("testConfigPatch", func(t *testing.T) { 133 got, err := TransformConfigPatchFromData(map[string]string{configFile: testData}, appsv1alpha1.Ini, nil) 134 require.Nil(t, err) 135 require.True(t, got.IsModify) 136 require.NotNil(t, got.UpdateConfig[configFile]) 137 138 var r any 139 require.Nil(t, json.Unmarshal(got.UpdateConfig[configFile], &r)) 140 maxConnections, _ := util.RetrievalWithJSONPath(r, "$.mysqld.max_connections") 141 generalLog, _ := util.RetrievalWithJSONPath(r, "$.mysqld.general_log") 142 require.EqualValues(t, string(maxConnections), "2000") 143 require.EqualValues(t, string(generalLog), "OFF") 144 }) 145 }