mirror of https://github.com/k3s-io/k3s
Merge pull request #1909 from derekwaynecarr/service_proxy_needs_namespace
Add unit test for namespace aware proxypull/6/head
commit
cba76ef1ef
|
@ -83,6 +83,7 @@ type SimpleRESTStorage struct {
|
||||||
// The id requested, and location to return for ResourceLocation
|
// The id requested, and location to return for ResourceLocation
|
||||||
requestedResourceLocationID string
|
requestedResourceLocationID string
|
||||||
resourceLocation string
|
resourceLocation string
|
||||||
|
expectedResourceNamespace string
|
||||||
|
|
||||||
// If non-nil, called inside the WorkFunc when answering update, delete, create.
|
// If non-nil, called inside the WorkFunc when answering update, delete, create.
|
||||||
// obj receives the original input to the update, delete, or create call.
|
// obj receives the original input to the update, delete, or create call.
|
||||||
|
@ -157,6 +158,11 @@ func (storage *SimpleRESTStorage) Watch(ctx api.Context, label, field labels.Sel
|
||||||
|
|
||||||
// Implement Redirector.
|
// Implement Redirector.
|
||||||
func (storage *SimpleRESTStorage) ResourceLocation(ctx api.Context, id string) (string, error) {
|
func (storage *SimpleRESTStorage) ResourceLocation(ctx api.Context, id string) (string, error) {
|
||||||
|
// validate that the namespace context on the request matches the expected input
|
||||||
|
requestedResourceNamespace := api.Namespace(ctx)
|
||||||
|
if storage.expectedResourceNamespace != requestedResourceNamespace {
|
||||||
|
return "", fmt.Errorf("Expected request namespace %s, but got namespace %s", storage.expectedResourceNamespace, requestedResourceNamespace)
|
||||||
|
}
|
||||||
storage.requestedResourceLocationID = id
|
storage.requestedResourceLocationID = id
|
||||||
if err := storage.errors["resourceLocation"]; err != nil {
|
if err := storage.errors["resourceLocation"]; err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
|
|
|
@ -132,15 +132,17 @@ func TestProxyTransport_fixLinks(t *testing.T) {
|
||||||
|
|
||||||
func TestProxy(t *testing.T) {
|
func TestProxy(t *testing.T) {
|
||||||
table := []struct {
|
table := []struct {
|
||||||
method string
|
method string
|
||||||
path string
|
path string
|
||||||
reqBody string
|
reqBody string
|
||||||
respBody string
|
respBody string
|
||||||
|
reqNamespace string
|
||||||
}{
|
}{
|
||||||
{"GET", "/some/dir", "", "answer"},
|
{"GET", "/some/dir", "", "answer", "default"},
|
||||||
{"POST", "/some/other/dir", "question", "answer"},
|
{"POST", "/some/other/dir", "question", "answer", "default"},
|
||||||
{"PUT", "/some/dir/id", "different question", "answer"},
|
{"PUT", "/some/dir/id", "different question", "answer", "default"},
|
||||||
{"DELETE", "/some/dir/id", "", "ok"},
|
{"DELETE", "/some/dir/id", "", "ok", "default"},
|
||||||
|
{"GET", "/some/dir/id?namespace=other", "", "answer", "other"},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, item := range table {
|
for _, item := range table {
|
||||||
|
@ -156,8 +158,9 @@ func TestProxy(t *testing.T) {
|
||||||
}))
|
}))
|
||||||
|
|
||||||
simpleStorage := &SimpleRESTStorage{
|
simpleStorage := &SimpleRESTStorage{
|
||||||
errors: map[string]error{},
|
errors: map[string]error{},
|
||||||
resourceLocation: proxyServer.URL,
|
resourceLocation: proxyServer.URL,
|
||||||
|
expectedResourceNamespace: item.reqNamespace,
|
||||||
}
|
}
|
||||||
handler := Handle(map[string]RESTStorage{
|
handler := Handle(map[string]RESTStorage{
|
||||||
"foo": simpleStorage,
|
"foo": simpleStorage,
|
||||||
|
|
Loading…
Reference in New Issue