github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/unstructured/viper_wrap_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 unstructured 21 22 import ( 23 "testing" 24 25 "github.com/stretchr/testify/assert" 26 27 appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1" 28 ) 29 30 func TestIniFormat(t *testing.T) { 31 const iniContext = ` 32 [client] 33 socket=/data/mysql/tmp/mysqld.sock 34 35 [mysqld] 36 gtid_mode=OFF 37 innodb-buffer-pool-size=512M 38 log_error=/data/mysql/log/mysqld.err 39 loose-caching_sha2_password_auto_generate_rsa_keys=0 40 port=3306 41 ` 42 43 iniConfigObj, err := LoadConfig("ini_test", iniContext, appsv1alpha1.Ini) 44 assert.Nil(t, err) 45 46 assert.EqualValues(t, iniConfigObj.Get("mysqld.gtid_mode"), "OFF") 47 assert.EqualValues(t, iniConfigObj.Get("mysqld.log_error"), "/data/mysql/log/mysqld.err") 48 assert.EqualValues(t, iniConfigObj.Get("client.socket"), "/data/mysql/tmp/mysqld.sock") 49 50 dumpContext, err := iniConfigObj.Marshal() 51 assert.Nil(t, err) 52 assert.EqualValues(t, dumpContext, iniContext[1:]) // trim "\n" 53 54 // test sub 55 subConfigObj := iniConfigObj.SubConfig("mysqld") 56 assert.Nil(t, subConfigObj.Update("gtid_mode", "ON")) 57 assert.EqualValues(t, subConfigObj.Get("gtid_mode"), "ON") 58 assert.EqualValues(t, subConfigObj.Get("log_error"), "/data/mysql/log/mysqld.err") 59 } 60 61 func TestPropertiesFormat(t *testing.T) { 62 const propertiesContext = ` 63 listen_addresses = '*' 64 port = '5432' 65 archive_command = '[[ $(date +%H%M) == 1200 ]] && rm -rf /home/postgres/pgdata/pgroot/arcwal/$(date -d"yesterday" +%Y%m%d); mkdir -p /home/postgres/pgdata/pgroot/arcwal/$(date +%Y%m%d) && gzip -kqc %p > /home/postgres/pgdata/pgroot/arcwal/$(date +%Y%m%d)/%f.gz' 66 67 #archive_mode = 'True' 68 auto_explain.log_analyze = 'True' 69 auto_explain.log_min_duration = '1s' 70 auto_explain.log_nested_statements = 'True' 71 auto_explain.log_timing = 'True' 72 auto_explain.log_verbose = 'True' 73 autovacuum_analyze_scale_factor = '0.05' 74 autovacuum_freeze_max_age = '100000000' 75 autovacuum_max_workers = '1' 76 autovacuum_naptime = '1min' 77 ` 78 propConfigObj, err := LoadConfig("prop_test", propertiesContext, appsv1alpha1.Properties) 79 assert.Nil(t, err) 80 81 assert.EqualValues(t, propConfigObj.Get("auto_explain.log_nested_statements"), "'True'") 82 assert.EqualValues(t, propConfigObj.Get("auto_explain.log_min_duration"), "'1s'") 83 assert.EqualValues(t, propConfigObj.Get("autovacuum_naptime"), "'1min'") 84 assert.EqualValues(t, propConfigObj.Get("archive_command"), `'[[ $(date +%H%M) == 1200 ]] && rm -rf /home/postgres/pgdata/pgroot/arcwal/$(date -d"yesterday" +%Y%m%d); mkdir -p /home/postgres/pgdata/pgroot/arcwal/$(date +%Y%m%d) && gzip -kqc %p > /home/postgres/pgdata/pgroot/arcwal/$(date +%Y%m%d)/%f.gz'`) 85 86 dumpContext, err := propConfigObj.Marshal() 87 assert.Nil(t, err) 88 assert.EqualValues(t, dumpContext, propertiesContext[1:]) // trim "\n" 89 90 assert.Nil(t, propConfigObj.Update("autovacuum_naptime", "'6min'")) 91 assert.EqualValues(t, propConfigObj.Get("autovacuum_naptime"), "'6min'") 92 } 93 94 func TestJSONFormat(t *testing.T) { 95 const jsonContext = ` 96 { 97 "id": "0001", 98 "name": "zhangsan", 99 "score": { 100 "chemistry": "80", 101 "literature": "78", 102 "math": 98 103 }, 104 "toplevel": 10, 105 "type": "student" 106 }` 107 108 jsonConfigObj, err := LoadConfig("json_test", jsonContext, appsv1alpha1.JSON) 109 assert.Nil(t, err) 110 111 assert.EqualValues(t, jsonConfigObj.Get("id"), "0001") 112 assert.EqualValues(t, jsonConfigObj.Get("score.chemistry"), "80") 113 114 dumpContext, err := jsonConfigObj.Marshal() 115 assert.Nil(t, err) 116 assert.EqualValues(t, dumpContext, jsonContext[1:]) // trim "\n" 117 118 assert.Nil(t, jsonConfigObj.Update("abcd", "test")) 119 assert.EqualValues(t, jsonConfigObj.Get("abcd"), "test") 120 121 assert.Nil(t, jsonConfigObj.Update("name", "test")) 122 assert.EqualValues(t, jsonConfigObj.Get("name"), "test") 123 124 }