github.com/polarismesh/polaris@v1.17.8/store/boltdb/admin_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 "os" 22 "strconv" 23 "testing" 24 "time" 25 26 "github.com/golang/protobuf/ptypes/wrappers" 27 apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage" 28 29 "github.com/polarismesh/polaris/common/eventhub" 30 "github.com/polarismesh/polaris/common/model" 31 commontime "github.com/polarismesh/polaris/common/time" 32 ) 33 34 func setup() { 35 eventhub.InitEventHub() 36 } 37 38 func teardown() { 39 } 40 41 func TestAdminStore_BatchCleanDeletedClients(t *testing.T) { 42 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 43 if err != nil { 44 t.Fatal(err) 45 } 46 defer func() { 47 handler.Close() 48 _ = os.RemoveAll("./table.bolt") 49 }() 50 51 store := &adminStore{handler: handler} 52 cStore := &clientStore{handler: handler} 53 54 mockClients := createMockClients(5) 55 err = cStore.BatchAddClients(mockClients) 56 if err != nil { 57 t.Fatal(err) 58 } 59 60 toDeleteClients := []string{ 61 mockClients[0].Proto().Id.GetValue(), 62 mockClients[1].Proto().Id.GetValue(), 63 mockClients[2].Proto().Id.GetValue(), 64 } 65 err = cStore.BatchDeleteClients(toDeleteClients) 66 if err != nil { 67 t.Fatal(err) 68 } 69 70 count, err := store.BatchCleanDeletedClients(0, 2) 71 if err != nil { 72 t.Fatal(err) 73 } 74 if count != 2 { 75 t.Fatalf("count not match, expect cnt=%d, actual cnt=%d", 2, count) 76 } 77 78 remainClients, err := cStore.GetMoreClients(time.Now(), true) 79 if err != nil { 80 t.Fatal(err) 81 } 82 validCount := 0 83 invalidCount := 0 84 for _, v := range remainClients { 85 if v.Valid() { 86 validCount++ 87 } else { 88 invalidCount++ 89 } 90 } 91 if validCount != 2 { 92 t.Fatalf("count not match, expect cnt=%d, actual cnt=%d", 2, validCount) 93 } 94 if invalidCount != 1 { 95 t.Fatalf("count not match, expect cnt=%d, actual cnt=%d", 1, invalidCount) 96 } 97 98 } 99 100 func TestAdminStore_BatchCleanDeletedInstances(t *testing.T) { 101 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 102 if err != nil { 103 t.Fatal(err) 104 } 105 defer func() { 106 handler.Close() 107 _ = os.RemoveAll("./table.bolt") 108 }() 109 110 store := &adminStore{handler: handler} 111 sStore := &serviceStore{handler: handler} 112 insStore := &instanceStore{handler: handler} 113 114 svcId := "svcid1" 115 sStore.AddService(&model.Service{ 116 ID: svcId, 117 Name: svcId, 118 Namespace: svcId, 119 Token: svcId, 120 Owner: svcId, 121 Valid: true, 122 }) 123 124 for i := 0; i < insCount; i++ { 125 nowt := commontime.Time2String(time.Now()) 126 err := insStore.AddInstance(&model.Instance{ 127 Proto: &apiservice.Instance{ 128 Id: &wrappers.StringValue{Value: "insid" + strconv.Itoa(i)}, 129 Host: &wrappers.StringValue{Value: "1.1.1." + strconv.Itoa(i)}, 130 Port: &wrappers.UInt32Value{Value: uint32(i + 1)}, 131 Protocol: &wrappers.StringValue{Value: "grpc"}, 132 Weight: &wrappers.UInt32Value{Value: uint32(i + 1)}, 133 EnableHealthCheck: &wrappers.BoolValue{Value: true}, 134 Healthy: &wrappers.BoolValue{Value: true}, 135 Isolate: &wrappers.BoolValue{Value: true}, 136 Metadata: map[string]string{ 137 "insk1": "insv1", 138 "insk2": "insv2", 139 }, 140 Ctime: &wrappers.StringValue{Value: nowt}, 141 Mtime: &wrappers.StringValue{Value: nowt}, 142 Revision: &wrappers.StringValue{Value: "revision" + strconv.Itoa(i)}, 143 }, 144 ServiceID: svcId, 145 ServicePlatformID: "svcPlatId1", 146 Valid: true, 147 ModifyTime: time.Now(), 148 }) 149 if err != nil { 150 t.Fatal(err) 151 } 152 } 153 154 toDeleteInstances := []interface{}{"insid1", "insid2", "insid3"} 155 err = insStore.BatchDeleteInstances(toDeleteInstances) 156 if err != nil { 157 t.Fatal(err) 158 } 159 160 count, err := store.BatchCleanDeletedInstances(0, 2) 161 if err != nil { 162 t.Fatal(err) 163 } 164 if count != 2 { 165 t.Fatalf("count not match, expect cnt=%d, actual cnt=%d", 2, count) 166 } 167 168 count, err = insStore.GetInstancesCount() 169 if err != nil { 170 t.Fatal(err) 171 } 172 if count != 2 { 173 t.Fatalf("count not match, expect cnt=%d, actual cnt=%d", 2, count) 174 } 175 176 } 177 178 func TestAdminStore_StartLeaderElection(t *testing.T) { 179 key := "TestElectKey" 180 mstore := &adminStore{handler: nil, leMap: make(map[string]bool)} 181 isLeader := mstore.IsLeader(key) 182 if isLeader { 183 t.Error("expect follower state") 184 } 185 186 mstore.StartLeaderElection(key) 187 isLeader = mstore.IsLeader(key) 188 if !isLeader { 189 t.Error("expect leader state") 190 } 191 } 192 193 func TestAdminStore_ReleaseLeaderElection(t *testing.T) { 194 key := "TestElectKey" 195 mstore := &adminStore{handler: nil, leMap: make(map[string]bool)} 196 mstore.StartLeaderElection(key) 197 mstore.ReleaseLeaderElection(key) 198 isLeader := mstore.IsLeader(key) 199 if isLeader { 200 t.Error("expect follower state") 201 } 202 } 203 204 func TestAdminStore_ListLeaderElections(t *testing.T) { 205 key := "TestElectKey" 206 mstore := &adminStore{handler: nil, leMap: make(map[string]bool)} 207 mstore.StartLeaderElection(key) 208 209 out, err := mstore.ListLeaderElections() 210 if err != nil { 211 t.Errorf("should not err: %v", err) 212 } 213 214 if len(out) != 1 { 215 t.Error("expect one leader election") 216 } 217 218 if out[0].ElectKey != key { 219 t.Errorf("expect key: %s, actual key: %s", key, out[0].ElectKey) 220 } 221 222 } 223 224 func TestAdminStore_getUnHealthyInstancesBefore(t *testing.T) { 225 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 226 if err != nil { 227 t.Fatal(err) 228 } 229 defer func() { 230 handler.Close() 231 _ = os.RemoveAll("./table.bolt") 232 }() 233 234 store := &adminStore{handler: handler} 235 sStore := &serviceStore{handler: handler} 236 insStore := &instanceStore{handler: handler} 237 238 svcId := "svcid1" 239 sStore.AddService(&model.Service{ 240 ID: svcId, 241 Name: svcId, 242 Namespace: svcId, 243 Token: svcId, 244 Owner: svcId, 245 Valid: true, 246 }) 247 248 mtime := time.Date(2023, 3, 4, 11, 0, 0, 0, time.Local) 249 for i := 0; i < insCount; i++ { 250 nowt := commontime.Time2String(mtime) 251 err := insStore.AddInstance(&model.Instance{ 252 Proto: &apiservice.Instance{ 253 Id: &wrappers.StringValue{Value: "insid" + strconv.Itoa(i)}, 254 Host: &wrappers.StringValue{Value: "1.1.1." + strconv.Itoa(i)}, 255 Port: &wrappers.UInt32Value{Value: uint32(i + 1)}, 256 Protocol: &wrappers.StringValue{Value: "grpc"}, 257 Weight: &wrappers.UInt32Value{Value: uint32(i + 1)}, 258 EnableHealthCheck: &wrappers.BoolValue{Value: true}, 259 Healthy: &wrappers.BoolValue{Value: true}, 260 Isolate: &wrappers.BoolValue{Value: true}, 261 Metadata: map[string]string{ 262 "insk1": "insv1", 263 "insk2": "insv2", 264 }, 265 Ctime: &wrappers.StringValue{Value: nowt}, 266 Mtime: &wrappers.StringValue{Value: nowt}, 267 Revision: &wrappers.StringValue{Value: "revision" + strconv.Itoa(i)}, 268 }, 269 ServiceID: svcId, 270 ServicePlatformID: "svcPlatId1", 271 Valid: true, 272 ModifyTime: time.Now(), 273 }) 274 if err != nil { 275 t.Fatal(err) 276 } 277 } 278 279 toUnHealthyInstances := []interface{}{"insid1", "insid2", "insid3"} 280 err = insStore.BatchSetInstanceHealthStatus(toUnHealthyInstances, 0, "revision-11") 281 if err != nil { 282 t.Fatal(err) 283 } 284 285 beforeTime := time.Date(2023, 3, 4, 11, 1, 0, 0, time.Local) 286 ids, err := store.getUnHealthyInstancesBefore(beforeTime, 2) 287 if err != nil { 288 t.Fatal(err) 289 } 290 if len(ids) != 2 { 291 t.Fatalf("count not match, expect cnt=%d, actual cnt=%d", 2, len(ids)) 292 } 293 } 294 295 func TestMain(m *testing.M) { 296 setup() 297 code := m.Run() 298 teardown() 299 os.Exit(code) 300 }