github.com/polarismesh/polaris@v1.17.8/test/integrate/client_grpc_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 apiconfig "github.com/polarismesh/specification/source/go/api/v1/config_manage" 28 apimodel "github.com/polarismesh/specification/source/go/api/v1/model" 29 apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage" 30 "github.com/stretchr/testify/assert" 31 "google.golang.org/protobuf/types/known/wrapperspb" 32 33 api "github.com/polarismesh/polaris/common/api/v1" 34 "github.com/polarismesh/polaris/common/utils" 35 "github.com/polarismesh/polaris/test/integrate/grpc" 36 "github.com/polarismesh/polaris/test/integrate/http" 37 "github.com/polarismesh/polaris/test/integrate/resource" 38 ) 39 40 /** 41 * @brief 测试客户端GRPC接口 42 */ 43 func TestClientGRPC_DiscoverInstance(t *testing.T) { 44 DiscoveryRunAndInitResource(t, 45 func(t *testing.T, clientHttp *http.Client, namespaces []*apimodel.Namespace, services []*apiservice.Service) { 46 clientGRPC, err := grpc.NewClient(grpcServerAddress) 47 if err != nil { 48 t.Fatalf("new grpc client fail") 49 } 50 defer clientGRPC.Close() 51 52 client := resource.CreateClient(0) 53 instances := resource.CreateInstances(services[0]) 54 55 t.Run("GRPC——上报SDK客户端信息", func(t *testing.T) { 56 // 上报客户端信息 57 err = clientGRPC.ReportClient(client) 58 if err != nil { 59 t.Fatalf("report client fail") 60 } 61 t.Log("report client success") 62 }) 63 64 t.Run("GRPC——实例注册", func(t *testing.T) { 65 // 注册服务实例 66 err = clientGRPC.RegisterInstance(instances[0]) 67 if err != nil { 68 t.Fatalf("register instance fail") 69 } 70 t.Log("register instance success") 71 }) 72 73 time.Sleep(3 * time.Second) // 延迟 74 75 var revision string 76 t.Run("GRPC——首次发现实例", func(t *testing.T) { 77 // 查询服务实例 78 err = clientGRPC.Discover(apiservice.DiscoverRequest_INSTANCE, services[0], func(resp *apiservice.DiscoverResponse) { 79 assert.Equal(t, api.ExecuteSuccess, resp.Code.GetValue(), "discover instance must be success") 80 assert.Equal(t, 1, len(resp.Instances), "instance size must not be zero") 81 revision = resp.Service.GetRevision().GetValue() 82 }) 83 if err != nil { 84 t.Fatalf("discover instance fail") 85 } 86 t.Log("discover instance success") 87 }) 88 89 time.Sleep(time.Second) 90 t.Run("GRPC——二次发现实例", func(t *testing.T) { 91 // 查询服务实例 92 copySvc := &(*services[0]) 93 copySvc.Revision = utils.NewStringValue(revision) 94 95 err = clientGRPC.Discover(apiservice.DiscoverRequest_INSTANCE, copySvc, func(resp *apiservice.DiscoverResponse) { 96 assert.Equal(t, api.DataNoChange, resp.Code.GetValue(), "discover instance must be datanotchange") 97 }) 98 if err != nil { 99 t.Fatalf("discover instance fail") 100 } 101 t.Log("discover instance success") 102 }) 103 104 t.Run("GRPC——客户端心跳上报", func(t *testing.T) { 105 // 上报心跳 106 if err = clientGRPC.Heartbeat(instances[0]); err != nil { 107 t.Fatalf("instance heartbeat fail") 108 } 109 t.Log("instance heartbeat success") 110 }) 111 112 t.Run("GRPC——控制台修改实例信息", func(t *testing.T) { 113 114 testMeta := map[string]string{ 115 "internal-mock-label": "polaris-auto-mock-test", 116 } 117 118 copyIns := &(*instances[0]) 119 copyIns.Metadata = testMeta 120 121 if err := clientHttp.UpdateInstances([]*apiservice.Instance{copyIns}); err != nil { 122 t.Fatalf("update instance fail : %s", err) 123 } 124 125 time.Sleep(5 * time.Second) 126 127 err = clientGRPC.Discover(apiservice.DiscoverRequest_INSTANCE, services[0], func(resp *apiservice.DiscoverResponse) { 128 assert.Equal(t, api.ExecuteSuccess, resp.Code.GetValue(), "discover instance must be success") 129 assert.Equal(t, 1, len(resp.Instances), "instance size must not be zero") 130 131 testMeta["version"] = instances[0].Version.GetValue() 132 testMeta["protocol"] = instances[0].Protocol.GetValue() 133 134 assert.True(t, resp.Instances[0].Metadata["internal-mock-label"] == "polaris-auto-mock-test", "instance metadata actual : %v", instances[0].Metadata) 135 }) 136 if err != nil { 137 t.Fatalf("discover instance fail") 138 } 139 t.Log("discover after update instance success") 140 }) 141 142 t.Run("GRPC——反注册实例", func(t *testing.T) { 143 // 反注册服务实例 144 err = clientGRPC.DeregisterInstance(instances[0]) 145 if err != nil { 146 t.Fatalf("deregister instance fail") 147 } 148 149 time.Sleep(2 * time.Second) 150 151 err = clientGRPC.Discover(apiservice.DiscoverRequest_INSTANCE, services[0], func(resp *apiservice.DiscoverResponse) { 152 assert.Equalf(t, api.ExecuteSuccess, resp.Code.GetValue(), 153 "discover instance must success, actual code: %d", resp.GetCode().GetValue()) 154 assert.Equal(t, 0, len(resp.Instances), "instance size must be zero") 155 }) 156 157 t.Log("deregister instance success") 158 }) 159 }) 160 } 161 162 func TestClientGRPC_DiscoverServices(t *testing.T) { 163 DiscoveryRunAndInitResource(t, 164 func(t *testing.T, clientHttp *http.Client, namespaces []*apimodel.Namespace, services []*apiservice.Service) { 165 clientGRPC, err := grpc.NewClient(grpcServerAddress) 166 if err != nil { 167 t.Fatalf("new grpc client fail") 168 } 169 defer clientGRPC.Close() 170 171 newNs := resource.CreateNamespaces() 172 newSvcs := resource.CreateServices(newNs[0]) 173 174 t.Run("命名空间下未创建服务", func(t *testing.T) { 175 resp, err := clientGRPC.DiscoverRequest(&apiservice.DiscoverRequest{ 176 Type: apiservice.DiscoverRequest_SERVICES, 177 Service: &apiservice.Service{ 178 Namespace: &wrapperspb.StringValue{Value: newNs[0].Name.Value}, 179 }, 180 }) 181 if err != nil { 182 t.Fatalf("discover services fail") 183 } 184 185 assert.True(t, len(resp.Services) == 0, "discover services response need empty") 186 }) 187 188 t.Run("命名空间下创建了服务", func(t *testing.T) { 189 190 // 创建服务 191 _, err := clientHttp.CreateServices(newSvcs) 192 if err != nil { 193 t.Fatalf("create services fail") 194 } 195 t.Log("create services success") 196 197 time.Sleep(5 * time.Second) 198 199 resp, err := clientGRPC.DiscoverRequest(&apiservice.DiscoverRequest{ 200 Type: apiservice.DiscoverRequest_SERVICES, 201 Service: &apiservice.Service{ 202 Namespace: &wrapperspb.StringValue{Value: newNs[0].Name.Value}, 203 }, 204 }) 205 if err != nil { 206 t.Fatalf("discover services fail") 207 } 208 209 assert.False(t, len(resp.Services) == 0, "discover services response not empty") 210 assert.Truef(t, len(newSvcs) == len(resp.Services), 211 "discover services size not equal, expect : %d, actual : %s", len(newSvcs), len(resp.Services)) 212 }) 213 214 }) 215 } 216 217 func Test_QueryGroups(t *testing.T) { 218 ConfigCenterRunAndInitResource(t, 219 func(t *testing.T, clientHttp *http.Client, namespaces []*apimodel.Namespace, configGroups []*apiconfig.ConfigFileGroup) { 220 221 }) 222 }