github.com/polarismesh/polaris@v1.17.8/store/boltdb/namespace_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/polarismesh/polaris/common/model" 28 ) 29 30 const ( 31 nsCount = 5 32 nsOwner = "user" 33 nsComment = "test ns" 34 nsToken = "xxxxx" 35 ) 36 37 func InitNamespaceData(nsStore *namespaceStore, nsCount int) error { 38 for i := 0; i < nsCount; i++ { 39 err := nsStore.AddNamespace(&model.Namespace{ 40 Name: "default" + strconv.Itoa(i), 41 Comment: nsComment, 42 Token: nsToken, 43 Owner: nsOwner, 44 Valid: true, 45 CreateTime: time.Now(), 46 ModifyTime: time.Now(), 47 }) 48 if err != nil { 49 return err 50 } 51 } 52 return nil 53 } 54 55 func TestNamespaceStore_AddNamespace(t *testing.T) { 56 _ = os.RemoveAll("./table.bolt") 57 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 58 if err != nil { 59 t.Fatal(err) 60 } 61 defer func() { 62 handler.Close() 63 }() 64 nsStore := &namespaceStore{handler: handler} 65 for i := 0; i < nsCount; i++ { 66 err = nsStore.AddNamespace(&model.Namespace{ 67 Name: "default" + strconv.Itoa(i), 68 Comment: nsComment, 69 Token: nsToken, 70 Owner: nsOwner, 71 Valid: true, 72 CreateTime: time.Now(), 73 ModifyTime: time.Now(), 74 }) 75 if err != nil { 76 t.Fatal(err) 77 } 78 } 79 } 80 81 func TestNamespaceStore_GetNamespaces(t *testing.T) { 82 _ = os.RemoveAll("./table.bolt") 83 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 84 if err != nil { 85 t.Fatal(err) 86 } 87 defer handler.Close() 88 nsStore := &namespaceStore{handler: handler} 89 90 if err := InitNamespaceData(nsStore, nsCount); err != nil { 91 t.Fatal(err) 92 } 93 94 t.Run("正常查询命名空间列表", func(t *testing.T) { 95 ret, retCnt, err := nsStore.GetNamespaces(map[string][]string{ 96 "": {}, 97 }, 0, nsCount) 98 99 if err != nil { 100 t.Fatal(err) 101 } 102 if len(ret) != int(retCnt) { 103 t.Fatal("len(ret) need equal int(retCnt)") 104 } 105 }) 106 107 t.Run("查询条件不满足-无法查询出结果", func(t *testing.T) { 108 // 只要有一个条件不满足,则对应的条目就不应该查出来 109 ret, _, err := nsStore.GetNamespaces(map[string][]string{ 110 OwnerAttribute: {"springliao"}, 111 }, 0, nsCount) 112 113 if err != nil { 114 t.Fatal(err) 115 } 116 if len(ret) != 0 { 117 t.Fatal("len(ret) must be zero") 118 } 119 120 ret, _, err = nsStore.GetNamespaces(map[string][]string{ 121 OwnerAttribute: {nsOwner}, 122 NameAttribute: {"springliao"}, 123 }, 0, nsCount) 124 125 if err != nil { 126 t.Fatal(err) 127 } 128 if len(ret) != 0 { 129 t.Fatal("len(ret) must be zero") 130 } 131 }) 132 133 t.Run("条件分页-offset为0开始查询-只查一条数据", func(t *testing.T) { 134 ret, retCnt, err := nsStore.GetNamespaces(map[string][]string{ 135 OwnerAttribute: {nsOwner}, 136 }, 0, 1) 137 138 if err != nil { 139 t.Fatal(err) 140 } 141 if !(len(ret) == 1 && nsCount == int(retCnt)) { 142 t.Fatalf("len(ret) must be 1 and retCnt must be %d", nsCount) 143 } 144 145 ret, retCnt, err = nsStore.GetNamespaces(map[string][]string{ 146 OwnerAttribute: {nsOwner}, 147 NameAttribute: {"default1"}, 148 }, 0, 1) 149 150 if err != nil { 151 t.Fatal(err) 152 } 153 if !(len(ret) == 1 && retCnt == 1) { 154 t.Fatalf("len(ret) must be 1 and retCnt must be 1, acutal len(ret) %d, retCnt : %d", len(ret), retCnt) 155 } 156 157 }) 158 159 t.Run("条件分页查询-从offset3开始查询-只查一条数据", func(t *testing.T) { 160 ret, retCnt, err := nsStore.GetNamespaces(map[string][]string{ 161 OwnerAttribute: {nsOwner}, 162 }, 3, 1) 163 164 if err != nil { 165 t.Fatal(err) 166 } 167 if !(len(ret) == 1 && nsCount == int(retCnt)) { 168 t.Fatalf("len(ret) must be 1 and retCnt must be %d", nsCount) 169 } 170 }) 171 172 t.Run("条件分页查询-查询多条数据", func(t *testing.T) { 173 ret, retCnt, err := nsStore.GetNamespaces(map[string][]string{ 174 OwnerAttribute: {nsOwner}, 175 }, 3, 10) 176 177 if err != nil { 178 t.Fatal(err) 179 } 180 if !(len(ret) == 2 && nsCount == int(retCnt)) { 181 t.Fatalf("len(ret) must be 1 and retCnt must be %d", nsCount) 182 } 183 }) 184 185 t.Run("分页查询-offset太大", func(t *testing.T) { 186 ret, retCnt, err := nsStore.GetNamespaces(map[string][]string{ 187 OwnerAttribute: {nsOwner}, 188 }, 1000000, 10) 189 190 if err != nil { 191 t.Fatal(err) 192 } 193 if !(len(ret) == 0 && nsCount == int(retCnt)) { 194 t.Fatalf("len(ret) must be 1 and retCnt must be %d", nsCount) 195 } 196 }) 197 198 } 199 200 func TestNamespaceStore_GetNamespace(t *testing.T) { 201 _ = os.RemoveAll("./table.bolt") 202 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 203 if err != nil { 204 t.Fatal(err) 205 } 206 defer handler.Close() 207 nsStore := &namespaceStore{handler: handler} 208 209 if err := InitNamespaceData(nsStore, nsCount); err != nil { 210 t.Fatal(err) 211 } 212 213 for i := 0; i < nsCount; i++ { 214 name := "default" + strconv.Itoa(i) 215 ns, err := nsStore.GetNamespace(name) 216 if err != nil { 217 t.Fatal(err) 218 } 219 if ns == nil { 220 t.Fatal(fmt.Sprintf("namespace %s not exists", name)) 221 } 222 } 223 } 224 225 func TestNamespaceStore_UpdateNamespace(t *testing.T) { 226 _ = os.RemoveAll("./table.bolt") 227 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 228 if err != nil { 229 t.Fatal(err) 230 } 231 defer handler.Close() 232 nsStore := &namespaceStore{handler: handler} 233 234 if err := InitNamespaceData(nsStore, nsCount); err != nil { 235 t.Fatal(err) 236 } 237 238 for i := 0; i < nsCount; i++ { 239 nsRaw := &model.Namespace{ 240 Name: "default" + strconv.Itoa(i), 241 Comment: nsComment + strconv.Itoa(i), 242 Owner: nsOwner, 243 } 244 err = nsStore.UpdateNamespace(nsRaw) 245 if err != nil { 246 t.Fatal(err) 247 } 248 } 249 // 检查update是否生效 250 for i := 0; i < nsCount; i++ { 251 name := "default" + strconv.Itoa(i) 252 ns, err := nsStore.GetNamespace(name) 253 if err != nil { 254 t.Fatal(err) 255 } 256 if ns == nil { 257 t.Fatal(fmt.Sprintf("namespace %s not exists", name)) 258 } 259 if ns.Comment != nsComment+strconv.Itoa(i) { 260 t.Fatal(fmt.Sprintf("comment not updated for %s", name)) 261 } 262 } 263 } 264 265 func TestNamespaceStore_UpdateNamespaceToken(t *testing.T) { 266 _ = os.RemoveAll("./table.bolt") 267 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 268 if err != nil { 269 t.Fatal(err) 270 } 271 defer handler.Close() 272 nsStore := &namespaceStore{handler: handler} 273 274 if err := InitNamespaceData(nsStore, nsCount); err != nil { 275 t.Fatal(err) 276 } 277 278 for i := 0; i < nsCount; i++ { 279 name := "default" + strconv.Itoa(i) 280 token := nsToken + strconv.Itoa(i) 281 err = nsStore.UpdateNamespaceToken(name, token) 282 if err != nil { 283 t.Fatal(err) 284 } 285 } 286 // 检查update是否生效 287 for i := 0; i < nsCount; i++ { 288 name := "default" + strconv.Itoa(i) 289 ns, err := nsStore.GetNamespace(name) 290 if err != nil { 291 t.Fatal(err) 292 } 293 if ns == nil { 294 t.Fatal(fmt.Sprintf("namespace %s not exists", name)) 295 } 296 if ns.Token != nsToken+strconv.Itoa(i) { 297 t.Fatal(fmt.Sprintf("comment not updated for %s", name)) 298 } 299 } 300 } 301 302 func TestNamespaceStore_GetMoreNamespaces(t *testing.T) { 303 _ = os.RemoveAll("./table.bolt") 304 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 305 if err != nil { 306 t.Fatal(err) 307 } 308 defer handler.Close() 309 nsStore := &namespaceStore{handler: handler} 310 if err := InitNamespaceData(nsStore, nsCount); err != nil { 311 t.Fatal(err) 312 } 313 314 before := time.Now().Add(0 - 1*time.Minute) 315 namespaces, err := nsStore.GetMoreNamespaces(before) 316 if err != nil { 317 t.Fatal(err) 318 } 319 if len(namespaces) != nsCount { 320 t.Fatal(fmt.Sprintf("more namespaces count not match, expect %d, got %d", nsCount, len(namespaces))) 321 } 322 } 323 324 func TestTransaction_LockNamespace(t *testing.T) { 325 _ = os.RemoveAll("./table.bolt") 326 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 327 if err != nil { 328 t.Fatal(err) 329 } 330 defer handler.Close() 331 trans := &transaction{handler: handler} 332 333 if err := InitNamespaceData(&namespaceStore{handler: handler}, nsCount); err != nil { 334 t.Fatal(err) 335 } 336 337 defer trans.Commit() 338 for i := 0; i < nsCount; i++ { 339 name := "default" + strconv.Itoa(i) 340 namespace, err := trans.LockNamespace(name) 341 if err != nil { 342 t.Fatal(err) 343 } 344 if namespace == nil { 345 t.Fatal(fmt.Sprintf("namespace %s not exists", name)) 346 } 347 } 348 } 349 350 func TestTransaction_DeleteNamespace(t *testing.T) { 351 _ = os.RemoveAll("./table.bolt") 352 handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"}) 353 if err != nil { 354 t.Fatal(err) 355 } 356 defer handler.Close() 357 trans := &transaction{handler: handler} 358 359 nsStore := &namespaceStore{handler: handler} 360 if err := InitNamespaceData(nsStore, nsCount); err != nil { 361 t.Fatal(err) 362 } 363 364 for i := 0; i < nsCount; i++ { 365 name := "default" + strconv.Itoa(i) 366 err := trans.DeleteNamespace(name) 367 if err != nil { 368 trans.Commit() 369 t.Fatal(err) 370 } 371 } 372 err = trans.Commit() 373 if err != nil { 374 t.Fatal(err) 375 } 376 for i := 0; i < nsCount; i++ { 377 name := "default" + strconv.Itoa(i) 378 ns, err := nsStore.GetNamespace(name) 379 if err != nil { 380 t.Fatal(err) 381 } 382 if ns != nil { 383 t.Fatal(fmt.Sprintf("namespace %s exists, delete fail", name)) 384 } 385 } 386 }