github.com/polarismesh/polaris@v1.17.8/store/boltdb/service_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 "strconv" 23 "testing" 24 "time" 25 26 "github.com/stretchr/testify/assert" 27 28 "github.com/polarismesh/polaris/common/model" 29 "github.com/polarismesh/polaris/common/utils" 30 ) 31 32 const ( 33 serviceCount = 5 34 aliasCount = 3 35 ) 36 37 func TestServiceStore_AddService(t *testing.T) { 38 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 39 if err != nil { 40 t.Fatal(err) 41 } 42 43 defer handler.Close() 44 45 sStore := &serviceStore{handler: handler} 46 47 for i := 0; i < serviceCount; i++ { 48 err := sStore.AddService(&model.Service{ 49 ID: "svcid" + strconv.Itoa(i), 50 Name: "svcname" + strconv.Itoa(i), 51 Namespace: "testsvc", 52 Business: "testbuss", 53 Ports: "8080", 54 Meta: map[string]string{ 55 "k1": "v1", 56 "k2": "v2", 57 }, 58 Comment: "testcomment", 59 Department: "testdepart", 60 Token: "testtoken", 61 Owner: "testowner", 62 Revision: "testrevision" + strconv.Itoa(i), 63 Reference: "", 64 Valid: true, 65 CreateTime: time.Now(), 66 ModifyTime: time.Now(), 67 }) 68 if err != nil { 69 t.Fatal(err) 70 } 71 } 72 73 for i := 0; i < aliasCount; i++ { 74 err := sStore.AddService(&model.Service{ 75 ID: "aliasid" + strconv.Itoa(i), 76 Name: "aliasname " + strconv.Itoa(i), 77 Namespace: "testsvc", 78 Business: "testbuss", 79 Ports: "8080", 80 Meta: map[string]string{ 81 "k1": "v1", 82 "k2": "v2", 83 }, 84 Comment: "testcomment", 85 Department: "testdepart", 86 Token: "testtoken", 87 Owner: "testowner", 88 Revision: "testrevision" + strconv.Itoa(i), 89 Reference: "svcid" + strconv.Itoa(i), 90 Valid: true, 91 CreateTime: time.Now(), 92 ModifyTime: time.Now(), 93 }) 94 if err != nil { 95 t.Fatal(err) 96 } 97 } 98 } 99 100 func TestServiceStore_GetServices(t *testing.T) { 101 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 102 if err != nil { 103 t.Fatal(err) 104 } 105 106 defer handler.Close() 107 108 sStore := &serviceStore{handler: handler} 109 110 serviceMetas := map[string]string{ 111 "k1": "v1", 112 } 113 114 _, ss, err := sStore.GetServices(nil, serviceMetas, nil, 0, 20) 115 if err != nil { 116 t.Fatal(err) 117 } 118 for _, s := range ss { 119 fmt.Printf("get service origin %+v\n", s) 120 } 121 } 122 123 func TestServiceStore_GetServicesBatch(t *testing.T) { 124 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 125 if err != nil { 126 t.Fatal(err) 127 } 128 129 defer handler.Close() 130 131 sStore := &serviceStore{handler: handler} 132 133 sArg := make([]*model.Service, 2) 134 for i := 0; i < 2; i++ { 135 sArg[i] = &model.Service{ 136 Name: "svcname" + strconv.Itoa(i), 137 Namespace: "testsvc", 138 } 139 } 140 141 ss, err := sStore.GetServicesBatch(sArg) 142 if err != nil { 143 t.Fatal(err) 144 } 145 146 if len(ss) != 2 { 147 t.Fatal(fmt.Sprintf("get service count error, except %d, got %d", 2, len(ss))) 148 } 149 } 150 151 func TestServiceStore_GetServiceByID(t *testing.T) { 152 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 153 if err != nil { 154 t.Fatal(err) 155 } 156 157 defer handler.Close() 158 159 sStore := &serviceStore{handler: handler} 160 161 ss, err := sStore.GetServiceByID("svcid1") 162 if err != nil { 163 t.Fatal(err) 164 } 165 166 fmt.Printf("get service by id: %+v\n", ss) 167 } 168 169 func TestServiceStore_UpdateService(t *testing.T) { 170 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 171 if err != nil { 172 t.Fatal(err) 173 } 174 175 defer handler.Close() 176 177 sStore := &serviceStore{handler: handler} 178 179 err = sStore.UpdateService(&model.Service{ 180 ID: "svcid1", 181 Name: "svcname1", 182 Namespace: "testsvc", 183 Token: "modifyToken1", 184 Meta: map[string]string{ 185 "k111": "v1111", 186 }, 187 Owner: "modifyOwner1", 188 Revision: "modifyRevision1", 189 Department: "modifyDepartment", 190 Business: "modifyBusiness", 191 }, true) 192 if err != nil { 193 t.Fatal(err) 194 } 195 196 // check update result 197 ss, err := sStore.getServiceByID("svcid1") 198 if err != nil { 199 t.Fatal(err) 200 } 201 202 fmt.Printf("get service %+v\n", ss) 203 204 if ss.Department != "modifyDepartment" || ss.Business != "modifyBusiness" || 205 ss.Reference != "" { 206 t.Fatal("update service error") 207 } 208 } 209 210 func TestServiceStore_UpdateServiceToken(t *testing.T) { 211 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 212 if err != nil { 213 t.Fatal(err) 214 } 215 216 defer handler.Close() 217 218 sStore := &serviceStore{handler: handler} 219 220 err = sStore.UpdateServiceToken("svcid1", "ttttt1", "rrrrrr1") 221 if err != nil { 222 t.Fatal(err) 223 } 224 225 // check update result 226 ss, err := sStore.getServiceByID("svcid1") 227 if err != nil { 228 t.Fatal(err) 229 } 230 231 fmt.Printf("get service %+v\n", ss) 232 233 if ss.Token != "ttttt1" || 234 ss.Revision != "rrrrrr1" || 235 ss.Reference != "" { 236 t.Fatal("update service error") 237 } 238 } 239 240 func TestServiceStore_GetSourceServiceToken(t *testing.T) { 241 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 242 if err != nil { 243 t.Fatal(err) 244 } 245 246 defer handler.Close() 247 248 sStore := &serviceStore{handler: handler} 249 250 ss, err := sStore.GetSourceServiceToken("svcname1", "testsvc") 251 if err != nil { 252 t.Fatal(err) 253 } 254 255 fmt.Printf("get service token: %+v\n", ss) 256 } 257 258 func TestServiceStore_GetService(t *testing.T) { 259 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 260 if err != nil { 261 t.Fatal(err) 262 } 263 264 defer handler.Close() 265 266 sStore := &serviceStore{handler: handler} 267 268 ss, err := sStore.GetService("modifyName1", "modifyNamespace1") 269 if err != nil { 270 t.Fatal(err) 271 } 272 273 fmt.Printf("get service by name and namespace: %+v\n", ss) 274 } 275 276 func TestServiceStore_GetServiceAliases(t *testing.T) { 277 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 278 if err != nil { 279 t.Fatal(err) 280 } 281 282 defer handler.Close() 283 284 sStore := &serviceStore{handler: handler} 285 286 total, ss, err := sStore.GetServiceAliases(nil, 0, 20) 287 if err != nil { 288 t.Fatal(err) 289 } 290 if total != aliasCount { 291 t.Fatal(fmt.Sprintf("service total count not match, expect %d, got %d", aliasCount, total)) 292 } 293 if len(ss) != aliasCount { 294 t.Fatal(fmt.Sprintf("service count not match, expect %d, got %d", aliasCount, len(ss))) 295 } 296 297 for _, s := range ss { 298 fmt.Printf("get service alias %+v\n", s) 299 } 300 } 301 302 func TestServiceStore_GetServicesCount(t *testing.T) { 303 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 304 if err != nil { 305 t.Fatal(err) 306 } 307 308 defer handler.Close() 309 310 sStore := &serviceStore{handler: handler} 311 312 _, err = sStore.GetServicesCount() 313 if err != nil { 314 t.Fatal(err) 315 } 316 } 317 318 func TestServiceStore_FuzzyGetService(t *testing.T) { 319 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 320 if err != nil { 321 t.Fatal(err) 322 } 323 defer handler.Close() 324 325 sStore := &serviceStore{handler: handler} 326 327 for i := 0; i < serviceCount; i++ { 328 idxStr := strconv.Itoa(i) 329 err := sStore.AddService(&model.Service{ 330 ID: "fuzzsvcid" + idxStr, 331 Name: "fuzzsvcname" + idxStr, 332 Namespace: "testsvc", 333 Business: "fuzztestbuss", 334 Ports: "8080", 335 Meta: map[string]string{ 336 "k1": "v1", 337 "k2": "v2", 338 }, 339 Comment: "fuzztestcomment", 340 Department: "fuzztestdepart", 341 Token: "testtoken", 342 Owner: "testowner", 343 Revision: "testrevision" + idxStr, 344 Reference: "", 345 Valid: true, 346 CreateTime: time.Now(), 347 ModifyTime: time.Now(), 348 }) 349 if err != nil { 350 t.Fatal(err) 351 } 352 } 353 defer func() { 354 for i := 0; i < serviceCount; i++ { 355 idxStr := strconv.Itoa(i) 356 sStore.DeleteService("fuzzsvcid"+idxStr, "fuzzsvcname"+idxStr, "testsvc") 357 } 358 }() 359 serviceFilters := make(map[string]string) 360 serviceFilters["name"] = "fuzzsvcname*" 361 362 count, _, err := sStore.GetServices(serviceFilters, nil, nil, 0, 10) 363 if err != nil { 364 t.Fatal(err) 365 } 366 if count != serviceCount { 367 t.Fatal(fmt.Sprintf("fuzzy query error, expect %d, actual %d", serviceCount, count)) 368 } 369 370 serviceFilters["name"] = "fuzzsvcname" 371 count, _, err = sStore.GetServices(serviceFilters, nil, nil, 0, 10) 372 if err != nil { 373 t.Fatal(err) 374 } 375 if count != 0 { 376 t.Fatal(fmt.Sprintf("fuzzy query error, expect %d, actual %d", 0, count)) 377 } 378 379 serviceFilters = make(map[string]string) 380 serviceFilters["department"] = "fuzztest*" 381 382 count, _, err = sStore.GetServices(serviceFilters, nil, nil, 0, 10) 383 if err != nil { 384 t.Fatal(err) 385 } 386 if count != serviceCount { 387 t.Fatal(fmt.Sprintf("fuzzy query error, expect %d, actual %d", serviceCount, count)) 388 } 389 390 serviceFilters = make(map[string]string) 391 serviceFilters["business"] = "fuzztest*" 392 393 count, _, err = sStore.GetServices(serviceFilters, nil, nil, 0, 10) 394 if err != nil { 395 t.Fatal(err) 396 } 397 if count != serviceCount { 398 t.Fatal(fmt.Sprintf("fuzzy query error, expect %d, actual %d", serviceCount, count)) 399 } 400 401 } 402 403 func TestServiceStore_GetMoreServices(t *testing.T) { 404 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 405 if err != nil { 406 t.Fatal(err) 407 } 408 409 defer handler.Close() 410 411 sStore := &serviceStore{handler: handler} 412 413 ss, err := sStore.GetService("svcname3", "testsvc") 414 if err != nil { 415 t.Fatal(err) 416 } 417 418 _, err = sStore.GetMoreServices(ss.ModifyTime, true, false, false) 419 if err != nil { 420 t.Fatal(err) 421 } 422 } 423 424 func TestServiceStore_UpdateServiceAlias(t *testing.T) { 425 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 426 if err != nil { 427 t.Fatal(err) 428 } 429 430 defer handler.Close() 431 432 sStore := &serviceStore{handler: handler} 433 434 err = sStore.UpdateServiceAlias(&model.Service{ 435 ID: "svcid2", 436 Name: "svcname1", 437 Namespace: "testsvc", 438 Owner: "testo", 439 Token: "t1", 440 Revision: "modifyRevision2", 441 Reference: "m1", 442 }, true) 443 if err != nil { 444 t.Fatal(err) 445 } 446 447 // check update result 448 ss, err := sStore.getServiceByID("svcid2") 449 if err != nil { 450 t.Fatal(err) 451 } 452 453 fmt.Printf("get service %+v\n", ss) 454 455 if ss.Reference != "m1" || ss.Revision != "modifyRevision2" { 456 t.Fatal("update service error") 457 } 458 } 459 460 func TestServiceStore_DeleteServiceAlias(t *testing.T) { 461 CreateTableDBHandlerAndRun(t, tblNameService, func(t *testing.T, handler BoltHandler) { 462 463 sStore := &serviceStore{handler: handler} 464 465 if err := sStore.AddService(&model.Service{ 466 ID: utils.NewUUID(), 467 Name: "service-alias", 468 Namespace: "namespace-alias", 469 }); err != nil { 470 t.Fatal(err) 471 } 472 473 err := sStore.DeleteServiceAlias("service-alias", "namespace-alias") 474 if err != nil { 475 t.Fatal(err) 476 } 477 478 svc, err := sStore.getServiceByNameAndNsIgnoreValid("service-alias", "namespace-alias") 479 assert.Nil(t, err, "error must be nil") 480 assert.False(t, svc.Valid, "delete service alias failed") 481 482 svc, err = sStore.getServiceByNameAndNs("service-alias", "namespace-alias") 483 assert.Nil(t, err, "error must be nil") 484 assert.Nil(t, svc, "service must be nil") 485 }) 486 } 487 488 func TestServiceStore_DeleteService(t *testing.T) { 489 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 490 if err != nil { 491 t.Fatal(err) 492 } 493 494 defer handler.Close() 495 496 sStore := &serviceStore{handler: handler} 497 498 _, ss, err := sStore.GetServices(nil, nil, nil, 0, 20) 499 if err != nil { 500 t.Fatal(err) 501 } 502 503 for _, s := range ss { 504 fmt.Printf("get service %+v\n", s) 505 err := sStore.DeleteService(s.ID, "", "") 506 if err != nil { 507 t.Fatal(err) 508 } 509 } 510 511 // check delete res 512 total, s, err := sStore.GetServices(nil, nil, nil, 0, 20) 513 if err != nil { 514 t.Fatal(err) 515 } 516 if total != 0 { 517 t.Fatal("delete service not effect") 518 } 519 520 for _, val := range s { 521 assert.False(t, val.Valid, "delete service not effect") 522 } 523 524 }