github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/unstructured/yaml_config_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 "github.com/1aal/kubeblocks/pkg/configuration/util" 29 "github.com/1aal/kubeblocks/test/testdata" 30 ) 31 32 func TestYAMLFormat(t *testing.T) { 33 const yamlContext = ` 34 spec: 35 clusterRef: pg 36 reconfigure: 37 componentName: postgresql 38 configurations: 39 - keys: 40 - key: postgresql.conf 41 parameters: 42 - key: max_connections 43 value: "2666" 44 name: postgresql-configuration 45 ` 46 47 yamlConfigObj, err := LoadConfig("yaml_test", yamlContext, appsv1alpha1.YAML) 48 assert.Nil(t, err) 49 50 assert.EqualValues(t, yamlConfigObj.Get("spec.clusterRef"), "pg") 51 assert.EqualValues(t, yamlConfigObj.Get("spec.reconfigure.componentName"), "postgresql") 52 53 dumpContext, err := yamlConfigObj.Marshal() 54 assert.Nil(t, err) 55 assert.EqualValues(t, dumpContext, yamlContext[1:]) // trim "\n" 56 57 assert.Nil(t, yamlConfigObj.Update("spec.my_test", "100")) 58 assert.EqualValues(t, yamlConfigObj.Get("spec.my_test"), "100") 59 assert.Nil(t, yamlConfigObj.RemoveKey("spec.my_test.xxx")) 60 assert.Nil(t, yamlConfigObj.RemoveKey("spec.my_test")) 61 assert.EqualValues(t, yamlConfigObj.Get("spec.my_test"), nil) 62 } 63 64 func TestYAMLFormatForBadCase(t *testing.T) { 65 b, err := testdata.GetTestDataFileContent("config_encoding/prometheus.yaml") 66 assert.Nil(t, err) 67 68 yamlConfigObj, err := LoadConfig("yaml_test", string(b), appsv1alpha1.YAML) 69 assert.Nil(t, err) 70 assert.NotNil(t, yamlConfigObj) 71 yamlConfigObj.GetAllParameters() 72 _, err = util.JSONPatch(nil, yamlConfigObj.GetAllParameters()) 73 assert.Nil(t, err) 74 } 75 76 func TestConvert(t *testing.T) { 77 tests := []struct { 78 name string 79 args any 80 want any 81 }{{ 82 name: "test", 83 args: 10, 84 want: 10, 85 }, { 86 name: "test", 87 args: map[string]interface{}{ 88 "key": "value", 89 "test2": 100, 90 }, 91 want: map[string]interface{}{ 92 "key": "value", 93 "test2": 100, 94 }, 95 }, { 96 name: "test", 97 args: []interface{}{ 98 "key", 99 "value", 100 }, 101 want: []interface{}{ 102 "key", 103 "value", 104 }, 105 }, { 106 name: "test", 107 args: map[interface{}]interface{}{ 108 1: "value", 109 2: 200, 110 }, 111 want: map[string]interface{}{ 112 "1": "value", 113 "2": 200, 114 }, 115 }} 116 for _, tt := range tests { 117 t.Run(tt.name, func(t *testing.T) { 118 assert.Equalf(t, tt.want, convert(tt.args), "convert(%v)", tt.args) 119 }) 120 } 121 }