diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index bc7b369..6c9dec5 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -32,8 +32,7 @@ jobs: - name: Lint uses: golangci/golangci-lint-action@v3 with: - # Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version - version: v1.29 + args: --exclude composites - name: Test - run: make test \ No newline at end of file + run: make test diff --git a/Makefile b/Makefile index c2d6082..f4f2176 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ .PHONY: lint lint: - golint + golangci-lint run --exclude composites .PHONY: dep dep: diff --git a/bridge/bridge.go b/bridge/bridge.go index a2a52a6..164a569 100644 --- a/bridge/bridge.go +++ b/bridge/bridge.go @@ -38,9 +38,6 @@ func readPbFrame(conn net.Conn) (data []byte, err error) { } data = make([]byte, len) - if data == nil { - return nil, errors.New("no memory") - } _, err = io.ReadFull(conn, data) if err != nil { diff --git a/bridge/bridgetest/bridgetest.go b/bridge/bridgetest/bridgetest.go index 8bf274e..83b937f 100644 --- a/bridge/bridgetest/bridgetest.go +++ b/bridge/bridgetest/bridgetest.go @@ -3,7 +3,6 @@ package bridgetest import ( "bytes" "encoding/binary" - "errors" "io" "net" "testing" @@ -25,9 +24,6 @@ func readPbFrame(conn net.Conn) (data []byte, err error) { } data = make([]byte, len) - if data == nil { - return nil, errors.New("no memory") - } _, err = io.ReadFull(conn, data) if err != nil { @@ -98,7 +94,11 @@ func Mock(t *testing.T, s []MockStep) net.Conn { break } } else { - writePbFrame(conB, []byte{}) + err = writePbFrame(conB, []byte{}) + if err != nil { + t.Errorf("step %d, writePbFrame(ret): %s", i, err) + break + } } } conB.Close() diff --git a/ctx/ctx.go b/ctx/ctx.go index 61ba133..c0ee992 100644 --- a/ctx/ctx.go +++ b/ctx/ctx.go @@ -49,7 +49,6 @@ func (c Ctx) SetShared(k string, value interface{}) error { } return c.Ask(`kong.ctx.shared.set`, &kong_plugin_protocol.KV{K: k, V: v}, nil) - return err } // kong.Ctx.GetSharedAny() returns a value from the `kong.ctx.shared` request context table. @@ -86,4 +85,3 @@ func (c Ctx) GetSharedFloat(k string) (float64, error) { return 0, bridge.ReturnTypeError("number") } - diff --git a/ip/ip_test.go b/ip/ip_test.go index 0c6e50f..3abafa7 100644 --- a/ip/ip_test.go +++ b/ip/ip_test.go @@ -11,8 +11,16 @@ import ( func TestIsTrusted(t *testing.T) { ip := Ip{bridge.New(bridgetest.Mock(t, []bridgetest.MockStep{ - {"kong.ip.is_trusted", bridge.WrapString("1.1.1.1"), &kong_plugin_protocol.Bool{V: true}}, - {"kong.ip.is_trusted", bridge.WrapString("1.0.0.1"), &kong_plugin_protocol.Bool{V: false}}, + { + Method: "kong.ip.is_trusted", + Args: bridge.WrapString("1.1.1.1"), + Ret: &kong_plugin_protocol.Bool{V: true}, + }, + { + Method: "kong.ip.is_trusted", + Args: bridge.WrapString("1.0.0.1"), + Ret: &kong_plugin_protocol.Bool{V: false}, + }, }))} ret, err := ip.IsTrusted("1.1.1.1") @@ -23,4 +31,3 @@ func TestIsTrusted(t *testing.T) { assert.NoError(t, err) assert.False(t, ret) } - diff --git a/node/node_test.go b/node/node_test.go index 1c5afb2..4aaaa5d 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -15,7 +15,7 @@ func mockNode(t *testing.T, s []bridgetest.MockStep) Node { func TestGetId(t *testing.T) { node := mockNode(t, []bridgetest.MockStep{ - {"kong.node.get_id", nil, bridge.WrapString("001:002:0003")}, + {Method: "kong.node.get_id", Ret: bridge.WrapString("001:002:0003")}, }) ret, err := node.GetId() @@ -25,8 +25,9 @@ func TestGetId(t *testing.T) { func TestGetMemoryStats(t *testing.T) { node := mockNode(t, []bridgetest.MockStep{ - {"kong.node.get_memory_stats", nil, - &kong_plugin_protocol.MemoryStats{ + { + Method: "kong.node.get_memory_stats", + Ret: &kong_plugin_protocol.MemoryStats{ LuaSharedDicts: &kong_plugin_protocol.MemoryStats_LuaSharedDicts{ Kong: &kong_plugin_protocol.MemoryStats_LuaSharedDicts_DictStats{ AllocatedSlabs: 1027, @@ -68,8 +69,8 @@ func TestGetMemoryStats(t *testing.T) { }{AllocatedSlabs: 4093, Capacity: 3424875}, }, WorkersLuaVms: []workerLuaVmStats{ - workerLuaVmStats{HttpAllocatedGc: 123456, Pid: 543}, - workerLuaVmStats{HttpAllocatedGc: 345678, Pid: 876}, + {HttpAllocatedGc: 123456, Pid: 543}, + {HttpAllocatedGc: 345678, Pid: 876}, }, }, ret) } diff --git a/request/request_test.go b/request/request_test.go index adc7fc3..baf274e 100644 --- a/request/request_test.go +++ b/request/request_test.go @@ -16,14 +16,14 @@ func mockRequest(t *testing.T, s []bridgetest.MockStep) Request { func TestGetInfos(t *testing.T) { q, err := bridge.WrapHeaders(map[string][]string{ - "ref": []string{"wayback"}, - "trail": []string{"faint"}, + "ref": {"wayback"}, + "trail": {"faint"}, }) assert.NoError(t, err) h, err := bridge.WrapHeaders(map[string][]string{ - "Host": []string{"example.com"}, - "X-Two-Things": []string{"first", "second"}, + "Host": {"example.com"}, + "X-Two-Things": {"first", "second"}, }) assert.NoError(t, err) diff --git a/response/response.go b/response/response.go index b7f2c8e..e009b40 100644 --- a/response/response.go +++ b/response/response.go @@ -227,7 +227,7 @@ func (r Response) Exit(status int, body string, headers map[string][]string) { Body: body, Headers: h, } - r.Ask(`kong.response.exit`, &arg, nil) + _ = r.Ask(`kong.response.exit`, &arg, nil) r.Close() } @@ -237,6 +237,6 @@ func (r Response) ExitStatus(status int) { arg := kong_plugin_protocol.ExitArgs{ Status: int32(status), } - r.Ask(`kong.response.exit`, &arg, nil) + _ = r.Ask(`kong.response.exit`, &arg, nil) r.Close() } diff --git a/router/router_test.go b/router/router_test.go index 14e06e6..ba1071c 100644 --- a/router/router_test.go +++ b/router/router_test.go @@ -3,45 +3,44 @@ package router import ( "testing" - "github.com/Kong/go-pdk/entities" "github.com/Kong/go-pdk/bridge" "github.com/Kong/go-pdk/bridge/bridgetest" + "github.com/Kong/go-pdk/entities" "github.com/Kong/go-pdk/server/kong_plugin_protocol" "github.com/stretchr/testify/assert" ) - func TestRouter(t *testing.T) { router := Router{bridge.New(bridgetest.Mock(t, []bridgetest.MockStep{ - {"kong.router.get_route", nil, &kong_plugin_protocol.Route{ - Id: "001:002", - Name: "route_66", + {Method: "kong.router.get_route", Ret: &kong_plugin_protocol.Route{ + Id: "001:002", + Name: "route_66", Protocols: []string{"http", "tcp"}, - Paths: []string{"/v0/left", "/v1/this"}, + Paths: []string{"/v0/left", "/v1/this"}, }}, - {"kong.router.get_service", nil, &kong_plugin_protocol.Service{ - Id: "003:004", - Name: "self_service", + {Method: "kong.router.get_service", Ret: &kong_plugin_protocol.Service{ + Id: "003:004", + Name: "self_service", Protocol: "http", - Path: "/v0/left", + Path: "/v0/left", }}, }))} ret_r, err := router.GetRoute() assert.NoError(t, err) assert.Equal(t, entities.Route{ - Id: "001:002", - Name: "route_66", + Id: "001:002", + Name: "route_66", Protocols: []string{"http", "tcp"}, - Paths: []string{"/v0/left", "/v1/this"}, + Paths: []string{"/v0/left", "/v1/this"}, }, ret_r) ret_s, err := router.GetService() assert.NoError(t, err) assert.Equal(t, entities.Service{ - Id: "003:004", - Name: "self_service", + Id: "003:004", + Name: "self_service", Protocol: "http", - Path: "/v0/left", + Path: "/v0/left", }, ret_s) } diff --git a/server/event.go b/server/event.go index 91b1f81..5143855 100644 --- a/server/event.go +++ b/server/event.go @@ -2,7 +2,6 @@ package server import ( "fmt" - "github.com/Kong/go-pdk" ) // Incoming data for a new event. @@ -14,72 +13,9 @@ type StartEventData struct { } type eventData struct { - id int // event id - instance *instanceData // plugin instance - ipc chan interface{} // communication channel (TODO: use decoded structs) - pdk *pdk.PDK // go-pdk instance + ipc chan interface{} // communication channel (TODO: use decoded structs) } -func (rh *rpcHandler) addEvent(event *eventData) { - rh.lock.Lock() - defer rh.lock.Unlock() - - event.id = rh.nextEventId - rh.nextEventId++ - rh.events[event.id] = event -} - -// HandleEvent starts the call/{callback/response}*/finish cycle. -// More than one event can be run concurrenty for a single plugin instance, -// they all receive the same object instance, so should be careful if it's -// mutated or holds references to mutable data. -// -// RPC exported method -// func (rh *rpcHandler) HandleEvent(in StartEventData, out *StepData) error { -// rh.lock.RLock() -// instance, ok := rh.instances[in.InstanceId] -// rh.lock.RUnlock() -// if !ok { -// return fmt.Errorf("no plugin instance %d", in.InstanceId) -// } -// -// h, ok := instance.handlers[in.EventName] -// if !ok { -// return fmt.Errorf("undefined method %s", in.EventName) -// } -// -// ipc := make(chan interface{}) -// -// event := eventData{ -// instance: instance, -// ipc: ipc, -// pdk: pdk.Init(ipc), -// } -// -// rh.addEvent(&event) -// -// //log.Printf("Will launch goroutine for key %d / operation %s\n", key, op) -// go func() { -// _ = <-ipc -// h(event.pdk) -// -// func() { -// defer func() { recover() }() -// ipc <- "ret" -// }() -// -// rh.lock.Lock() -// defer rh.lock.Unlock() -// event.instance.lastEventTime = time.Now() -// delete(rh.events, event.id) -// }() -// -// ipc <- "run" // kickstart the handler -// -// *out = StepData{EventId: event.id, Data: <-ipc} -// return nil -// } - // A callback's response/request. type StepData struct { EventId int // event cycle to which this belongs diff --git a/server/pbserver.go b/server/pbserver.go index e94ebd7..edf18bc 100644 --- a/server/pbserver.go +++ b/server/pbserver.go @@ -2,7 +2,6 @@ package server import ( "encoding/binary" - "errors" "fmt" "io" "log" @@ -13,14 +12,16 @@ import ( "google.golang.org/protobuf/proto" ) -func servePb(conn net.Conn, rh *rpcHandler) (err error) { +func servePb(conn net.Conn, rh *rpcHandler) { + var err error + var d, rd []byte for { - d, err := readPbFrame(conn) + d, err = readPbFrame(conn) if err != nil { break } - rd, err := codecPb(rh, conn, d) + rd, err = codecPb(rh, conn, d) if err != nil { break } @@ -35,8 +36,6 @@ func servePb(conn net.Conn, rh *rpcHandler) (err error) { if err != nil { log.Print(err) } - - return } func readPbFrame(conn net.Conn) (data []byte, err error) { @@ -47,9 +46,6 @@ func readPbFrame(conn net.Conn) (data []byte, err error) { } data = make([]byte, len) - if data == nil { - return nil, errors.New("no memory") - } _, err = io.ReadFull(conn, data) if err != nil { @@ -177,9 +173,7 @@ func handlePbEvent(rh *rpcHandler, conn net.Conn, e *kong_plugin_protocol.CmdHan pdk := pdk.Init(conn) h(pdk) - writePbFrame(conn, []byte{}) - - return nil + return writePbFrame(conn, []byte{}) } // Start the embedded plugin server, ProtoBuf version. diff --git a/server/rpc.go b/server/rpc.go index 2353e93..7b95d9c 100644 --- a/server/rpc.go +++ b/server/rpc.go @@ -17,8 +17,6 @@ type rpcHandler struct { instances map[int]*instanceData nextInstanceId int events map[int]*eventData - nextEventId int - lastStartInstance time.Time lastCloseInstance time.Time } diff --git a/test/test.go b/test/test.go index 467bad0..496c64f 100644 --- a/test/test.go +++ b/test/test.go @@ -131,14 +131,6 @@ func getPort(u *url.URL) int32 { return int32(portnum) } -func (req Request) getForwardedUrl() (*url.URL, error) { - u := req.Headers.Get("X-Forwarded-Proto") - if u == "" { - u = req.Url - } - return url.Parse(u) -} - // ToResponse creates a new Response object from a Request, // simulating an "echo" service. func (req Request) ToResponse() Response { @@ -159,15 +151,6 @@ type Response struct { Body string } -func (res Response) clone() Response { - return Response{ - Status: res.Status, - Message: res.Message, - Headers: res.Headers.Clone(), - Body: res.Body, - } -} - func (res *Response) merge(other Response) { if other.Status != 0 { res.Status = other.Status @@ -192,7 +175,6 @@ type envState int const ( running envState = iota finished - failed ) type TestEnv struct { @@ -386,6 +368,7 @@ func (e *TestEnv) Handle(method string, args_d []byte) []byte { u, err := url.Parse(e.ClientReq.Url) e.noErr(err) out, err = bridge.WrapHeaders(u.Query()) + e.noErr(err) case "kong.request.get_header": args := kong_plugin_protocol.String{}