Skip to content

Commit

Permalink
adp: Add default version unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
praminda committed Mar 4, 2022
1 parent 3bc9a1f commit b2bbe87
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 18 deletions.
4 changes: 2 additions & 2 deletions adapter/internal/discovery/xds/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,8 +482,8 @@ func GetAllEnvironments(apiUUID, vhost string, newEnvironments []string) []strin
return allEnvironments
}

// GetDeployedEnvironmets returns all the environments the API with `apiUUID` is deployed to
func GetDeployedEnvironmets(apiUUID string) []string {
// GetDeployedEnvironments returns all the environments the API with `apiUUID` is deployed to
func GetDeployedEnvironments(apiUUID string) []string {
var envs []string
if envMap, ok := apiUUIDToGatewayToVhosts[apiUUID]; ok {
envs = make([]string, 0, len(envMap))
Expand Down
4 changes: 2 additions & 2 deletions adapter/internal/eventhub/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ func LoadSubscriptionData(configFile *config.Config, initialAPIUUIDListMap map[s
go retrieveAPIListFromChannel(APIListChannel, nil)
}

// UpdatAPIMetadataFromCP Invokes `ApisEndpoint` and updates APIList synchronously.
func UpdatAPIMetadataFromCP(params map[string]string) {
// UpdateAPIMetadataFromCP Invokes `ApisEndpoint` and updates APIList synchronously.
func UpdateAPIMetadataFromCP(params map[string]string) {
var apiList *types.APIList
var responseChannel = make(chan response)
go InvokeService(ApisEndpoint, apiList, params, responseChannel, 0)
Expand Down
4 changes: 2 additions & 2 deletions adapter/internal/messaging/notification_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,13 @@ func processNotificationEvent(conf *config.Config, notification *msg.EventNotifi
// map and update runtime artifact's `isDefaultVersion` field to correctly deploy default
// versioned API.
func handleDefaultVersionUpdate(event msg.APIEvent) {
deployedEnvs := xds.GetDeployedEnvironmets(event.UUID)
deployedEnvs := xds.GetDeployedEnvironments(event.UUID)
for _, env := range deployedEnvs {
query := make(map[string]string, 3)
query[eh.GatewayLabelParam] = env
query[eh.ContextParam] = event.APIContext
query[eh.VersionParam] = event.APIVersion
eh.UpdatAPIMetadataFromCP(query)
eh.UpdateAPIMetadataFromCP(query)
}

synchronizer.FetchAPIsFromControlPlane(event.UUID, deployedEnvs)
Expand Down
27 changes: 18 additions & 9 deletions adapter/internal/oasparser/envoyconf/envoyconf_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package envoyconf

import (
"fmt"
"io/ioutil"
"regexp"
"strings"
Expand Down Expand Up @@ -133,7 +134,7 @@ func TestCreateRoute(t *testing.T) {
}

generatedRouteWithXWso2BasePath := createRoute(generateRouteCreateParamsForUnitTests(title, apiType, vHost, xWso2BasePath, version,
endpoint.Basepath, resourceWithGet.GetPath(), resourceWithGet.GetMethodList(), clusterName, "", nil))
endpoint.Basepath, resourceWithGet.GetPath(), resourceWithGet.GetMethodList(), clusterName, "", nil, false))
assert.NotNil(t, generatedRouteWithXWso2BasePath, "Route should not be null.")
assert.Equal(t, expectedRouteActionWithXWso2BasePath, generatedRouteWithXWso2BasePath.Action,
"Route generation mismatch when xWso2BasePath option is provided.")
Expand All @@ -142,7 +143,7 @@ func TestCreateRoute(t *testing.T) {
"Assigned HTTP Method Regex is incorrect when single method is available.")

generatedRouteWithoutXWso2BasePath := createRoute(generateRouteCreateParamsForUnitTests(title, apiType, vHost, "", version,
endpoint.Basepath, resourceWithGetPost.GetPath(), resourceWithGetPost.GetMethodList(), clusterName, "", nil))
endpoint.Basepath, resourceWithGetPost.GetPath(), resourceWithGetPost.GetMethodList(), clusterName, "", nil, false))
assert.NotNil(t, generatedRouteWithoutXWso2BasePath, "Route should not be null")
assert.NotNil(t, generatedRouteWithoutXWso2BasePath.GetMatch().Headers, "Headers property should not be null")
assert.Equal(t, "^(GET|POST|OPTIONS)$", generatedRouteWithoutXWso2BasePath.GetMatch().Headers[0].GetStringMatch().GetSafeRegex().Regex,
Expand All @@ -151,6 +152,13 @@ func TestCreateRoute(t *testing.T) {
assert.Equal(t, expectedRouteActionWithoutXWso2BasePath, generatedRouteWithoutXWso2BasePath.Action,
"Route generation mismatch when xWso2BasePath option is provided")

context := fmt.Sprintf("%s/%s", xWso2BasePath, version)
generatedRouteWithDefaultVersion := createRoute(generateRouteCreateParamsForUnitTests(title, apiType, vHost, context, version,
endpoint.Basepath, resourceWithGetPost.GetPath(), resourceWithGetPost.GetMethodList(), clusterName, "", nil, true))
assert.NotNil(t, generatedRouteWithDefaultVersion, "Route should not be null")
assert.True(t, strings.HasPrefix(generatedRouteWithDefaultVersion.GetMatch().GetSafeRegex().Regex, fmt.Sprintf("^(%s|%s)", context, xWso2BasePath)),
"Default version basepath is not generated correctly")

}

func TestCreateRouteClusterSpecifier(t *testing.T) {
Expand All @@ -172,21 +180,21 @@ func TestCreateRouteClusterSpecifier(t *testing.T) {
"resource_operation_id", []model.Endpoint{}, []model.Endpoint{})

routeWithProdEp := createRoute(generateRouteCreateParamsForUnitTests(title, apiType, vHost, xWso2BasePath, version, endpointBasePath,
resourceWithGet.GetPath(), resourceWithGet.GetMethodList(), prodClusterName, "", nil))
resourceWithGet.GetPath(), resourceWithGet.GetMethodList(), prodClusterName, "", nil, false))
assert.NotNil(t, routeWithProdEp, "Route should not be null")
assert.NotNil(t, routeWithProdEp.GetRoute().GetClusterHeader(), "Route Cluster Header should not be null.")
assert.Empty(t, routeWithProdEp.GetRoute().GetCluster(), "Route Cluster Name should be empty.")
assert.Equal(t, clusterHeaderName, routeWithProdEp.GetRoute().GetClusterHeader(), "Route Cluster Name mismatch.")

routeWithSandEp := createRoute(generateRouteCreateParamsForUnitTests(title, apiType, vHost, xWso2BasePath, version, endpointBasePath,
resourceWithGet.GetPath(), resourceWithGet.GetMethodList(), "", sandClusterName, nil))
resourceWithGet.GetPath(), resourceWithGet.GetMethodList(), "", sandClusterName, nil, false))
assert.NotNil(t, routeWithSandEp, "Route should not be null")
assert.NotNil(t, routeWithSandEp.GetRoute().GetClusterHeader(), "Route Cluster Header should not be null.")
assert.Empty(t, routeWithSandEp.GetRoute().GetCluster(), "Route Cluster Name should be empty.")
assert.Equal(t, clusterHeaderName, routeWithSandEp.GetRoute().GetClusterHeader(), "Route Cluster Name mismatch.")

routeWithProdSandEp := createRoute(generateRouteCreateParamsForUnitTests(title, apiType, vHost, xWso2BasePath, version, endpointBasePath,
resourceWithGet.GetPath(), resourceWithGet.GetMethodList(), prodClusterName, sandClusterName, nil))
resourceWithGet.GetPath(), resourceWithGet.GetMethodList(), prodClusterName, sandClusterName, nil, false))
assert.NotNil(t, routeWithProdSandEp, "Route should not be null")
assert.NotNil(t, routeWithProdSandEp.GetRoute().GetClusterHeader(), "Route Cluster Header should not be null.")
assert.Empty(t, routeWithProdSandEp.GetRoute().GetCluster(), "Route Cluster Name should be empty.")
Expand All @@ -211,7 +219,7 @@ func TestCreateRouteExtAuthzContext(t *testing.T) {
"resource_operation_id", []model.Endpoint{}, []model.Endpoint{})

routeWithProdEp := createRoute(generateRouteCreateParamsForUnitTests(title, apiType, vHost, xWso2BasePath, version,
endpointBasePath, resourceWithGet.GetPath(), resourceWithGet.GetMethodList(), prodClusterName, sandClusterName, nil))
endpointBasePath, resourceWithGet.GetPath(), resourceWithGet.GetMethodList(), prodClusterName, sandClusterName, nil, false))
assert.NotNil(t, routeWithProdEp, "Route should not be null")
assert.NotNil(t, routeWithProdEp.GetTypedPerFilterConfig(), "TypedPerFilter config should not be null")
assert.NotNil(t, routeWithProdEp.GetTypedPerFilterConfig()[wellknown.HTTPExternalAuthorization],
Expand Down Expand Up @@ -484,18 +492,18 @@ func TestGetCorsPolicy(t *testing.T) {

// Route without CORS configuration
routeWithoutCors := createRoute(generateRouteCreateParamsForUnitTests("test", "HTTP", "localhost", "/test", "1.0.0", "/test",
"/testPath", []string{"GET"}, "test-cluster", "", nil))
"/testPath", []string{"GET"}, "test-cluster", "", nil, false))
assert.Nil(t, routeWithoutCors.GetRoute().Cors, "Cors Configuration should be null.")

// Route with CORS configuration
routeWithCors := createRoute(generateRouteCreateParamsForUnitTests("test", "HTTP", "localhost", "/test", "1.0.0", "/test",
"/testPath", []string{"GET"}, "test-cluster", "", corsConfigModel3))
"/testPath", []string{"GET"}, "test-cluster", "", corsConfigModel3, false))
assert.NotNil(t, routeWithCors.GetRoute().Cors, "Cors Configuration should not be null.")
}

func generateRouteCreateParamsForUnitTests(title string, apiType string, vhost string, xWso2Basepath string, version string, endpointBasepath string,
resourcePathParam string, resourceMethods []string, prodClusterName string, sandClusterName string,
corsConfig *model.CorsConfig) *routeCreateParams {
corsConfig *model.CorsConfig, isDefaultVersion bool) *routeCreateParams {
return &routeCreateParams{
title: title,
apiType: apiType,
Expand All @@ -508,5 +516,6 @@ func generateRouteCreateParamsForUnitTests(title string, apiType string, vhost s
corsPolicy: corsConfig,
resourcePathParam: resourcePathParam,
resourceMethods: resourceMethods,
isDefaultVersion: isDefaultVersion,
}
}
6 changes: 3 additions & 3 deletions adapter/internal/oasparser/envoyconf/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ func testCreateRoutesForUnitTests(t *testing.T) []*routev3.Route {
}

route1 := createRoute(generateRouteCreateParamsForUnitTests("test", "HTTP", "localhost", "/test", "1.0.0", "/test",
"/testPath", []string{"GET"}, "test-cluster", "", corsConfigModel3))
"/testPath", []string{"GET"}, "test-cluster", "", corsConfigModel3, false))
route2 := createRoute(generateRouteCreateParamsForUnitTests("test", "HTTP", "localhost", "/test", "1.0.0", "/test",
"/testPath", []string{"POST"}, "test-cluster", "", corsConfigModel3))
"/testPath", []string{"POST"}, "test-cluster", "", corsConfigModel3, false))
route3 := createRoute(generateRouteCreateParamsForUnitTests("test", "HTTP", "localhost", "/test", "1.0.0", "/test",
"/testPath", []string{"PUT"}, "test-cluster", "", corsConfigModel3))
"/testPath", []string{"PUT"}, "test-cluster", "", corsConfigModel3, false))

routes := []*routev3.Route{route1, route2, route3}

Expand Down

0 comments on commit b2bbe87

Please sign in to comment.