github.com/polarismesh/polaris@v1.17.8/store/boltdb/config_file_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 mockConfigFile(total int, param map[string]string) []*model.ConfigFile {
    31  	ret := make([]*model.ConfigFile, 0, total)
    32  
    33  	for i := 0; i < total; i++ {
    34  
    35  		namespace := param["namespace"]
    36  		group := param["group"]
    37  
    38  		if namespace == "" {
    39  			namespace = fmt.Sprintf("cpnfig-file-%d", i)
    40  		}
    41  
    42  		if group == "" {
    43  			group = fmt.Sprintf("cpnfig-file-%d", i)
    44  		}
    45  
    46  		ret = append(ret, &model.ConfigFile{
    47  			Id:         0,
    48  			Name:       fmt.Sprintf("cpnfig-file-%d", i),
    49  			Namespace:  namespace,
    50  			Group:      group,
    51  			Content:    fmt.Sprintf("cpnfig-file-%d", i),
    52  			Comment:    fmt.Sprintf("cpnfig-file-%d", i),
    53  			Format:     "yaml",
    54  			Flag:       0,
    55  			CreateTime: time.Time{},
    56  			CreateBy:   "polaris",
    57  			ModifyTime: time.Time{},
    58  			ModifyBy:   "polaris",
    59  			Valid:      false,
    60  			Metadata:   map[string]string{},
    61  		})
    62  	}
    63  
    64  	return ret
    65  }
    66  
    67  func Test_configFileStore(t *testing.T) {
    68  	t.Run("创建配置文件-无事务", func(t *testing.T) {
    69  		CreateTableDBHandlerAndRun(t, tblConfigFile, func(t *testing.T, handler BoltHandler) {
    70  
    71  			s := &configFileStore{handler: handler}
    72  
    73  			mocks := mockConfigFile(10, map[string]string{})
    74  
    75  			for i := range mocks {
    76  				waitSave := mocks[i]
    77  				tx, err := s.handler.StartTx()
    78  				assert.NoError(t, err, "%+v", err)
    79  				defer func() {
    80  					_ = tx.Rollback()
    81  				}()
    82  				err = s.CreateConfigFileTx(tx, waitSave)
    83  				assert.NoError(t, err, "%+v", err)
    84  				err = tx.Commit()
    85  				assert.NoError(t, err, "%+v", err)
    86  			}
    87  		})
    88  	})
    89  
    90  	t.Run("查询配置文件", func(t *testing.T) {
    91  		CreateTableDBHandlerAndRun(t, tblConfigFile, func(t *testing.T, handler BoltHandler) {
    92  
    93  			s := &configFileStore{handler: handler}
    94  
    95  			mocks := mockConfigFile(10, map[string]string{})
    96  
    97  			for i := range mocks {
    98  
    99  				waitSave := mocks[i]
   100  				tx, err := s.handler.StartTx()
   101  				assert.NoError(t, err, "%+v", err)
   102  				defer func() {
   103  					_ = tx.Rollback()
   104  				}()
   105  				err = s.CreateConfigFileTx(tx, waitSave)
   106  				assert.NoError(t, err, "%+v", err)
   107  				err = tx.Commit()
   108  				assert.NoError(t, err, "%+v", err)
   109  
   110  				r, err := s.GetConfigFile(waitSave.Namespace, waitSave.Group, waitSave.Name)
   111  				assert.NoError(t, err, "%+v", err)
   112  				assert.NotNil(t, r)
   113  			}
   114  		})
   115  	})
   116  
   117  	t.Run("删除配置文件", func(t *testing.T) {
   118  		CreateTableDBHandlerAndRun(t, tblConfigFile, func(t *testing.T, handler BoltHandler) {
   119  
   120  			s := &configFileStore{handler: handler}
   121  
   122  			mocks := mockConfigFile(10, map[string]string{})
   123  
   124  			for i := range mocks {
   125  
   126  				waitSave := mocks[i]
   127  				tx, err := s.handler.StartTx()
   128  				assert.NoError(t, err, "%+v", err)
   129  				err = s.CreateConfigFileTx(tx, waitSave)
   130  				assert.NoError(t, err, "%+v", err)
   131  				_ = tx.Commit()
   132  
   133  				tx, err = s.handler.StartTx()
   134  				err = s.DeleteConfigFileTx(tx, waitSave.Namespace, waitSave.Group, waitSave.Name)
   135  				assert.NoError(t, err, "%+v", err)
   136  				_ = tx.Commit()
   137  
   138  				tx, err = s.handler.StartTx()
   139  				r, err := s.GetConfigFileTx(tx, waitSave.Namespace, waitSave.Group, waitSave.Name)
   140  				assert.NoError(t, err, "%+v", err)
   141  				assert.Nil(t, r)
   142  				_ = tx.Commit()
   143  			}
   144  		})
   145  	})
   146  
   147  	t.Run("更新配置文件", func(t *testing.T) {
   148  		CreateTableDBHandlerAndRun(t, tblConfigFile, func(t *testing.T, handler BoltHandler) {
   149  
   150  			s := &configFileStore{handler: handler}
   151  
   152  			mocks := mockConfigFile(10, map[string]string{})
   153  
   154  			for i := range mocks {
   155  
   156  				waitSave := mocks[i]
   157  				tx, err := s.handler.StartTx()
   158  				assert.NoError(t, err, "%+v", err)
   159  				err = s.CreateConfigFileTx(tx, waitSave)
   160  				assert.NoError(t, err, "%+v", err)
   161  				_ = tx.Commit()
   162  
   163  				newCf := *waitSave
   164  
   165  				newCf.Comment = "update config file"
   166  
   167  				tx, err = s.handler.StartTx()
   168  				assert.NoError(t, err, "%+v", err)
   169  				err = s.UpdateConfigFileTx(tx, &newCf)
   170  				assert.NoError(t, err, "%+v", err)
   171  				_ = tx.Commit()
   172  
   173  				r, err := s.GetConfigFile(waitSave.Namespace, waitSave.Group, waitSave.Name)
   174  				assert.NoError(t, err, "%+v", err)
   175  
   176  				_n := &newCf
   177  
   178  				r.CreateTime = time.Unix(0, 0)
   179  				r.ModifyTime = time.Unix(0, 0)
   180  				r.ReleaseTime = time.Unix(0, 0)
   181  				_n.CreateTime = time.Unix(0, 0)
   182  				_n.ModifyTime = time.Unix(0, 0)
   183  				_n.ReleaseTime = time.Unix(0, 0)
   184  				assert.Equal(t, _n, r, "expect : %#v, actual : %#v", _n, r)
   185  			}
   186  		})
   187  	})
   188  
   189  	t.Run("查询配置文件", func(t *testing.T) {
   190  		CreateTableDBHandlerAndRun(t, tblConfigFile, func(t *testing.T, handler BoltHandler) {
   191  
   192  			s := &configFileStore{handler: handler}
   193  
   194  			mocks := mockConfigFile(10, map[string]string{})
   195  			results := make([]*model.ConfigFile, 0, len(mocks))
   196  
   197  			for i := range mocks {
   198  				waitSave := mocks[i]
   199  				tx, err := s.handler.StartTx()
   200  				assert.NoError(t, err, "%+v", err)
   201  				err = s.CreateConfigFileTx(tx, waitSave)
   202  				assert.NoError(t, err, "%+v", err)
   203  				_ = tx.Commit()
   204  
   205  				assert.NoError(t, err, "%+v", err)
   206  				results = append(results, waitSave)
   207  			}
   208  
   209  			total, ret, err := s.QueryConfigFiles(map[string]string{
   210  				"namespace": "cpnfig*",
   211  				"group":     "cpnfig*",
   212  				"name":      "cpnfig*",
   213  			}, 0, 100)
   214  
   215  			for i := range ret {
   216  				ret[i].CreateTime = time.Time{}
   217  				ret[i].ModifyTime = time.Time{}
   218  				results[i].CreateTime = time.Time{}
   219  				results[i].ModifyTime = time.Time{}
   220  			}
   221  
   222  			assert.NoError(t, err, "%+v", err)
   223  			assert.Equal(t, len(results), len(ret))
   224  			assert.Equal(t, len(mocks), int(total))
   225  
   226  			total, ret, err = s.QueryConfigFiles(map[string]string{
   227  				"namespace": "qweq",
   228  				"group":     "qweq",
   229  				"name":      "qweq",
   230  			}, 0, 100)
   231  
   232  			for i := range ret {
   233  				ret[i].CreateTime = time.Time{}
   234  				ret[i].ModifyTime = time.Time{}
   235  				results[i].CreateTime = time.Time{}
   236  				results[i].ModifyTime = time.Time{}
   237  			}
   238  
   239  			assert.NoError(t, err, "%+v", err)
   240  			assert.Equal(t, 0, int(total))
   241  			assert.Empty(t, ret)
   242  		})
   243  	})
   244  }