mirror of
https://github.com/alibaba/higress.git
synced 2026-05-09 05:17:27 +08:00
feat(wasm-go): add wasm go plugin unit test and ci workflow (#2809)
This commit is contained in:
@@ -5,15 +5,20 @@ go 1.24.1
|
||||
toolchain go1.24.4
|
||||
|
||||
require (
|
||||
github.com/higress-group/proxy-wasm-go-sdk v0.0.0-20250611100342-5654e89a7a80
|
||||
github.com/higress-group/wasm-go v1.0.0
|
||||
github.com/higress-group/proxy-wasm-go-sdk v0.0.0-20250822030947-8345453fddd0
|
||||
github.com/higress-group/wasm-go v1.0.2-0.20250821081215-b573359becf8
|
||||
github.com/stretchr/testify v1.9.0
|
||||
github.com/tidwall/gjson v1.18.0
|
||||
github.com/tidwall/sjson v1.2.5
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/tetratelabs/wazero v1.7.2 // indirect
|
||||
github.com/tidwall/match v1.1.1 // indirect
|
||||
github.com/tidwall/pretty v1.2.1 // indirect
|
||||
github.com/tidwall/resp v0.1.1 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
@@ -2,14 +2,16 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/higress-group/proxy-wasm-go-sdk v0.0.0-20250611100342-5654e89a7a80 h1:xqmtTZI0JQ2O+Lg9/CE6c+Tw9KD6FnvWw8EpLVuuvfg=
|
||||
github.com/higress-group/proxy-wasm-go-sdk v0.0.0-20250611100342-5654e89a7a80/go.mod h1:tRI2LfMudSkKHhyv1uex3BWzcice2s/l8Ah8axporfA=
|
||||
github.com/higress-group/wasm-go v1.0.0 h1:4Ik5n3FsJ5+r13KLQl2ky+8NuAE8dfWQwoKxXYD2KAw=
|
||||
github.com/higress-group/wasm-go v1.0.0/go.mod h1:ODBV27sjmhIW8Cqv3R74EUcTnbdkE69bmXBQFuRkY1M=
|
||||
github.com/higress-group/proxy-wasm-go-sdk v0.0.0-20250822030947-8345453fddd0 h1:YGdj8KBzVjabU3STUfwMZghB+VlX6YLfJtLbrsWaOD0=
|
||||
github.com/higress-group/proxy-wasm-go-sdk v0.0.0-20250822030947-8345453fddd0/go.mod h1:tRI2LfMudSkKHhyv1uex3BWzcice2s/l8Ah8axporfA=
|
||||
github.com/higress-group/wasm-go v1.0.2-0.20250821081215-b573359becf8 h1:rs+AH1wfZy4swzuAyiRXT7xPUm8gycXt9Gwy0tqOq0o=
|
||||
github.com/higress-group/wasm-go v1.0.2-0.20250821081215-b573359becf8/go.mod h1:9k7L730huS/q4V5iH9WLDgf5ZUHEtfhM/uXcegKDG/M=
|
||||
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/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/tetratelabs/wazero v1.7.2 h1:1+z5nXJNwMLPAWaTePFi49SSTL0IMx/i3Fg8Yc25GDc=
|
||||
github.com/tetratelabs/wazero v1.7.2/go.mod h1:ytl6Zuh20R/eROuyDaGPkp82O9C/DJfXAwJfQ3X6/7Y=
|
||||
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||
github.com/tidwall/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY=
|
||||
github.com/tidwall/gjson v1.18.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||
@@ -22,5 +24,7 @@ github.com/tidwall/resp v0.1.1 h1:Ly20wkhqKTmDUPlyM1S7pWo5kk0tDu8OoC/vFArXmwE=
|
||||
github.com/tidwall/resp v0.1.1/go.mod h1:3/FrruOBAxPTPtundW0VXgmsQ4ZBA0Aw714lVYgwFa0=
|
||||
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
|
||||
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
|
||||
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.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
435
plugins/wasm-go/extensions/api-workflow/main_test.go
Normal file
435
plugins/wasm-go/extensions/api-workflow/main_test.go
Normal file
@@ -0,0 +1,435 @@
|
||||
// Copyright (c) 2024 Alibaba Group Holding Ltd.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||
"github.com/higress-group/wasm-go/pkg/test"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// 测试配置:基本工作流配置
|
||||
var basicWorkflowConfig = func() json.RawMessage {
|
||||
data, _ := json.Marshal(map[string]interface{}{
|
||||
"env": map[string]interface{}{
|
||||
"timeout": 5000,
|
||||
"max_depth": 100,
|
||||
},
|
||||
"workflow": map[string]interface{}{
|
||||
"edges": []map[string]interface{}{
|
||||
{
|
||||
"source": "start",
|
||||
"target": "A",
|
||||
},
|
||||
{
|
||||
"source": "A",
|
||||
"target": "end",
|
||||
},
|
||||
},
|
||||
"nodes": []map[string]interface{}{
|
||||
{
|
||||
"name": "A",
|
||||
"service_name": "test-service.static",
|
||||
"service_port": 80,
|
||||
"service_path": "/api/test",
|
||||
"service_method": "POST",
|
||||
"service_body_tmpl": map[string]interface{}{
|
||||
"message": "hello",
|
||||
"data": "",
|
||||
},
|
||||
"service_body_replace_keys": []map[string]interface{}{
|
||||
{
|
||||
"from": "start||message",
|
||||
"to": "data",
|
||||
},
|
||||
},
|
||||
"service_headers": []map[string]interface{}{
|
||||
{
|
||||
"key": "Content-Type",
|
||||
"value": "application/json",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
return data
|
||||
}()
|
||||
|
||||
// 测试配置:条件分支工作流配置
|
||||
var conditionalWorkflowConfig = func() json.RawMessage {
|
||||
data, _ := json.Marshal(map[string]interface{}{
|
||||
"env": map[string]interface{}{
|
||||
"timeout": 3000,
|
||||
"max_depth": 50,
|
||||
},
|
||||
"workflow": map[string]interface{}{
|
||||
"edges": []map[string]interface{}{
|
||||
{
|
||||
"source": "start",
|
||||
"target": "A",
|
||||
},
|
||||
{
|
||||
"source": "A",
|
||||
"target": "end",
|
||||
"conditional": "gt {{A||score}} 0.5",
|
||||
},
|
||||
{
|
||||
"source": "A",
|
||||
"target": "B",
|
||||
"conditional": "lt {{A||score}} 0.5",
|
||||
},
|
||||
{
|
||||
"source": "B",
|
||||
"target": "end",
|
||||
},
|
||||
},
|
||||
"nodes": []map[string]interface{}{
|
||||
{
|
||||
"name": "A",
|
||||
"service_name": "service-a.static",
|
||||
"service_port": 80,
|
||||
"service_path": "/api/score",
|
||||
"service_method": "GET",
|
||||
},
|
||||
{
|
||||
"name": "B",
|
||||
"service_name": "service-b.static",
|
||||
"service_port": 80,
|
||||
"service_path": "/api/fallback",
|
||||
"service_method": "POST",
|
||||
"service_body_tmpl": map[string]interface{}{
|
||||
"fallback": "default",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
return data
|
||||
}()
|
||||
|
||||
// 测试配置:并行执行工作流配置
|
||||
var parallelWorkflowConfig = func() json.RawMessage {
|
||||
data, _ := json.Marshal(map[string]interface{}{
|
||||
"env": map[string]interface{}{
|
||||
"timeout": 5000,
|
||||
"max_depth": 100,
|
||||
},
|
||||
"workflow": map[string]interface{}{
|
||||
"edges": []map[string]interface{}{
|
||||
{
|
||||
"source": "start",
|
||||
"target": "A",
|
||||
},
|
||||
{
|
||||
"source": "start",
|
||||
"target": "B",
|
||||
},
|
||||
{
|
||||
"source": "start",
|
||||
"target": "C",
|
||||
},
|
||||
{
|
||||
"source": "A",
|
||||
"target": "D",
|
||||
},
|
||||
{
|
||||
"source": "B",
|
||||
"target": "D",
|
||||
},
|
||||
{
|
||||
"source": "C",
|
||||
"target": "D",
|
||||
},
|
||||
{
|
||||
"source": "D",
|
||||
"target": "end",
|
||||
},
|
||||
},
|
||||
"nodes": []map[string]interface{}{
|
||||
{
|
||||
"name": "A",
|
||||
"service_name": "service-a.static",
|
||||
"service_port": 80,
|
||||
"service_path": "/api/a",
|
||||
"service_method": "GET",
|
||||
},
|
||||
{
|
||||
"name": "B",
|
||||
"service_name": "service-b.static",
|
||||
"service_port": 80,
|
||||
"service_path": "/api/b",
|
||||
"service_method": "GET",
|
||||
},
|
||||
{
|
||||
"name": "C",
|
||||
"service_name": "service-c.static",
|
||||
"service_port": 80,
|
||||
"service_path": "/api/c",
|
||||
"service_method": "GET",
|
||||
},
|
||||
{
|
||||
"name": "D",
|
||||
"service_name": "service-d.static",
|
||||
"service_port": 80,
|
||||
"service_path": "/api/d",
|
||||
"service_method": "POST",
|
||||
"service_body_tmpl": map[string]interface{}{
|
||||
"a_result": "",
|
||||
"b_result": "",
|
||||
"c_result": "",
|
||||
},
|
||||
"service_body_replace_keys": []map[string]interface{}{
|
||||
{
|
||||
"from": "A||result",
|
||||
"to": "a_result",
|
||||
},
|
||||
{
|
||||
"from": "B||result",
|
||||
"to": "b_result",
|
||||
},
|
||||
{
|
||||
"from": "C||result",
|
||||
"to": "c_result",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
return data
|
||||
}()
|
||||
|
||||
// 测试配置:continue 工作流配置
|
||||
var continueWorkflowConfig = func() json.RawMessage {
|
||||
data, _ := json.Marshal(map[string]interface{}{
|
||||
"env": map[string]interface{}{
|
||||
"timeout": 5000,
|
||||
"max_depth": 100,
|
||||
},
|
||||
"workflow": map[string]interface{}{
|
||||
"edges": []map[string]interface{}{
|
||||
{
|
||||
"source": "start",
|
||||
"target": "A",
|
||||
},
|
||||
{
|
||||
"source": "A",
|
||||
"target": "continue",
|
||||
},
|
||||
},
|
||||
"nodes": []map[string]interface{}{
|
||||
{
|
||||
"name": "A",
|
||||
"service_name": "service-a.static",
|
||||
"service_port": 80,
|
||||
"service_path": "/api/process",
|
||||
"service_method": "POST",
|
||||
"service_body_tmpl": map[string]interface{}{
|
||||
"processed": true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
return data
|
||||
}()
|
||||
|
||||
func TestParseConfig(t *testing.T) {
|
||||
test.RunGoTest(t, func(t *testing.T) {
|
||||
// 测试基本工作流配置解析
|
||||
t.Run("basic workflow config", func(t *testing.T) {
|
||||
host, status := test.NewTestHost(basicWorkflowConfig)
|
||||
defer host.Reset()
|
||||
require.Equal(t, types.OnPluginStartStatusOK, status)
|
||||
|
||||
config, err := host.GetMatchConfig()
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, config)
|
||||
})
|
||||
|
||||
// 测试条件分支工作流配置解析
|
||||
t.Run("conditional workflow config", func(t *testing.T) {
|
||||
host, status := test.NewTestHost(conditionalWorkflowConfig)
|
||||
defer host.Reset()
|
||||
require.Equal(t, types.OnPluginStartStatusOK, status)
|
||||
|
||||
config, err := host.GetMatchConfig()
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, config)
|
||||
})
|
||||
|
||||
// 测试并行执行工作流配置解析
|
||||
t.Run("parallel workflow config", func(t *testing.T) {
|
||||
host, status := test.NewTestHost(parallelWorkflowConfig)
|
||||
defer host.Reset()
|
||||
require.Equal(t, types.OnPluginStartStatusOK, status)
|
||||
|
||||
config, err := host.GetMatchConfig()
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, config)
|
||||
})
|
||||
|
||||
// 测试 continue 工作流配置解析
|
||||
t.Run("continue workflow config", func(t *testing.T) {
|
||||
host, status := test.NewTestHost(continueWorkflowConfig)
|
||||
defer host.Reset()
|
||||
require.Equal(t, types.OnPluginStartStatusOK, status)
|
||||
|
||||
config, err := host.GetMatchConfig()
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, config)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func TestOnHttpRequestBody(t *testing.T) {
|
||||
test.RunTest(t, func(t *testing.T) {
|
||||
// 测试基本工作流执行
|
||||
t.Run("basic workflow execution", func(t *testing.T) {
|
||||
host, status := test.NewTestHost(basicWorkflowConfig)
|
||||
defer host.Reset()
|
||||
require.Equal(t, types.OnPluginStartStatusOK, status)
|
||||
|
||||
// 设置请求体
|
||||
requestBody := []byte(`{"message": "test message"}`)
|
||||
action := host.CallOnHttpRequestBody(requestBody)
|
||||
|
||||
// 应该返回 ActionPause,因为需要等待外部 HTTP 调用完成
|
||||
require.Equal(t, types.ActionPause, action)
|
||||
|
||||
// 模拟外部服务的 HTTP 调用响应
|
||||
// 模拟成功响应
|
||||
host.CallOnHttpCall([][2]string{
|
||||
{"Content-Type", "application/json"},
|
||||
{":status", "200"},
|
||||
}, []byte(`{"result": "success", "data": "processed"}`))
|
||||
|
||||
// 检查插件的响应状态
|
||||
localResponse := host.GetLocalResponse()
|
||||
require.NotNil(t, localResponse)
|
||||
// 如果插件发送了响应,验证响应内容
|
||||
require.Equal(t, uint32(200), localResponse.StatusCode)
|
||||
require.Contains(t, string(localResponse.Data), "success")
|
||||
|
||||
host.CompleteHttp()
|
||||
})
|
||||
|
||||
// 测试条件分支工作流执行
|
||||
t.Run("conditional workflow execution", func(t *testing.T) {
|
||||
host, status := test.NewTestHost(conditionalWorkflowConfig)
|
||||
defer host.Reset()
|
||||
require.Equal(t, types.OnPluginStartStatusOK, status)
|
||||
|
||||
// 设置请求体
|
||||
requestBody := []byte(`{"input": "test"}`)
|
||||
action := host.CallOnHttpRequestBody(requestBody)
|
||||
|
||||
// 应该返回 ActionPause,因为需要等待外部 HTTP 调用完成
|
||||
require.Equal(t, types.ActionPause, action)
|
||||
|
||||
// 模拟外部服务的 HTTP 调用响应
|
||||
// 模拟成功响应
|
||||
host.CallOnHttpCall([][2]string{
|
||||
{"Content-Type", "application/json"},
|
||||
{":status", "200"},
|
||||
}, []byte(`{"score": 0.8}`))
|
||||
|
||||
// 检查插件的响应状态
|
||||
localResponse := host.GetLocalResponse()
|
||||
require.NotNil(t, localResponse)
|
||||
// 如果插件发送了响应,验证响应内容
|
||||
require.Equal(t, uint32(200), localResponse.StatusCode)
|
||||
|
||||
host.CompleteHttp()
|
||||
})
|
||||
|
||||
// 测试并行执行工作流执行
|
||||
t.Run("parallel workflow execution", func(t *testing.T) {
|
||||
host, status := test.NewTestHost(parallelWorkflowConfig)
|
||||
defer host.Reset()
|
||||
require.Equal(t, types.OnPluginStartStatusOK, status)
|
||||
|
||||
// 设置请求体
|
||||
requestBody := []byte(`{"data": "test data"}`)
|
||||
action := host.CallOnHttpRequestBody(requestBody)
|
||||
|
||||
// 应该返回 ActionPause,因为需要等待外部 HTTP 调用完成
|
||||
require.Equal(t, types.ActionPause, action)
|
||||
|
||||
// 模拟外部服务的 HTTP 调用响应
|
||||
// 模拟 A 服务的响应
|
||||
host.CallOnHttpCall([][2]string{
|
||||
{"Content-Type", "application/json"},
|
||||
{":status", "200"},
|
||||
}, []byte(`{"result": "a_result"}`))
|
||||
|
||||
// 模拟 B 服务的响应
|
||||
host.CallOnHttpCall([][2]string{
|
||||
{"Content-Type", "application/json"},
|
||||
{":status", "200"},
|
||||
}, []byte(`{"result": "b_result"}`))
|
||||
|
||||
// 模拟 C 服务的响应
|
||||
host.CallOnHttpCall([][2]string{
|
||||
{"Content-Type", "application/json"},
|
||||
{":status", "200"},
|
||||
}, []byte(`{"result": "c_result"}`))
|
||||
|
||||
// 模拟 D 服务的响应(这是汇聚节点)
|
||||
host.CallOnHttpCall([][2]string{
|
||||
{"Content-Type", "application/json"},
|
||||
{":status", "200"},
|
||||
}, []byte(`{"final_result": "success"}`))
|
||||
|
||||
// 检查插件的响应状态
|
||||
localResponse := host.GetLocalResponse()
|
||||
require.NotNil(t, localResponse)
|
||||
// 如果插件发送了响应,验证响应内容
|
||||
require.Equal(t, uint32(200), localResponse.StatusCode)
|
||||
|
||||
host.CompleteHttp()
|
||||
})
|
||||
|
||||
// 测试 continue 工作流执行
|
||||
t.Run("continue workflow execution", func(t *testing.T) {
|
||||
host, status := test.NewTestHost(continueWorkflowConfig)
|
||||
defer host.Reset()
|
||||
require.Equal(t, types.OnPluginStartStatusOK, status)
|
||||
|
||||
// 设置请求体
|
||||
requestBody := []byte(`{"process": true}`)
|
||||
action := host.CallOnHttpRequestBody(requestBody)
|
||||
|
||||
// 应该返回 ActionPause,因为需要等待外部 HTTP 调用完成
|
||||
require.Equal(t, types.ActionPause, action)
|
||||
|
||||
// 模拟外部服务的 HTTP 调用响应
|
||||
// 模拟成功响应
|
||||
host.CallOnHttpCall([][2]string{
|
||||
{"Content-Type", "application/json"},
|
||||
{":status", "200"},
|
||||
}, []byte(`{"processed": true, "status": "success"}`))
|
||||
|
||||
// 检查插件的响应状态
|
||||
action = host.GetHttpStreamAction()
|
||||
require.Equal(t, types.ActionContinue, action)
|
||||
host.CompleteHttp()
|
||||
})
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user