...

Source file src/github.com/emissary-ingress/emissary/v3/cmd/entrypoint/testutil_fake_findutils_test.go

Documentation: github.com/emissary-ingress/emissary/v3/cmd/entrypoint

     1  package entrypoint_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	bootstrap "github.com/emissary-ingress/emissary/v3/pkg/api/envoy/config/bootstrap/v3"
     7  	v3listener "github.com/emissary-ingress/emissary/v3/pkg/api/envoy/config/listener/v3"
     8  	route "github.com/emissary-ingress/emissary/v3/pkg/api/envoy/config/route/v3"
     9  	http "github.com/emissary-ingress/emissary/v3/pkg/api/envoy/extensions/filters/network/http_connection_manager/v3"
    10  	"github.com/emissary-ingress/emissary/v3/pkg/envoy-control-plane/resource/v3"
    11  	"github.com/emissary-ingress/emissary/v3/pkg/envoy-control-plane/wellknown"
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  // findListener finds the first listener in a given Envoy configuration that matches a
    16  // given predicate. If no listener is found, nil is returned.
    17  //
    18  // Obviously, in a perfect world, the given predicate would be constructed to match only
    19  // a single listener...
    20  func findListener(envoyConfig *bootstrap.Bootstrap, predicate func(*v3listener.Listener) bool) *v3listener.Listener {
    21  	for _, listener := range envoyConfig.StaticResources.Listeners {
    22  		if predicate(listener) {
    23  			return listener
    24  		}
    25  	}
    26  	return nil
    27  }
    28  
    29  // findListenerByName finds uses findListener to find a listener with a given name.
    30  func findListenerByName(envoyConfig *bootstrap.Bootstrap, name string) *v3listener.Listener {
    31  	return findListener(envoyConfig, func(listener *v3listener.Listener) bool {
    32  		return listener.Name == name
    33  	})
    34  }
    35  
    36  // mustFindListenerByName looks for a listener with a given name, and asserts that it
    37  // must be present.
    38  func mustFindListenerByName(t *testing.T, envoyConfig *bootstrap.Bootstrap, name string) *v3listener.Listener {
    39  	listener := findListenerByName(envoyConfig, name)
    40  	assert.NotNil(t, listener)
    41  	return listener
    42  }
    43  
    44  // findRoutes finds all the routes within a given listener that match a
    45  // given predicate. If no matching routes are found, an empty list is returned.
    46  func findRoutes(listener *v3listener.Listener, predicate func(*route.Route) bool) []*route.Route {
    47  	routes := make([]*route.Route, 0)
    48  
    49  	// fmt.Printf("---- findRoutes\n")
    50  
    51  	for _, chain := range listener.FilterChains {
    52  		for _, filter := range chain.Filters {
    53  			if filter.Name != wellknown.HTTPConnectionManager {
    54  				continue
    55  			}
    56  
    57  			hcm := resource.GetHTTPConnectionManager(filter)
    58  
    59  			if hcm != nil {
    60  				rs, ok := hcm.RouteSpecifier.(*http.HttpConnectionManager_RouteConfig)
    61  
    62  				if ok {
    63  					for _, vh := range rs.RouteConfig.VirtualHosts {
    64  						for _, vhr := range vh.Routes {
    65  							// if !(strings.HasPrefix(vhr.Match.GetPrefix(), "/ambassador/")) {
    66  							// 	fmt.Printf("ROUTE: #%v\n", vhr)
    67  							// }
    68  
    69  							if predicate(vhr) {
    70  								routes = append(routes, vhr)
    71  							}
    72  						}
    73  					}
    74  				}
    75  			}
    76  		}
    77  	}
    78  
    79  	return routes
    80  }
    81  
    82  // findRoutesToCluster finds all the routes in a listener that route to a given cluster.
    83  func findRoutesToCluster(l *v3listener.Listener, cluster_name string) []*route.Route {
    84  	return findRoutes(l, func(r *route.Route) bool {
    85  		routeAction, ok := r.Action.(*route.Route_Route)
    86  
    87  		if !ok {
    88  			return false
    89  		}
    90  
    91  		return routeAction.Route.GetCluster() == cluster_name
    92  	})
    93  }
    94  
    95  // mustFindRoutesToCluster uses findRoutesToCluster to find all the routes that route to
    96  // a given cluster, and asserts that some must be present.
    97  func mustFindRoutesToCluster(t *testing.T, listener *v3listener.Listener, cluster_name string) []*route.Route {
    98  	routes := findRoutesToCluster(listener, cluster_name)
    99  	assert.NotEmpty(t, routes)
   100  	return routes
   101  }
   102  
   103  // findRouteAction finds uses findVirtualHostRoute to find a route whose action
   104  // is Route, and matches a given predicate. The RouteAction is returned if found; otherwise,
   105  // nil is returned.
   106  func findRouteAction(listener *v3listener.Listener, predicate func(*route.RouteAction) bool) *route.RouteAction {
   107  	routes := findRoutes(listener, func(r *route.Route) bool {
   108  		routeAction, ok := r.Action.(*route.Route_Route)
   109  
   110  		if ok {
   111  			return predicate(routeAction.Route)
   112  		}
   113  
   114  		return false
   115  	})
   116  
   117  	if len(routes) == 0 {
   118  		return nil
   119  	}
   120  
   121  	return routes[0].Action.(*route.Route_Route).Route
   122  }
   123  
   124  // mustFindRouteAction wraps findVirtualHostRouteAction, and asserts that a
   125  // match is found.
   126  func mustFindRouteAction(t *testing.T, listener *v3listener.Listener, predicate func(*route.RouteAction) bool) *route.RouteAction {
   127  	routeAction := findRouteAction(listener, predicate)
   128  	assert.NotNil(t, routeAction)
   129  	return routeAction
   130  }
   131  
   132  // mustFindRouteActionToCluster uses mustFindVirtualHostRouteAction to find a
   133  // route whose action routes to a given cluster name, and asserts that a match is found.
   134  func mustFindRouteActionToCluster(t *testing.T, listener *v3listener.Listener, clusterName string) *route.RouteAction {
   135  	routeAction := mustFindRouteAction(t, listener, func(ra *route.RouteAction) bool {
   136  		return ra.GetCluster() == clusterName
   137  	})
   138  	return routeAction
   139  }
   140  

View as plain text