github.com/polarismesh/polaris@v1.17.8/config/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 config_test
    19  
    20  import (
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  
    25  	api "github.com/polarismesh/polaris/common/api/v1"
    26  )
    27  
    28  var (
    29  	randomGroupPrefix = "randomGroup"
    30  	randomGroupSize   = uint32(7)
    31  )
    32  
    33  // TestConfigFileGroupCRUD 测试配置文件组增删改查
    34  func TestConfigFileGroupCRUD(t *testing.T) {
    35  	testSuit := &ConfigCenterTest{}
    36  	if err := testSuit.Initialize(); err != nil {
    37  		t.Fatal(err)
    38  	}
    39  
    40  	t.Cleanup(func() {
    41  		if err := testSuit.clearTestData(); err != nil {
    42  			t.Fatal(err)
    43  		}
    44  		testSuit.Destroy()
    45  	})
    46  
    47  	// 查询不存在的 group
    48  	t.Run("step1-query-none", func(t *testing.T) {
    49  		rsp := testSuit.ConfigServer().QueryConfigFileGroups(testSuit.DefaultCtx, map[string]string{
    50  			"namespace": testNamespace,
    51  			"group":     testGroup,
    52  			"offset":    "0",
    53  			"limit":     "1",
    54  		})
    55  		assert.Equal(t, api.ExecuteSuccess, rsp.Code.GetValue())
    56  		assert.Equal(t, 0, len(rsp.ConfigFileGroups))
    57  	})
    58  
    59  	// 创建 group
    60  	t.Run("step2-create", func(t *testing.T) {
    61  		rsp := testSuit.ConfigServer().CreateConfigFileGroup(testSuit.DefaultCtx, assembleConfigFileGroup())
    62  		assert.Equal(t, api.ExecuteSuccess, rsp.Code.GetValue())
    63  
    64  		rsp2 := testSuit.ConfigServer().CreateConfigFileGroup(testSuit.DefaultCtx, assembleConfigFileGroup())
    65  		assert.Equal(t, uint32(api.ExistedResource), rsp2.Code.GetValue())
    66  	})
    67  
    68  	// 再次查询 group,能查询到上一步创建的 group
    69  	t.Run("step3-query-existed", func(t *testing.T) {
    70  		rsp := testSuit.ConfigServer().QueryConfigFileGroups(testSuit.DefaultCtx, map[string]string{
    71  			"namespace": testNamespace,
    72  			"group":     testGroup,
    73  			"offset":    "0",
    74  			"limit":     "1",
    75  		})
    76  		assert.Equal(t, api.ExecuteSuccess, rsp.Code.GetValue())
    77  		assert.Equal(t, 1, len(rsp.ConfigFileGroups))
    78  	})
    79  
    80  	// 创建配置文件
    81  	t.Run("create-config-files", func(t *testing.T) {
    82  		rsp := testSuit.ConfigServer().CreateConfigFile(testSuit.DefaultCtx, assembleConfigFile())
    83  		assert.Equal(t, api.ExecuteSuccess, rsp.Code.GetValue())
    84  
    85  		rsp2 := testSuit.ConfigServer().SearchConfigFile(testSuit.DefaultCtx, map[string]string{
    86  			"namespace": testNamespace,
    87  			"group":     testGroup,
    88  			"offset":    "0",
    89  			"limit":     "10",
    90  		})
    91  		assert.Equal(t, api.ExecuteSuccess, rsp2.Code.GetValue())
    92  		assert.Equal(t, uint32(1), rsp2.Total.GetValue())
    93  	})
    94  
    95  	// 删除 group
    96  	t.Run("step4-delete", func(t *testing.T) {
    97  		rsp := testSuit.ConfigServer().DeleteConfigFile(testSuit.DefaultCtx, assembleConfigFile())
    98  		assert.Equal(t, api.ExecuteSuccess, rsp.Code.GetValue(), rsp.GetInfo().GetValue())
    99  
   100  		rsp = testSuit.ConfigServer().DeleteConfigFileGroup(testSuit.DefaultCtx, testNamespace, testGroup)
   101  		assert.Equal(t, api.ExecuteSuccess, rsp.Code.GetValue(), rsp.GetInfo().GetValue())
   102  
   103  		rsp2 := testSuit.ConfigServer().SearchConfigFile(testSuit.DefaultCtx, map[string]string{
   104  			"namespace": testNamespace,
   105  			"group":     testGroup,
   106  			"offset":    "0",
   107  			"limit":     "10",
   108  		})
   109  		assert.Equal(t, api.ExecuteSuccess, rsp2.Code.GetValue())
   110  		assert.Equal(t, uint32(0), rsp2.GetTotal().GetValue())
   111  	})
   112  
   113  	// 再次查询group,由于被删除,所以查不到
   114  	t.Run("step5-query-none", func(t *testing.T) {
   115  		rsp := testSuit.ConfigServer().QueryConfigFileGroups(testSuit.DefaultCtx, map[string]string{
   116  			"namespace": testNamespace,
   117  			"group":     testGroup,
   118  			"offset":    "0",
   119  			"limit":     "1",
   120  		})
   121  		assert.Equal(t, api.ExecuteSuccess, rsp.Code.GetValue())
   122  		assert.Equal(t, 0, len(rsp.ConfigFileGroups))
   123  	})
   124  
   125  	// 创建 7个随机 group 和一个固定的 group
   126  	t.Run("step6-create", func(t *testing.T) {
   127  		for i := 0; i < int(randomGroupSize); i++ {
   128  			rsp := testSuit.ConfigServer().CreateConfigFileGroup(testSuit.DefaultCtx, assembleRandomConfigFileGroup())
   129  			assert.Equal(t, api.ExecuteSuccess, rsp.Code.GetValue())
   130  		}
   131  
   132  		rsp2 := testSuit.ConfigServer().CreateConfigFileGroup(testSuit.DefaultCtx, assembleConfigFileGroup())
   133  		assert.Equal(t, api.ExecuteSuccess, rsp2.Code.GetValue(), rsp2.GetInfo().GetValue())
   134  	})
   135  
   136  	// 模糊查询
   137  	t.Run("step7-query-random", func(t *testing.T) {
   138  		rsp := testSuit.ConfigServer().QueryConfigFileGroups(testSuit.DefaultCtx, map[string]string{
   139  			"namespace": testNamespace,
   140  			"name":      randomGroupPrefix + "*",
   141  			"offset":    "0",
   142  			"limit":     "2",
   143  		})
   144  		assert.Equal(t, api.ExecuteSuccess, rsp.Code.GetValue())
   145  		assert.Equal(t, 2, len(rsp.ConfigFileGroups))
   146  		assert.Equal(t, randomGroupSize, rsp.Total.GetValue())
   147  	})
   148  
   149  	// 测试翻页
   150  	t.Run("step8-query-by-page", func(t *testing.T) {
   151  		// 最后一页
   152  		rsp := testSuit.ConfigServer().QueryConfigFileGroups(testSuit.DefaultCtx, map[string]string{
   153  			"namespace": testNamespace,
   154  			"name":      randomGroupPrefix + "*",
   155  			"offset":    "6",
   156  			"limit":     "2",
   157  		})
   158  		assert.Equal(t, api.ExecuteSuccess, rsp.Code.GetValue())
   159  		assert.Equal(t, 1, len(rsp.ConfigFileGroups))
   160  		assert.Equal(t, randomGroupSize, rsp.Total.GetValue())
   161  
   162  		// 超出页范围
   163  		rsp2 := testSuit.ConfigServer().QueryConfigFileGroups(testSuit.DefaultCtx, map[string]string{
   164  			"namespace": testNamespace,
   165  			"name":      randomGroupPrefix + "*",
   166  			"offset":    "8",
   167  			"limit":     "2",
   168  		})
   169  		assert.Equal(t, api.ExecuteSuccess, rsp2.Code.GetValue())
   170  		assert.Equal(t, 0, len(rsp2.ConfigFileGroups))
   171  		assert.Equal(t, randomGroupSize, rsp2.Total.GetValue())
   172  	})
   173  }