github.com/polarismesh/polaris@v1.17.8/store/boltdb/config_file_group_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 "testing" 23 "time" 24 25 "github.com/stretchr/testify/assert" 26 27 "github.com/polarismesh/polaris/common/model" 28 ) 29 30 func mockConfigFileGroup(total int) []*model.ConfigFileGroup { 31 ret := make([]*model.ConfigFileGroup, 0, total) 32 33 for i := 0; i < total; i++ { 34 val := &model.ConfigFileGroup{ 35 Name: fmt.Sprintf("history-%d", i), 36 Namespace: "default", 37 CreateTime: time.Now(), 38 ModifyTime: time.Now(), 39 Valid: true, 40 Metadata: map[string]string{ 41 "mock_data": "mock_value", 42 }, 43 } 44 ret = append(ret, val) 45 } 46 return ret 47 } 48 49 func resetConfigFileGroupTimeAndIDField(tN time.Time, restID bool, datas ...*model.ConfigFileGroup) { 50 for i := range datas { 51 if restID { 52 datas[i].Id = 0 53 } 54 datas[i].CreateTime = tN 55 datas[i].ModifyTime = tN 56 } 57 } 58 59 func Test_configFileGroupStore(t *testing.T) { 60 t.Run("配置分组插入", func(t *testing.T) { 61 CreateTableDBHandlerAndRun(t, tblConfigFileGroup, func(t *testing.T, handler BoltHandler) { 62 store := newConfigFileGroupStore(handler) 63 total := 10 64 mockGroups := mockConfigFileGroup(total) 65 66 for i := 0; i < total; i++ { 67 if _, err := store.CreateConfigFileGroup(mockGroups[i]); err != nil { 68 t.Fatal(err) 69 } 70 } 71 72 idMap := make(map[uint64]struct{}) 73 74 for i := 0; i < total; i++ { 75 mockVal := mockGroups[i] 76 val, err := store.GetConfigFileGroup(mockVal.Namespace, mockVal.Name) 77 if err != nil { 78 t.Fatal(err) 79 } 80 81 assert.NotNil(t, val) 82 83 copyVal := *val 84 resetConfigFileGroupTimeAndIDField(time.Now(), true, val, mockVal) 85 assert.Equal(t, mockVal, val) 86 idMap[copyVal.Id] = struct{}{} 87 } 88 assert.Equal(t, total, len(idMap)) 89 }) 90 }) 91 92 t.Run("配置分组更新", func(t *testing.T) { 93 CreateTableDBHandlerAndRun(t, tblConfigFileGroup, func(t *testing.T, handler BoltHandler) { 94 store := newConfigFileGroupStore(handler) 95 total := 10 96 mockGroups := mockConfigFileGroup(total) 97 98 for i := 0; i < total; i++ { 99 if _, err := store.CreateConfigFileGroup(mockGroups[i]); err != nil { 100 t.Fatal(err) 101 } 102 103 mockGroups[i].Comment = fmt.Sprintf("update_group_%d", i) 104 105 if err := store.UpdateConfigFileGroup(mockGroups[i]); err != nil { 106 t.Fatal(err) 107 } 108 } 109 110 for i := 0; i < total; i++ { 111 mockVal := mockGroups[i] 112 val, err := store.GetConfigFileGroup(mockVal.Namespace, mockVal.Name) 113 if err != nil { 114 t.Fatal(err) 115 } 116 117 assert.NotNil(t, val) 118 119 resetConfigFileGroupTimeAndIDField(time.Now(), false, val, mockVal) 120 assert.Equal(t, mockVal, val) 121 } 122 }) 123 }) 124 125 t.Run("配置分组插入-删除", func(t *testing.T) { 126 CreateTableDBHandlerAndRun(t, tblConfigFileGroup, func(t *testing.T, handler BoltHandler) { 127 store := newConfigFileGroupStore(handler) 128 total := 10 129 mockGroups := mockConfigFileGroup(total) 130 131 for i := 0; i < total; i++ { 132 if _, err := store.CreateConfigFileGroup(mockGroups[i]); err != nil { 133 t.Fatal(err) 134 } 135 } 136 137 for i := 0; i < total; i++ { 138 if err := store.DeleteConfigFileGroup(mockGroups[i].Namespace, mockGroups[i].Name); err != nil { 139 t.Fatal(err) 140 } 141 } 142 143 for i := 0; i < total; i++ { 144 mockVal := mockGroups[i] 145 val, err := store.GetConfigFileGroup(mockVal.Namespace, mockVal.Name) 146 if err != nil { 147 t.Fatal(err) 148 } 149 150 assert.Nil(t, val) 151 } 152 }) 153 }) 154 }