github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/go-control-plane/pkg/cache/v3/cache_test.go (about) 1 package cache_test 2 3 import ( 4 "testing" 5 6 "github.com/golang/protobuf/ptypes" 7 "github.com/golang/protobuf/ptypes/any" 8 "github.com/stretchr/testify/assert" 9 10 route "github.com/hxx258456/ccgo/go-control-plane/envoy/config/route/v3" 11 discovery "github.com/hxx258456/ccgo/go-control-plane/envoy/service/discovery/v3" 12 "github.com/hxx258456/ccgo/go-control-plane/pkg/cache/types" 13 "github.com/hxx258456/ccgo/go-control-plane/pkg/cache/v3" 14 "github.com/hxx258456/ccgo/go-control-plane/pkg/resource/v3" 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 assert.Nil(t, err) 41 assert.Equal(t, r.Name, resourceName) 42 } 43 44 func TestPassthroughResponseGetDiscoveryResponse(t *testing.T) { 45 routes := []types.Resource{&route.RouteConfiguration{Name: resourceName}} 46 rsrc, err := ptypes.MarshalAny(routes[0]) 47 assert.Nil(t, err) 48 dr := &discovery.DiscoveryResponse{ 49 TypeUrl: resource.RouteType, 50 Resources: []*any.Any{rsrc}, 51 VersionInfo: "v", 52 } 53 resp := cache.PassthroughResponse{ 54 Request: &discovery.DiscoveryRequest{TypeUrl: resource.RouteType}, 55 DiscoveryResponse: dr, 56 } 57 58 discoveryResponse, err := resp.GetDiscoveryResponse() 59 assert.Nil(t, err) 60 assert.Equal(t, discoveryResponse.VersionInfo, resp.DiscoveryResponse.VersionInfo) 61 assert.Equal(t, len(discoveryResponse.Resources), 1) 62 63 r := &route.RouteConfiguration{} 64 err = ptypes.UnmarshalAny(discoveryResponse.Resources[0], r) 65 assert.Nil(t, err) 66 assert.Equal(t, r.Name, resourceName) 67 assert.Equal(t, discoveryResponse, dr) 68 } 69 70 func TestHeartbeatResponseGetDiscoveryResponse(t *testing.T) { 71 routes := []types.ResourceWithTTL{{Resource: &route.RouteConfiguration{Name: resourceName}}} 72 resp := cache.RawResponse{ 73 Request: &discovery.DiscoveryRequest{TypeUrl: resource.RouteType}, 74 Version: "v", 75 Resources: routes, 76 Heartbeat: true, 77 } 78 79 discoveryResponse, err := resp.GetDiscoveryResponse() 80 assert.Nil(t, err) 81 assert.Equal(t, discoveryResponse.VersionInfo, resp.Version) 82 assert.Equal(t, len(discoveryResponse.Resources), 1) 83 assert.False(t, isTTLResource(discoveryResponse.Resources[0])) 84 85 cachedResponse, err := resp.GetDiscoveryResponse() 86 assert.Nil(t, err) 87 assert.Same(t, discoveryResponse, cachedResponse) 88 89 r := &route.RouteConfiguration{} 90 err = ptypes.UnmarshalAny(discoveryResponse.Resources[0], r) 91 assert.Nil(t, err) 92 assert.Equal(t, r.Name, resourceName) 93 } 94 95 func isTTLResource(resource *any.Any) bool { 96 wrappedResource := &discovery.Resource{} 97 err := ptypes.UnmarshalAny(resource, wrappedResource) 98 if err != nil { 99 return false 100 } 101 102 return wrappedResource.Resource == nil 103 }