github.com/polarismesh/polaris@v1.17.8/test/integrate/instance_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  	"google.golang.org/protobuf/types/known/wrapperspb"
    27  
    28  	"github.com/polarismesh/polaris/test/integrate/http"
    29  	"github.com/polarismesh/polaris/test/integrate/resource"
    30  )
    31  
    32  /**
    33   * @brief 测试增删改查服务实例
    34   */
    35  func TestInstance(t *testing.T) {
    36  	t.Log("test instance interface")
    37  
    38  	client := http.NewClient(httpserverAddress, httpserverVersion)
    39  
    40  	namespaces := resource.CreateNamespaces()
    41  	services := resource.CreateServices(namespaces[0])
    42  
    43  	// 创建命名空间
    44  	ret, err := client.CreateNamespaces(namespaces)
    45  	if err != nil {
    46  		t.Fatalf("create namespaces fail: %s", err.Error())
    47  	}
    48  	for index, item := range ret.GetResponses() {
    49  		namespaces[index].Token = item.GetNamespace().GetToken()
    50  	}
    51  	t.Log("create namespaces success")
    52  
    53  	// 创建服务
    54  	ret, err = client.CreateServices(services)
    55  	if err != nil {
    56  		t.Fatalf("create services fail: %s", err.Error())
    57  	}
    58  	for index, item := range ret.GetResponses() {
    59  		services[index].Token = item.GetService().GetToken()
    60  	}
    61  	t.Log("create services success")
    62  
    63  	// -------------------------------------------------------
    64  
    65  	instances := resource.CreateInstances(services[0])
    66  
    67  	// 创建实例
    68  	ret, err = client.CreateInstances(instances)
    69  	if err != nil {
    70  		t.Fatalf("create instances fail: %s", err.Error())
    71  	}
    72  	for index, item := range ret.GetResponses() {
    73  		instances[index].Id = item.GetInstance().GetId()
    74  	}
    75  	t.Log("create instances success")
    76  
    77  	// 查询实例
    78  	err = client.GetInstances(instances)
    79  	if err != nil {
    80  		t.Fatalf("get instances fail: %s", err.Error())
    81  	}
    82  	t.Log("get instances success")
    83  
    84  	// 更新实例
    85  	resource.UpdateInstances(instances)
    86  
    87  	err = client.UpdateInstances(instances)
    88  	if err != nil {
    89  		t.Fatalf("update instances fail: %s", err.Error())
    90  	}
    91  	t.Log("update instances success")
    92  
    93  	// 查询实例
    94  	err = client.GetInstances(instances)
    95  	if err != nil {
    96  		t.Fatalf("get instances fail: %s", err.Error())
    97  	}
    98  	t.Log("get instances success")
    99  
   100  	// 删除实例
   101  	err = client.DeleteInstances(instances)
   102  	if err != nil {
   103  		t.Fatalf("delete instances fail: %s", err.Error())
   104  	}
   105  	t.Log("delete instances success")
   106  
   107  	// -------------------------------------------------------
   108  
   109  	// 删除服务
   110  	err = client.DeleteServices(services)
   111  	if err != nil {
   112  		t.Fatalf("delete services fail: %s", err.Error())
   113  	}
   114  	t.Log("delete services success")
   115  
   116  	// 删除命名空间
   117  	err = client.DeleteNamespaces(namespaces)
   118  	if err != nil {
   119  		t.Fatalf("delete namespaces fail: %s", err.Error())
   120  	}
   121  	t.Log("delete namespaces success")
   122  }
   123  
   124  // TestInstanceWithoutService 测试注册实例的时候,没有创建服务时可以自动创建服务出来
   125  func TestInstanceWithoutService(t *testing.T) {
   126  	t.Log("test instance interface")
   127  
   128  	client := http.NewClient(httpserverAddress, httpserverVersion)
   129  
   130  	namespaces := resource.CreateNamespaces()
   131  	services := resource.CreateServices(namespaces[0])
   132  
   133  	for i := range services {
   134  		services[i].Name = wrapperspb.String("WithoutService_" + services[i].Name.GetValue())
   135  	}
   136  
   137  	// 创建命名空间
   138  	ret, err := client.CreateNamespaces(namespaces)
   139  	if err != nil {
   140  		t.Fatalf("create namespaces fail: %s", err.Error())
   141  	}
   142  	for index, item := range ret.GetResponses() {
   143  		namespaces[index].Token = item.GetNamespace().GetToken()
   144  	}
   145  	t.Log("create namespaces success")
   146  
   147  	// -------------------------------------------------------
   148  
   149  	instances := resource.CreateInstances(services[0])
   150  
   151  	// 创建实例
   152  	ret, err = client.CreateInstances(instances)
   153  	if err != nil {
   154  		t.Fatalf("create instances fail: %s", err.Error())
   155  	}
   156  	for index, item := range ret.GetResponses() {
   157  		instances[index].Id = item.GetInstance().GetId()
   158  	}
   159  	t.Log("create instances success")
   160  
   161  	// 查询实例
   162  	err = client.GetInstances(instances)
   163  	if err != nil {
   164  		t.Fatalf("get instances fail: %s", err.Error())
   165  	}
   166  	t.Log("get instances success")
   167  
   168  	// 更新实例
   169  	resource.UpdateInstances(instances)
   170  
   171  	err = client.UpdateInstances(instances)
   172  	if err != nil {
   173  		t.Fatalf("update instances fail: %s", err.Error())
   174  	}
   175  	t.Log("update instances success")
   176  
   177  	// 查询实例
   178  	err = client.GetInstances(instances)
   179  	if err != nil {
   180  		t.Fatalf("get instances fail: %s", err.Error())
   181  	}
   182  	t.Log("get instances success")
   183  
   184  	// 删除实例
   185  	err = client.DeleteInstances(instances)
   186  	if err != nil {
   187  		t.Fatalf("delete instances fail: %s", err.Error())
   188  	}
   189  	t.Log("delete instances success")
   190  
   191  	// -------------------------------------------------------
   192  
   193  	// 删除服务
   194  	err = client.DeleteServices(services)
   195  	if err != nil {
   196  		t.Fatalf("delete services fail: %s", err.Error())
   197  	}
   198  	t.Log("delete services success")
   199  
   200  	// 删除命名空间
   201  	err = client.DeleteNamespaces(namespaces)
   202  	if err != nil {
   203  		t.Fatalf("delete namespaces fail: %s", err.Error())
   204  	}
   205  	t.Log("delete namespaces success")
   206  }