Skip to content

Commit

Permalink
Run unit tests with wasm binary (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
anuraaga authored Aug 22, 2022
1 parent 4d7dbf0 commit 332b706
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 35 deletions.
Binary file added .DS_Store
Binary file not shown.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ jobs:
restore-keys: |
${{ runner.os }}-tinygo-
- name: Run unit tests
run: make test

- name: Build WASM filter
run: make build

- name: Run unit tests
run: make test

- name: Run e2e tests
working-directory: e2e
run: docker-compose up --abort-on-container-exit
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.17
require (
github.com/corazawaf/coraza/v3 v3.0.0-20220818013656-f749c07295aa
github.com/stretchr/testify v1.7.1
github.com/tetratelabs/proxy-wasm-go-sdk v0.18.1-0.20220510133519-6240ca761207
github.com/tetratelabs/proxy-wasm-go-sdk v0.19.1-0.20220822060051-f9d179a57f8c
github.com/tidwall/gjson v1.14.2
)

Expand All @@ -16,9 +16,10 @@ require (
github.com/magefile/mage v1.13.0 // indirect
github.com/petar-dambovaliev/aho-corasick v0.0.0-20211021192214-5ab2d9280aa9 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/tetratelabs/wazero v0.0.0-20220819021810-7f8e629c653f // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
golang.org/x/net v0.0.0-20220809184613-07c6da5e1ced // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tetratelabs/proxy-wasm-go-sdk v0.18.1-0.20220510133519-6240ca761207 h1:2QpMF4ADtHe8QDgvDJVa04Bu3diiQ04iezw7AaSH9PU=
github.com/tetratelabs/proxy-wasm-go-sdk v0.18.1-0.20220510133519-6240ca761207/go.mod h1:Aq7ewLe7qv2pmzRgoYWk+5kYLj/oSYMJjvmPXpGSAbc=
github.com/tetratelabs/proxy-wasm-go-sdk v0.19.1-0.20220822060051-f9d179a57f8c h1:OCUFXVGixHLfNjg6/QYEhv+jHJ5mRGhpEUVFv9eWPJE=
github.com/tetratelabs/proxy-wasm-go-sdk v0.19.1-0.20220822060051-f9d179a57f8c/go.mod h1:5t/pWFNJ9eMyu/K/Z+OeGhDJ9sN9eCo8fc2pyM/Qjg4=
github.com/tetratelabs/wazero v0.0.0-20220819021810-7f8e629c653f h1:+InPNMTyR4bufxW+MzqigSOXpe9Ph++NOIz/N9wtEYs=
github.com/tetratelabs/wazero v0.0.0-20220819021810-7f8e629c653f/go.mod h1:CD5smBN5rGZo7UNe8aUiWyYE3bDWED/CQSonog9NSEg=
github.com/tidwall/gjson v1.14.2 h1:6BBkirS0rAHjumnjHF6qgy5d2YAJ1TLIaFE2lzfOLqo=
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
Expand Down Expand Up @@ -89,5 +91,5 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
77 changes: 51 additions & 26 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
package main

import (
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -16,52 +18,75 @@ import (
)

func TestHttpHeaders_OnHttpRequestHeaders(t *testing.T) {
type testCase struct {
tests := []struct {
name string
path string
expectedAction types.Action
responded403 bool
}

for name, tCase := range map[string]testCase{
"not matching URL": {
}{
{
name: "not matching URL",
path: "/",
expectedAction: types.ActionContinue,
responded403: false,
},
"matching URL": {
{
name: "matching URL",
path: "/admin",
expectedAction: types.ActionContinue,
responded403: true,
},
} {
t.Run(name, func(t *testing.T) {
opt := proxytest.
NewEmulatorOption().
WithVMContext(&vmContext{}).
WithPluginConfiguration([]byte(`
}

for _, runner := range []string{"go", "wasm"} {
t.Run(runner, func(t *testing.T) {
var vm types.VMContext
switch runner {
case "go":
vm = &vmContext{}
case "wasm":
wasm, err := os.ReadFile(filepath.Join("build", "main.wasm"))
if err != nil {
t.Skip("wasm not found")
}
v, err := proxytest.NewWasmVMContext(wasm)
require.NoError(t, err)
vm = v
}

for _, tc := range tests {
tt := tc

t.Run(tt.name, func(t *testing.T) {
opt := proxytest.
NewEmulatorOption().
WithVMContext(vm).
WithPluginConfiguration([]byte(`
{
"rules" : "SecRuleEngine On\nSecRule REQUEST_URI \"@streq /admin\" \"id:101,phase:1,t:lowercase,deny\""
}
`))
host, reset := proxytest.NewHostEmulator(opt)
defer reset()
host, reset := proxytest.NewHostEmulator(opt)
defer reset()

require.Equal(t, types.OnPluginStartStatusOK, host.StartPlugin())
require.Equal(t, types.OnPluginStartStatusOK, host.StartPlugin())

// Initialize http context.
id := host.InitializeHttpContext()
// Initialize http context.
id := host.InitializeHttpContext()

// Call OnHttpRequestHeaders.
hs := [][2]string{{":path", tCase.path}, {":method", "GET"}}
action := host.CallOnRequestHeaders(id, hs, false)
require.Equal(t, tCase.expectedAction, action)
// Call OnHttpRequestHeaders.
hs := [][2]string{{":path", tt.path}, {":method", "GET"}}
action := host.CallOnRequestHeaders(id, hs, false)
require.Equal(t, tt.expectedAction, action)

// Call OnHttpStreamDone.
host.CompleteHttpContext(id)
// Call OnHttpStreamDone.
host.CompleteHttpContext(id)

if tCase.responded403 {
resp := host.GetSentLocalResponse(id)
require.EqualValues(t, 403, resp.StatusCode)
if tt.responded403 {
resp := host.GetSentLocalResponse(id)
require.EqualValues(t, 403, resp.StatusCode)
}
})
}
})
}
Expand Down

0 comments on commit 332b706

Please sign in to comment.