github.com/polarismesh/polaris@v1.17.8/test/integrate/common_test.go (about)

     1  //go:build integration
     2  // +build integration
     3  
     4  /**
     5   * Tencent is pleased to support the open source community by making Polaris available.
     6   *
     7   * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
     8   *
     9   * Licensed under the BSD 3-Clause License (the "License");
    10   * you may not use this file except in compliance with the License.
    11   * You may obtain a copy of the License at
    12   *
    13   * https://opensource.org/licenses/BSD-3-Clause
    14   *
    15   * Unless required by applicable law or agreed to in writing, software distributed
    16   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    17   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
    18   * specific language governing permissions and limitations under the License.
    19   */
    20  
    21  package test
    22  
    23  import (
    24  	"testing"
    25  
    26  	apiconfig "github.com/polarismesh/specification/source/go/api/v1/config_manage"
    27  	apimodel "github.com/polarismesh/specification/source/go/api/v1/model"
    28  	apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage"
    29  
    30  	"github.com/polarismesh/polaris/test/integrate/http"
    31  	"github.com/polarismesh/polaris/test/integrate/resource"
    32  )
    33  
    34  const (
    35  	httpserverVersion = "v1"
    36  	httpserverAddress = "127.0.0.1:8090"
    37  	grpcServerAddress = "127.0.0.1"
    38  )
    39  
    40  type (
    41  	DiscoveryRunner func(t *testing.T, clientHttp *http.Client, namespaces []*apimodel.Namespace, services []*apiservice.Service)
    42  
    43  	ConfigRunner func(t *testing.T, clientHttp *http.Client, namespaces []*apimodel.Namespace, configGroups []*apiconfig.ConfigFileGroup)
    44  )
    45  
    46  func DiscoveryRunAndInitResource(t *testing.T, runner DiscoveryRunner) {
    47  	clientHTTP := http.NewClient(httpserverAddress, httpserverVersion)
    48  
    49  	namespaces := resource.CreateNamespaces()
    50  	services := resource.CreateServices(namespaces[0])
    51  
    52  	// 创建命名空间
    53  	ret, err := clientHTTP.CreateNamespaces(namespaces)
    54  	if err != nil {
    55  		t.Fatalf("create namespaces fail")
    56  	}
    57  	for index, item := range ret.GetResponses() {
    58  		namespaces[index].Token = item.GetNamespace().GetToken()
    59  	}
    60  	t.Log("create namespaces success")
    61  
    62  	// 创建服务
    63  	ret, err = clientHTTP.CreateServices(services)
    64  	if err != nil {
    65  		t.Fatalf("create services fail")
    66  	}
    67  	for index, item := range ret.GetResponses() {
    68  		services[index].Token = item.GetService().GetToken()
    69  	}
    70  	t.Log("create services success")
    71  
    72  	defer func() {
    73  		// 删除服务
    74  		err = clientHTTP.DeleteServices(services)
    75  		if err != nil {
    76  			t.Fatalf("delete services fail")
    77  		}
    78  		t.Log("delete services success")
    79  
    80  		// 删除命名空间
    81  		err = clientHTTP.DeleteNamespaces(namespaces)
    82  		if err != nil {
    83  			t.Fatalf("delete namespaces fail")
    84  		}
    85  		t.Log("delete namespaces success")
    86  	}()
    87  
    88  	runner(t, clientHTTP, namespaces, services)
    89  }
    90  
    91  func ConfigCenterRunAndInitResource(t *testing.T, runner ConfigRunner) {
    92  	clientHTTP := http.NewClient(httpserverAddress, httpserverVersion)
    93  
    94  	namespaces := resource.CreateNamespaces()
    95  	groups := resource.MockConfigGroups(namespaces[0])
    96  
    97  	// 创建命名空间
    98  	ret, err := clientHTTP.CreateNamespaces(namespaces)
    99  	if err != nil {
   100  		t.Fatalf("create namespaces fail")
   101  	}
   102  	for index, item := range ret.GetResponses() {
   103  		namespaces[index].Token = item.GetNamespace().GetToken()
   104  	}
   105  	t.Log("create namespaces success")
   106  
   107  	// 创建服务
   108  	_, err = clientHTTP.CreateConfigGroup(groups[0])
   109  	if err != nil {
   110  		t.Fatalf("create config group fail")
   111  	}
   112  	t.Log("create config group success")
   113  
   114  	defer func() {
   115  		// 删除配置分组
   116  		_, err := clientHTTP.DeleteConfigGroup(groups[0])
   117  		if err != nil {
   118  			t.Fatalf("delete config group fail")
   119  		}
   120  		t.Log("delete config group success")
   121  
   122  		// 删除命名空间
   123  		err = clientHTTP.DeleteNamespaces(namespaces)
   124  		if err != nil {
   125  			t.Fatalf("delete namespaces fail")
   126  		}
   127  		t.Log("delete namespaces success")
   128  	}()
   129  
   130  	runner(t, clientHTTP, namespaces, groups)
   131  }