Skip to content

Commit

Permalink
fake test
Browse files Browse the repository at this point in the history
  • Loading branch information
acookin committed Jul 15, 2021
1 parent 8df983f commit a9265d5
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 1 deletion.
2 changes: 1 addition & 1 deletion builder/builder.mk
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ setup-venv:
pip install orjson==3.3.1; \
rm -f venv/lib/python3.8/site-packages/_manylinux.py; \
else \
pip install orjson==3.3.1; \
pip install orjson; \
fi; \
pip install -r $(OSS_HOME)/builder/requirements.txt; \
pip install -e $(OSS_HOME)/python; \
Expand Down
139 changes: 139 additions & 0 deletions cmd/entrypoint/fake_mapping_cors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package entrypoint_test

import (
"testing"

"github.com/datawire/ambassador/cmd/entrypoint"
envoy "github.com/datawire/ambassador/pkg/api/envoy/api/v2"
route "github.com/datawire/ambassador/pkg/api/envoy/api/v2/route"
bootstrap "github.com/datawire/ambassador/pkg/api/envoy/config/bootstrap/v2"
http "github.com/datawire/ambassador/pkg/api/envoy/config/filter/network/http_connection_manager/v2"
"github.com/datawire/ambassador/pkg/envoy-control-plane/resource/v2"
"github.com/datawire/ambassador/pkg/envoy-control-plane/wellknown"

"github.com/stretchr/testify/assert"
)

func TestMappingCORSOriginsSlice(t *testing.T) {
f := entrypoint.RunFake(t, entrypoint.FakeConfig{EnvoyConfig: true}, nil)
f.UpsertYAML(`
---
apiVersion: getambassador.io/v2
kind: Mapping
metadata:
name: foo
namespace: default
spec:
prefix: /foo
service: foo.default
cors:
origins:
- foo.example.com
- bar.example.com
`)
f.Upsert(makeService("default", "foo"))
f.Flush()
snap := f.GetSnapshot(HasMapping("default", "foo"))
assert.NotNil(t, snap)

config := f.GetEnvoyConfig(func(config *bootstrap.Bootstrap) bool {
return FindCluster(config, ClusterNameContains("cluster_foo_default_default")) != nil
})

listener := findListener(config, func(l *envoy.Listener) bool {
return l.Name == "ambassador-listener-8080"
})

assert.NotNil(t, listener)

routeAction := findVirtualHostRoute(listener, func(r *route.RouteAction) bool {
return r.GetCluster() == "cluster_foo_default_default"
})
assert.NotNil(t, routeAction)
assert.NotNil(t, routeAction.Cors)
assert.Equal(t, len(routeAction.Cors.AllowOriginStringMatch), 2)
for _, m := range routeAction.Cors.AllowOriginStringMatch {
assert.Contains(t, []string{"bar.example.com", "foo.example.com"}, m.GetExact())

}
}

func TestMappingCORSOriginsString(t *testing.T) {
f := entrypoint.RunFake(t, entrypoint.FakeConfig{EnvoyConfig: true}, nil)
f.UpsertYAML(`
---
apiVersion: getambassador.io/v2
kind: Mapping
metadata:
name: foo
namespace: default
spec:
prefix: /foo
service: foo.default
cors:
origins: "foo.example.com,bar.example.com"
`)
f.Upsert(makeService("default", "foo"))
f.Flush()
snap := f.GetSnapshot(HasMapping("default", "foo"))
assert.NotNil(t, snap)

config := f.GetEnvoyConfig(func(config *bootstrap.Bootstrap) bool {
return FindCluster(config, ClusterNameContains("cluster_foo_default_default")) != nil
})

listener := findListener(config, func(l *envoy.Listener) bool {
return l.Name == "ambassador-listener-8080"
})

assert.NotNil(t, listener)

routeAction := findVirtualHostRoute(listener, func(r *route.RouteAction) bool {
return r.GetCluster() == "cluster_foo_default_default"
})
assert.NotNil(t, routeAction)
assert.NotNil(t, routeAction.Cors)
assert.Equal(t, len(routeAction.Cors.AllowOriginStringMatch), 2)
for _, m := range routeAction.Cors.AllowOriginStringMatch {
assert.Contains(t, []string{"bar.example.com", "foo.example.com"}, m.GetExact())

}
}

func findVirtualHostRoute(listener *envoy.Listener, predicate func(*route.RouteAction) bool) *route.RouteAction {
for _, fc := range listener.FilterChains {
for _, filter := range fc.Filters {
if filter.Name != wellknown.HTTPConnectionManager {
continue
}
hcm := resource.GetHTTPConnectionManager(filter)
if hcm != nil {
rs, ok := hcm.RouteSpecifier.(*http.HttpConnectionManager_RouteConfig)
if ok {
for _, vh := range rs.RouteConfig.VirtualHosts {
for _, vhr := range vh.Routes {
routeAction, ok := vhr.Action.(*route.Route_Route)
if ok {
if predicate(routeAction.Route) {
return routeAction.Route
}
}
}
}
}
}
}

}
return nil

}

func findListener(envoyConfig *bootstrap.Bootstrap, predicate func(*envoy.Listener) bool) *envoy.Listener {
for _, listener := range envoyConfig.StaticResources.Listeners {
if predicate(listener) {
return listener
}
}
return nil
}

0 comments on commit a9265d5

Please sign in to comment.