forked from tetratelabs/proxy-wasm-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain_test.go
66 lines (55 loc) · 1.96 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// The framework emulates the expected behavior of Envoyproxy, and you can test your extensions without running Envoy and with
// the standard Go CLI. To run tests, simply run
// go test ./...
package main
import (
"os"
"testing"
"github.com/proxy-wasm/proxy-wasm-go-sdk/proxywasm/proxytest"
"github.com/proxy-wasm/proxy-wasm-go-sdk/proxywasm/types"
"github.com/stretchr/testify/require"
)
func TestData(t *testing.T) {
vmTest(t, func(t *testing.T, vm types.VMContext) {
opt := proxytest.NewEmulatorOption().WithVMContext(vm)
host, reset := proxytest.NewHostEmulator(opt)
defer reset()
// Call OnVMStart -> set initial value.
require.Equal(t, types.OnVMStartStatusOK, host.StartVM())
// Initialize http context.
contextID := host.InitializeHttpContext()
// Call OnHttpRequestHeaders.
action := host.CallOnRequestHeaders(contextID, nil, false)
require.Equal(t, types.ActionContinue, action)
// Check Envoy logs.
logs := host.GetInfoLogs()
require.Contains(t, logs, "shared value: 1")
// Call OnHttpRequestHeaders again.
action = host.CallOnRequestHeaders(contextID, nil, false)
require.Equal(t, types.ActionContinue, action)
action = host.CallOnRequestHeaders(contextID, nil, false)
require.Equal(t, types.ActionContinue, action)
// Check Envoy logs.
logs = host.GetInfoLogs()
require.Contains(t, logs, "shared value: 3")
})
}
// vmTest executes f twice, once with a types.VMContext that executes plugin code directly
// in the host, and again by executing the plugin code within the compiled main.wasm binary.
// Execution with main.wasm will be skipped if the file cannot be found.
func vmTest(t *testing.T, f func(*testing.T, types.VMContext)) {
t.Helper()
t.Run("go", func(t *testing.T) {
f(t, &vmContext{})
})
t.Run("wasm", func(t *testing.T) {
wasm, err := os.ReadFile("main.wasm")
if err != nil {
t.Skip("wasm not found")
}
v, err := proxytest.NewWasmVMContext(wasm)
require.NoError(t, err)
defer v.Close()
f(t, v)
})
}