dubbo.apache.org/dubbo-go/v3@v3.1.1/remoting/xds/mapping/debug_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  	"encoding/json"
    22  	"testing"
    23  )
    24  
    25  import (
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  const (
    30  	debugAdszDataFoo             = `{"totalClients":2,"clients":[{"connectionId":"dubbo-go-app-0.0.1-77b8cd56f9-4hpmb.default-4","connectedAt":"2022-03-23T13:32:49.884373692Z","address":"172.17.80.28:57150","metadata":{"LABELS":{"DUBBO_GO":"{\"providers:api.Greeter::\":\"dubbo-go-app.default.svc.cluster.local:20000\",\"providers:grpc.reflection.v1alpha.ServerReflection::\":\"dubbo-go-app.default.svc.cluster.local:20000\"}","topology.istio.io/cluster":"Kubernetes","topology.kubernetes.io/region":"cn-hangzhou","topology.kubernetes.io/zone":"cn-hangzhou-b"},"CLUSTER_ID":"Kubernetes"},"watches":{"type.googleapis.com/envoy.config.endpoint.v3.ClusterLoadAssignment":["outbound|443||istiod.istio-system.svc.cluster.local","outbound|443||metrics-server.kube-system.svc.cluster.local","outbound|80||heapster.kube-system.svc.cluster.local","outbound|443||storage-crd-validate-service.kube-system.svc.cluster.local","outbound|20000||laurence-svc.default.svc.cluster.local","outbound|11280||storage-monitor-service.kube-system.svc.cluster.local","outbound|15010||istiod.istio-system.svc.cluster.local","outbound|443||kubernetes.default.svc.cluster.local","outbound|9153||kube-dns.kube-system.svc.cluster.local","outbound|53||kube-dns.kube-system.svc.cluster.local","","outbound|15012||istiod.istio-system.svc.cluster.local","outbound|8848||nacos.default.svc.cluster.local","outbound|15014||istiod.istio-system.svc.cluster.local","outbound|20000||dubbo-go-app.default.svc.cluster.local"]}},{"connectionId":"dubbo-go-app-0.0.2-f465d67f7-k5ckq.default-2","connectedAt":"2022-03-23T13:32:15.790703481Z","address":"172.17.80.29:35406","metadata":{"LABELS":{"DUBBO_GO":"{\"providers:api.Greeter::\":\"dubbo-go-app.default.svc.cluster.local:20000\",\"providers:grpc.reflection.v1alpha.ServerReflection::\":\"dubbo-go-app.default.svc.cluster.local:20000\"}","topology.istio.io/cluster":"Kubernetes","topology.kubernetes.io/region":"cn-hangzhou","topology.kubernetes.io/zone":"cn-hangzhou-b"},"CLUSTER_ID":"Kubernetes"},"watches":{"type.googleapis.com/envoy.config.endpoint.v3.ClusterLoadAssignment":["outbound|15014||istiod.istio-system.svc.cluster.local","outbound|15010||istiod.istio-system.svc.cluster.local","outbound|53||kube-dns.kube-system.svc.cluster.local","outbound|11280||storage-monitor-service.kube-system.svc.cluster.local","outbound|8848||nacos.default.svc.cluster.local","outbound|20000||dubbo-go-app.default.svc.cluster.local","outbound|9153||kube-dns.kube-system.svc.cluster.local","","outbound|443||storage-crd-validate-service.kube-system.svc.cluster.local","outbound|443||metrics-server.kube-system.svc.cluster.local","outbound|15012||istiod.istio-system.svc.cluster.local","outbound|443||istiod.istio-system.svc.cluster.local","outbound|80||heapster.kube-system.svc.cluster.local","outbound|20000||laurence-svc.default.svc.cluster.local","outbound|443||kubernetes.default.svc.cluster.local"]}}]}`
    31  	debugAdszInvalidDataFoo      = `"totalClients":2,"clients":[{"connectionId":"dubbo-go-app-0.0.1-77b8cd56f9-4hpmb.default-4","connectedAt":"2022-03-23T13:32:49.884373692Z","address":"172.17.80.28:57150","metadata":{"LABELS":{"DUBBO_GO":"{\"providers:api.Greeter::\":\"dubbo-go-app.default.svc.cluster.local:20000\",\"providers:grpc.reflection.v1alpha.ServerReflection::\":\"dubbo-go-app.default.svc.cluster.local:20000\"}","topology.istio.io/cluster":"Kubernetes","topology.kubernetes.io/region":"cn-hangzhou","topology.kubernetes.io/zone":"cn-hangzhou-b"},"CLUSTER_ID":"Kubernetes"},"watches":{"type.googleapis.com/envoy.config.endpoint.v3.ClusterLoadAssignment":["outbound|443||istiod.istio-system.svc.cluster.local","outbound|443||metrics-server.kube-system.svc.cluster.local","outbound|80||heapster.kube-system.svc.cluster.local","outbound|443||storage-crd-validate-service.kube-system.svc.cluster.local","outbound|20000||laurence-svc.default.svc.cluster.local","outbound|11280||storage-monitor-service.kube-system.svc.cluster.local","outbound|15010||istiod.istio-system.svc.cluster.local","outbound|443||kubernetes.default.svc.cluster.local","outbound|9153||kube-dns.kube-system.svc.cluster.local","outbound|53||kube-dns.kube-system.svc.cluster.local","","outbound|15012||istiod.istio-system.svc.cluster.local","outbound|8848||nacos.default.svc.cluster.local","outbound|15014||istiod.istio-system.svc.cluster.local","outbound|20000||dubbo-go-app.default.svc.cluster.local"]}},{"connectionId":"dubbo-go-app-0.0.2-f465d67f7-k5ckq.default-2","connectedAt":"2022-03-23T13:32:15.790703481Z","address":"172.17.80.29:35406","metadata":{"LABELS":{"DUBBO_GO":"{\"providers:api.Greeter::\":\"dubbo-go-app.default.svc.cluster.local:20000\",\"providers:grpc.reflection.v1alpha.ServerReflection::\":\"dubbo-go-app.default.svc.cluster.local:20000\"}","topology.istio.io/cluster":"Kubernetes","topology.kubernetes.io/region":"cn-hangzhou","topology.kubernetes.io/zone":"cn-hangzhou-b"},"CLUSTER_ID":"Kubernetes"},"watches":{"type.googleapis.com/envoy.config.endpoint.v3.ClusterLoadAssignment":["outbound|15014||istiod.istio-system.svc.cluster.local","outbound|15010||istiod.istio-system.svc.cluster.local","outbound|53||kube-dns.kube-system.svc.cluster.local","outbound|11280||storage-monitor-service.kube-system.svc.cluster.local","outbound|8848||nacos.default.svc.cluster.local","outbound|20000||dubbo-go-app.default.svc.cluster.local","outbound|9153||kube-dns.kube-system.svc.cluster.local","","outbound|443||storage-crd-validate-service.kube-system.svc.cluster.local","outbound|443||metrics-server.kube-system.svc.cluster.local","outbound|15012||istiod.istio-system.svc.cluster.local","outbound|443||istiod.istio-system.svc.cluster.local","outbound|80||heapster.kube-system.svc.cluster.local","outbound|20000||laurence-svc.default.svc.cluster.local","outbound|443||kubernetes.default.svc.cluster.local"]}}]}`
    32  	debugAdszEmptyDubbogoDataFoo = `{"totalClients":2,"clients":[{"connectionId":"dubbo-go-app-0.0.1-77b8cd56f9-4hpmb.default-4","connectedAt":"2022-03-23T13:32:49.884373692Z","address":"172.17.80.28:57150","metadata":{"LABELS":{"topology.istio.io/cluster":"Kubernetes","topology.kubernetes.io/region":"cn-hangzhou","topology.kubernetes.io/zone":"cn-hangzhou-b"},"CLUSTER_ID":"Kubernetes"},"watches":{"type.googleapis.com/envoy.config.endpoint.v3.ClusterLoadAssignment":["outbound|443||istiod.istio-system.svc.cluster.local","outbound|443||metrics-server.kube-system.svc.cluster.local","outbound|80||heapster.kube-system.svc.cluster.local","outbound|443||storage-crd-validate-service.kube-system.svc.cluster.local","outbound|20000||laurence-svc.default.svc.cluster.local","outbound|11280||storage-monitor-service.kube-system.svc.cluster.local","outbound|15010||istiod.istio-system.svc.cluster.local","outbound|443||kubernetes.default.svc.cluster.local","outbound|9153||kube-dns.kube-system.svc.cluster.local","outbound|53||kube-dns.kube-system.svc.cluster.local","","outbound|15012||istiod.istio-system.svc.cluster.local","outbound|8848||nacos.default.svc.cluster.local","outbound|15014||istiod.istio-system.svc.cluster.local","outbound|20000||dubbo-go-app.default.svc.cluster.local"]}},{"connectionId":"dubbo-go-app-0.0.2-f465d67f7-k5ckq.default-2","connectedAt":"2022-03-23T13:32:15.790703481Z","address":"172.17.80.29:35406","metadata":{"LABELS":{"topology.istio.io/cluster":"Kubernetes","topology.kubernetes.io/region":"cn-hangzhou","topology.kubernetes.io/zone":"cn-hangzhou-b"},"CLUSTER_ID":"Kubernetes"},"watches":{"type.googleapis.com/envoy.config.endpoint.v3.ClusterLoadAssignment":["outbound|15014||istiod.istio-system.svc.cluster.local","outbound|15010||istiod.istio-system.svc.cluster.local","outbound|53||kube-dns.kube-system.svc.cluster.local","outbound|11280||storage-monitor-service.kube-system.svc.cluster.local","outbound|8848||nacos.default.svc.cluster.local","outbound|20000||dubbo-go-app.default.svc.cluster.local","outbound|9153||kube-dns.kube-system.svc.cluster.local","","outbound|443||storage-crd-validate-service.kube-system.svc.cluster.local","outbound|443||metrics-server.kube-system.svc.cluster.local","outbound|15012||istiod.istio-system.svc.cluster.local","outbound|443||istiod.istio-system.svc.cluster.local","outbound|80||heapster.kube-system.svc.cluster.local","outbound|20000||laurence-svc.default.svc.cluster.local","outbound|443||kubernetes.default.svc.cluster.local"]}}]}`
    33  	key1                         = "providers:api.Greeter::"
    34  	val1                         = "dubbo-go-app.default.svc.cluster.local:20000"
    35  	key2                         = "providers:grpc.reflection.v1alpha.ServerReflection::"
    36  	val2                         = "dubbo-go-app.default.svc.cluster.local:20000"
    37  )
    38  
    39  func TestADSZResponseGetMap(t *testing.T) {
    40  	adszRsp := &ADSZResponse{}
    41  	assert.Nil(t, json.Unmarshal([]byte(debugAdszDataFoo), adszRsp))
    42  
    43  	adszMap := adszRsp.GetMap()
    44  	assert.True(t, len(adszMap) == 2)
    45  	v1, ok1 := adszMap[key1]
    46  	assert.True(t, ok1)
    47  	assert.Equal(t, val1, v1)
    48  
    49  	v2, ok2 := adszMap[key2]
    50  	assert.True(t, ok2)
    51  	assert.Equal(t, val2, v2)
    52  }
    53  
    54  func TestInvalidADSZResponseGetMap(t *testing.T) {
    55  	adszRsp := &ADSZResponse{}
    56  	json.Unmarshal([]byte(debugAdszInvalidDataFoo), adszRsp)
    57  
    58  	adszMap := adszRsp.GetMap()
    59  	assert.True(t, len(adszMap) == 0)
    60  }
    61  
    62  func TestEmptyDubbogoMapADSZResponseGetMap(t *testing.T) {
    63  	adszRsp := &ADSZResponse{}
    64  	assert.Nil(t, json.Unmarshal([]byte(debugAdszEmptyDubbogoDataFoo), adszRsp))
    65  
    66  	adszMap := adszRsp.GetMap()
    67  	assert.True(t, len(adszMap) == 0)
    68  }