github.com/polarismesh/polaris@v1.17.8/config/config_file_template_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  	apiconfig "github.com/polarismesh/specification/source/go/api/v1/config_manage"
    24  	"github.com/stretchr/testify/assert"
    25  
    26  	api "github.com/polarismesh/polaris/common/api/v1"
    27  	"github.com/polarismesh/polaris/common/utils"
    28  )
    29  
    30  const (
    31  	templateName1 = "t1"
    32  	templateName2 = "t2"
    33  )
    34  
    35  // TestConfigFileTemplateCRUD the base test for config file template
    36  func TestConfigFileTemplateCRUD(t *testing.T) {
    37  	testSuit := &ConfigCenterTest{}
    38  	if err := testSuit.Initialize(); err != nil {
    39  		t.Fatal(err)
    40  	}
    41  	t.Cleanup(func() {
    42  		testSuit.Destroy()
    43  	})
    44  
    45  	defer func() {
    46  		if err := testSuit.clearTestData(); err != nil {
    47  			t.Fatal(err)
    48  		}
    49  	}()
    50  
    51  	t.Run("first-query", func(t *testing.T) {
    52  		queryRsp := testSuit.ConfigServer().GetConfigFileTemplate(testSuit.DefaultCtx, templateName1)
    53  		assert.Equal(t, api.NotFoundResource, queryRsp.Code.Value)
    54  	})
    55  
    56  	template1 := assembleConfigFileTemplate(templateName1)
    57  	t.Run("first-create", func(t *testing.T) {
    58  		createRsp := testSuit.ConfigServer().CreateConfigFileTemplate(testSuit.DefaultCtx, template1)
    59  		assert.Equal(t, api.ExecuteSuccess, createRsp.Code.GetValue())
    60  		//repeat create
    61  		createRsp = testSuit.ConfigServer().CreateConfigFileTemplate(testSuit.DefaultCtx, template1)
    62  		assert.Equal(t, api.ExistedResource, createRsp.Code.GetValue(), createRsp.GetInfo().GetValue())
    63  	})
    64  
    65  	t.Run("second-query", func(t *testing.T) {
    66  		queryRsp := testSuit.ConfigServer().GetConfigFileTemplate(testSuit.DefaultCtx, templateName1)
    67  		assert.Equal(t, api.ExecuteSuccess, queryRsp.Code.Value)
    68  		assert.Equal(t, template1.Name.GetValue(), queryRsp.ConfigFileTemplate.Name.GetValue())
    69  		assert.Equal(t, template1.Content.GetValue(), queryRsp.ConfigFileTemplate.Content.GetValue())
    70  		assert.Equal(t, template1.Comment.GetValue(), queryRsp.ConfigFileTemplate.Comment.GetValue())
    71  		assert.Equal(t, template1.Format.GetValue(), queryRsp.ConfigFileTemplate.Format.GetValue())
    72  	})
    73  
    74  	template2 := assembleConfigFileTemplate(templateName2)
    75  	t.Run("second-create", func(t *testing.T) {
    76  		createRsp := testSuit.ConfigServer().CreateConfigFileTemplate(testSuit.DefaultCtx, template2)
    77  		assert.Equal(t, api.ExecuteSuccess, createRsp.Code.GetValue())
    78  	})
    79  
    80  	t.Run("query-all", func(t *testing.T) {
    81  		rsp := testSuit.ConfigServer().GetAllConfigFileTemplates(testSuit.DefaultCtx)
    82  		assert.Equal(t, api.ExecuteSuccess, rsp.Code.GetValue())
    83  		assert.True(t, 2 <= len(rsp.ConfigFileTemplates))
    84  	})
    85  }
    86  
    87  func assembleConfigFileTemplate(name string) *apiconfig.ConfigFileTemplate {
    88  	return &apiconfig.ConfigFileTemplate{
    89  		Name:     utils.NewStringValue(name),
    90  		Content:  utils.NewStringValue("some content"),
    91  		Comment:  utils.NewStringValue("comment"),
    92  		Format:   utils.NewStringValue("json"),
    93  		CreateBy: utils.NewStringValue("testUser"),
    94  		ModifyBy: utils.NewStringValue("testUser"),
    95  	}
    96  }