github.com/polarismesh/polaris@v1.17.8/service/l5service_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
    19  
    20  // . "github.com/smartystreets/goconvey/convey"
    21  
    22  // // safe get cache data
    23  // func safeSyncByAgentCmd(ctx context.Context, sbac *l5.Cl5SyncByAgentCmd) (*l5.Cl5SyncByAgentAckCmd, error) {
    24  // 	time.Sleep(updateCacheInterval)
    25  // 	return server.SyncByAgentCmd(ctx, sbac)
    26  // }
    27  
    28  // // get maxFlow from t_route
    29  // func getMaxRouteFlow(t *testing.T) int {
    30  // 	maxStr := "select IFNULL(max(fflow),0) from t_route"
    31  // 	var maxFlow int
    32  // 	err := db.QueryRow(maxStr).Scan(&maxFlow)
    33  // 	switch {
    34  // 	case err == sql.ErrNoRows:
    35  // 		maxFlow = 0
    36  // 	case err != nil:
    37  // 		t.Fatalf("error: %s", err.Error())
    38  // 	}
    39  
    40  // 	return maxFlow + 1
    41  // }
    42  
    43  // // add l5 t_route
    44  // func addL5Route(t *testing.T, ip, modID, cmdID int32, setID string) {
    45  // 	maxFlow := getMaxRouteFlow(t)
    46  // 	str := "replace into t_route(fip, fmodid, fcmdid, fsetId, fflag, fstamp, fflow) values(?,?,?,?,0,now(),?)"
    47  // 	if _, err := db.Exec(str, ip, modID, cmdID, setID, maxFlow+1); err != nil {
    48  // 		t.Fatalf("error: %s", err.Error())
    49  // 	}
    50  // }
    51  
    52  // // 删除t_route
    53  // func deleteL5Route(t *testing.T, ip, modID, cmdID int32) {
    54  // 	maxFlow := getMaxRouteFlow(t)
    55  // 	str := "update t_route set fflag = 1, fflow = ? where fip = ? and fmodid = ? and fcmdid = ?"
    56  // 	if _, err := db.Exec(str, maxFlow, ip, modID, cmdID); err != nil {
    57  // 		t.Fatalf("error: %s", err.Error())
    58  // 	}
    59  // }
    60  
    61  // // 创建带SetID的实例列表
    62  // // setID可以为空
    63  // func createInstanceWithSetID(service *api.Service, index int, setIDs string, weights string) *api.Instance {
    64  // 	instance := &api.Instance{
    65  // 		Service:      service.GetName(),
    66  // 		Namespace:    service.GetNamespace(),
    67  // 		Host:         utils.NewStringValue(fmt.Sprintf("10.235.25.%d", index)),
    68  // 		Port:         utils.NewUInt32Value(8080),
    69  // 		ServiceToken: service.GetToken(),
    70  // 	}
    71  // 	if setIDs != "" {
    72  // 		instance.Metadata = map[string]string{"internal-cl5-setId": setIDs}
    73  // 	}
    74  // 	if weights != "" {
    75  // 		if instance.Metadata == nil {
    76  // 			instance.Metadata = make(map[string]string)
    77  // 		}
    78  // 		instance.Metadata["internal-cl5-weight"] = weights
    79  // 	}
    80  // 	resp := server.CreateInstance(defaultCtx, instance)
    81  // 	So(respSuccess(resp), ShouldEqual, true)
    82  // 	return resp.GetInstance()
    83  // }
    84  
    85  // // 测试兼容l5协议的流程
    86  // func TestSyncByAgentCmd(t *testing.T) {
    87  // 	Convey("获取老Cl5的Sid数据", t, func() {
    88  // 		reqCmd := &l5.Cl5SyncByAgentCmd{AgentIp: proto.Int32(11111), SyncFlow: proto.Int32(22222)}
    89  // 		modID := int32(64850433)
    90  // 		cmdID := int32(65540)
    91  // 		service := &api.Service{
    92  // 			Name:      utils.NewStringValue(fmt.Sprintf("%d:%d", modID, cmdID)),
    93  // 			Namespace: utils.NewStringValue("default"),
    94  // 			Owners:    utils.NewStringValue("aa"),
    95  // 		}
    96  // 		serviceResp := server.CreateService(defaultCtx, service)
    97  // 		So(respSuccess(serviceResp), ShouldEqual, true)
    98  // 		defer cleanServiceName(service.GetName().GetValue(), service.GetNamespace().GetValue())
    99  
   100  // 		Convey("正常数据获取", func() {
   101  // 			reqCmd.OptList = &l5.Cl5OptList{
   102  // 				Opt: []*l5.Cl5OptObj{{ModId: proto.Int32(modID), CmdId: proto.Int32(cmdID)}},
   103  // 			}
   104  
   105  // 			ack, err := safeSyncByAgentCmd(defaultCtx, reqCmd)
   106  // 			So(err, ShouldBeNil)
   107  // 			So(ack.GetAgentIp(), ShouldEqual, reqCmd.GetAgentIp())
   108  // 			So(ack.GetSyncFlow(), ShouldEqual, reqCmd.GetSyncFlow()+1)
   109  // 			So(len(ack.GetServList().GetServ()), ShouldEqual, 0)
   110  
   111  // 			for i := 0; i < 5; i++ {
   112  // 				resp := createInstanceWithSetID(serviceResp.GetService(), i, "", "")
   113  // 				defer cleanInstance(resp.GetId().GetValue())
   114  // 			}
   115  
   116  // 			ack, _ = safeSyncByAgentCmd(defaultCtx, reqCmd)
   117  // 			So(len(ack.GetServList().GetServ()), ShouldEqual, 5)
   118  // 		})
   119  // 		Convey("一个实例属于一个set功能验证", func() {
   120  // 			// 新建一些带set的被调
   121  // 			for i := 0; i < 10; i++ {
   122  // 				resp := createInstanceWithSetID(serviceResp.GetService(), i, fmt.Sprintf("SET_%d", i%2), "")
   123  // 				defer cleanInstance(resp.GetId().GetValue())
   124  // 			}
   125  
   126  // 			ack, _ := safeSyncByAgentCmd(defaultCtx, reqCmd)
   127  // 			So(len(ack.GetServList().GetServ()), ShouldEqual, 0)
   128  
   129  // 			addL5Route(t, reqCmd.GetAgentIp(), modID, cmdID, "SET_1")
   130  // 			defer deleteL5Route(t, reqCmd.GetAgentIp(), modID, cmdID)
   131  // 			ack, _ = safeSyncByAgentCmd(defaultCtx, reqCmd)
   132  // 			So(len(ack.GetServList().GetServ()), ShouldEqual, 5) // SET_0 SET_1 各一半
   133  // 		})
   134  // 		Convey("一个实例多个set功能验证", func() {
   135  // 			// 新建一些带set的被调
   136  // 			setIDs := "SET_X"
   137  // 			weights := "0"
   138  // 			for i := 0; i < 10; i++ {
   139  // 				setIDs = setIDs + fmt.Sprintf(",SET_%d", i)
   140  // 				weights = weights + fmt.Sprintf(",%d", (i+1)*100)
   141  // 				resp := createInstanceWithSetID(serviceResp.GetService(), i, setIDs, weights)
   142  // 				defer cleanInstance(resp.GetId().GetValue())
   143  // 			}
   144  // 			// SET_X,SET_0,  			0,100
   145  // 			// SET_X,SET_0,SET_1 		0,100,200
   146  // 			// SET_X,SET_0,SET_1,SET_2 	0,100,200,300
   147  // 			// ...
   148  // 			ack, _ := safeSyncByAgentCmd(defaultCtx, reqCmd)
   149  // 			So(len(ack.GetServList().GetServ()), ShouldEqual, 0)
   150  // 			for i := 0; i < 10; i++ {
   151  // 				addL5Route(t, reqCmd.GetAgentIp(), modID, cmdID, fmt.Sprintf("SET_%d", i))
   152  // 				ack, _ = safeSyncByAgentCmd(defaultCtx, reqCmd)
   153  // 				So(len(ack.GetServList().GetServ()), ShouldEqual, 10-i)
   154  // 				for _, callee := range ack.GetServList().GetServ() {
   155  // 					So(callee.GetWeight(), ShouldEqual, (i+1)*100)
   156  // 				}
   157  // 			}
   158  
   159  // 			// SET_X weight=0
   160  // 			addL5Route(t, reqCmd.GetAgentIp(), modID, cmdID, "SET_X")
   161  // 			ack, _ = safeSyncByAgentCmd(defaultCtx, reqCmd)
   162  // 			So(len(ack.GetServList().GetServ()), ShouldEqual, 0)
   163  
   164  // 			addL5Route(t, reqCmd.GetAgentIp(), modID, cmdID, fmt.Sprintf("SET_%d", 20))
   165  // 			defer deleteL5Route(t, reqCmd.GetAgentIp(), modID, cmdID)
   166  // 			ack, _ = safeSyncByAgentCmd(defaultCtx, reqCmd)
   167  // 			So(len(ack.GetServList().GetServ()), ShouldEqual, 0)
   168  // 		})
   169  // 	})
   170  // }
   171  
   172  // // 测试根据埋点server获取到后端serverList的功能
   173  // func TestCl5DiscoverTest(t *testing.T) {
   174  // 	createDiscoverServer := func(name string) *api.Service {
   175  // 		service := &api.Service{
   176  // 			Name:      utils.NewStringValue(name),
   177  // 			Namespace: utils.NewStringValue("Polaris"),
   178  // 			Owners:    utils.NewStringValue("my"),
   179  // 		}
   180  // 		resp := server.CreateService(defaultCtx, service)
   181  // 		So(respSuccess(resp), ShouldEqual, true)
   182  // 		So(resp.GetService(), ShouldNotBeNil)
   183  // 		return resp.GetService()
   184  // 	}
   185  // 	createDiscoverInstance := func(service *api.Service, index int) *api.Instance {
   186  // 		instance := &api.Instance{
   187  // 			Service:      service.GetName(),
   188  // 			Namespace:    service.GetNamespace(),
   189  // 			ServiceToken: service.GetToken(),
   190  // 			Host:         utils.NewStringValue(fmt.Sprintf("10.0.0.%d", index)),
   191  // 			Port:         utils.NewUInt32Value(7779),
   192  // 			Protocol:     utils.NewStringValue("l5pb"),
   193  // 			Healthy:      utils.NewBoolValue(true),
   194  // 		}
   195  // 		resp := server.CreateInstance(defaultCtx, instance)
   196  // 		So(respSuccess(resp), ShouldEqual, true)
   197  // 		So(resp.GetInstance(), ShouldNotBeNil)
   198  // 		return resp.GetInstance()
   199  // 	}
   200  
   201  // 	Convey("测试根据埋点server获取到后端serverList的功能", t, func() {
   202  // 		reqCmd := &l5.Cl5SyncByAgentCmd{
   203  // 			AgentIp: proto.Int32(123),
   204  // 			OptList: &l5.Cl5OptList{Opt: []*l5.Cl5OptObj{{ModId: proto.Int32(111), CmdId: proto.Int32(222)}}},
   205  // 		}
   206  // 		name := "test-api.cl5.discover"
   207  // 		discover := createDiscoverServer(name)
   208  // 		defer cleanServiceName(discover.GetName().GetValue(), discover.GetNamespace().GetValue())
   209  // 		instance := createDiscoverInstance(discover, 0)
   210  // 		defer cleanInstance(instance.GetId().GetValue())
   211  
   212  // 		discover1 := createDiscoverServer(name + ".1")
   213  // 		defer cleanServiceName(discover1.GetName().GetValue(), discover1.GetNamespace().GetValue())
   214  // 		discover2 := createDiscoverServer(name + ".2")
   215  // 		defer cleanServiceName(discover2.GetName().GetValue(), discover2.GetNamespace().GetValue())
   216  
   217  // 		ctx := context.WithValue(defaultCtx, utils.Cl5ServerCluster{}, name)
   218  // 		ctx = context.WithValue(ctx, utils.Cl5ServerProtocol{}, "l5pb")
   219  // 		Convey("只有默认集群,则返回默认集群的数据", func() {
   220  // 			ack, _ := safeSyncByAgentCmd(ctx, reqCmd)
   221  // 			So(len(ack.GetL5SvrList().GetIp()), ShouldEqual, 1)
   222  // 			t.Logf("%+v", ack)
   223  // 		})
   224  // 		Convey("不同请求IP获取到不同的集群", func() {
   225  // 			discover.Metadata = map[string]string{"internal-cluster-count": "2"}
   226  // 			So(respSuccess(server.UpdateService(defaultCtx, discover)), ShouldEqual, true)
   227  // 			instance1 := createDiscoverInstance(discover1, 1)
   228  // 			defer cleanInstance(instance1.GetId().GetValue())
   229  // 			instance2 := createDiscoverInstance(discover1, 2)
   230  // 			defer cleanInstance(instance2.GetId().GetValue())
   231  
   232  // 			instance3 := createDiscoverInstance(discover2, 3)
   233  // 			defer cleanInstance(instance3.GetId().GetValue())
   234  // 			instance4 := createDiscoverInstance(discover2, 4)
   235  // 			defer cleanInstance(instance4.GetId().GetValue())
   236  // 			instance5 := createDiscoverInstance(discover2, 5)
   237  // 			defer cleanInstance(instance5.GetId().GetValue())
   238  
   239  // 			reqCmd.AgentIp = proto.Int32(56352420) // clusterIndex := ip %count + 1
   240  // 			ack, _ := safeSyncByAgentCmd(ctx, reqCmd)
   241  // 			So(len(ack.GetL5SvrList().GetIp()), ShouldEqual, 2) // cluster1
   242  
   243  // 			reqCmd.AgentIp = proto.Int32(56352421)
   244  // 			ack, _ = safeSyncByAgentCmd(ctx, reqCmd)
   245  // 			So(len(ack.GetL5SvrList().GetIp()), ShouldEqual, 3) // cluster2
   246  // 		})
   247  // 	})
   248  // }
   249  
   250  // // 测试别名sid可以正常获取数据
   251  // func TestCl5AliasSyncCmd(t *testing.T) {
   252  // 	reqCmd := &l5.Cl5SyncByAgentCmd{
   253  // 		AgentIp:  proto.Int32(11111),
   254  // 		SyncFlow: proto.Int32(22222),
   255  // 	}
   256  // 	testFunc := func(namespace string) {
   257  // 		Convey(fmt.Sprintf("%s, alias sid, discover ok", namespace), t, func() {
   258  // 			service := &api.Service{
   259  // 				Name:      utils.NewStringValue("my-name-for-alias"),
   260  // 				Namespace: utils.NewStringValue(namespace),
   261  // 				Owners:    utils.NewStringValue("aa"),
   262  // 			}
   263  // 			resp := server.CreateService(defaultCtx, service)
   264  // 			So(respSuccess(resp), ShouldEqual, true)
   265  // 			serviceResp := resp.Service
   266  // 			defer cleanServiceName(serviceResp.Name.Value, serviceResp.Namespace.Value)
   267  
   268  // 			resp = createCommonAlias(serviceResp, "", serviceResp.Namespace.GetValue(), api.AliasType_CL5SID)
   269  // 			So(respSuccess(resp), ShouldEqual, true)
   270  // 			defer cleanServiceName(resp.Alias.Alias.Value, serviceResp.Namespace.Value)
   271  // 			modID, cmdID := parseStr2Sid(resp.Alias.Alias.Value)
   272  // 			reqCmd.OptList = &l5.Cl5OptList{
   273  // 				Opt: []*l5.Cl5OptObj{{ModId: proto.Int32(int32(modID)), CmdId: proto.Int32(int32(cmdID))}},
   274  // 			}
   275  
   276  // 			count := 5
   277  // 			for i := 0; i < count; i++ {
   278  // 				_, instanceResp := createCommonInstance(t, serviceResp, i)
   279  // 				defer cleanInstance(instanceResp.GetId().GetValue())
   280  // 			}
   281  // 			time.Sleep(updateCacheInterval)
   282  
   283  // 			ack, _ := server.SyncByAgentCmd(defaultCtx, reqCmd)
   284  // 			So(ack.GetAgentIp(), ShouldEqual, reqCmd.GetAgentIp())
   285  // 			So(ack.GetSyncFlow(), ShouldEqual, reqCmd.GetSyncFlow()+1)
   286  // 			So(len(ack.GetServList().GetServ()), ShouldEqual, count)
   287  
   288  // 		})
   289  // 	}
   290  
   291  // 	namespaces := []string{"default", "Polaris"}
   292  // 	for _, entry := range namespaces {
   293  // 		testFunc(entry)
   294  // 	}
   295  // }