15
15
* specific language governing permissions and limitations under the License.
16
16
*/
17
17
18
- package config
18
+ package config_test
19
19
20
20
import (
21
- "context"
22
- "errors"
23
- "fmt"
24
- "os"
25
- "testing"
26
21
"time"
27
22
28
- "github.com/boltdb/bolt"
29
23
"github.com/google/uuid"
30
24
apiconfig "github.com/polarismesh/specification/source/go/api/v1/config_manage"
31
- "gopkg.in/yaml.v2"
32
25
33
26
"github.com/polarismesh/polaris/auth"
34
27
_ "github.com/polarismesh/polaris/auth/defaultauth"
35
28
"github.com/polarismesh/polaris/cache"
36
29
_ "github.com/polarismesh/polaris/cache"
37
30
commonlog "github.com/polarismesh/polaris/common/log"
38
31
"github.com/polarismesh/polaris/common/utils"
32
+ "github.com/polarismesh/polaris/config"
39
33
"github.com/polarismesh/polaris/namespace"
40
34
"github.com/polarismesh/polaris/plugin"
41
35
_ "github.com/polarismesh/polaris/plugin/crypto/aes"
@@ -44,11 +38,9 @@ import (
44
38
_ "github.com/polarismesh/polaris/plugin/history/logger"
45
39
_ "github.com/polarismesh/polaris/plugin/password"
46
40
"github.com/polarismesh/polaris/store"
47
- "github.com/polarismesh/polaris/store/boltdb"
48
41
_ "github.com/polarismesh/polaris/store/boltdb"
49
42
_ "github.com/polarismesh/polaris/store/mysql"
50
- sqldb "github.com/polarismesh/polaris/store/mysql"
51
- testdata "github.com/polarismesh/polaris/test/data"
43
+ testsuit "github.com/polarismesh/polaris/test/suit"
52
44
)
53
45
54
46
type Bootstrap struct {
@@ -59,222 +51,22 @@ type TestConfig struct {
59
51
Bootstrap Bootstrap `yaml:"bootstrap"`
60
52
Cache cache.Config `yaml:"cache"`
61
53
Namespace namespace.Config `yaml:"namespace"`
62
- Config Config `yaml:"config"`
54
+ Config config. Config `yaml:"config"`
63
55
Store store.Config `yaml:"store"`
64
56
Auth auth.Config `yaml:"auth"`
65
57
Plugin plugin.Config `yaml:"plugin"`
66
58
}
67
59
68
60
type ConfigCenterTest struct {
69
- cfg * TestConfig
70
- testService ConfigCenterServer
71
- testServer * Server
72
- defaultCtx context.Context
73
- cancel context.CancelFunc
74
- storage store.Store
75
- }
76
-
77
- func newConfigCenterTest (t * testing.T ) (* ConfigCenterTest , error ) {
78
- if err := os .RemoveAll ("./config_center_test.bolt" ); err != nil {
79
- return nil , err
80
- }
81
-
82
- c := & ConfigCenterTest {
83
- defaultCtx : context .Background (),
84
- testServer : new (Server ),
85
- cfg : new (TestConfig ),
86
- }
87
-
88
- if err := c .doInitialize (); err != nil {
89
- fmt .Printf ("bootstrap config test module error. %s" , err .Error ())
90
- return nil , err
91
- }
92
-
93
- return c , nil
94
- }
95
-
96
- func (c * ConfigCenterTest ) doInitialize () error {
97
- // 加载启动配置文件
98
- if err := c .loadBootstrapConfig (); err != nil {
99
- return err
100
- }
101
- _ = commonlog .Configure (c .cfg .Bootstrap .Logger )
102
- ctx , cancel := context .WithCancel (context .Background ())
103
- c .cancel = cancel
104
- plugin .SetPluginConfig (& c .cfg .Plugin )
105
-
106
- // 初始化存储层
107
- store .SetStoreConfig (& c .cfg .Store )
108
- s , err := store .TestGetStore ()
109
- if err != nil {
110
- fmt .Printf ("[ERROR] configure get store fail: %v\n " , err )
111
- return err
112
- }
113
- c .storage = s
114
-
115
- cacheMgr , err := cache .TestCacheInitialize (ctx , & c .cfg .Cache , s )
116
- if err != nil {
117
- fmt .Printf ("[ERROR] configure init cache fail: %v\n " , err )
118
- return err
119
- }
120
-
121
- userMgn , strategyMgn , err := auth .TestInitialize (ctx , & c .cfg .Auth , s , cacheMgr )
122
- if err != nil {
123
- fmt .Printf ("[ERROR] configure init auth fail: %v\n " , err )
124
- return err
125
- }
126
-
127
- nsOp , err := namespace .TestInitialize (ctx , & c .cfg .Namespace , s , cacheMgr , userMgn , strategyMgn )
128
- if err != nil {
129
- fmt .Printf ("[ERROR] configure init namespace fail: %v\n " , err )
130
- return err
131
- }
132
-
133
- // 初始化配置中心模块
134
- if err := c .testServer .initialize (ctx , c .cfg .Config , s , nsOp , cacheMgr ); err != nil {
135
- return err
136
- }
137
- c .testServer .initialized = true
138
- c .testService = newServerAuthAbility (c .testServer , userMgn , strategyMgn )
139
-
140
- time .Sleep (5 * time .Second )
141
-
142
- return nil
143
- }
144
-
145
- func (c * ConfigCenterTest ) loadBootstrapConfig () error {
146
- confFileName := testdata .Path ("config_test.yaml" )
147
-
148
- // 初始化defaultCtx
149
- c .defaultCtx = context .WithValue (c .defaultCtx , utils .StringContext ("request-id" ), "config-test-request-id" )
150
- c .defaultCtx = context .WithValue (c .defaultCtx , utils .ContextUserNameKey , "polaris" )
151
-
152
- if os .Getenv ("STORE_MODE" ) == "sqldb" {
153
- fmt .Printf ("run store mode : sqldb\n " )
154
- confFileName = testdata .Path ("config_test_sqldb.yaml" )
155
- c .defaultCtx = context .WithValue (c .defaultCtx , utils .ContextAuthTokenKey , "nu/0WRA4EqSR1FagrjRj0fZwPXuGlMpX+zCuWu4uMqy8xr1vRjisSbA25aAC3mtU8MeeRsKhQiDAynUR09I=" )
156
- } else {
157
- c .defaultCtx = context .WithValue (c .defaultCtx , utils .ContextAuthTokenKey , "nu/0WRA4EqSR1FagrjRj0fZwPXuGlMpX+zCuWu4uMqy8xr1vRjisSbA25aAC3mtU8MeeRsKhQiDAynUR09I=" )
158
- }
159
-
160
- file , err := os .Open (confFileName )
161
- if err != nil {
162
- fmt .Printf ("[ERROR] %v\n " , err )
163
- return err
164
- }
165
-
166
- err = yaml .NewDecoder (file ).Decode (c .cfg )
167
- if err != nil {
168
- fmt .Printf ("[ERROR] %v\n " , err )
169
- return err
170
- }
171
-
172
- return err
61
+ testsuit.DiscoverTestSuit
62
+ cfg * TestConfig
173
63
}
174
64
175
65
func (c * ConfigCenterTest ) clearTestData () error {
176
66
defer func () {
177
- c .cancel ()
178
- time .Sleep (5 * time .Second )
179
-
180
- c .storage .Destroy ()
181
67
time .Sleep (5 * time .Second )
182
68
}()
183
-
184
- if c .storage .Name () == sqldb .STORENAME {
185
- if err := c .clearTestDataWhenUseRDS (); err != nil {
186
- return err
187
- }
188
- } else if c .storage .Name () == boltdb .STORENAME {
189
- if err := c .clearTestDataWhenUseBoltdb (); err != nil {
190
- return err
191
- }
192
- } else {
193
- return errors .New ("store impl unexpect" )
194
- }
195
-
196
- return nil
197
- }
198
-
199
- func (c * ConfigCenterTest ) clearTestDataWhenUseBoltdb () error {
200
-
201
- proxyTx , err := c .storage .StartTx ()
202
- if err != nil {
203
- return err
204
- }
205
-
206
- tx := proxyTx .GetDelegateTx ().(* bolt.Tx )
207
-
208
- bucketName := []string {
209
- "ConfigFileGroup" ,
210
- "ConfigFileGroupID" ,
211
- "ConfigFile" ,
212
- "ConfigFileID" ,
213
- "ConfigFileReleaseHistory" ,
214
- "ConfigFileReleaseHistoryID" ,
215
- "ConfigFileRelease" ,
216
- "ConfigFileReleaseID" ,
217
- "ConfigFileTag" ,
218
- "ConfigFileTagID" ,
219
- "namespace" ,
220
- }
221
-
222
- defer tx .Rollback ()
223
-
224
- for i := range bucketName {
225
- if err := tx .DeleteBucket ([]byte (bucketName [i ])); err != nil {
226
- if ! errors .Is (err , bolt .ErrBucketNotFound ) {
227
- return err
228
- }
229
- }
230
- }
231
-
232
- return tx .Commit ()
233
- }
234
-
235
- func (c * ConfigCenterTest ) clearTestDataWhenUseRDS () error {
236
-
237
- proxyTx , err := c .storage .StartTx ()
238
- if err != nil {
239
- return err
240
- }
241
-
242
- tx := proxyTx .GetDelegateTx ().(* sqldb.BaseTx )
243
-
244
- defer tx .Rollback ()
245
-
246
- _ , err = tx .Exec ("delete from config_file_group where namespace = ? " , testNamespace )
247
- if err != nil {
248
- return err
249
- }
250
- _ , err = tx .Exec ("delete from config_file where namespace = ? " , testNamespace )
251
- if err != nil {
252
- return err
253
- }
254
- _ , err = tx .Exec ("delete from config_file_release where namespace = ? " , testNamespace )
255
- if err != nil {
256
- return err
257
- }
258
- _ , err = tx .Exec ("delete from config_file_release_history where namespace = ? " , testNamespace )
259
- if err != nil {
260
- return err
261
- }
262
- _ , err = tx .Exec ("delete from config_file_tag where namespace = ? " , testNamespace )
263
- if err != nil {
264
- return err
265
- }
266
- _ , err = tx .Exec ("delete from namespace where name = ? " , testNamespace )
267
- if err != nil {
268
- return err
269
- }
270
- _ , err = tx .Exec ("delete from config_file_template where name in (?,?) " , templateName1 , templateName2 )
271
- if err != nil {
272
- return err
273
- }
274
- // 清理缓存
275
- c .testServer .Cache ().CleanAll ()
276
-
277
- return tx .Commit ()
69
+ return c .GetTestDataClean ().ClearTestDataWhenUseRDS ()
278
70
}
279
71
280
72
func randomStr () string {
0 commit comments