github.com/polarismesh/polaris@v1.17.8/store/boltdb/handler_test.go (about) 1 /** 2 * Tencent is pleased to support the open source community by making Polaris available. 3 * 4 * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. 5 * 6 * Licensed under the BSD 3-Clause License (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * https://opensource.org/licenses/BSD-3-Clause 11 * 12 * Unless required by applicable law or agreed to in writing, software distributed 13 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 14 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 15 * specific language governing permissions and limitations under the License. 16 */ 17 18 package boltdb 19 20 import ( 21 "fmt" 22 "io/ioutil" 23 "os" 24 "path/filepath" 25 "strconv" 26 "testing" 27 "time" 28 29 "github.com/golang/protobuf/ptypes/wrappers" 30 apimodel "github.com/polarismesh/specification/source/go/api/v1/model" 31 32 "github.com/polarismesh/polaris/common/model" 33 ) 34 35 func CreateTableDBHandlerAndRun(t *testing.T, tableName string, tf func(t *testing.T, handler BoltHandler)) { 36 tempDir, _ := ioutil.TempDir("", tableName) 37 t.Logf("temp dir : %s", tempDir) 38 _ = os.Remove(filepath.Join(tempDir, fmt.Sprintf("%s.bolt", tableName))) 39 handler, err := NewBoltHandler(&BoltConfig{FileName: filepath.Join(tempDir, fmt.Sprintf("%s.bolt", tableName))}) 40 if err != nil { 41 t.Fatal(err) 42 } 43 44 defer func() { 45 _ = handler.Close() 46 _ = os.Remove(filepath.Join(tempDir, fmt.Sprintf("%s.bolt", tableName))) 47 }() 48 tf(t, handler) 49 } 50 51 func TestBoltHandler_SaveNamespace(t *testing.T) { 52 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 53 if err != nil { 54 t.Fatal(err) 55 } 56 defer handler.Close() 57 nsValue := &model.Namespace{ 58 Name: "Test", 59 Comment: "test ns", 60 Token: "111111", 61 Owner: "user1", 62 Valid: true, 63 CreateTime: time.Now(), 64 ModifyTime: time.Now(), 65 } 66 err = handler.SaveValue(tblNameNamespace, nsValue.Name, nsValue) 67 if err != nil { 68 t.Fatal(err) 69 } 70 } 71 72 func TestBoltHandler_LoadNamespace(t *testing.T) { 73 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 74 if err != nil { 75 t.Fatal(err) 76 } 77 defer handler.Close() 78 nsValue := &model.Namespace{ 79 Name: "Test", 80 Comment: "test ns", 81 Token: "111111", 82 Owner: "user1", 83 Valid: true, 84 CreateTime: time.Now(), 85 ModifyTime: time.Now(), 86 } 87 nsValues, err := handler.LoadValues(tblNameNamespace, []string{nsValue.Name}, &model.Namespace{}) 88 if err != nil { 89 t.Fatal(err) 90 } 91 targetNsValue := nsValues[nsValue.Name] 92 targetNs := targetNsValue.(*model.Namespace) 93 fmt.Printf("loaded ns is %+v\n", targetNs) 94 if nsValue.Name != targetNs.Name { 95 fmt.Printf("name not equals\n") 96 } 97 } 98 99 func TestBoltHandler_DeleteNamespace(t *testing.T) { 100 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 101 if err != nil { 102 t.Fatal(err) 103 } 104 defer handler.Close() 105 nsValue := &model.Namespace{ 106 Name: "Test", 107 } 108 err = handler.DeleteValues(tblNameNamespace, []string{nsValue.Name}) 109 if err != nil { 110 t.Fatal(err) 111 } 112 } 113 114 func TestBoltHandler_Service(t *testing.T) { 115 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 116 if err != nil { 117 t.Fatal(err) 118 } 119 defer handler.Close() 120 svcValue := &model.Service{ 121 ID: "idSvc1234", 122 Namespace: "Test", 123 Name: "TestSvc", 124 Comment: "test svc", 125 Token: "111111", 126 Owner: "user1", 127 Valid: true, 128 CreateTime: time.Now(), 129 ModifyTime: time.Now(), 130 Meta: map[string]string{"k1": "v1", "k2": "v2", "k3": "v3"}, 131 } 132 err = handler.SaveValue("service", svcValue.ID, svcValue) 133 if err != nil { 134 t.Fatal(err) 135 } 136 nsValues, err := handler.LoadValues("service", []string{svcValue.ID}, &model.Service{}) 137 if err != nil { 138 t.Fatal(err) 139 } 140 targetSvcValue := nsValues[svcValue.ID] 141 targetSvc := targetSvcValue.(*model.Service) 142 fmt.Printf("loaded svc is %+v\n", targetSvc) 143 if svcValue.Name != targetSvc.Name || len(svcValue.Meta) != len(targetSvc.Meta) { 144 fmt.Printf("name not equals\n") 145 } 146 fmt.Printf("trget meta is %v\n", targetSvc.Meta) 147 148 _, _ = handler.LoadValuesByFilter("service", []string{"Meta"}, &model.Service{}, func(m map[string]interface{}) bool { 149 values := m["Meta"] 150 fmt.Printf("values are %v\n", values) 151 return true 152 }) 153 154 err = handler.DeleteValues("service", []string{svcValue.ID}) 155 if err != nil { 156 t.Fatal(err) 157 } 158 } 159 160 func TestBoltHandler_Location(t *testing.T) { 161 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 162 if err != nil { 163 t.Fatal(err) 164 } 165 defer handler.Close() 166 id := "12345" 167 locValue := &model.Location{ 168 Proto: &apimodel.Location{ 169 Region: &wrappers.StringValue{Value: "huabei"}, 170 Zone: &wrappers.StringValue{Value: "shenzhen"}, 171 Campus: &wrappers.StringValue{Value: "longgang1"}, 172 }, 173 RegionID: 111, 174 ZoneID: 1112, 175 CampusID: 1113, 176 Valid: true, 177 } 178 err = handler.SaveValue("location", id, locValue) 179 if err != nil { 180 t.Fatal(err) 181 } 182 locValues, err := handler.LoadValues("location", []string{id}, &model.Location{}) 183 if err != nil { 184 t.Fatal(err) 185 } 186 targetLocValue := locValues[id] 187 targetLoc := targetLocValue.(*model.Location) 188 fmt.Printf("loaded loc is %+v\n", targetLoc) 189 err = handler.DeleteValues("location", []string{id}) 190 if err != nil { 191 t.Fatal(err) 192 } 193 } 194 195 const ( 196 tblService = "service" 197 ) 198 199 func TestBoltHandler_CountValues(t *testing.T) { 200 201 // 删除之前测试的遗留文件 202 _ = os.RemoveAll("./table.bolt") 203 204 count := 5 205 var idToServices = make(map[string]*model.Service) 206 var ids = make([]string, 0) 207 for i := 0; i < count; i++ { 208 svcValue := &model.Service{ 209 ID: "idSvcCount" + strconv.Itoa(i), 210 Namespace: "Test", 211 Name: "TestSvc" + strconv.Itoa(i), 212 Comment: "test svc", 213 Token: "111111", 214 Owner: "user1", 215 Valid: true, 216 CreateTime: time.Now(), 217 ModifyTime: time.Now(), 218 Meta: map[string]string{"k1": "v1", "k2": "v2", "k3": "v3"}, 219 } 220 idToServices[svcValue.ID] = svcValue 221 ids = append(ids, svcValue.ID) 222 } 223 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 224 if err != nil { 225 t.Fatal(err) 226 } 227 defer func() { 228 handler.Close() 229 _ = os.RemoveAll("./table.bolt") 230 }() 231 for id, svc := range idToServices { 232 err = handler.SaveValue(tblService, id, svc) 233 if err != nil { 234 t.Fatal(err) 235 } 236 } 237 services, err := handler.LoadValues(tblService, ids, &model.Service{}) 238 if err != nil { 239 t.Fatal(err) 240 } 241 if len(services) != count { 242 t.Fatal("load value count not match") 243 } 244 nCount, err := handler.CountValues(tblService) 245 if err != nil { 246 t.Fatal(err) 247 } 248 if nCount != count { 249 t.Fatalf("count not match, expect cnt=%d, actual cnt=%d", count, nCount) 250 } 251 252 properties := make(map[string]interface{}) 253 properties[svcFieldValid] = false 254 err = handler.UpdateValue(tblService, "idSvcCount1", properties) 255 if err != nil { 256 t.Fatal(err) 257 } 258 259 nCount, err = handler.CountValues(tblService) 260 if err != nil { 261 t.Fatal(err) 262 } 263 if nCount != count-1 { 264 t.Fatalf("count not match, expect cnt=%d, actual cnt=%d", count-1, nCount) 265 } 266 267 err = handler.DeleteValues("service", ids) 268 if err != nil { 269 t.Fatal(err) 270 } 271 272 } 273 274 func TestBoltHandler_LoadValuesByFilter(t *testing.T) { 275 count := 5 276 var idToServices = make(map[string]*model.Service) 277 var ids = make([]string, 0) 278 for i := 0; i < count; i++ { 279 svcValue := &model.Service{ 280 ID: "idSvcCount" + strconv.Itoa(i), 281 Namespace: "Test", 282 Name: "TestSvc" + strconv.Itoa(i), 283 Comment: "test svc", 284 Token: "111111", 285 Owner: "user" + strconv.Itoa(i), 286 Valid: true, 287 CreateTime: time.Now(), 288 ModifyTime: time.Now(), 289 Meta: map[string]string{"k1": "v1", "k2": "v2", "k3": "v3"}, 290 } 291 idToServices[svcValue.ID] = svcValue 292 ids = append(ids, svcValue.ID) 293 } 294 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 295 if err != nil { 296 t.Fatal(err) 297 } 298 defer func() { 299 handler.Close() 300 _ = os.RemoveAll("./table.bolt") 301 }() 302 for id, svc := range idToServices { 303 err = handler.SaveValue(tblService, id, svc) 304 if err != nil { 305 t.Fatal(err) 306 } 307 } 308 values, err := handler.LoadValuesByFilter(tblService, []string{"Owner"}, 309 &model.Service{}, func(props map[string]interface{}) bool { 310 owner := props["Owner"].(string) 311 return owner == "user1" || owner == "user2" 312 }) 313 if err != nil { 314 t.Fatal(err) 315 } 316 if len(values) != 2 { 317 t.Fatal("filter count not match 2") 318 } 319 err = handler.DeleteValues("service", ids) 320 if err != nil { 321 t.Fatal(err) 322 } 323 } 324 325 func TestBoltHandler_IterateFields(t *testing.T) { 326 count := 5 327 var idToServices = make(map[string]*model.Service) 328 var ids = make([]string, 0) 329 for i := 0; i < count; i++ { 330 svcValue := &model.Service{ 331 ID: "idSvcCount" + strconv.Itoa(i), 332 Namespace: "Test", 333 Name: "TestSvc" + strconv.Itoa(i), 334 Comment: "test svc", 335 Token: "111111", 336 Owner: "user" + strconv.Itoa(i), 337 Valid: true, 338 CreateTime: time.Now(), 339 ModifyTime: time.Now(), 340 Meta: map[string]string{"k1": "v1", "k2": "v2", "k3": "v3"}, 341 } 342 idToServices[svcValue.ID] = svcValue 343 ids = append(ids, svcValue.ID) 344 } 345 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 346 if err != nil { 347 t.Fatal(err) 348 } 349 defer func() { 350 handler.Close() 351 _ = os.RemoveAll("./table.bolt") 352 }() 353 for id, svc := range idToServices { 354 err = handler.SaveValue(tblService, id, svc) 355 if err != nil { 356 t.Fatal(err) 357 } 358 } 359 names := make([]string, 0) 360 err = handler.IterateFields(tblService, "Name", &model.Service{}, func(value interface{}) { 361 names = append(names, value.(string)) 362 }) 363 if err != nil { 364 t.Fatal(err) 365 } 366 if len(names) != count { 367 t.Fatalf("iterate count not match %d", count) 368 } 369 err = handler.DeleteValues("service", ids) 370 if err != nil { 371 t.Fatal(err) 372 } 373 } 374 375 func TestBoltHandler_UpdateValue(t *testing.T) { 376 count := 5 377 var idToServices = make(map[string]*model.Service) 378 var ids = make([]string, 0) 379 for i := 0; i < count; i++ { 380 svcValue := &model.Service{ 381 ID: "idSvcCount" + strconv.Itoa(i), 382 Namespace: "Test", 383 Name: "TestSvc" + strconv.Itoa(i), 384 Comment: "test svc", 385 Token: "111111", 386 Owner: "user" + strconv.Itoa(i), 387 Valid: true, 388 CreateTime: time.Now(), 389 ModifyTime: time.Now(), 390 Meta: map[string]string{"k1": "v1", "k2": "v2", "k3": "v3"}, 391 } 392 idToServices[svcValue.ID] = svcValue 393 ids = append(ids, svcValue.ID) 394 } 395 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 396 if err != nil { 397 t.Fatal(err) 398 } 399 defer func() { 400 handler.Close() 401 _ = os.RemoveAll("./table.bolt") 402 }() 403 for id, svc := range idToServices { 404 err = handler.SaveValue(tblService, id, svc) 405 if err != nil { 406 t.Fatal(err) 407 } 408 } 409 targetId := ids[0] 410 afterComment := "comment1" 411 err = handler.UpdateValue(tblService, targetId, map[string]interface{}{ 412 "Comment": afterComment, 413 }) 414 if err != nil { 415 t.Fatal(err) 416 } 417 418 values, err := handler.LoadValues(tblService, []string{targetId}, &model.Service{}) 419 if err != nil { 420 t.Fatal(err) 421 } 422 value, ok := values[targetId] 423 if !ok { 424 t.Fatalf("not exists %s", targetId) 425 } 426 427 if value.(*model.Service).Comment != afterComment { 428 t.Fatalf("after comment not match") 429 } 430 431 }