github.com/polarismesh/polaris@v1.17.8/store/boltdb/instance_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 "os" 23 "strconv" 24 "testing" 25 "time" 26 27 "github.com/golang/protobuf/ptypes/wrappers" 28 apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage" 29 "github.com/stretchr/testify/assert" 30 31 "github.com/polarismesh/polaris/common/model" 32 commontime "github.com/polarismesh/polaris/common/time" 33 ) 34 35 const ( 36 insCount = 5 37 ) 38 39 func TestInstanceStore_AddInstance(t *testing.T) { 40 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 41 if err != nil { 42 t.Fatal(err) 43 } 44 defer func() { 45 handler.Close() 46 _ = os.RemoveAll("./table.bolt") 47 }() 48 insStore := &instanceStore{handler: handler} 49 batchAddInstances(t, insStore, "svcid1", insCount) 50 } 51 52 func batchAddInstances(t *testing.T, insStore *instanceStore, svcId string, insCount int) { 53 sStore := &serviceStore{ 54 handler: insStore.handler, 55 } 56 57 sStore.AddService(&model.Service{ 58 ID: svcId, 59 Name: svcId, 60 Namespace: svcId, 61 Token: svcId, 62 Owner: svcId, 63 Valid: true, 64 }) 65 66 for i := 0; i < insCount; i++ { 67 68 nowt := commontime.Time2String(time.Now()) 69 70 err := insStore.AddInstance(&model.Instance{ 71 Proto: &apiservice.Instance{ 72 Id: &wrappers.StringValue{Value: "insid" + strconv.Itoa(i)}, 73 Host: &wrappers.StringValue{Value: "1.1.1." + strconv.Itoa(i)}, 74 Port: &wrappers.UInt32Value{Value: uint32(i + 1)}, 75 Protocol: &wrappers.StringValue{Value: "grpc"}, 76 Weight: &wrappers.UInt32Value{Value: uint32(i + 1)}, 77 EnableHealthCheck: &wrappers.BoolValue{Value: true}, 78 Healthy: &wrappers.BoolValue{Value: true}, 79 Isolate: &wrappers.BoolValue{Value: true}, 80 Metadata: map[string]string{ 81 "insk1": "insv1", 82 "insk2": "insv2", 83 }, 84 Ctime: &wrappers.StringValue{Value: nowt}, 85 Mtime: &wrappers.StringValue{Value: nowt}, 86 Revision: &wrappers.StringValue{Value: "revision" + strconv.Itoa(i)}, 87 }, 88 ServiceID: svcId, 89 ServicePlatformID: "svcPlatId1", 90 Valid: true, 91 ModifyTime: time.Now(), 92 }) 93 if err != nil { 94 t.Fatal(err) 95 } 96 } 97 } 98 99 func TestInstanceStore_BatchAddInstances(t *testing.T) { 100 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 101 if err != nil { 102 t.Fatal(err) 103 } 104 defer func() { 105 handler.Close() 106 _ = os.RemoveAll("./table.bolt") 107 }() 108 insStore := &instanceStore{handler: handler} 109 110 instances := make([]*model.Instance, 0) 111 for i := insCount; i < insCount+5; i++ { 112 113 nowt := commontime.Time2String(time.Now()) 114 115 ins := &model.Instance{ 116 Proto: &apiservice.Instance{ 117 Id: &wrappers.StringValue{Value: "insid" + strconv.Itoa(i)}, 118 Host: &wrappers.StringValue{Value: "1.1.1." + strconv.Itoa(i)}, 119 Port: &wrappers.UInt32Value{Value: uint32(i)}, 120 Protocol: &wrappers.StringValue{Value: "grpc"}, 121 Weight: &wrappers.UInt32Value{Value: uint32(i)}, 122 EnableHealthCheck: &wrappers.BoolValue{Value: true}, 123 Healthy: &wrappers.BoolValue{Value: true}, 124 Isolate: &wrappers.BoolValue{Value: true}, 125 Metadata: map[string]string{ 126 "insk1": "insv1", 127 "insk2": "insv2", 128 }, 129 Ctime: &wrappers.StringValue{Value: nowt}, 130 Mtime: &wrappers.StringValue{Value: nowt}, 131 Revision: &wrappers.StringValue{Value: "revision" + strconv.Itoa(i)}, 132 }, 133 ServiceID: "svcid2", 134 ServicePlatformID: "svcPlatId1", 135 Valid: true, 136 ModifyTime: time.Now(), 137 } 138 139 instances = append(instances, ins) 140 } 141 142 err = insStore.BatchAddInstances(instances) 143 if err != nil { 144 t.Fatal(err) 145 } 146 } 147 148 func TestInstanceStore_GetExpandInstances(t *testing.T) { 149 _ = os.RemoveAll("./table.bolt") 150 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 151 if err != nil { 152 t.Fatal(err) 153 } 154 defer func() { 155 handler.Close() 156 _ = os.RemoveAll("./table.bolt") 157 }() 158 insStore := &instanceStore{handler: handler} 159 batchAddInstances(t, insStore, "svcid1", insCount) 160 161 _, ii, err := insStore.GetExpandInstances(nil, nil, 0, 20) 162 if err != nil { 163 t.Fatal(err) 164 } 165 166 for _, i := range ii { 167 fmt.Printf("instances is %+v\n", i) 168 } 169 } 170 171 func TestInstanceStore_GetMoreInstances(t *testing.T) { 172 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 173 if err != nil { 174 t.Fatal(err) 175 } 176 defer func() { 177 handler.Close() 178 _ = os.RemoveAll("./table.bolt") 179 }() 180 insStore := &instanceStore{handler: handler} 181 batchAddInstances(t, insStore, "svcid2", insCount) 182 183 tx, err := insStore.handler.StartTx() 184 assert.NoError(t, err) 185 186 t.Cleanup(func() { 187 tx.Rollback() 188 }) 189 190 tt, _ := time.Parse("2006-01-02 15:04:05", "2021-01-02 15:04:05") 191 m, err := insStore.GetMoreInstances(tx, tt, false, false, []string{"svcid2"}) 192 if err != nil { 193 t.Fatal(err) 194 } 195 _ = tx.Commit() 196 197 if len(m) != 5 { 198 t.Fatal(fmt.Sprintf("get more instances error, except len is %d, got %d", 5, len(m))) 199 } 200 201 } 202 203 func TestInstanceStore_SetInstanceHealthStatus(t *testing.T) { 204 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 205 if err != nil { 206 t.Fatal(err) 207 } 208 defer func() { 209 handler.Close() 210 _ = os.RemoveAll("./table.bolt") 211 }() 212 insStore := &instanceStore{handler: handler} 213 batchAddInstances(t, insStore, "svcid1", 8) 214 215 err = insStore.SetInstanceHealthStatus("insid7", 0, "rev-no-healthy") 216 if err != nil { 217 t.Fatal("set instance healthy error") 218 } 219 220 ins, err := insStore.GetInstance("insid7") 221 if err != nil { 222 t.Fatal(err) 223 } 224 225 if ins.Proto.GetHealthy().GetValue() != false { 226 t.Fatal(fmt.Sprintf("set instance healthy status error, except %t, got %t", 227 false, ins.Proto.GetHealthy().GetValue())) 228 } 229 } 230 231 func TestInstanceStore_BatchSetInstanceIsolate(t *testing.T) { 232 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 233 if err != nil { 234 t.Fatal(err) 235 } 236 defer func() { 237 handler.Close() 238 _ = os.RemoveAll("./table.bolt") 239 }() 240 insStore := &instanceStore{handler: handler} 241 batchAddInstances(t, insStore, "svcid1", 10) 242 243 err = insStore.BatchSetInstanceIsolate([]interface{}{"insid7", "insid1"}, 0, "rev-no-Isolate") 244 if err != nil { 245 t.Fatal("set instance isolate error") 246 } 247 248 ins, err := insStore.GetInstance("insid7") 249 if err != nil { 250 t.Fatal(err) 251 } 252 253 if ins.Proto.GetIsolate().GetValue() != false { 254 t.Fatal(fmt.Sprintf("set instance isolate error, except %t, got %t", 255 false, ins.Proto.GetIsolate().GetValue())) 256 } 257 258 ins, err = insStore.GetInstance("insid1") 259 if err != nil { 260 t.Fatal(err) 261 } 262 263 if ins.Proto.GetIsolate().GetValue() != false { 264 t.Fatal(fmt.Sprintf("set instance isolate error, except %t, got %t", 265 false, ins.Proto.GetIsolate().GetValue())) 266 } 267 } 268 269 func TestInstanceStore_GetInstancesMainByService(t *testing.T) { 270 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 271 if err != nil { 272 t.Fatal(err) 273 } 274 defer func() { 275 handler.Close() 276 _ = os.RemoveAll("./table.bolt") 277 }() 278 insStore := &instanceStore{handler: handler} 279 batchAddInstances(t, insStore, "svcid1", insCount) 280 281 ii, err := insStore.GetInstancesMainByService("svcid1", "1.1.1.1") 282 if err != nil { 283 t.Fatal(err) 284 } 285 286 for _, i := range ii { 287 fmt.Printf("get instance %+v\n", i) 288 } 289 } 290 291 func TestInstanceStore_UpdateInstance(t *testing.T) { 292 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 293 if err != nil { 294 t.Fatal(err) 295 } 296 defer func() { 297 handler.Close() 298 _ = os.RemoveAll("./table.bolt") 299 }() 300 insStore := &instanceStore{handler: handler} 301 batchAddInstances(t, insStore, "svcid1", insCount) 302 303 insM := &model.Instance{ 304 Proto: &apiservice.Instance{ 305 Id: &wrappers.StringValue{Value: "insid" + strconv.Itoa(0)}, 306 Service: &wrappers.StringValue{Value: "svcid1"}, 307 Namespace: &wrappers.StringValue{Value: "testns"}, 308 Host: &wrappers.StringValue{Value: "1.2.3." + strconv.Itoa(0)}, 309 Port: &wrappers.UInt32Value{Value: uint32(8080)}, 310 Protocol: &wrappers.StringValue{Value: "trpc"}, 311 Weight: &wrappers.UInt32Value{Value: uint32(0)}, 312 EnableHealthCheck: &wrappers.BoolValue{Value: true}, 313 Healthy: &wrappers.BoolValue{Value: true}, 314 Isolate: &wrappers.BoolValue{Value: true}, 315 Metadata: map[string]string{ 316 "modifyK1": "modifyV1", 317 "modifyK2": "modifyV1", 318 }, 319 }, 320 ServiceID: "svcid1", 321 ServicePlatformID: "svcPlatId1", 322 Valid: true, 323 ModifyTime: time.Now(), 324 } 325 326 err = insStore.UpdateInstance(insM) 327 if err != nil { 328 t.Fatal(err) 329 } 330 331 // check the result 332 ins, err := insStore.GetInstance("insid0") 333 if err != nil { 334 t.Fatal(err) 335 } 336 337 if ins.Proto.GetHost().GetValue() != "1.2.3.0" || 338 ins.Proto.GetPort().GetValue() != 8080 { 339 t.Fatal("udpate instance error") 340 } 341 } 342 343 func TestInstanceStore_GetInstancesBrief(t *testing.T) { 344 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 345 if err != nil { 346 t.Fatal(err) 347 } 348 defer func() { 349 handler.Close() 350 _ = os.RemoveAll("./table.bolt") 351 }() 352 insStore := &instanceStore{handler: handler} 353 batchAddInstances(t, insStore, "svcid2", 10) 354 batchAddInstances(t, insStore, "svcid1", 5) 355 sStore := &serviceStore{handler: handler} 356 357 err = sStore.AddService(&model.Service{ 358 ID: "svcid1", 359 Name: "svcname1", 360 Namespace: "testsvc", 361 Business: "testbuss", 362 Ports: "8080", 363 Meta: map[string]string{ 364 "k1": "v1", 365 "k2": "v2", 366 }, 367 Comment: "testcomment", 368 Department: "testdepart", 369 Token: "testtoken1", 370 Owner: "testowner", 371 Revision: "testrevision1", 372 Reference: "", 373 Valid: true, 374 CreateTime: time.Now(), 375 ModifyTime: time.Now(), 376 }) 377 if err != nil { 378 t.Fatal(err) 379 } 380 381 err = sStore.AddService(&model.Service{ 382 ID: "svcid2", 383 Name: "svcname2", 384 Namespace: "testsvc", 385 Business: "testbuss", 386 Ports: "8080", 387 Meta: map[string]string{ 388 "k1": "v1", 389 "k2": "v2", 390 }, 391 Comment: "testcomment", 392 Department: "testdepart", 393 Token: "testtoken2", 394 Owner: "testowner", 395 Revision: "testrevision2", 396 Reference: "", 397 Valid: true, 398 CreateTime: time.Now(), 399 ModifyTime: time.Now(), 400 }) 401 if err != nil { 402 t.Fatal(err) 403 } 404 405 m, err := insStore.GetInstancesBrief(map[string]bool{ 406 "insid1": true, 407 "insid8": true, 408 }) 409 if err != nil { 410 t.Fatal(err) 411 } 412 413 ins1 := m["insid1"] 414 ins2 := m["insid8"] 415 416 if ins1.Proto.GetService().GetValue() != "svcname1" { 417 t.Fatal(fmt.Sprintf("get instance brief error, except %s, got %s", 418 "svcname1", ins1.Proto.GetService().GetValue())) 419 } 420 421 if ins2.Proto.GetService().GetValue() != "svcname2" { 422 t.Fatal(fmt.Sprintf("get instance brief error, except %s, got %s", 423 "svcname2", ins2.Proto.GetService().GetValue())) 424 } 425 426 for _, instance := range m { 427 fmt.Printf("get instance from brief %+v\n", instance) 428 } 429 430 // delete services 431 sStore.DeleteService("svcid1", "", "") 432 sStore.DeleteService("svcid2", "", "") 433 } 434 435 func TestInstanceStore_GetInstancesCount(t *testing.T) { 436 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 437 if err != nil { 438 t.Fatal(err) 439 } 440 defer func() { 441 handler.Close() 442 _ = os.RemoveAll("./table.bolt") 443 }() 444 insStore := &instanceStore{handler: handler} 445 batchAddInstances(t, insStore, "svcid1", insCount) 446 447 c, err := insStore.GetInstancesCount() 448 if err != nil { 449 t.Fatal(err) 450 } 451 452 if c != insCount { 453 t.Fatal("get instance count error") 454 } 455 } 456 457 func TestInstanceStore_CheckInstancesExisted(t *testing.T) { 458 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 459 if err != nil { 460 t.Fatal(err) 461 } 462 defer func() { 463 handler.Close() 464 _ = os.RemoveAll("./table.bolt") 465 }() 466 insStore := &instanceStore{handler: handler} 467 batchAddInstances(t, insStore, "svcid1", insCount) 468 469 m := map[string]bool{ 470 "insid1": false, 471 "insid2": false, 472 "test-not-exist": false, 473 "test-not-exist1": false, 474 } 475 476 mm, err := insStore.BatchGetInstanceIsolate(m) 477 if err != nil { 478 t.Fatal(err) 479 } 480 481 if !mm["insid1"] || !mm["insid2"] || mm["test-not-exist"] || mm["test-not-exist1"] { 482 t.Fatal("check instance existed error") 483 } 484 } 485 486 func TestInstanceStore_DeleteInstance(t *testing.T) { 487 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 488 if err != nil { 489 t.Fatal(err) 490 } 491 defer func() { 492 handler.Close() 493 _ = os.RemoveAll("./table.bolt") 494 }() 495 insStore := &instanceStore{handler: handler} 496 batchAddInstances(t, insStore, "svcid1", insCount) 497 498 err = insStore.DeleteInstance("insid1") 499 if err != nil { 500 t.Fatal(err) 501 } 502 503 // check the result 504 ins, err := insStore.GetInstance("insid1") 505 if err != nil { 506 t.Fatal(err) 507 } 508 509 if ins != nil && !ins.Valid { 510 t.Fatal("delete instance error") 511 } 512 } 513 514 func TestInstanceStore_BatchDeleteInstances(t *testing.T) { 515 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 516 if err != nil { 517 t.Fatal(err) 518 } 519 defer func() { 520 handler.Close() 521 _ = os.RemoveAll("./table.bolt") 522 }() 523 insStore := &instanceStore{handler: handler} 524 batchAddInstances(t, insStore, "svcid1", insCount) 525 526 err = insStore.BatchDeleteInstances([]interface{}{"insid2", "insid3"}) 527 if err != nil { 528 t.Fatal(err) 529 } 530 531 // check the result 532 ins, err := insStore.GetInstance("insid2") 533 if err != nil { 534 t.Fatal(err) 535 } 536 537 if ins != nil && !ins.Valid { 538 t.Fatal("delete instance error") 539 } 540 541 ins, err = insStore.GetInstance("insid3") 542 if err != nil { 543 t.Fatal(err) 544 } 545 546 if ins != nil && !ins.Valid { 547 t.Fatal("delete instance error") 548 } 549 }