Skip to content

Commit

Permalink
Move coprocess tests to the coprocess package and update ci-test.sh (T…
Browse files Browse the repository at this point in the history
  • Loading branch information
furkansenharputlu authored and buger committed Jul 1, 2019
1 parent 6a56026 commit 33e50b4
Show file tree
Hide file tree
Showing 32 changed files with 299 additions and 272 deletions.
63 changes: 31 additions & 32 deletions bin/ci-test.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
#!/bin/bash


MATRIX=(
"-tags 'coprocess python goplugin'"
"-tags 'coprocess grpc goplugin'"
)
TEST_TIMEOUT=2m
TEST_TIMEOUT=3m

# print a command and execute it
show() {
Expand All @@ -18,7 +13,8 @@ fatal() {
exit 1
}

if [[ $LATEST_GO ]]; then
race=""
if [[ ${LATEST_GO} ]]; then
FMT_FILES=$(gofmt -l . | grep -v vendor)
if [[ -n $FMT_FILES ]]; then
fatal "Run 'gofmt -w' on these files:\n$FMT_FILES"
Expand All @@ -32,40 +28,43 @@ if [[ $LATEST_GO ]]; then
fi

echo "goimports check is ok!"
fi

PKGS="$(go list ./... | grep -v /vendor/ |grep -v /tyk$)"
# Run with race if latest
race="-race"
fi

i=0
PKGS="$(go list -tags "coprocess python grpc" ./...)"

go get -t

# build Go-plugin used in tests
go build -o ./test/goplugins/goplugins.so -buildmode=plugin ./test/goplugins || fatal "building Go-plugin failed"
go build ${race} -o ./test/goplugins/goplugins.so -buildmode=plugin ./test/goplugins || fatal "building Go-plugin failed"

# need to do per-pkg because go test doesn't support a single coverage
# profile for multiple pkgs
for pkg in $PKGS; do
for opts in "${MATRIX[@]}"; do
show go test -v -timeout $TEST_TIMEOUT -coverprofile=test-$i.cov $opts $pkg \
|| fatal "go test errored"
let i++ || true
done
done

if [[ ! $LATEST_GO ]]; then
echo "Skipping race, checks, and coverage report"
exit 0
fi
tags=""

# TODO: Remove skipRace variable after solving race conditions in tests.
skipRace=false
if [[ ${pkg} == *"coprocess/grpc" ]]; then
tags="-tags 'coprocess grpc'"
skipRace=true
elif [[ ${pkg} == *"coprocess/python" ]]; then
tags="-tags 'coprocess python'"
elif [[ ${pkg} == *"coprocess" ]]; then
tags="-tags 'coprocess'"
skipRace=true
elif [[ ${pkg} == *"goplugin" ]]; then
tags="-tags 'goplugin'"
fi

# build Go-plugin used in tests but with race support
mv ./test/goplugins/goplugins.so ./test/goplugins/goplugins_old.so
go build -race -o ./test/goplugins/goplugins.so -buildmode=plugin ./test/goplugins \
|| fatal "building Go-plugin with race failed"
race=""

go test -race -v -timeout $TEST_TIMEOUT $PKGS || fatal "go test -race failed"
mv ./test/goplugins/goplugins_old.so ./test/goplugins/goplugins.so
# Some tests should not be run with -race. Therefore, test them with penultimate Go version.
# And, test with -race in latest Go version.
if [[ ${LATEST_GO} && ${skipRace} = false ]]; then
race="-race"
fi

for opts in "${MATRIX[@]}"; do
show go vet $opts $PKGS || fatal "go vet errored"
show go test -v ${race} -timeout ${TEST_TIMEOUT} -coverprofile=test.cov $pkg ${tags} || fatal "Test Failed"
show go vet ${tags} $pkg || fatal "go vet errored"
done
96 changes: 51 additions & 45 deletions gateway/coprocess_test.go → coprocess/coprocess_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
// +build !python
// +build !grpc

package gateway
package coprocess_test

import (
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"os"
"testing"

"github.com/golang/protobuf/proto"
Expand All @@ -18,20 +19,25 @@ import (

"github.com/TykTechnologies/tyk/apidef"
"github.com/TykTechnologies/tyk/coprocess"
"github.com/TykTechnologies/tyk/gateway"
logger "github.com/TykTechnologies/tyk/log"
)

const baseMiddlewarePath = "middleware/python"

var (
CoProcessName = apidef.MiddlewareDriver("test")
MessageType = coprocess.ProtobufMessage
testDispatcher, _ = NewCoProcessDispatcher()
testDispatcher, _ = gateway.NewCoProcessDispatcher()
log = logger.Get()

coprocessLog = log.WithFields(logrus.Fields{
"prefix": "coprocess",
})
)

func TestMain(m *testing.M) {
os.Exit(gateway.InitTestMain(m))
}

/* Dispatcher functions */

func TestCoProcessDispatch(t *testing.T) {
Expand All @@ -46,26 +52,26 @@ func TestCoProcessDispatch(t *testing.T) {
}

func TestCoProcessDispatchEvent(t *testing.T) {
spec := createSpecTest(t, basicCoProcessDef)
spec := gateway.CreateSpecTest(t, basicCoProcessDef)
remote, _ := url.Parse(spec.Proxy.TargetURL)
proxy := TykNewSingleHostReverseProxy(remote, spec)
baseMid := BaseMiddleware{spec, proxy, coprocessLog}
proxy := gateway.TykNewSingleHostReverseProxy(remote, spec)
baseMid := gateway.BaseMiddleware{Spec: spec, Proxy: proxy}

meta := EventKeyFailureMeta{
EventMetaDefault: EventMetaDefault{Message: "Auth Failure"},
meta := gateway.EventKeyFailureMeta{
EventMetaDefault: gateway.EventMetaDefault{Message: "Auth Failure"},
Path: "/",
Origin: "127.0.0.1",
Key: "abc",
}

baseMid.FireEvent(EventAuthFailure, meta)
baseMid.FireEvent(gateway.EventAuthFailure, meta)

wrapper := CoProcessEventWrapper{}
if err := json.Unmarshal(<-CoProcessDispatchEvent, &wrapper); err != nil {
wrapper := gateway.CoProcessEventWrapper{}
if err := json.Unmarshal(<-gateway.CoProcessDispatchEvent, &wrapper); err != nil {
t.Fatal(err)
}

if wrapper.Event.Type != EventAuthFailure {
if wrapper.Event.Type != gateway.EventAuthFailure {
t.Fatal("Wrong event type")
}

Expand All @@ -77,9 +83,9 @@ func TestCoProcessDispatchEvent(t *testing.T) {

func TestCoProcessReload(t *testing.T) {
// Use this as the GlobalDispatcher:
GlobalDispatcher = testDispatcher
doCoprocessReload()
if !testDispatcher.reloaded {
gateway.GlobalDispatcher = testDispatcher
gateway.DoCoprocessReload()
if !testDispatcher.Reloaded {
t.Fatal("coprocess reload wasn't run")
}
}
Expand Down Expand Up @@ -112,63 +118,63 @@ func TestCoProcessGetSetData(t *testing.T) {
value := "testvalue"
ttl := 1000

TestTykStoreData(key, value, ttl)
gateway.TestTykStoreData(key, value, ttl)

retrievedValue := TestTykGetData("testkey")
retrievedValue := gateway.TestTykGetData("testkey")

if retrievedValue != value {
t.Fatal("Couldn't retrieve key value using CP API")
}
}

func TestCoProcessTykTriggerEvent(t *testing.T) {
TestTykTriggerEvent("testevent", "testpayload")
gateway.TestTykTriggerEvent("testevent", "testpayload")
}

/* Middleware */

func buildCoProcessChain(spec *APISpec, hookName string, hookType coprocess.HookType, driver apidef.MiddlewareDriver) http.Handler {
func buildCoProcessChain(spec *gateway.APISpec, hookName string, hookType coprocess.HookType, driver apidef.MiddlewareDriver) http.Handler {
remote, _ := url.Parse(spec.Proxy.TargetURL)
proxy := TykNewSingleHostReverseProxy(remote, spec)
proxyHandler := ProxyHandler(proxy, spec)
baseMid := BaseMiddleware{spec, proxy, coprocessLog}
mw := CreateCoProcessMiddleware(hookName, hookType, driver, baseMid)
proxy := gateway.TykNewSingleHostReverseProxy(remote, spec)
proxyHandler := gateway.ProxyHandler(proxy, spec)
baseMid := gateway.BaseMiddleware{Spec: spec, Proxy: proxy} // TODO
mw := gateway.CreateCoProcessMiddleware(hookName, hookType, driver, baseMid)
return alice.New(mw).Then(proxyHandler)
}

func TestCoProcessMiddleware(t *testing.T) {
spec := createSpecTest(t, basicCoProcessDef)
spec := gateway.CreateSpecTest(t, basicCoProcessDef)

chain := buildCoProcessChain(spec, "hook_test", coprocess.HookType_Pre, apidef.MiddlewareDriver("python"))

session := CreateStandardSession()
session := gateway.CreateStandardSession()
spec.SessionManager.UpdateSession("abc", session, 60, false)

recorder := httptest.NewRecorder()

req := testReq(t, "GET", "/headers", nil)
req := gateway.TestReq(t, "GET", "/headers", nil)
req.Header.Set("authorization", "abc")

chain.ServeHTTP(recorder, req)
}

func TestCoProcessObjectPostProcess(t *testing.T) {
spec := createSpecTest(t, basicCoProcessDef)
spec := gateway.CreateSpecTest(t, basicCoProcessDef)

chain := buildCoProcessChain(spec, "hook_test_object_postprocess", coprocess.HookType_Pre, apidef.MiddlewareDriver("python"))

session := CreateStandardSession()
session := gateway.CreateStandardSession()
spec.SessionManager.UpdateSession("abc", session, 60, false)

recorder := httptest.NewRecorder()

req := testReq(t, "GET", "/headers", nil)
req := gateway.TestReq(t, "GET", "/headers", nil)
req.Header.Set("authorization", "abc")
req.Header.Set("Deletethisheader", "value")

chain.ServeHTTP(recorder, req)

resp := testHttpResponse{}
resp := gateway.TestHttpResponse{}
if err := json.Unmarshal(recorder.Body.Bytes(), &resp); err != nil {
t.Fatal(err)
}
Expand All @@ -182,12 +188,12 @@ func TestCoProcessObjectPostProcess(t *testing.T) {
recorder = httptest.NewRecorder()

uri := "/get?a=a_value&b=123&remove=3"
req = testReq(t, "GET", uri, nil)
req = gateway.TestReq(t, "GET", uri, nil)
req.Header.Set("authorization", "abc")

chain.ServeHTTP(recorder, req)

resp = testHttpResponse{}
resp = gateway.TestHttpResponse{}
if err := json.Unmarshal(recorder.Body.Bytes(), &resp); err != nil {
t.Fatal(err)
}
Expand All @@ -207,35 +213,35 @@ func TestCoProcessObjectPostProcess(t *testing.T) {

func TestCoProcessAuth(t *testing.T) {
t.Log("CP AUTH")
spec := createSpecTest(t, protectedCoProcessDef)
spec := gateway.CreateSpecTest(t, protectedCoProcessDef)

chain := buildCoProcessChain(spec, "hook_test_bad_auth", coprocess.HookType_CustomKeyCheck, apidef.MiddlewareDriver("python"))

session := CreateStandardSession()
session := gateway.CreateStandardSession()
spec.SessionManager.UpdateSession("abc", session, 60, false)

recorder := httptest.NewRecorder()

req := testReq(t, "GET", "/headers", nil)
req := gateway.TestReq(t, "GET", "/headers", nil)
req.Header.Set("authorization", "abc")

chain.ServeHTTP(recorder, req)

if recorder.Code != 403 {
t.Fatal("Authentication should fail! But it's returning:", recorder.Code)
}
<-CoProcessDispatchEvent
<-gateway.CoProcessDispatchEvent
}

func TestCoProcessReturnOverrides(t *testing.T) {
spec := createSpecTest(t, basicCoProcessDef)
spec := gateway.CreateSpecTest(t, basicCoProcessDef)
chain := buildCoProcessChain(spec, "hook_test_return_overrides", coprocess.HookType_Pre, apidef.MiddlewareDriver("python"))
session := CreateStandardSession()
session := gateway.CreateStandardSession()
spec.SessionManager.UpdateSession("abc", session, 60, false)

recorder := httptest.NewRecorder()

req := testReq(t, "GET", "/headers", nil)
req := gateway.TestReq(t, "GET", "/headers", nil)
req.Header.Set("authorization", "abc")
chain.ServeHTTP(recorder, req)
if recorder.Code != 200 || recorder.Body.String() != "body" {
Expand All @@ -248,14 +254,14 @@ func TestCoProcessReturnOverrides(t *testing.T) {
}

func TestCoProcessReturnOverridesErrorMessage(t *testing.T) {
spec := createSpecTest(t, basicCoProcessDef)
spec := gateway.CreateSpecTest(t, basicCoProcessDef)
chain := buildCoProcessChain(spec, "hook_test_return_overrides_error", coprocess.HookType_Pre, apidef.MiddlewareDriver("python"))
session := CreateStandardSession()
session := gateway.CreateStandardSession()
spec.SessionManager.UpdateSession("abc", session, 60, false)

recorder := httptest.NewRecorder()

req := testReq(t, "GET", "/headers", nil)
req := gateway.TestReq(t, "GET", "/headers", nil)
req.Header.Set("authorization", "abc")
chain.ServeHTTP(recorder, req)
if recorder.Code != 401 || recorder.Body.String() != "{\n \"error\": \"custom error message\"\n}" {
Expand Down Expand Up @@ -286,7 +292,7 @@ const basicCoProcessDef = `{
},
"proxy": {
"listen_path": "/v1",
"target_url": "` + testHttpGet + `"
"target_url": "` + gateway.TestHttpGet + `"
}
}`

Expand Down Expand Up @@ -316,6 +322,6 @@ const protectedCoProcessDef = `{
},
"proxy": {
"listen_path": "/v1",
"target_url": "` + testHttpGet + `"
"target_url": "` + gateway.TestHttpGet + `"
}
}`
Loading

0 comments on commit 33e50b4

Please sign in to comment.