github.com/polarismesh/polaris@v1.17.8/test/integrate/namespace_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  	"time"
    26  
    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  	v1 "github.com/polarismesh/polaris/common/api/v1"
    31  	"github.com/polarismesh/polaris/common/model"
    32  	"github.com/polarismesh/polaris/test/integrate/http"
    33  	"github.com/polarismesh/polaris/test/integrate/resource"
    34  )
    35  
    36  /**
    37   * @brief 测试增删改查命名空间
    38   */
    39  func TestNamespace(t *testing.T) {
    40  	t.Log("test namespace interface")
    41  
    42  	client := http.NewClient(httpserverAddress, httpserverVersion)
    43  
    44  	namespaces := resource.CreateNamespaces()
    45  
    46  	// 创建命名空间
    47  	ret, err := client.CreateNamespaces(namespaces)
    48  	if err != nil {
    49  		t.Fatalf("create namespaces fail: %s", err.Error())
    50  	}
    51  	for index, item := range ret.GetResponses() {
    52  		namespaces[index].Token = item.GetNamespace().GetToken()
    53  	}
    54  	t.Log("create namespaces success")
    55  
    56  	// 查询命名空间
    57  	_, err = client.GetNamespaces(namespaces)
    58  	if err != nil {
    59  		t.Fatalf("get namespaces fail: %s", err.Error())
    60  	}
    61  	t.Log("get namespaces success")
    62  
    63  	// 更新命名空间
    64  	resource.UpdateNamespaces(namespaces)
    65  
    66  	err = client.UpdateNamesapces(namespaces)
    67  	if err != nil {
    68  		t.Fatalf("update namespaces fail: %s", err.Error())
    69  	}
    70  	t.Log("update namespaces success")
    71  
    72  	// 查询命名空间
    73  	_, err = client.GetNamespaces(namespaces)
    74  	if err != nil {
    75  		t.Fatalf("get namespaces fail: %s", err.Error())
    76  	}
    77  	t.Log("get namespaces success")
    78  
    79  	// 删除命名空间
    80  	err = client.DeleteNamespaces(namespaces)
    81  	if err != nil {
    82  		t.Fatalf("delete namespaces fail: %s", err.Error())
    83  	}
    84  	t.Log("delete namespaces success")
    85  }
    86  
    87  // TestCountNamespaceService 统计命名空间下的服务数以及实例数
    88  func TestCountNamespaceService(t *testing.T) {
    89  	t.Log("test namepsace interface")
    90  	client := http.NewClient(httpserverAddress, httpserverVersion)
    91  
    92  	namespaces := resource.CreateNamespaces()
    93  
    94  	// 创建命名空间
    95  	ret, err := client.CreateNamespaces(namespaces)
    96  	if err != nil {
    97  		t.Fatalf("create namespaces fail: %s", err.Error())
    98  	}
    99  	for index, item := range ret.GetResponses() {
   100  		namespaces[index].Token = item.GetNamespace().GetToken()
   101  	}
   102  	t.Log("create namepsaces success")
   103  
   104  	expectRes := make(map[string]model.NamespaceServiceCount)
   105  
   106  	for _, namespace := range ret.Responses {
   107  		createServiceAndInstance(t, &expectRes, client, namespace.Namespace)
   108  	}
   109  
   110  	//
   111  	time.Sleep(time.Duration(5) * time.Second)
   112  
   113  	// 获取namespace info 列表
   114  
   115  	resp, err := client.GetNamespaces(namespaces)
   116  
   117  	for _, namespace := range resp {
   118  		expectVal := expectRes[namespace.GetName().GetValue()]
   119  
   120  		if expectVal.ServiceCount == namespace.TotalServiceCount.GetValue() && expectVal.InstanceCnt.TotalInstanceCount == namespace.TotalInstanceCount.Value {
   121  			continue
   122  		} else {
   123  			t.Fatalf("namespace %s cnt info not expect", namespace.Name.GetValue())
   124  		}
   125  	}
   126  
   127  	t.Logf("TestNamespaceServiceCnt success")
   128  
   129  	// 开始清理所有的数据
   130  }
   131  
   132  // TestDeleteNamespaceWhenHaveService 当命名空间下还有服务时进行删除命名空间的操作
   133  func TestDeleteNamespaceWhenHaveService(t *testing.T) {
   134  	t.Log("test namepsace delete when has service")
   135  	client := http.NewClient(httpserverAddress, httpserverVersion)
   136  
   137  	namespaces := resource.CreateNamespaces()
   138  
   139  	// 创建命名空间
   140  	_, err := client.CreateNamespaces(namespaces)
   141  	if err != nil {
   142  		t.Fatalf("create namespaces fail: %s", err.Error())
   143  	}
   144  	t.Log("create namepsaces success")
   145  
   146  	services := resource.CreateServices(namespaces[0])
   147  	if _, err = client.CreateServices(services); err != nil {
   148  		t.Fatal(err)
   149  	}
   150  
   151  	resp, err := client.DeleteNamespacesGetResp(namespaces)
   152  	if resp != nil {
   153  		if resp.GetCode().GetValue() != v1.NamespaceExistedServices {
   154  			t.Fatalf("delete namespace need return code:NamespaceExistedServices, actual : %d, %s", int(resp.GetCode().GetValue()), resp.GetInfo().GetValue())
   155  		}
   156  	}
   157  	if err != nil && resp == nil {
   158  		t.Fatalf("delete namespaces fail: %s", err.Error())
   159  	}
   160  
   161  	// 删除 service
   162  	if err := client.DeleteServices(services); err != nil {
   163  		t.Fatalf("delete service fail: %+v", err)
   164  	}
   165  
   166  	resp, err = client.DeleteNamespacesGetResp(namespaces)
   167  	if resp != nil {
   168  		if resp.GetCode().GetValue() != v1.ExecuteSuccess {
   169  			t.Fatalf("delete namespace need return code:ExecuteSuccess, actual : %d, %s", int(resp.GetCode().GetValue()), resp.GetInfo().GetValue())
   170  		}
   171  	}
   172  	if err != nil && resp == nil {
   173  		t.Fatalf("delete namespaces fail: %s", err.Error())
   174  	}
   175  }
   176  
   177  // TestDeleteNamespaceWhenHaveConfigGroup 当命名空间下还有配置时进行删除命名空间的操作
   178  func TestDeleteNamespaceWhenHaveConfigGroup(t *testing.T) {
   179  	t.Log("test namepsace interface")
   180  	client := http.NewClient(httpserverAddress, httpserverVersion)
   181  
   182  	namespaces := resource.CreateNamespaces()
   183  
   184  	// 创建命名空间
   185  	_, err := client.CreateNamespaces(namespaces)
   186  	if err != nil {
   187  		t.Fatalf("create namespaces fail: %s", err.Error())
   188  	}
   189  	t.Log("create namepsaces success")
   190  
   191  	groups := resource.MockConfigGroups(namespaces[0])
   192  	if _, err = client.CreateConfigGroup(groups[0]); err != nil {
   193  		t.Fatal(err)
   194  	}
   195  
   196  	resp, err := client.DeleteNamespacesGetResp(namespaces)
   197  	if resp != nil {
   198  		if resp.GetCode().GetValue() != v1.NamespaceExistedConfigGroups {
   199  			t.Fatalf("delete namespace need return code:NamespaceExistedConfigGroups, actual : %d, %s", int(resp.GetCode().GetValue()), resp.GetInfo().GetValue())
   200  		}
   201  	}
   202  	if err != nil && resp == nil {
   203  		t.Fatalf("delete namespaces fail: %s", err.Error())
   204  	}
   205  
   206  	// 删除配置分组
   207  	if _, err := client.DeleteConfigGroup(groups[0]); err != nil {
   208  		t.Fatalf("delete service fail: %+v", err)
   209  	}
   210  
   211  	resp, err = client.DeleteNamespacesGetResp(namespaces)
   212  	if resp != nil {
   213  		if resp.GetCode().GetValue() != v1.ExecuteSuccess {
   214  			t.Fatalf("delete namespace need return code:ExecuteSuccess, actual : %d, %s", int(resp.GetCode().GetValue()), resp.GetInfo().GetValue())
   215  		}
   216  	}
   217  	if err != nil && resp == nil {
   218  		t.Fatalf("delete namespaces fail: %s", err.Error())
   219  	}
   220  }
   221  
   222  func createServiceAndInstance(t *testing.T, expectRes *map[string]model.NamespaceServiceCount, client *http.Client, namespace *apimodel.Namespace) ([]*apiservice.Service, []*apiservice.Instance) {
   223  	services := resource.CreateServices(namespace)
   224  
   225  	_, err := client.CreateServices(services)
   226  
   227  	if err != nil {
   228  		t.Fatal(err)
   229  	}
   230  
   231  	cntVal := &model.NamespaceServiceCount{
   232  		ServiceCount: uint32(len(services)),
   233  		InstanceCnt:  &model.InstanceCount{},
   234  	}
   235  
   236  	finalInstances := make([]*apiservice.Instance, 0)
   237  
   238  	for _, service := range services {
   239  		instances := resource.CreateInstances(service)
   240  		if _, err := client.CreateInstances(instances); err != nil {
   241  			t.Fatal(err)
   242  		}
   243  
   244  		finalInstances = append(finalInstances, instances...)
   245  		cntVal.InstanceCnt.TotalInstanceCount += uint32(len(instances))
   246  	}
   247  
   248  	(*expectRes)[namespace.GetName().GetValue()] = *cntVal
   249  
   250  	return services, finalInstances
   251  }