-
Notifications
You must be signed in to change notification settings - Fork 45
Support TinyGo SDK for extension toolkit #104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 21 commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
73a23a2
Add tinygo lang
mathetake 11ee7a8
allow abi 0.1.0
mathetake d6f8eba
Fix e2e
mathetake ebb1b11
Fix e2e
mathetake 9dce679
allow abi 0.1.0
mathetake fbc011b
Fix e2e
mathetake f53036f
Fix e2e
mathetake f4f46d3
fix e2e on mac
mathetake e3c77b0
fix e2e
mathetake 29743bb
fix e2e
mathetake 98e0f9e
fix e2e
mathetake e568706
Clean up e2e
mathetake 54a9e7a
Run tinygo with 1.17.0
mathetake 98ebe99
Merge branch 'tinygo-extension' of github.com:tetratelabs/getenvoy in…
mathetake 3292cfd
Update tinygo builder
mathetake e046f90
Fix e2e
mathetake f4df444
Merge branch 'master' of github.com:tetratelabs/getenvoy into tinygo-…
mathetake 62d5aa7
merge origin/master
mathetake 25e93d8
Update TinyGo version
mathetake 7afb2c2
Fix e2e
mathetake dc70c79
merge origin/master
mathetake d5e430c
review: fix styles
mathetake 20e83c0
review: fix styles part 2
mathetake 93b40f6
improve access logger example: reading config
mathetake f250078
improve http example: reading config
mathetake 482909d
improve network example: reading config
mathetake 2f146fd
Fix unittests
mathetake 6f2e72c
Improve comments and namings
mathetake 2a300e9
Remove left 'exec'
mathetake b1b223d
Remove unnecessary rm in clean command
mathetake bbb29b3
Reivew: use enumed OnPluginStart, host.GetCounterMetric
mathetake f75cff7
Add go mod download
mathetake f960379
Re: fix entrypoint
mathetake 23e8fbc
Revert: add go.sum
mathetake 7443281
Revert debug code
mathetake 45a02f3
Add .gitignore, and set extension name in go.mod
mathetake b9024b0
default output to build/extension.wasm
mathetake 436d507
Update SDK: fix error
mathetake b485f05
Fix permission on host
mathetake 5ce9aa6
Fix unittest
mathetake b3c33e1
Fix comment
mathetake 52b7944
Fix permission, showcase config in access logger
mathetake 9715e55
Update SDK
mathetake File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
data/extension/init/templates/tinygo/envoy.access_loggers/default/go.mod
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| module my.filters.http | ||
|
|
||
| go 1.15 | ||
|
|
||
| require ( | ||
| github.com/stretchr/testify v1.6.1 | ||
| github.com/tetratelabs/proxy-wasm-go-sdk v0.0.14 | ||
| ) | ||
13 changes: 13 additions & 0 deletions
13
data/extension/init/templates/tinygo/envoy.access_loggers/default/go.sum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= | ||
mathetake marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
| github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
| github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
| github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
| github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= | ||
| github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
| github.com/tetratelabs/proxy-wasm-go-sdk v0.0.14 h1:13uKVfbtQJGshUqrGN7ujlJZwKYCS8N46NvBwKu5VvU= | ||
| github.com/tetratelabs/proxy-wasm-go-sdk v0.0.14/go.mod h1:y1ZQT4bQEBnR8Do4nSOzb3roczzPvcAp8UrF6NEYWNY= | ||
| gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
| gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
| gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= | ||
| gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||
29 changes: 29 additions & 0 deletions
29
data/extension/init/templates/tinygo/envoy.access_loggers/default/main.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package main | ||
mathetake marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| import ( | ||
| "github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm" | ||
| ) | ||
|
|
||
| func main() { | ||
| proxywasm.SetNewRootContext(newAccessLogger) | ||
| } | ||
|
|
||
| type accessLogger struct { | ||
| // you must embed the default context | ||
| proxywasm.DefaultRootContext | ||
| } | ||
|
|
||
| func newAccessLogger(contextID uint32) proxywasm.RootContext { | ||
| return &accessLogger{} | ||
| } | ||
|
|
||
| // override | ||
| func (ctx *accessLogger) OnLog() { | ||
| hdr, err := proxywasm.GetHttpRequestHeader(":path") | ||
| if err != nil { | ||
| proxywasm.LogCritical(err.Error()) | ||
| return | ||
| } | ||
|
|
||
| proxywasm.LogInfof("OnLog: :path = %s", hdr) | ||
| } | ||
28 changes: 28 additions & 0 deletions
28
data/extension/init/templates/tinygo/envoy.access_loggers/default/main_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/tetratelabs/proxy-wasm-go-sdk/proxytest" | ||
| "github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types" | ||
| ) | ||
|
|
||
| func TestHelloWorld_OnTick(t *testing.T) { | ||
mathetake marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| opt := proxytest.NewEmulatorOption(). | ||
| WithNewRootContext(newAccessLogger) | ||
| host := proxytest.NewHostEmulator(opt) | ||
| defer host.Done() // release the host emulation lock so that other test cases can insert their own host emulation | ||
|
|
||
mathetake marked this conversation as resolved.
Show resolved
Hide resolved
mathetake marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| path := [2]string{":path", "/this/is/path"} | ||
| host.CallOnLogForAccessLogger([][2]string{path}, nil) // call OnLog | ||
mathetake marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| logs := host.GetLogs(types.LogLevelInfo) | ||
| require.Greater(t, len(logs), 0) | ||
mathetake marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| msg := logs[len(logs)-1] | ||
|
|
||
| assert.True(t, strings.Contains(msg, "/this/is/path")) | ||
mathetake marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
8 changes: 8 additions & 0 deletions
8
data/extension/init/templates/tinygo/envoy.filters.http/default/go.mod
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| module my.filters.http | ||
mathetake marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| go 1.15 | ||
|
|
||
| require ( | ||
| github.com/stretchr/testify v1.6.1 | ||
| github.com/tetratelabs/proxy-wasm-go-sdk v0.0.14 | ||
| ) | ||
13 changes: 13 additions & 0 deletions
13
data/extension/init/templates/tinygo/envoy.filters.http/default/go.sum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= | ||
| github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
| github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
| github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
| github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
| github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= | ||
| github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
| github.com/tetratelabs/proxy-wasm-go-sdk v0.0.14 h1:13uKVfbtQJGshUqrGN7ujlJZwKYCS8N46NvBwKu5VvU= | ||
| github.com/tetratelabs/proxy-wasm-go-sdk v0.0.14/go.mod h1:y1ZQT4bQEBnR8Do4nSOzb3roczzPvcAp8UrF6NEYWNY= | ||
| gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
| gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
| gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= | ||
| gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
68 changes: 68 additions & 0 deletions
68
data/extension/init/templates/tinygo/envoy.filters.http/default/main.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package main | ||
mathetake marked this conversation as resolved.
Show resolved
Hide resolved
mathetake marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| import ( | ||
| "github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm" | ||
| "github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types" | ||
| ) | ||
|
|
||
| var ( | ||
| requestCounterName = "my_http_filter.request_counter" | ||
| counter proxywasm.MetricCounter | ||
| ) | ||
|
|
||
| func main() { | ||
| proxywasm.SetNewRootContext(newRootContext) | ||
| proxywasm.SetNewHttpContext(newHttpContext) | ||
| } | ||
|
|
||
| type rootContext struct { | ||
| // we must embed the default context | ||
mathetake marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| proxywasm.DefaultRootContext | ||
| contextID uint32 | ||
| } | ||
|
|
||
| func newRootContext(rootContextID uint32) proxywasm.RootContext { | ||
| return &rootContext{contextID: rootContextID} | ||
| } | ||
|
|
||
| func (ctx *rootContext) OnVMStart(vmConfigurationSize int) bool { | ||
| counter = proxywasm.DefineCounterMetric(requestCounterName) | ||
| return true | ||
| } | ||
|
|
||
| type httpContext struct { | ||
| // you must embed the default context | ||
| proxywasm.DefaultHttpContext | ||
| rootContextID, contextID uint32 | ||
| } | ||
|
|
||
| func newHttpContext(rootContextID, contextID uint32) proxywasm.HttpContext { | ||
| return &httpContext{contextID: contextID, rootContextID: rootContextID} | ||
| } | ||
|
|
||
| func (ctx *httpContext) OnHttpRequestHeaders(numHeaders int, endOfStream bool) types.Action { | ||
| hs, err := proxywasm.GetHttpRequestHeaders() | ||
| if err != nil { | ||
| proxywasm.LogCriticalf("failed to get request headers: %v", err) | ||
| return types.ActionPause | ||
| } | ||
|
|
||
| proxywasm.LogInfo("observing request headers") | ||
| for _, h := range hs { | ||
| proxywasm.LogInfof("%s: %s", h[0], h[1]) | ||
| } | ||
|
|
||
| return types.ActionContinue | ||
| } | ||
|
|
||
| func (ctx *httpContext) OnHttpResponseHeaders(numHeaders int, endOfStream bool) types.Action { | ||
| if err := proxywasm.SetHttpResponseHeader("additional", "header"); err != nil { | ||
| proxywasm.LogCriticalf("failed to add header: %v", err) | ||
| return types.ActionPause | ||
| } | ||
| return types.ActionContinue | ||
| } | ||
|
|
||
| func (ctx *httpContext) OnHttpStreamDone() { | ||
| counter.Increment(1) | ||
| } | ||
37 changes: 37 additions & 0 deletions
37
data/extension/init/templates/tinygo/envoy.filters.http/default/main_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/tetratelabs/proxy-wasm-go-sdk/proxytest" | ||
| "github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types" | ||
| ) | ||
|
|
||
| func TestHttpHeaders_OnHttpRequestHeaders(t *testing.T) { | ||
| opt := proxytest.NewEmulatorOption(). | ||
| WithNewRootContext(newRootContext). | ||
| WithNewHttpContext(newHttpContext) | ||
| host := proxytest.NewHostEmulator(opt) | ||
| defer host.Done() // release the host emulation lock so that other test cases can insert their own host emulation | ||
|
|
||
| host.StartVM() // call OnVMStart -> the metric is initialized | ||
|
|
||
| contextID := host.HttpFilterInitContext() // create http stream | ||
|
|
||
| hs := [][2]string{ | ||
| {"key1", "value1"}, | ||
| {"key2", "value2"}, | ||
| } | ||
|
|
||
| host.HttpFilterPutRequestHeaders(contextID, hs) // call OnHttpRequestHeaders | ||
mathetake marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| logs := host.GetLogs(types.LogLevelInfo) | ||
| require.Greater(t, len(logs), 2) | ||
|
|
||
| assert.Equal(t, "key2: value2", logs[len(logs)-1]) | ||
| assert.Equal(t, "key1: value1", logs[len(logs)-2]) | ||
| assert.Equal(t, "observing request headers", logs[len(logs)-3]) | ||
| } | ||
8 changes: 8 additions & 0 deletions
8
data/extension/init/templates/tinygo/envoy.filters.network/default/go.mod
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| module my.filters.http | ||
mathetake marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| go 1.15 | ||
|
|
||
| require ( | ||
| github.com/stretchr/testify v1.6.1 | ||
| github.com/tetratelabs/proxy-wasm-go-sdk v0.0.14 | ||
| ) | ||
13 changes: 13 additions & 0 deletions
13
data/extension/init/templates/tinygo/envoy.filters.network/default/go.sum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= | ||
| github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
| github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
| github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
| github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
| github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= | ||
| github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
| github.com/tetratelabs/proxy-wasm-go-sdk v0.0.14 h1:13uKVfbtQJGshUqrGN7ujlJZwKYCS8N46NvBwKu5VvU= | ||
| github.com/tetratelabs/proxy-wasm-go-sdk v0.0.14/go.mod h1:y1ZQT4bQEBnR8Do4nSOzb3roczzPvcAp8UrF6NEYWNY= | ||
| gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
| gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
| gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= | ||
| gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
51 changes: 51 additions & 0 deletions
51
data/extension/init/templates/tinygo/envoy.filters.network/default/main.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package main | ||
mathetake marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| import ( | ||
| "github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm" | ||
| "github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types" | ||
| ) | ||
|
|
||
| var ( | ||
| connectionCounterName = "my_network_filter.connection_counter" | ||
| counter proxywasm.MetricCounter | ||
| ) | ||
|
|
||
| func main() { | ||
| proxywasm.SetNewRootContext(newRootContext) | ||
| proxywasm.SetNewStreamContext(newStreamContext) | ||
| } | ||
|
|
||
| type rootContext struct { | ||
| // we must embed the default context | ||
| proxywasm.DefaultRootContext | ||
| contextID uint32 | ||
| } | ||
|
|
||
| func newRootContext(rootContextID uint32) proxywasm.RootContext { | ||
| return &rootContext{contextID: rootContextID} | ||
| } | ||
|
|
||
| func (ctx *rootContext) OnVMStart(vmConfigurationSize int) bool { | ||
| counter = proxywasm.DefineCounterMetric(connectionCounterName) | ||
| return true | ||
| } | ||
|
|
||
| type streamContext struct { | ||
| // we must embed the default context | ||
| proxywasm.DefaultStreamContext | ||
| rootContextID, contextID uint32 | ||
| } | ||
|
|
||
| func newStreamContext(rootContextID, contextID uint32) proxywasm.StreamContext { | ||
| return &streamContext{contextID: contextID, rootContextID: rootContextID} | ||
| } | ||
|
|
||
| func (ctx *streamContext) OnNewConnection() types.Action { | ||
| proxywasm.LogInfo("new connection!") | ||
| return types.ActionContinue | ||
| } | ||
|
|
||
| func (ctx *streamContext) OnStreamDone() { | ||
| counter.Increment(1) | ||
| proxywasm.LogInfo("connection complete!") | ||
| } | ||
43 changes: 43 additions & 0 deletions
43
data/extension/init/templates/tinygo/envoy.filters.network/default/main_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package main | ||
mathetake marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "github.com/tetratelabs/proxy-wasm-go-sdk/proxytest" | ||
| "github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types" | ||
| ) | ||
|
|
||
| func TestNetwork_OnNewConnection(t *testing.T) { | ||
| opt := proxytest.NewEmulatorOption(). | ||
| WithNewRootContext(newRootContext). | ||
| WithNewStreamContext(newStreamContext) | ||
| host := proxytest.NewHostEmulator(opt) | ||
| defer host.Done() // release the host emulation lock so that other test cases can insert their own host emulation | ||
|
|
||
| host.NetworkFilterInitConnection() // OnNewConnection is called | ||
|
|
||
| logs := host.GetLogs(types.LogLevelInfo) // retrieve logs emitted to Envoy | ||
| assert.Equal(t, logs[0], "new connection!") | ||
| } | ||
|
|
||
| func TestNetwork_counter(t *testing.T) { | ||
| opt := proxytest.NewEmulatorOption(). | ||
| WithNewRootContext(newRootContext). | ||
| WithNewStreamContext(newStreamContext) | ||
| host := proxytest.NewHostEmulator(opt) | ||
| defer host.Done() // release the host emulation lock so that other test cases can insert their own host emulation | ||
|
|
||
| host.StartVM() // init metric | ||
|
|
||
| contextID := host.NetworkFilterInitConnection() | ||
| host.NetworkFilterCompleteConnection(contextID) // call OnDone on contextID -> increment the connection counter | ||
|
|
||
| logs := host.GetLogs(types.LogLevelInfo) | ||
| require.Greater(t, len(logs), 0) | ||
|
|
||
| assert.Equal(t, "connection complete!", logs[len(logs)-1]) | ||
| actual := counter.Get() | ||
mathetake marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| assert.Equal(t, uint64(1), actual) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.