dubbo.apache.org/dubbo-go/v3@v3.1.1/remoting/xds/mapping/handler_test.go (about) 1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package mapping 19 20 import ( 21 "net/http" 22 "os" 23 "testing" 24 "time" 25 26 "dubbo.apache.org/dubbo-go/v3/common/constant" 27 "dubbo.apache.org/dubbo-go/v3/remoting/xds/common" 28 "dubbo.apache.org/dubbo-go/v3/xds/client/mocks" 29 structpb "github.com/golang/protobuf/ptypes/struct" 30 "github.com/stretchr/testify/assert" 31 "github.com/stretchr/testify/mock" 32 ) 33 34 const ( 35 istioDebugPortFoo = "38080" 36 istiodDebugAddrStrFoo = "127.0.0.1:" + istioDebugPortFoo 37 localPodServiceAddr = "dubbo-go-app.default.svc.cluster.local:20000" 38 serviceKey1 = "providers:api.Greeter::" 39 serviceKey2 = "providers:grpc.reflection.v1alpha.ServerReflection::" 40 metadataService1 = `{"` + serviceKey1 + `":"` + localPodServiceAddr + `"}` 41 metadataService1And2 = `{"` + serviceKey1 + `":"` + localPodServiceAddr + `","` + serviceKey2 + `":"` + localPodServiceAddr + `"}` 42 metadataService2 = `{"` + serviceKey2 + `":"` + localPodServiceAddr + `"}` 43 metadataNoService = `{}` 44 istioTokenPathFoo = "/tmp/dubbo-go-mesh-test-token" 45 istioTokenFoo = "mock-token" 46 ) 47 48 func TestNewInterfaceMapHandler(t *testing.T) { 49 mockXDSClient := &mocks.XDSClient{} 50 interfaceMapHandler := NewInterfaceMapHandlerImpl(mockXDSClient, istioTokenPathFoo, common.NewHostNameOrIPAddr(istiodDebugAddrStrFoo), common.NewHostNameOrIPAddr(localPodServiceAddr), false) 51 assert.NotNil(t, interfaceMapHandler) 52 } 53 54 func TestInterfaceMapHandlerRegisterAndUnregister(t *testing.T) { 55 mockXDSClient := &mocks.XDSClient{} 56 mockXDSClient.On("SetMetadata", mock.AnythingOfType("*structpb.Struct")).Return(nil) 57 58 interfaceMapHandler := NewInterfaceMapHandlerImpl(mockXDSClient, istioTokenPathFoo, common.NewHostNameOrIPAddr(istiodDebugAddrStrFoo), common.NewHostNameOrIPAddr(localPodServiceAddr), false) 59 60 assert.Nil(t, interfaceMapHandler.Register(serviceKey1)) 61 assert.Nil(t, interfaceMapHandler.Register(serviceKey2)) 62 assert.Nil(t, interfaceMapHandler.UnRegister(serviceKey1)) 63 assert.Nil(t, interfaceMapHandler.UnRegister(serviceKey2)) 64 65 mockXDSClient.AssertCalled(t, "SetMetadata", mock.MatchedBy(getMatchFunction(metadataService1))) 66 mockXDSClient.AssertCalled(t, "SetMetadata", mock.MatchedBy(getMatchFunction(metadataService1And2))) 67 mockXDSClient.AssertCalled(t, "SetMetadata", mock.MatchedBy(getMatchFunction(metadataService2))) 68 mockXDSClient.AssertCalled(t, "SetMetadata", mock.MatchedBy(getMatchFunction(metadataNoService))) 69 } 70 71 func TestGetServiceUniqueKeyHostAddrMapFromPilot(t *testing.T) { 72 mockXDSClient := &mocks.XDSClient{} 73 interfaceMapHandler := NewInterfaceMapHandlerImpl(mockXDSClient, istioTokenPathFoo, common.NewHostNameOrIPAddr(istiodDebugAddrStrFoo), common.NewHostNameOrIPAddr(localPodServiceAddr), false) 74 assert.Nil(t, generateMockToken()) 75 76 // 1. start mock http server 77 http.HandleFunc("/debug/adsz", func(writer http.ResponseWriter, request *http.Request) { 78 if len(request.Header[authorizationHeader]) != 1 { 79 writer.Write([]byte("")) 80 return 81 } 82 if request.Header[authorizationHeader][0] != istiodTokenPrefix+istioTokenFoo { 83 writer.Write([]byte("")) 84 return 85 } 86 writer.Write([]byte(debugAdszDataFoo)) 87 }) 88 go http.ListenAndServe(":"+istioDebugPortFoo, nil) 89 time.Sleep(time.Second) 90 91 // 2. get map from client 92 hostAddr1, err := interfaceMapHandler.GetHostAddrMap(serviceKey1) 93 assert.Nil(t, err) 94 assert.Equal(t, localPodServiceAddr, hostAddr1) 95 96 // 3. assert 97 hostAddr2, err := interfaceMapHandler.GetHostAddrMap(serviceKey2) 98 assert.Nil(t, err) 99 assert.Equal(t, localPodServiceAddr, hostAddr2) 100 101 } 102 103 func getMatchFunction(metadata string) func(abc *structpb.Struct) bool { 104 return func(abc *structpb.Struct) bool { 105 metadatas, ok := abc.Fields[constant.XDSMetadataLabelsKey] 106 if !ok { 107 return false 108 } 109 subFields := metadatas.GetStructValue().GetFields() 110 subValue, ok2 := subFields[constant.XDSMetadataDubboGoMapperKey] 111 if !ok2 { 112 return false 113 } 114 if subValue.GetStringValue() != metadata { 115 return false 116 } 117 return true 118 } 119 } 120 121 func generateMockToken() error { 122 return os.WriteFile(istioTokenPathFoo, []byte(istioTokenFoo), 0777) 123 }