Skip to content

Commit

Permalink
Merging to release-5.2: [TT-10531] improve plugin tests area (#5777)
Browse files Browse the repository at this point in the history
[TT-10531] improve plugin tests area (#5777)

PR does the following:

- compiles a plugin with and without race
- adds test.IsRaceEnabled
- fixes goplugin tests to load appropriate plugin

https://tyktech.atlassian.net/browse/TT-10531

---------

Co-authored-by: Tit Petric <tit@tyk.io>
  • Loading branch information
buger and Tit Petric authored Nov 17, 2023
1 parent 065a99b commit e410b7c
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 26 deletions.
3 changes: 2 additions & 1 deletion bin/ci-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ set -e

# build Go-plugin used in tests
echo "Building go plugin"
go build -race -o ./test/goplugins/goplugins.so -buildmode=plugin ./test/goplugins
go build -o ./test/goplugins/goplugins.so -buildmode=plugin ./test/goplugins
go build -race -o ./test/goplugins/goplugins_race.so -buildmode=plugin ./test/goplugins

for pkg in ${PKGS}; do
tags=""
Expand Down
57 changes: 32 additions & 25 deletions goplugin/mw_go_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ import (
"github.com/TykTechnologies/tyk/test"
)

func goPluginFilename() string {
if test.IsRaceEnabled() {
return "../test/goplugins/goplugins_race.so"
}
return "../test/goplugins/goplugins.so"
}

/*func TestMain(m *testing.M) {
os.Exit(gateway.InitTestMain(context.Background(), m))
}*/
Expand All @@ -43,23 +50,23 @@ func TestGoPluginMWs(t *testing.T) {
Pre: []apidef.MiddlewareDefinition{
{
Name: "MyPluginPre",
Path: "../test/goplugins/goplugins.so",
Path: goPluginFilename(),
},
},
AuthCheck: apidef.MiddlewareDefinition{
Name: "MyPluginAuthCheck",
Path: "../test/goplugins/goplugins.so",
Path: goPluginFilename(),
},
PostKeyAuth: []apidef.MiddlewareDefinition{
{
Name: "MyPluginPostKeyAuth",
Path: "../test/goplugins/goplugins.so",
Path: goPluginFilename(),
},
},
Post: []apidef.MiddlewareDefinition{
{
Name: "MyPluginPost",
Path: "../test/goplugins/goplugins.so",
Path: goPluginFilename(),
},
},
}
Expand All @@ -78,23 +85,23 @@ func TestGoPluginMWs(t *testing.T) {
Pre: []apidef.MiddlewareDefinition{
{
Name: "MyPluginPre",
Path: "../test/goplugins/goplugins.so",
Path: goPluginFilename(),
},
},
AuthCheck: apidef.MiddlewareDefinition{
Name: "MyPluginAuthCheck",
Path: "../test/goplugins/goplugins.so",
Path: goPluginFilename(),
},
PostKeyAuth: []apidef.MiddlewareDefinition{
{
Name: "MyPluginPostKeyAuth",
Path: "../test/goplugins/goplugins.so",
Path: goPluginFilename(),
},
},
Post: []apidef.MiddlewareDefinition{
{
Name: "MyPluginPost",
Path: "../test/goplugins/goplugins.so",
Path: goPluginFilename(),
},
},
}
Expand All @@ -113,21 +120,21 @@ func TestGoPluginMWs(t *testing.T) {
{
Disabled: true,
Name: "MyPluginPre",
Path: "../test/goplugins/goplugins.so",
Path: goPluginFilename(),
},
},
PostKeyAuth: []apidef.MiddlewareDefinition{
{
Disabled: true,
Name: "MyPluginPostKeyAuth",
Path: "../test/goplugins/goplugins.so",
Path: goPluginFilename(),
},
},
Post: []apidef.MiddlewareDefinition{
{
Disabled: true,
Name: "MyPluginPost",
Path: "../test/goplugins/goplugins.so",
Path: goPluginFilename(),
},
},
}
Expand All @@ -148,26 +155,26 @@ func TestGoPluginMWs(t *testing.T) {
{
Disabled: true,
Name: "MyPluginPre",
Path: "../test/goplugins/goplugins.so",
Path: goPluginFilename(),
},
},
AuthCheck: apidef.MiddlewareDefinition{
Disabled: true,
Name: "MyPluginAuthCheck",
Path: "../test/goplugins/goplugins.so",
Path: goPluginFilename(),
},
PostKeyAuth: []apidef.MiddlewareDefinition{
{
Disabled: true,
Name: "MyPluginPostKeyAuth",
Path: "../test/goplugins/goplugins.so",
Path: goPluginFilename(),
},
},
Post: []apidef.MiddlewareDefinition{
{
Disabled: true,
Name: "MyPluginPost",
Path: "../test/goplugins/goplugins.so",
Path: goPluginFilename(),
},
},
}
Expand Down Expand Up @@ -289,7 +296,7 @@ func TestGoPluginResponseHook(t *testing.T) {
Response: []apidef.MiddlewareDefinition{
{
Name: "MyPluginResponse",
Path: "../test/goplugins/goplugins.so",
Path: goPluginFilename(),
},
},
}
Expand Down Expand Up @@ -329,29 +336,29 @@ func TestGoPluginPerPathSingleFile(t *testing.T) {
goPluginMetaFoo := apidef.GoPluginMeta{
Path: "/foo",
Method: "GET",
PluginPath: "../test/goplugins/goplugins.so",
PluginPath: goPluginFilename(),
SymbolName: "MyPluginPerPathFoo",
}

goPluginMetaBar := apidef.GoPluginMeta{
Path: "/bar",
Method: "GET",
PluginPath: "../test/goplugins/goplugins.so",
PluginPath: goPluginFilename(),
SymbolName: "MyPluginPerPathBar",
}

goPluginMetaResp := apidef.GoPluginMeta{
Path: "/resp",
Method: "GET",
PluginPath: "../test/goplugins/goplugins.so",
PluginPath: goPluginFilename(),
SymbolName: "MyPluginPerPathResp",
}

goPluginMetaDisabled := apidef.GoPluginMeta{
Disabled: true,
Path: "/disabled",
Method: "GET",
PluginPath: "../test/goplugins/goplugins.so",
PluginPath: goPluginFilename(),
SymbolName: "MyPluginPerPathResp",
}

Expand Down Expand Up @@ -424,14 +431,14 @@ func TestGoPluginAPIandPerPath(t *testing.T) {
Pre: []apidef.MiddlewareDefinition{
{
Name: "MyPluginPre",
Path: "../test/goplugins/goplugins.so",
Path: goPluginFilename(),
},
},
}
goPluginMetaFoo := apidef.GoPluginMeta{
Path: "/foo",
Method: "GET",
PluginPath: "../test/goplugins/goplugins.so",
PluginPath: goPluginFilename(),
SymbolName: "MyPluginPerPathFoo",
}
v := spec.VersionData.Versions["v1"]
Expand Down Expand Up @@ -533,7 +540,7 @@ func TestGoPlugin_AccessingOASAPIDef(t *testing.T) {
Pre: []apidef.MiddlewareDefinition{
{
Name: "MyPluginAccessingOASAPI",
Path: "../test/goplugins/goplugins.so",
Path: goPluginFilename(),
},
},
}
Expand Down Expand Up @@ -563,7 +570,7 @@ func TestGoPlugin_PreventDoubleError(t *testing.T) {
Pre: []apidef.MiddlewareDefinition{
{
Name: "MyPluginReturningError",
Path: "../test/goplugins/goplugins.so",
Path: goPluginFilename(),
},
},
}
Expand Down Expand Up @@ -599,7 +606,7 @@ func TestGoPlugin_ApplyPolicy(t *testing.T) {
Pre: []apidef.MiddlewareDefinition{
{
Name: "MyPluginApplyingPolicy",
Path: "../test/goplugins/goplugins.so",
Path: goPluginFilename(),
},
},
}
Expand Down
8 changes: 8 additions & 0 deletions test/race_off.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:build !race
// +build !race

package test

func IsRaceEnabled() bool {
return false
}
8 changes: 8 additions & 0 deletions test/race_on.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:build race
// +build race

package test

func IsRaceEnabled() bool {
return true
}
11 changes: 11 additions & 0 deletions test/race_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package test

import "testing"

func TestIsRaceEnabled(t *testing.T) {
if enabled := IsRaceEnabled(); enabled {
t.Log("Flag -race passed")
} else {
t.Log("Flag -race omitted")
}
}

0 comments on commit e410b7c

Please sign in to comment.