Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit 29b5de7

Browse files
authored
Merge pull request #68 from tetratelabs/refactoring
refactoring: clean up API
2 parents d91a09b + 04c9acd commit 29b5de7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1555
-1269
lines changed

README.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,12 @@ TinyGo's official release of WASI target will come soon, and after that you coul
5252
just follow https://tinygo.org/getting-started/ to install the requirement on any platform. Stay tuned!
5353

5454

55-
### compatible Envoy builds
56-
57-
| proxy-wasm-go-sdk| proxy-wasm ABI version | envoyproxy/envoy-wasm| istio/proxyv2|
58-
|:-------------:|:-------------:|:-------------:|:-------------:|
59-
| main | 0.2.0| N/A | v1.17.x |
60-
| v0.0.4 | 0.2.0| N/A | v1.17.x |
61-
| v0.0.3 | 0.2.0| N/A | v1.17.x |
62-
| v0.0.2 | 0.1.0|release/v1.15 | N/A |
55+
### compatible ABI / Envoy builds
56+
57+
| proxy-wasm-go-sdk| proxy-wasm ABI version |istio/proxyv2|
58+
|:-------------:|:-------------:|:-------------:|
59+
| main | 0.2.0| v1.17.x |
60+
| v0.0.4 | 0.2.0| v1.17.x |
6361

6462

6563
## run examples

examples/helloworld/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func main() {
2626

2727
type helloWorld struct {
2828
// you must embed the default context so that you need not to reimplement all the methods by yourself
29-
proxywasm.DefaultContext
29+
proxywasm.DefaultRootContext
3030
contextID uint32
3131
}
3232

@@ -37,14 +37,14 @@ func newHelloWorld(contextID uint32) proxywasm.RootContext {
3737
// override
3838
func (ctx *helloWorld) OnVMStart(int) bool {
3939
proxywasm.LogInfo("proxy_on_vm_start from Go!")
40-
if err := proxywasm.HostCallSetTickPeriodMilliSeconds(tickMilliseconds); err != nil {
40+
if err := proxywasm.SetTickPeriodMilliSeconds(tickMilliseconds); err != nil {
4141
proxywasm.LogCriticalf("failed to set tick period: %v", err)
4242
}
4343
return true
4444
}
4545

4646
// override
4747
func (ctx *helloWorld) OnTick() {
48-
t := proxywasm.HostCallGetCurrentTime()
48+
t := proxywasm.GetCurrentTime()
4949
proxywasm.LogInfof("OnTick on %d, it's %d", ctx.contextID, t)
5050
}

examples/helloworld/main_test.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package main
33
import (
44
"strings"
55
"testing"
6+
"time"
7+
8+
"github.com/stretchr/testify/require"
69

710
"github.com/stretchr/testify/assert"
811

@@ -11,23 +14,29 @@ import (
1114
)
1215

1316
func TestHelloWorld_OnTick(t *testing.T) {
14-
ctx := newHelloWorld(100)
15-
host, done := proxytest.NewRootFilterHost(ctx, nil, nil)
16-
defer done() // release the host emulation lock so that other test cases can insert their own host emulation
17-
ctx.OnTick()
17+
opt := proxytest.NewEmulatorOption().
18+
WithNewRootContext(newHelloWorld)
19+
host := proxytest.NewHostEmulator(opt)
20+
defer host.Done() // release the host emulation lock so that other test cases can insert their own host emulation
21+
22+
host.StartVM() // call OnVMStart
23+
24+
time.Sleep(time.Duration(tickMilliseconds) * 4 * time.Millisecond)
1825

1926
logs := host.GetLogs(types.LogLevelInfo)
27+
require.Greater(t, len(logs), 0)
2028
msg := logs[len(logs)-1]
2129

2230
assert.True(t, strings.Contains(msg, "OnTick on"))
2331
}
2432

2533
func TestHelloWorld_OnVMStart(t *testing.T) {
26-
ctx := newHelloWorld(0)
27-
host, done := proxytest.NewRootFilterHost(ctx, nil, nil)
28-
defer done() // release the host emulation lock so that other test cases can insert their own host emulation
34+
opt := proxytest.NewEmulatorOption().
35+
WithNewRootContext(newHelloWorld)
36+
host := proxytest.NewHostEmulator(opt)
37+
defer host.Done() // release the host emulation lock so that other test cases can insert their own host emulation
2938

30-
host.StartVM()
39+
host.StartVM() // call OnVMStart
3140
logs := host.GetLogs(types.LogLevelInfo)
3241
msg := logs[len(logs)-1]
3342

examples/http_auth_random/main.go

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func main() {
2929

3030
type httpAuthRandom struct {
3131
// you must embed the default context so that you need not to reimplement all the methods by yourself
32-
proxywasm.DefaultContext
32+
proxywasm.DefaultHttpContext
3333
contextID uint32
3434
}
3535

@@ -39,7 +39,7 @@ func newContext(contextID uint32) proxywasm.HttpContext {
3939

4040
// override default
4141
func (ctx *httpAuthRandom) OnHttpRequestHeaders(int, bool) types.Action {
42-
hs, err := proxywasm.HostCallGetHttpRequestHeaders()
42+
hs, err := proxywasm.GetHttpRequestHeaders()
4343
if err != nil {
4444
proxywasm.LogCriticalf("failed to get request headers: %v", err)
4545
return types.ActionContinue
@@ -48,19 +48,18 @@ func (ctx *httpAuthRandom) OnHttpRequestHeaders(int, bool) types.Action {
4848
proxywasm.LogInfof("request header: %s: %s", h[0], h[1])
4949
}
5050

51-
if _, err := proxywasm.HostCallDispatchHttpCall(
52-
clusterName, hs, "", [][2]string{}, 50000); err != nil {
51+
if _, err := proxywasm.DispatchHttpCall(clusterName, hs, "", [][2]string{},
52+
50000, httpCallResponseCallback); err != nil {
5353
proxywasm.LogCriticalf("dipatch httpcall failed: %v", err)
54+
return types.ActionContinue
5455
}
5556

5657
proxywasm.LogInfof("http call dispatched to %s", clusterName)
57-
5858
return types.ActionPause
5959
}
6060

61-
// override default
62-
func (ctx *httpAuthRandom) OnHttpCallResponse(_ int, bodySize int, _ int) {
63-
hs, err := proxywasm.HostCallGetHttpCallResponseHeaders()
61+
func httpCallResponseCallback(_ int, bodySize int, _ int) {
62+
hs, err := proxywasm.GetHttpCallResponseHeaders()
6463
if err != nil {
6564

6665
proxywasm.LogCriticalf("failed to get response body: %v", err)
@@ -71,34 +70,29 @@ func (ctx *httpAuthRandom) OnHttpCallResponse(_ int, bodySize int, _ int) {
7170
proxywasm.LogInfof("response header from %s: %s: %s", clusterName, h[0], h[1])
7271
}
7372

74-
b, err := proxywasm.HostCallGetHttpCallResponseBody(0, bodySize)
73+
b, err := proxywasm.GetHttpCallResponseBody(0, bodySize)
7574
if err != nil {
7675
proxywasm.LogCriticalf("failed to get response body: %v", err)
77-
proxywasm.HostCallResumeHttpRequest()
76+
proxywasm.ResumeHttpRequest()
7877
return
7978
}
8079

8180
s := fnv.New32a()
8281
if _, err := s.Write(b); err != nil {
8382
proxywasm.LogCriticalf("failed to calculate hash: %v", err)
84-
proxywasm.HostCallResumeHttpRequest()
83+
proxywasm.ResumeHttpRequest()
8584
return
8685
}
8786

8887
if s.Sum32()%2 == 0 {
8988
proxywasm.LogInfo("access granted")
90-
proxywasm.HostCallResumeHttpRequest()
89+
proxywasm.ResumeHttpRequest()
9190
return
9291
}
9392

9493
msg := "access forbidden"
9594
proxywasm.LogInfo(msg)
96-
proxywasm.HostCallSendHttpResponse(403, [][2]string{
95+
proxywasm.SendHttpResponse(403, [][2]string{
9796
{"powered-by", "proxy-wasm-go-sdk!!"},
9897
}, msg)
9998
}
100-
101-
// override default
102-
func (ctx *httpAuthRandom) OnLog() {
103-
proxywasm.LogInfof("%d finished", ctx.contextID)
104-
}

examples/http_auth_random/main_test.go

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,20 @@ import (
1111
)
1212

1313
func TestHttpAuthRandom_OnHttpRequestHeaders(t *testing.T) {
14-
host, done := proxytest.NewHttpFilterHost(newContext)
15-
defer done()
14+
opt := proxytest.NewEmulatorOption().
15+
WithNewHttpContext(newContext)
16+
host := proxytest.NewHostEmulator(opt)
17+
defer host.Done()
1618

17-
id := host.InitContext()
18-
host.PutRequestHeaders(id, [][2]string{{"key", "value"}}) // OnHttpRequestHeaders called
19+
contextID := host.HttpFilterInitContext()
20+
host.HttpFilterPutRequestHeaders(contextID, [][2]string{{"key", "value"}}) // OnHttpRequestHeaders called
1921

20-
require.True(t, host.IsDispatchCalled(id)) // check if http call is dispatched
21-
require.Equal(t, types.ActionPause, host.GetCurrentAction(id)) // check if the current action is pause
22+
attrs := host.GetCalloutAttributesFromContext(contextID)
23+
require.Equal(t, len(attrs), 1) // verify DispatchHttpCall is called
24+
25+
require.Equal(t, "httpbin", attrs[0].Upstream)
26+
require.Equal(t, types.ActionPause,
27+
host.HttpFilterGetCurrentStreamAction(contextID)) // check if the current action is pause
2228

2329
logs := host.GetLogs(types.LogLevelInfo)
2430
require.GreaterOrEqual(t, len(logs), 2)
@@ -28,8 +34,10 @@ func TestHttpAuthRandom_OnHttpRequestHeaders(t *testing.T) {
2834
}
2935

3036
func TestHttpAuthRandom_OnHttpCallResponse(t *testing.T) {
31-
host, done := proxytest.NewHttpFilterHost(newContext)
32-
defer done()
37+
opt := proxytest.NewEmulatorOption().
38+
WithNewHttpContext(newContext)
39+
host := proxytest.NewHostEmulator(opt)
40+
defer host.Done()
3341

3442
// http://httpbin.org/uuid
3543
headers := [][2]string{
@@ -40,20 +48,30 @@ func TestHttpAuthRandom_OnHttpCallResponse(t *testing.T) {
4048
}
4149

4250
// access granted body
43-
id := host.InitContext()
51+
contextID := host.HttpFilterInitContext()
52+
host.HttpFilterPutRequestHeaders(contextID, nil) // OnHttpRequestHeaders called
53+
4454
body := []byte(`{"uuid": "7b10a67a-1c67-4199-835b-cbefcd4a63d4"}`)
45-
host.PutCalloutResponse(id, headers, nil, body)
46-
assert.Nil(t, host.GetSentLocalResponse(id))
55+
attrs := host.GetCalloutAttributesFromContext(contextID)
56+
require.Equal(t, len(attrs), 1) // verify DispatchHttpCall is called
57+
58+
host.PutCalloutResponse(attrs[0].CalloutID, headers, nil, body)
59+
assert.Nil(t, host.HttpFilterGetSentLocalResponse(contextID))
4760

4861
logs := host.GetLogs(types.LogLevelInfo)
4962
require.Greater(t, len(logs), 1)
5063
assert.Equal(t, "access granted", logs[len(logs)-1])
5164

5265
// access denied body
53-
id = host.InitContext()
66+
contextID = host.HttpFilterInitContext()
67+
host.HttpFilterPutRequestHeaders(contextID, nil) // OnHttpRequestHeaders called
68+
5469
body = []byte(`{"uuid": "aaaaaaaa-1c67-4199-835b-cbefcd4a63d4"}`)
55-
host.PutCalloutResponse(id, headers, nil, body)
56-
localResponse := host.GetSentLocalResponse(id) // check local responses
70+
attrs = host.GetCalloutAttributesFromContext(contextID)
71+
require.Equal(t, len(attrs), 1) // verify DispatchHttpCall is called
72+
73+
host.PutCalloutResponse(attrs[0].CalloutID, headers, nil, body)
74+
localResponse := host.HttpFilterGetSentLocalResponse(contextID) // check local responses
5775
assert.NotNil(t, localResponse)
5876
logs = host.GetLogs(types.LogLevelInfo)
5977
assert.Equal(t, "access forbidden", logs[len(logs)-1])

examples/http_headers/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func main() {
2525

2626
type httpHeaders struct {
2727
// you must embed the default context so that you need not to reimplement all the methods by yourself
28-
proxywasm.DefaultContext
28+
proxywasm.DefaultHttpContext
2929
contextID uint32
3030
}
3131

@@ -35,7 +35,7 @@ func newContext(contextID uint32) proxywasm.HttpContext {
3535

3636
// override
3737
func (ctx *httpHeaders) OnHttpRequestHeaders(int, bool) types.Action {
38-
hs, err := proxywasm.HostCallGetHttpRequestHeaders()
38+
hs, err := proxywasm.GetHttpRequestHeaders()
3939
if err != nil {
4040
proxywasm.LogCriticalf("failed to get request headers: %v", err)
4141
}
@@ -48,7 +48,7 @@ func (ctx *httpHeaders) OnHttpRequestHeaders(int, bool) types.Action {
4848

4949
// override
5050
func (ctx *httpHeaders) OnHttpResponseHeaders(int, bool) types.Action {
51-
hs, err := proxywasm.HostCallGetHttpResponseHeaders()
51+
hs, err := proxywasm.GetHttpResponseHeaders()
5252
if err != nil {
5353
proxywasm.LogCriticalf("failed to get request headers: %v", err)
5454
}
@@ -60,6 +60,6 @@ func (ctx *httpHeaders) OnHttpResponseHeaders(int, bool) types.Action {
6060
}
6161

6262
// override
63-
func (ctx *httpHeaders) OnLog() {
63+
func (ctx *httpHeaders) OnHttpStreamDone() {
6464
proxywasm.LogInfof("%d finished", ctx.contextID)
6565
}

examples/http_headers/main_test.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"fmt"
45
"testing"
56

67
"github.com/stretchr/testify/assert"
@@ -11,31 +12,40 @@ import (
1112
)
1213

1314
func TestHttpHeaders_OnHttpRequestHeaders(t *testing.T) {
14-
host, done := proxytest.NewHttpFilterHost(newContext)
15-
defer done()
16-
id := host.InitContext()
15+
opt := proxytest.NewEmulatorOption().
16+
WithNewHttpContext(newContext)
17+
host := proxytest.NewHostEmulator(opt)
18+
defer host.Done()
19+
id := host.HttpFilterInitContext()
1720

1821
hs := [][2]string{{"key1", "value1"}, {"key2", "value2"}}
19-
host.PutRequestHeaders(id, hs) // call OnHttpRequestHeaders
22+
host.HttpFilterPutRequestHeaders(id, hs) // call OnHttpRequestHeaders
23+
24+
host.HttpFilterCompleteHttpStream(id)
2025

2126
logs := host.GetLogs(types.LogLevelInfo)
2227
require.Greater(t, len(logs), 1)
2328

24-
assert.Equal(t, "request header --> key2: value2", logs[len(logs)-1])
25-
assert.Equal(t, "request header --> key1: value1", logs[len(logs)-2])
29+
assert.Equal(t, fmt.Sprintf("%d finished", id), logs[len(logs)-1])
30+
assert.Equal(t, "request header --> key2: value2", logs[len(logs)-2])
31+
assert.Equal(t, "request header --> key1: value1", logs[len(logs)-3])
2632
}
2733

2834
func TestHttpHeaders_OnHttpResponseHeaders(t *testing.T) {
29-
host, done := proxytest.NewHttpFilterHost(newContext)
30-
defer done()
31-
id := host.InitContext()
35+
opt := proxytest.NewEmulatorOption().
36+
WithNewHttpContext(newContext)
37+
host := proxytest.NewHostEmulator(opt)
38+
defer host.Done()
39+
id := host.HttpFilterInitContext()
3240

3341
hs := [][2]string{{"key1", "value1"}, {"key2", "value2"}}
34-
host.PutResponseHeaders(id, hs) // call OnHttpResponseHeaders
42+
host.HttpFilterPutResponseHeaders(id, hs) // call OnHttpRequestHeaders
43+
host.HttpFilterCompleteHttpStream(id) // call OnHttpStreamDone
3544

3645
logs := host.GetLogs(types.LogLevelInfo)
3746
require.Greater(t, len(logs), 1)
3847

39-
assert.Equal(t, "response header <-- key2: value2", logs[len(logs)-1])
40-
assert.Equal(t, "response header <-- key1: value1", logs[len(logs)-2])
48+
assert.Equal(t, fmt.Sprintf("%d finished", id), logs[len(logs)-1])
49+
assert.Equal(t, "response header <-- key2: value2", logs[len(logs)-2])
50+
assert.Equal(t, "response header <-- key1: value1", logs[len(logs)-3])
4151
}

0 commit comments

Comments
 (0)