forked from apecloud/kubeblocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyaml_config_test.go
121 lines (105 loc) · 3.04 KB
/
yaml_config_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
Copyright (C) 2022-2023 ApeCloud Co., Ltd
This file is part of KubeBlocks project
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package unstructured
import (
"testing"
"github.com/stretchr/testify/assert"
appsv1alpha1 "github.com/apecloud/kubeblocks/apis/apps/v1alpha1"
"github.com/apecloud/kubeblocks/internal/configuration/util"
"github.com/apecloud/kubeblocks/test/testdata"
)
func TestYAMLFormat(t *testing.T) {
const yamlContext = `
spec:
clusterRef: pg
reconfigure:
componentName: postgresql
configurations:
- keys:
- key: postgresql.conf
parameters:
- key: max_connections
value: "2666"
name: postgresql-configuration
`
yamlConfigObj, err := LoadConfig("yaml_test", yamlContext, appsv1alpha1.YAML)
assert.Nil(t, err)
assert.EqualValues(t, yamlConfigObj.Get("spec.clusterRef"), "pg")
assert.EqualValues(t, yamlConfigObj.Get("spec.reconfigure.componentName"), "postgresql")
dumpContext, err := yamlConfigObj.Marshal()
assert.Nil(t, err)
assert.EqualValues(t, dumpContext, yamlContext[1:]) // trim "\n"
assert.Nil(t, yamlConfigObj.Update("spec.my_test", "100"))
assert.EqualValues(t, yamlConfigObj.Get("spec.my_test"), "100")
assert.Nil(t, yamlConfigObj.RemoveKey("spec.my_test.xxx"))
assert.Nil(t, yamlConfigObj.RemoveKey("spec.my_test"))
assert.EqualValues(t, yamlConfigObj.Get("spec.my_test"), nil)
}
func TestYAMLFormatForBadCase(t *testing.T) {
b, err := testdata.GetTestDataFileContent("config_encoding/prometheus.yaml")
assert.Nil(t, err)
yamlConfigObj, err := LoadConfig("yaml_test", string(b), appsv1alpha1.YAML)
assert.Nil(t, err)
assert.NotNil(t, yamlConfigObj)
yamlConfigObj.GetAllParameters()
_, err = util.JSONPatch(nil, yamlConfigObj.GetAllParameters())
assert.Nil(t, err)
}
func TestConvert(t *testing.T) {
tests := []struct {
name string
args any
want any
}{{
name: "test",
args: 10,
want: 10,
}, {
name: "test",
args: map[string]interface{}{
"key": "value",
"test2": 100,
},
want: map[string]interface{}{
"key": "value",
"test2": 100,
},
}, {
name: "test",
args: []interface{}{
"key",
"value",
},
want: []interface{}{
"key",
"value",
},
}, {
name: "test",
args: map[interface{}]interface{}{
1: "value",
2: 200,
},
want: map[string]interface{}{
"1": "value",
"2": 200,
},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, convert(tt.args), "convert(%v)", tt.args)
})
}
}