Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clone request to ready twice #504

Merged
merged 2 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion testing/fake/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package fake

import (
"bytes"
"io"
"net/http"

"github.com/emicklei/go-restful/v3"
Expand Down Expand Up @@ -44,7 +46,8 @@ func (h *PolicyHandler) Filter(req *restful.Request, resp *restful.Response, cha
}

// Extract input for the OPA policy evaluation from the request.
input, err := InputFromRequest(req)
clone := cloneRequest(req)
input, err := InputFromRequest(clone)
if err != nil {
kerrors.HandleError(req, resp, err)
return
Expand Down Expand Up @@ -72,3 +75,17 @@ func (h *PolicyHandler) Filter(req *restful.Request, resp *restful.Response, cha

_ = resp.WriteHeaderAndEntity(status, result)
}

func cloneRequest(original *restful.Request) *restful.Request {
clone := *original

if original.Request.Body != nil {
bodyBytes, _ := io.ReadAll(original.Request.Body)
original.Request.Body = io.NopCloser(bytes.NewReader(bodyBytes))

clone.Request = original.Request.Clone(original.Request.Context())
clone.Request.Body = io.NopCloser(bytes.NewReader(bodyBytes))
}

return &clone
}
14 changes: 9 additions & 5 deletions testing/fake/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ func TestPolicyHandlerFilter(t *testing.T) {

h := PolicyHandler{Store: store}
ws := new(restful.WebService).Filter(h.Filter)
ws.Route(ws.Produces(restful.MIME_JSON).POST(name).To(simple))
ws.Route(ws.Produces(restful.MIME_JSON).GET(name).To(simple))
ws.Route(ws.Produces(restful.MIME_JSON).POST(name).To(simple(g, item.body)))
ws.Route(ws.Produces(restful.MIME_JSON).GET(name).To(simple(g, item.body)))
c := restful.NewContainer().Add(ws)
c.Dispatch(httpWriter, httpRequest)

Expand All @@ -114,9 +114,13 @@ func TestPolicyHandlerFilter(t *testing.T) {
g.Expect(result).To(Equal(item.expected))
})
}

}
func simple(g Gomega, body string) restful.RouteFunction {
return func(req *restful.Request, resp *restful.Response) {
bytes, err := io.ReadAll(req.Request.Body)
g.Expect(err).To(BeNil())
g.Expect(string(bytes)).To(Equal(body))

func simple(req *restful.Request, resp *restful.Response) {
_, _ = io.WriteString(resp.ResponseWriter, `{"result":"simple"}`)
_, _ = io.WriteString(resp.ResponseWriter, `{"result":"simple"}`)
}
}
36 changes: 36 additions & 0 deletions testing/framework/cluster/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Copyright 2023 The Katanomi Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cluster
airycanon marked this conversation as resolved.
Show resolved Hide resolved

import (
"context"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// PluginService Get plugin service by plugin name
func PluginService(ctx context.Context, clt client.Client, name string) (*corev1.Service, error) {
services := &corev1.ServiceList{}
if err := clt.List(ctx, services, client.MatchingLabels{"plugin": name}); err != nil {
return nil, err
}

if len(services.Items) > 0 {
return &services.Items[0], nil
}
return nil, nil
}