github.com/polarismesh/polaris@v1.17.8/service/namespace_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 service_test
    19  
    20  import (
    21  	"fmt"
    22  	"testing"
    23  
    24  	apimodel "github.com/polarismesh/specification/source/go/api/v1/model"
    25  	apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage"
    26  
    27  	"github.com/polarismesh/polaris/common/utils"
    28  )
    29  
    30  // create
    31  func (d *DiscoverTestSuit) createCommonNamespace(t *testing.T, id int) (*apimodel.Namespace, *apimodel.Namespace) {
    32  	req := &apimodel.Namespace{
    33  		Name:    utils.NewStringValue(fmt.Sprintf("namespace-%d", id)),
    34  		Comment: utils.NewStringValue(fmt.Sprintf("comment-%d", id)),
    35  		Owners:  utils.NewStringValue(fmt.Sprintf("owner-%d", id)),
    36  	}
    37  	d.cleanNamespace(req.GetName().GetValue())
    38  
    39  	resp := d.NamespaceServer().CreateNamespace(d.DefaultCtx, req)
    40  	if !respSuccess(resp) {
    41  		t.Fatalf("error: %s", resp.GetInfo().GetValue())
    42  	}
    43  
    44  	if resp.GetNamespace().GetToken().GetValue() == "" ||
    45  		req.GetName().GetValue() != resp.GetNamespace().GetName().GetValue() {
    46  		t.Fatalf("errors: %+v", resp)
    47  	}
    48  
    49  	return req, resp.GetNamespace()
    50  }
    51  
    52  // remove
    53  func (d *DiscoverTestSuit) removeCommonNamespaces(t *testing.T, req []*apimodel.Namespace) {
    54  	resp := d.NamespaceServer().DeleteNamespaces(d.DefaultCtx, req)
    55  	if !respSuccess(resp) {
    56  		t.Fatalf("error: %s", resp.GetInfo().GetValue())
    57  	}
    58  }
    59  
    60  // update
    61  func (d *DiscoverTestSuit) updateCommonNamespaces(t *testing.T, req []*apimodel.Namespace) {
    62  	resp := d.NamespaceServer().UpdateNamespaces(d.DefaultCtx, req)
    63  	if !respSuccess(resp) {
    64  		t.Fatalf("error: %s", resp.GetInfo().GetValue())
    65  	}
    66  }
    67  
    68  // 测试新建命名空间
    69  func TestCreateNamespace(t *testing.T) {
    70  
    71  	discoverSuit := &DiscoverTestSuit{}
    72  	if err := discoverSuit.Initialize(); err != nil {
    73  		t.Fatal(err)
    74  	}
    75  	defer discoverSuit.Destroy()
    76  
    77  	t.Run("正常创建命名空间", func(t *testing.T) {
    78  		_, resp := discoverSuit.createCommonNamespace(t, 100)
    79  		defer discoverSuit.cleanNamespace(resp.GetName().GetValue())
    80  		t.Logf("pass")
    81  	})
    82  
    83  	t.Run("新建命名空间,删除,再创建一个同样的,可以成功", func(t *testing.T) {
    84  		req, resp := discoverSuit.createCommonNamespace(t, 10)
    85  		defer discoverSuit.cleanNamespace(req.GetName().GetValue())
    86  
    87  		// remove
    88  		discoverSuit.removeCommonNamespaces(t, []*apimodel.Namespace{resp})
    89  		apiResp := discoverSuit.NamespaceServer().CreateNamespace(discoverSuit.DefaultCtx, req)
    90  		if !respSuccess(apiResp) {
    91  			t.Fatalf("error: %s", apiResp.GetInfo().GetValue())
    92  		}
    93  
    94  		t.Logf("pass")
    95  	})
    96  
    97  	t.Run("新建命名空间和服务,删除命名空间和服务,再创建命名空间", func(t *testing.T) {
    98  		_, namespaceResp := discoverSuit.createCommonNamespace(t, 10)
    99  		defer discoverSuit.cleanNamespace(namespaceResp.GetName().GetValue())
   100  
   101  		_, serviceResp := discoverSuit.createCommonService(t, 100)
   102  		defer discoverSuit.cleanServiceName(serviceResp.GetName().GetValue(), serviceResp.GetNamespace().GetValue())
   103  
   104  		discoverSuit.removeCommonServices(t, []*apiservice.Service{serviceResp})
   105  		discoverSuit.removeCommonNamespaces(t, []*apimodel.Namespace{namespaceResp})
   106  
   107  		_, namespaceResp = discoverSuit.createCommonNamespace(t, 10)
   108  		defer discoverSuit.cleanNamespace(namespaceResp.GetName().GetValue())
   109  	})
   110  }
   111  
   112  // 删除命名空间
   113  func TestRemoveNamespace(t *testing.T) {
   114  	discoverSuit := &DiscoverTestSuit{}
   115  	if err := discoverSuit.Initialize(); err != nil {
   116  		t.Fatal(err)
   117  	}
   118  	defer discoverSuit.Destroy()
   119  
   120  	t.Run("可以删除命名空间", func(t *testing.T) {
   121  		_, resp := discoverSuit.createCommonNamespace(t, 99)
   122  		defer discoverSuit.cleanNamespace(resp.GetName().GetValue())
   123  
   124  		discoverSuit.removeCommonNamespaces(t, []*apimodel.Namespace{resp})
   125  		out := discoverSuit.NamespaceServer().GetNamespaces(discoverSuit.DefaultCtx, map[string][]string{"name": {resp.GetName().GetValue()}})
   126  		if !respSuccess(out) {
   127  			t.Fatalf("error: %s", out.GetInfo().GetValue())
   128  		}
   129  		if len(out.GetNamespaces()) != 0 {
   130  			t.Fatalf("error: %d", len(out.GetNamespaces()))
   131  		}
   132  	})
   133  
   134  	t.Run("批量删除命名空间", func(t *testing.T) {
   135  		var reqs []*apimodel.Namespace
   136  		for i := 0; i < 20; i++ {
   137  			_, resp := discoverSuit.createCommonNamespace(t, i)
   138  			defer discoverSuit.cleanNamespace(resp.GetName().GetValue())
   139  			reqs = append(reqs, resp)
   140  		}
   141  
   142  		_ = discoverSuit.DiscoverServer().Cache().TestUpdate()
   143  		discoverSuit.removeCommonNamespaces(t, reqs)
   144  		t.Logf("pass")
   145  	})
   146  
   147  	t.Run("新建命名空间和服务,直接删除名空间,因为有服务,删除会失败", func(t *testing.T) {
   148  		_, namespaceResp := discoverSuit.createCommonNamespace(t, 100)
   149  		defer discoverSuit.cleanNamespace(namespaceResp.GetName().GetValue())
   150  
   151  		serviceReq := &apiservice.Service{
   152  			Name:      utils.NewStringValue("abc"),
   153  			Namespace: namespaceResp.GetName(),
   154  			Owners:    utils.NewStringValue("123"),
   155  		}
   156  		if resp := discoverSuit.DiscoverServer().CreateServices(discoverSuit.DefaultCtx, []*apiservice.Service{serviceReq}); !respSuccess(resp) {
   157  			t.Fatalf("errror: %s", resp.GetInfo().GetValue())
   158  		}
   159  		defer discoverSuit.cleanServiceName(serviceReq.GetName().GetValue(), serviceReq.GetNamespace().GetValue())
   160  
   161  		resp := discoverSuit.NamespaceServer().DeleteNamespace(discoverSuit.DefaultCtx, namespaceResp)
   162  		if resp.GetCode().GetValue() != uint32(apimodel.Code_NamespaceExistedServices) {
   163  			t.Fatalf("error: %s", resp.GetInfo().GetValue())
   164  		}
   165  		t.Logf("%s", resp.GetInfo().GetValue())
   166  	})
   167  }
   168  
   169  // 更新命名空间
   170  func TestUpdateNamespace(t *testing.T) {
   171  	discoverSuit := &DiscoverTestSuit{}
   172  	if err := discoverSuit.Initialize(); err != nil {
   173  		t.Fatal(err)
   174  	}
   175  	defer discoverSuit.Destroy()
   176  
   177  	t.Run("正常更新命名空间", func(t *testing.T) {
   178  		req, resp := discoverSuit.createCommonNamespace(t, 200)
   179  		defer discoverSuit.cleanNamespace(resp.GetName().GetValue())
   180  
   181  		_ = discoverSuit.DiscoverServer().Cache().TestUpdate()
   182  
   183  		req.Token = resp.Token
   184  		req.Comment = utils.NewStringValue("new-comment")
   185  
   186  		discoverSuit.updateCommonNamespaces(t, []*apimodel.Namespace{req})
   187  		t.Logf("pass")
   188  	})
   189  }
   190  
   191  // 获取命名空间列表
   192  func TestGetNamespaces(t *testing.T) {
   193  
   194  	discoverSuit := &DiscoverTestSuit{}
   195  	if err := discoverSuit.Initialize(); err != nil {
   196  		t.Fatal(err)
   197  	}
   198  	defer discoverSuit.Destroy()
   199  
   200  	t.Run("正常获取命名空间,可以正常获取", func(t *testing.T) {
   201  		total := 50
   202  		for i := 0; i < total; i++ {
   203  			req, _ := discoverSuit.createCommonNamespace(t, i+200)
   204  			defer discoverSuit.cleanNamespace(req.GetName().GetValue())
   205  		}
   206  
   207  		resp := discoverSuit.NamespaceServer().GetNamespaces(discoverSuit.DefaultCtx, map[string][]string{})
   208  		if !respSuccess(resp) {
   209  			t.Fatalf("error: %s", resp.GetInfo().GetValue())
   210  		}
   211  		if resp.GetSize().GetValue() == uint32(total) {
   212  			t.Fatalf("error: %d", resp.GetSize().GetValue())
   213  		}
   214  	})
   215  
   216  	t.Run("前缀匹配可以正常过滤", func(t *testing.T) {
   217  		total := 50
   218  		for i := 0; i < total; i++ {
   219  			req, _ := discoverSuit.createCommonNamespace(t, i+200)
   220  			defer discoverSuit.cleanNamespace(req.GetName().GetValue())
   221  		}
   222  
   223  		query := map[string][]string{
   224  			"offset": {"0"},
   225  			"limit":  {"100"},
   226  			"name":   {"namespace-20*"},
   227  		}
   228  		resp := discoverSuit.NamespaceServer().GetNamespaces(discoverSuit.DefaultCtx, query)
   229  		if !respSuccess(resp) {
   230  			t.Fatalf("error: %s", resp.GetInfo().GetValue())
   231  		}
   232  		if resp.GetSize().GetValue() != 10 {
   233  			t.Fatalf("error: %d", resp.GetSize().GetValue())
   234  		}
   235  	})
   236  
   237  	t.Run("模糊匹配可以正常过滤", func(t *testing.T) {
   238  		total := 50
   239  		for i := 0; i < total; i++ {
   240  			req, _ := discoverSuit.createCommonNamespace(t, i+200)
   241  			defer discoverSuit.cleanNamespace(req.GetName().GetValue())
   242  		}
   243  
   244  		query := map[string][]string{
   245  			"offset": {"0"},
   246  			"limit":  {"100"},
   247  			"name":   {"*espace-21*"},
   248  		}
   249  		resp := discoverSuit.NamespaceServer().GetNamespaces(discoverSuit.DefaultCtx, query)
   250  		if !respSuccess(resp) {
   251  			t.Fatalf("error: %s", resp.GetInfo().GetValue())
   252  		}
   253  		if resp.GetSize().GetValue() != 10 {
   254  			t.Fatalf("error: %d", resp.GetSize().GetValue())
   255  		}
   256  	})
   257  
   258  	t.Run("分页参数可以正常过滤", func(t *testing.T) {
   259  		total := 20
   260  		for i := 0; i < total; i++ {
   261  			req, _ := discoverSuit.createCommonNamespace(t, i+200)
   262  			defer discoverSuit.cleanNamespace(req.GetName().GetValue())
   263  		}
   264  
   265  		query := map[string][]string{
   266  			"offset": {"10"},
   267  			"limit":  {"10"},
   268  		}
   269  		resp := discoverSuit.NamespaceServer().GetNamespaces(discoverSuit.DefaultCtx, query)
   270  		if !respSuccess(resp) {
   271  			t.Fatalf("error: %s", resp.GetInfo().GetValue())
   272  		}
   273  		if resp.GetSize().GetValue() != 10 {
   274  			t.Fatalf("error: %d", resp.GetSize().GetValue())
   275  		}
   276  	})
   277  }
   278  
   279  // 测试命名空间的token
   280  func TestNamespaceToken(t *testing.T) {
   281  
   282  	discoverSuit := &DiscoverTestSuit{}
   283  	if err := discoverSuit.Initialize(); err != nil {
   284  		t.Fatal(err)
   285  	}
   286  	defer discoverSuit.Destroy()
   287  
   288  	t.Run("可以正常获取到namespaceToken", func(t *testing.T) {
   289  		_, namespaceResp := discoverSuit.createCommonNamespace(t, 1)
   290  		defer discoverSuit.cleanNamespace(namespaceResp.GetName().GetValue())
   291  
   292  		resp := discoverSuit.NamespaceServer().GetNamespaceToken(discoverSuit.DefaultCtx, namespaceResp)
   293  		if !respSuccess(resp) {
   294  			t.Fatalf("error: %s", resp.GetInfo().GetValue())
   295  		}
   296  		if resp.GetNamespace().GetToken().GetValue() != namespaceResp.GetToken().GetValue() {
   297  			t.Fatalf("error")
   298  		}
   299  	})
   300  	t.Run("可以正常更新namespace的token", func(t *testing.T) {
   301  		_, namespaceResp := discoverSuit.createCommonNamespace(t, 2)
   302  		defer discoverSuit.cleanNamespace(namespaceResp.GetName().GetValue())
   303  
   304  		resp := discoverSuit.NamespaceServer().UpdateNamespaceToken(discoverSuit.DefaultCtx, namespaceResp)
   305  		if !respSuccess(resp) {
   306  			t.Fatalf("error: %s", resp.GetInfo().GetValue())
   307  		}
   308  		if resp.GetNamespace().GetToken().GetValue() == namespaceResp.GetToken().GetValue() {
   309  			t.Fatalf("error")
   310  		}
   311  		t.Logf("%s %s", resp.GetNamespace().GetToken().GetValue(),
   312  			namespaceResp.GetToken().GetValue())
   313  	})
   314  }