github.com/polarismesh/polaris@v1.17.8/store/boltdb/routing_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 boltdb
    19  
    20  import (
    21  	"fmt"
    22  	"strconv"
    23  	"testing"
    24  	"time"
    25  
    26  	"github.com/stretchr/testify/assert"
    27  
    28  	"github.com/polarismesh/polaris/common/model"
    29  )
    30  
    31  const (
    32  	routeCount = 5
    33  )
    34  
    35  func TestRoutingStore_CreateRoutingConfig(t *testing.T) {
    36  	handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"})
    37  	if err != nil {
    38  		t.Fatal(err)
    39  	}
    40  
    41  	defer handler.Close()
    42  
    43  	rStore := &routingStore{handler: handler}
    44  
    45  	for i := 0; i < routeCount; i++ {
    46  		rStore.CreateRoutingConfig(&model.RoutingConfig{
    47  			ID:         "testid" + strconv.Itoa(i),
    48  			InBounds:   "v1" + strconv.Itoa(i),
    49  			OutBounds:  "v2" + strconv.Itoa(i),
    50  			Revision:   "revision" + strconv.Itoa(i),
    51  			Valid:      true,
    52  			CreateTime: time.Now(),
    53  			ModifyTime: time.Now(),
    54  		})
    55  	}
    56  
    57  	if err != nil {
    58  		t.Fatal(err)
    59  	}
    60  }
    61  
    62  func TestRoutingStore_GetRoutingConfigs(t *testing.T) {
    63  	CreateTableDBHandlerAndRun(t, "RoutingConfig", func(t *testing.T, handler BoltHandler) {
    64  
    65  		rStore := &routingStore{handler: handler}
    66  		sStore := &serviceStore{handler: handler}
    67  
    68  		for i := 0; i < routeCount; i++ {
    69  			err := sStore.AddService(&model.Service{
    70  				ID:        "testid" + strconv.Itoa(i),
    71  				Name:      "testid" + strconv.Itoa(i),
    72  				Namespace: "testid" + strconv.Itoa(i),
    73  			})
    74  			assert.NoError(t, err)
    75  
    76  			err = rStore.CreateRoutingConfig(&model.RoutingConfig{
    77  				ID:         "testid" + strconv.Itoa(i),
    78  				InBounds:   "v1" + strconv.Itoa(i),
    79  				OutBounds:  "v2" + strconv.Itoa(i),
    80  				Revision:   "revision" + strconv.Itoa(i),
    81  				Valid:      true,
    82  				CreateTime: time.Now(),
    83  				ModifyTime: time.Now(),
    84  			})
    85  			assert.NoError(t, err)
    86  		}
    87  
    88  		totalCount, rs, err := rStore.GetRoutingConfigs(nil, 0, 20)
    89  		if err != nil {
    90  			t.Fatal(err)
    91  		}
    92  		if totalCount != routeCount {
    93  			t.Fatal(fmt.Sprintf("routing total count not match, expect %d, got %d", routeCount, totalCount))
    94  		}
    95  		if len(rs) != routeCount {
    96  			t.Fatal(fmt.Sprintf("routing count not match, expect %d, got %d", routeCount, len(rs)))
    97  		}
    98  		for _, r := range rs {
    99  			fmt.Printf("routing conf is %+v\n", r.Config)
   100  		}
   101  	})
   102  }
   103  
   104  func TestRoutingStore_UpdateRoutingConfig(t *testing.T) {
   105  	CreateTableDBHandlerAndRun(t, "TestRoutingStore_GetRoutingConfigsForCache", func(t *testing.T, handler BoltHandler) {
   106  
   107  		rStore := &routingStore{handler: handler}
   108  		sStore := &serviceStore{handler: handler}
   109  
   110  		for i := 0; i < routeCount; i++ {
   111  			err := sStore.AddService(&model.Service{
   112  				ID:        "testid" + strconv.Itoa(i),
   113  				Name:      "testid" + strconv.Itoa(i),
   114  				Namespace: "testid" + strconv.Itoa(i),
   115  			})
   116  			assert.NoError(t, err)
   117  
   118  			conf := &model.RoutingConfig{
   119  				ID:        "testid" + strconv.Itoa(i),
   120  				InBounds:  "vv1" + strconv.Itoa(i),
   121  				OutBounds: "vv2" + strconv.Itoa(i),
   122  				Revision:  "revi" + strconv.Itoa(i),
   123  			}
   124  
   125  			err = rStore.CreateRoutingConfig(conf)
   126  			assert.NoError(t, err)
   127  
   128  			err = rStore.UpdateRoutingConfig(conf)
   129  			assert.NoError(t, err)
   130  		}
   131  
   132  		// check update result
   133  		totalCount, rs, err := rStore.GetRoutingConfigs(nil, 0, 20)
   134  		if err != nil {
   135  			t.Fatal(err)
   136  		}
   137  		if totalCount != routeCount {
   138  			t.Fatal(fmt.Sprintf("routing total count not match, expect %d, got %d", routeCount, totalCount))
   139  		}
   140  		if len(rs) != routeCount {
   141  			t.Fatal(fmt.Sprintf("routing count not match, expect %d, got %d", routeCount, len(rs)))
   142  		}
   143  		for _, r := range rs {
   144  			fmt.Printf("routing conf is %+v\n", r.Config)
   145  		}
   146  	})
   147  }
   148  
   149  func TestRoutingStore_GetRoutingConfigsForCache(t *testing.T) {
   150  	CreateTableDBHandlerAndRun(t, "TestRoutingStore_GetRoutingConfigsForCache",
   151  		func(t *testing.T, handler BoltHandler) {
   152  			rStore := &routingStore{handler: handler}
   153  			sStore := &serviceStore{handler: handler}
   154  
   155  			for i := 0; i < routeCount; i++ {
   156  				err := sStore.AddService(&model.Service{
   157  					ID:        "testid" + strconv.Itoa(i),
   158  					Name:      "testid" + strconv.Itoa(i),
   159  					Namespace: "testid" + strconv.Itoa(i),
   160  				})
   161  				assert.NoError(t, err)
   162  
   163  				err = rStore.CreateRoutingConfig(&model.RoutingConfig{
   164  					ID:         "testid" + strconv.Itoa(i),
   165  					InBounds:   "v1" + strconv.Itoa(i),
   166  					OutBounds:  "v2" + strconv.Itoa(i),
   167  					Revision:   "revision" + strconv.Itoa(i),
   168  					Valid:      true,
   169  					CreateTime: time.Now(),
   170  					ModifyTime: time.Now(),
   171  				})
   172  				assert.NoError(t, err)
   173  			}
   174  
   175  			// get create modify time
   176  			totalCount, rs, err := rStore.GetRoutingConfigs(nil, 0, 20)
   177  			if err != nil {
   178  				t.Fatal(err)
   179  			}
   180  			if totalCount != routeCount {
   181  				t.Fatal(fmt.Sprintf("routing total count not match, expect %d, got %d", routeCount, totalCount))
   182  			}
   183  			if len(rs) != routeCount {
   184  				t.Fatal(fmt.Sprintf("routing count not match, expect %d, got %d", routeCount, len(rs)))
   185  			}
   186  
   187  			rss, err := rStore.GetRoutingConfigsForCache(rs[2].Config.ModifyTime, false)
   188  			if err != nil {
   189  				t.Fatal(err)
   190  			}
   191  			if len(rss) != routeCount-2 {
   192  				t.Fatal(fmt.Sprintf("routing config count mismatch, except %d, got %d", routeCount-2, len(rss)))
   193  			}
   194  		})
   195  }
   196  
   197  func TestRoutingStore_GetRoutingConfigWithService(t *testing.T) {
   198  
   199  	// find service
   200  	handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"})
   201  	if err != nil {
   202  		t.Fatal(err)
   203  	}
   204  
   205  	defer handler.Close()
   206  
   207  	sStore := &serviceStore{handler: handler}
   208  	err = sStore.AddService(&model.Service{
   209  		ID:        "testid3",
   210  		Name:      "test-svc-name",
   211  		Namespace: "test-svc-namespace",
   212  		Owner:     "test-owner",
   213  		Token:     "test-token",
   214  	})
   215  	if err != nil {
   216  		t.Fatal(err)
   217  	}
   218  
   219  	rStore := &routingStore{handler: handler}
   220  	rc, err := rStore.GetRoutingConfigWithService("test-svc-name", "test-svc-namespace")
   221  	if err != nil {
   222  		t.Fatal(err)
   223  	}
   224  	t.Logf("get routing config with service %+v", rc)
   225  }
   226  
   227  func TestRoutingStore_GetRoutingConfigWithID(t *testing.T) {
   228  	handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"})
   229  	if err != nil {
   230  		t.Fatal(err)
   231  	}
   232  
   233  	defer handler.Close()
   234  
   235  	rStore := &routingStore{handler: handler}
   236  
   237  	rc, err := rStore.GetRoutingConfigWithID("testid0")
   238  	if err != nil {
   239  		t.Fatal(err)
   240  	}
   241  	fmt.Printf("get routing conf %+v\n", rc)
   242  }
   243  
   244  func TestRoutingStore_DeleteRoutingConfig(t *testing.T) {
   245  	handler, err := NewBoltHandler(&BoltConfig{FileName: "./table.bolt"})
   246  	if err != nil {
   247  		t.Fatal(err)
   248  	}
   249  
   250  	defer handler.Close()
   251  
   252  	rStore := &routingStore{handler: handler}
   253  	for i := 0; i < routeCount; i++ {
   254  		err := rStore.DeleteRoutingConfig("testid" + strconv.Itoa(i))
   255  		assert.Nil(t, err, "err must nil")
   256  	}
   257  }