gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/go-control-plane/pkg/cache/v3/cache_test.go (about) 1 package cache_test 2 3 import ( 4 "gitee.com/ks-custle/core-gm/go-control-plane/pkg/cache/v3" 5 "gitee.com/ks-custle/core-gm/go-control-plane/pkg/resource/v3" 6 "google.golang.org/protobuf/types/known/anypb" 7 "testing" 8 9 "github.com/golang/protobuf/ptypes/any" 10 "github.com/stretchr/testify/assert" 11 12 route "gitee.com/ks-custle/core-gm/go-control-plane/envoy/config/route/v3" 13 discovery "gitee.com/ks-custle/core-gm/go-control-plane/envoy/service/discovery/v3" 14 "gitee.com/ks-custle/core-gm/go-control-plane/pkg/cache/types" 15 ) 16 17 const ( 18 resourceName = "route1" 19 ) 20 21 func TestResponseGetDiscoveryResponse(t *testing.T) { 22 routes := []types.ResourceWithTTL{{Resource: &route.RouteConfiguration{Name: resourceName}}} 23 resp := cache.RawResponse{ 24 Request: &discovery.DiscoveryRequest{TypeUrl: resource.RouteType}, 25 Version: "v", 26 Resources: routes, 27 } 28 29 discoveryResponse, err := resp.GetDiscoveryResponse() 30 assert.Nil(t, err) 31 assert.Equal(t, discoveryResponse.VersionInfo, resp.Version) 32 assert.Equal(t, len(discoveryResponse.Resources), 1) 33 34 cachedResponse, err := resp.GetDiscoveryResponse() 35 assert.Nil(t, err) 36 assert.Same(t, discoveryResponse, cachedResponse) 37 38 r := &route.RouteConfiguration{} 39 //err = ptypes.UnmarshalAny(discoveryResponse.Resources[0], r) 40 err = discoveryResponse.Resources[0].UnmarshalTo(r) 41 assert.Nil(t, err) 42 assert.Equal(t, r.Name, resourceName) 43 } 44 45 func TestPassthroughResponseGetDiscoveryResponse(t *testing.T) { 46 routes := []types.Resource{&route.RouteConfiguration{Name: resourceName}} 47 //rsrc, err := ptypes.MarshalAny(routes[0]) 48 rsrc, err := anypb.New(routes[0].(*route.RouteConfiguration)) 49 50 dr := &discovery.DiscoveryResponse{ 51 TypeUrl: resource.RouteType, 52 Resources: []*any.Any{rsrc}, 53 VersionInfo: "v", 54 } 55 resp := cache.PassthroughResponse{ 56 Request: &discovery.DiscoveryRequest{TypeUrl: resource.RouteType}, 57 DiscoveryResponse: dr, 58 } 59 60 discoveryResponse, err := resp.GetDiscoveryResponse() 61 assert.Nil(t, err) 62 assert.Equal(t, discoveryResponse.VersionInfo, resp.DiscoveryResponse.VersionInfo) 63 assert.Equal(t, len(discoveryResponse.Resources), 1) 64 65 r := &route.RouteConfiguration{} 66 //err = ptypes.UnmarshalAny(discoveryResponse.Resources[0], r) 67 err = discoveryResponse.Resources[0].UnmarshalTo(r) 68 assert.Nil(t, err) 69 assert.Equal(t, r.Name, resourceName) 70 assert.Equal(t, discoveryResponse, dr) 71 } 72 73 func TestHeartbeatResponseGetDiscoveryResponse(t *testing.T) { 74 routes := []types.ResourceWithTTL{{Resource: &route.RouteConfiguration{Name: resourceName}}} 75 resp := cache.RawResponse{ 76 Request: &discovery.DiscoveryRequest{TypeUrl: resource.RouteType}, 77 Version: "v", 78 Resources: routes, 79 Heartbeat: true, 80 } 81 82 discoveryResponse, err := resp.GetDiscoveryResponse() 83 assert.Nil(t, err) 84 assert.Equal(t, discoveryResponse.VersionInfo, resp.Version) 85 assert.Equal(t, len(discoveryResponse.Resources), 1) 86 assert.False(t, isTTLResource(discoveryResponse.Resources[0])) 87 88 cachedResponse, err := resp.GetDiscoveryResponse() 89 assert.Nil(t, err) 90 assert.Same(t, discoveryResponse, cachedResponse) 91 92 r := &route.RouteConfiguration{} 93 //err = ptypes.UnmarshalAny(discoveryResponse.Resources[0], r) 94 err = discoveryResponse.Resources[0].UnmarshalTo(r) 95 assert.Nil(t, err) 96 assert.Equal(t, r.Name, resourceName) 97 } 98 99 func isTTLResource(resource *any.Any) bool { 100 wrappedResource := &discovery.Resource{} 101 //err := ptypes.UnmarshalAny(resource, wrappedResource) 102 err := resource.UnmarshalTo(wrappedResource) 103 if err != nil { 104 return false 105 } 106 107 return wrappedResource.Resource == nil 108 }