Skip to content

Commit

Permalink
test: add ut for pkg/wasm (#1013)
Browse files Browse the repository at this point in the history
* test: add ut for wasm/dispatch.go

* test: add ut for wasm/factory.go

* test: add ut for wasm/imports.go

* test: add ut for wasm/filter.go

* test: add ut for wasm/factory.go:OnPluginStart

* test: add ut for wasm/filter.go:OnReceive

* chore: add license header for pkg/wasm/dispatch_test.go

* chore: fix ci problem

* Update endpoint.go

* Update factory_test.go

* Update endpoint.go

* Update endpoint.go

* fmt: reformat some file to fix golangci-lint problem

* fmt: reformat some file to fix golangci-lint problem

* fmt: reformat some file to fix golangci-lint problem

* fmt: reformat some file to fix golangci-lint problem

---------

Co-authored-by: ChloroplastYu <[email protected]>
Co-authored-by: 梓淞 <[email protected]>
Co-authored-by: wenxuwan <[email protected]>
  • Loading branch information
4 people authored Oct 31, 2023
1 parent 8f73577 commit fe66f40
Show file tree
Hide file tree
Showing 13 changed files with 2,260 additions and 67 deletions.
443 changes: 443 additions & 0 deletions pkg/mock/wasm/exports.go

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions pkg/wasm/abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func abiImplFactory(instance types.WasmInstance) types.ABI {
return abi
}

// easy for extension
// AbiV2Impl easy for extension
type AbiV2Impl struct {
v1.ABIContext
}
Expand All @@ -45,17 +45,17 @@ var (
_ Exports = &AbiV2Impl{}
)

// Get abi name
// Name Get abi name
func (a *AbiV2Impl) Name() string {
return AbiV2
}

// Get abi
// GetABIExports Get abi
func (a *AbiV2Impl) GetABIExports() interface{} {
return a
}

// Get id
// ProxyGetID Get id
func (a *AbiV2Impl) ProxyGetID() (string, error) {
// store the funcName and common.WasmFunction, then return the common.WasmFunction
ff, err := a.Instance.GetExportsFunc("proxy_get_id")
Expand Down
3 changes: 2 additions & 1 deletion pkg/wasm/dispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ func (route *Router) RegisterRoute(id string, plugin *WasmPlugin) {
}
}

// RemoveRoute remove group by id
func (route *Router) RemoveRoute(id string) {
delete(route.routes, id)
}

// Get random plugin with rand id
// GetRandomPluginByID Get random plugin with rand id
func (route *Router) GetRandomPluginByID(id string) (*WasmPlugin, error) {
group, ok := route.routes[id]
if !ok {
Expand Down
218 changes: 218 additions & 0 deletions pkg/wasm/dispatch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
/*
* Copyright 2021 Layotto Authors
*
* 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 wasm

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

var (
wasmPluginName1 = "test1"
wasmPluginName2 = "test2"

idValid = "wasm_test1"
idInvalid = "wasm_test2"
)

func mockRouters() map[string]*Group {
wasmPlugin := &WasmPlugin{
pluginName: wasmPluginName1,
}
group := &Group{
count: 1,
plugins: []*WasmPlugin{wasmPlugin},
}

return map[string]*Group{
idValid: group,
}
}

func TestRouter_GetRandomPluginByID(t *testing.T) {
type fields struct {
routes map[string]*Group
}
type args struct {
id string
}
tests := []struct {
name string
fields fields
args args
want *WasmPlugin
wantErr assert.ErrorAssertionFunc
}{
{
name: "normal",
fields: fields{
routes: mockRouters(),
},
args: args{
id: idValid,
},
want: &WasmPlugin{
pluginName: wasmPluginName1,
},
wantErr: assert.NoError,
},
{
name: "not found",
fields: fields{
routes: mockRouters(),
},
args: args{
id: idInvalid,
},
want: nil,
wantErr: assert.Error,
},
{
name: "empty",
fields: fields{
routes: make(map[string]*Group),
},
args: args{
id: idInvalid,
},
want: nil,
wantErr: assert.Error,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
route := &Router{
routes: tt.fields.routes,
}
got, err := route.GetRandomPluginByID(tt.args.id)
if !tt.wantErr(t, err, fmt.Sprintf("GetRandomPluginByID(%v)", tt.args.id)) {
return
}
assert.Equalf(t, tt.want, got, "GetRandomPluginByID(%v)", tt.args.id)
})
}
}

func TestRouter_RegisterRoute(t *testing.T) {
type fields struct {
routes map[string]*Group
}
type args struct {
id string
plugin *WasmPlugin
}
tests := []struct {
name string
fields fields
args args
groupCnt int
}{
{
name: "add",
fields: fields{
routes: mockRouters(),
},
args: args{
id: idValid,
plugin: &WasmPlugin{
pluginName: wasmPluginName2,
},
},
groupCnt: 2,
},
{
name: "replace",
fields: fields{
routes: mockRouters(),
},
args: args{
id: idValid,
plugin: &WasmPlugin{
pluginName: wasmPluginName1,
},
},
groupCnt: 1,
},
{
name: "empty",
fields: fields{
routes: make(map[string]*Group),
},
args: args{
id: idValid,
plugin: &WasmPlugin{
pluginName: wasmPluginName1,
},
},
groupCnt: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
route := &Router{
routes: tt.fields.routes,
}
route.RegisterRoute(tt.args.id, tt.args.plugin)
_, err := route.GetRandomPluginByID(tt.args.id)
assert.NoError(t, err)
assert.Equal(t, tt.groupCnt, route.routes[tt.args.id].count)
})
}
}

func TestRouter_RemoveRoute(t *testing.T) {
type fields struct {
routes map[string]*Group
}
type args struct {
id string
}
tests := []struct {
name string
fields fields
args args
}{
{
name: "normal",
fields: fields{
routes: mockRouters(),
},
args: args{
id: idValid,
},
},
{
name: "not exist",
fields: fields{
routes: mockRouters(),
},
args: args{
id: idInvalid,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
route := &Router{
routes: tt.fields.routes,
}
route.RemoveRoute(tt.args.id)
})
}
}
27 changes: 14 additions & 13 deletions pkg/wasm/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ func createProxyWasmFilterFactory(confs map[string]interface{}) (api.StreamFilte
log.DefaultLogger.Errorf("[proxywasm][factory] createProxyWasmFilterFactory config not a map, configID: %s", configID)
return nil, errors.New("config not a map")
}
err := factory.Install(conf)
manager := wasm.GetWasmManager()
err := factory.Install(conf, manager)
if err != nil {
log.DefaultLogger.Errorf("[proxywasm][factory] createProxyWasmFilterFactory install error: %v", err)
return nil, err
Expand Down Expand Up @@ -98,7 +99,7 @@ func (f *FilterConfigFactory) IsRegister(id string) bool {
return err == nil && plugin != nil
}

func (f *FilterConfigFactory) Install(conf map[string]interface{}) error {
func (f *FilterConfigFactory) Install(conf map[string]interface{}, manager types.WasmManager) error {
config, err := parseFilterConfigItem(conf)
if err != nil {
return err
Expand All @@ -111,7 +112,7 @@ func (f *FilterConfigFactory) Install(conf map[string]interface{}) error {
VmConfig: config.VmConfig,
InstanceNum: config.InstanceNum,
}
err = wasm.GetWasmManager().AddOrUpdateWasm(v2Config)
err = manager.AddOrUpdateWasm(v2Config)
if err != nil {
config.PluginName = pluginName
addWatchFile(config, f)
Expand All @@ -122,7 +123,7 @@ func (f *FilterConfigFactory) Install(conf map[string]interface{}) error {
pluginName = config.FromWasmPlugin
}
config.PluginName = pluginName
pw := wasm.GetWasmManager().GetWasmPluginWrapperByName(pluginName)
pw := manager.GetWasmPluginWrapperByName(pluginName)
if pw == nil {
return errors.New("plugin not found")
}
Expand All @@ -141,7 +142,7 @@ func (f *FilterConfigFactory) Install(conf map[string]interface{}) error {
return nil
}

func (f *FilterConfigFactory) UpdateInstanceNum(id string, instanceNum int) error {
func (f *FilterConfigFactory) UpdateInstanceNum(id string, instanceNum int, manager types.WasmManager) error {
wasmPlugin, _ := f.router.GetRandomPluginByID(id)
if wasmPlugin == nil {
log.DefaultLogger.Errorf("[proxywasm][factory] GetRandomPluginByID id not registered, id: %s", id)
Expand Down Expand Up @@ -169,11 +170,11 @@ func (f *FilterConfigFactory) UpdateInstanceNum(id string, instanceNum int) erro
VmConfig: config.VmConfig,
InstanceNum: config.InstanceNum,
}
err := wasm.GetWasmManager().AddOrUpdateWasm(v2Config)
err := manager.AddOrUpdateWasm(v2Config)
if err != nil {
return err
}
pw := wasm.GetWasmManager().GetWasmPluginWrapperByName(config.PluginName)
pw := manager.GetWasmPluginWrapperByName(config.PluginName)
if pw == nil {
return errors.New("plugin not found")
}
Expand All @@ -187,13 +188,13 @@ func (f *FilterConfigFactory) UpdateInstanceNum(id string, instanceNum int) erro
return nil
}

func (f *FilterConfigFactory) UnInstall(id string) error {
func (f *FilterConfigFactory) UnInstall(id string, manager types.WasmManager) error {
wasmPlugin, _ := f.router.GetRandomPluginByID(id)
if wasmPlugin == nil {
log.DefaultLogger.Errorf("[proxywasm][factory] GetRandomPluginByID id not registered, id: %s", id)
return errors.New(id + " is not registered")
}
err := wasm.GetWasmManager().UninstallWasmPluginByName(wasmPlugin.pluginName)
err := manager.UninstallWasmPluginByName(wasmPlugin.pluginName)
if err != nil {
return err
}
Expand All @@ -214,7 +215,7 @@ func (f *FilterConfigFactory) UnInstall(id string) error {
return nil
}

// Get RootContext's ID
// GetRootContextID Get RootContext's ID
func (f *FilterConfigFactory) GetRootContextID() int32 {
return f.RootContextID
}
Expand All @@ -223,7 +224,7 @@ func (f *FilterConfigFactory) GetRootContextID() int32 {
// for `pw.RegisterPluginHandler(factory)`
var _ types.WasmPluginHandler = &FilterConfigFactory{}

// update config of FilterConfigFactory
// OnConfigUpdate Update config of FilterConfigFactory
func (f *FilterConfigFactory) OnConfigUpdate(config v2.WasmPluginConfig) {
for _, plugin := range f.config {
if plugin.PluginName == config.PluginName {
Expand All @@ -233,7 +234,7 @@ func (f *FilterConfigFactory) OnConfigUpdate(config v2.WasmPluginConfig) {
}
}

// Execute the plugin of FilterConfigFactory
// OnPluginStart Execute the plugin of FilterConfigFactory
func (f *FilterConfigFactory) OnPluginStart(plugin types.WasmPlugin) {
plugin.Exec(func(instance types.WasmInstance) bool {
wasmPlugin, ok := f.plugins[plugin.PluginName()]
Expand Down Expand Up @@ -292,5 +293,5 @@ func (f *FilterConfigFactory) OnPluginStart(plugin types.WasmPlugin) {
})
}

// Destroy the plugin of FilterConfigFactory
// OnPluginDestroy Destroy the plugin of FilterConfigFactory
func (f *FilterConfigFactory) OnPluginDestroy(types.WasmPlugin) {}
Loading

0 comments on commit fe66f40

Please sign in to comment.