mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-12 11:19:43 +08:00
refactor: plugin structure
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package shared
|
||||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -1,4 +1,4 @@
|
||||
package shared
|
||||
package common
|
||||
|
||||
import (
|
||||
"errors"
|
||||
90
plugin/common/go_plugin_test.go
Normal file
90
plugin/common/go_plugin_test.go
Normal file
@@ -0,0 +1,90 @@
|
||||
// +build linux freebsd darwin
|
||||
// go plugin doesn't support windows
|
||||
|
||||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func buildGoPlugin() {
|
||||
fmt.Println("[setup] build go plugin")
|
||||
// flag -race is necessary in order to be consistent with go test
|
||||
cmd := exec.Command("go", "build", "-buildmode=plugin", "-race",
|
||||
"-o=debugtalk.so", "../../examples/plugin/debugtalk.go")
|
||||
if err := cmd.Run(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func removeGoPlugin() {
|
||||
fmt.Println("[teardown] remove go plugin")
|
||||
os.Remove("debugtalk.so")
|
||||
}
|
||||
|
||||
func TestLocatePlugin(t *testing.T) {
|
||||
buildGoPlugin()
|
||||
defer removeGoPlugin()
|
||||
|
||||
_, err := locateFile("../", goPluginFile)
|
||||
if !assert.Error(t, err) {
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
_, err = locateFile("", goPluginFile)
|
||||
if !assert.Error(t, err) {
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
startPath := "debugtalk.so"
|
||||
_, err = locateFile(startPath, goPluginFile)
|
||||
if !assert.Nil(t, err) {
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
startPath = "call.go"
|
||||
_, err = locateFile(startPath, goPluginFile)
|
||||
if !assert.Nil(t, err) {
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
startPath = "."
|
||||
_, err = locateFile(startPath, goPluginFile)
|
||||
if !assert.Nil(t, err) {
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
startPath = "/abc"
|
||||
_, err = locateFile(startPath, goPluginFile)
|
||||
if !assert.Error(t, err) {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestCallPluginFunction(t *testing.T) {
|
||||
buildGoPlugin()
|
||||
defer removeGoPlugin()
|
||||
|
||||
plugin, err := Init("debugtalk.so")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !assert.True(t, plugin.Has("Concatenate")) {
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
// call function without arguments
|
||||
result, err := plugin.Call("Concatenate", "1", 2, "3.14")
|
||||
if !assert.NoError(t, err) {
|
||||
t.Fail()
|
||||
}
|
||||
if !assert.Equal(t, "123.14", result) {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
90
plugin/common/hashicorp_plugin_test.go
Normal file
90
plugin/common/hashicorp_plugin_test.go
Normal file
@@ -0,0 +1,90 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func buildHashicorpPlugin() {
|
||||
fmt.Println("[init] build hashicorp go plugin")
|
||||
cmd := exec.Command("go", "build",
|
||||
"-o=debugtalk.bin",
|
||||
"../../examples/plugin/hashicorp.go", "../../examples/plugin/debugtalk.go")
|
||||
if err := cmd.Run(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func removeHashicorpPlugin() {
|
||||
fmt.Println("[teardown] remove hashicorp plugin")
|
||||
os.Remove("debugtalk.bin")
|
||||
}
|
||||
|
||||
func TestInitHashicorpPlugin(t *testing.T) {
|
||||
buildHashicorpPlugin()
|
||||
defer removeHashicorpPlugin()
|
||||
|
||||
plugin, err := Init("debugtalk.bin")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer plugin.Quit()
|
||||
|
||||
if !assert.True(t, plugin.Has("sum_ints")) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !assert.True(t, plugin.Has("concatenate")) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
var v2 interface{}
|
||||
v2, err = plugin.Call("sum_ints", 1, 2, 3, 4)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !assert.Equal(t, 10, v2) {
|
||||
t.Fail()
|
||||
}
|
||||
v2, err = plugin.Call("sum_two_int", 1, 2)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !assert.Equal(t, 3, v2) {
|
||||
t.Fail()
|
||||
}
|
||||
v2, err = plugin.Call("sum", 1, 2, 3.4, 5)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !assert.Equal(t, 11.4, v2) {
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
var v3 interface{}
|
||||
v3, err = plugin.Call("sum_two_string", "a", "b")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !assert.Equal(t, "ab", v3) {
|
||||
t.Fail()
|
||||
}
|
||||
v3, err = plugin.Call("sum_strings", "a", "b", "c")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !assert.Equal(t, "abc", v3) {
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
v3, err = plugin.Call("concatenate", "a", 2, "c", 3.4)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !assert.Equal(t, "a2c3.4", v3) {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
221
plugin/common/init.go
Normal file
221
plugin/common/init.go
Normal file
@@ -0,0 +1,221 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"plugin"
|
||||
"reflect"
|
||||
"runtime"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/httprunner/hrp/internal/ga"
|
||||
pluginHost "github.com/httprunner/hrp/plugin/host"
|
||||
pluginShared "github.com/httprunner/hrp/plugin/shared"
|
||||
)
|
||||
|
||||
type pluginFile string
|
||||
|
||||
const (
|
||||
goPluginFile pluginFile = pluginShared.Name + ".so" // built from go plugin
|
||||
hashicorpGoPluginFile pluginFile = pluginShared.Name + ".bin" // built from hashicorp go plugin
|
||||
hashicorpPyPluginFile pluginFile = pluginShared.Name + ".py"
|
||||
)
|
||||
|
||||
type Plugin interface {
|
||||
Init(path string) error // init plugin
|
||||
Has(funcName string) bool // check if plugin has function
|
||||
Call(funcName string, args ...interface{}) (interface{}, error) // call function
|
||||
Quit() error // quit plugin
|
||||
}
|
||||
|
||||
// goPlugin implements golang official plugin
|
||||
type goPlugin struct {
|
||||
*plugin.Plugin
|
||||
cachedFunctions map[string]reflect.Value // cache loaded functions to improve performance
|
||||
}
|
||||
|
||||
func (p *goPlugin) Init(path string) error {
|
||||
if runtime.GOOS == "windows" {
|
||||
log.Warn().Msg("go plugin does not support windows")
|
||||
return fmt.Errorf("go plugin does not support windows")
|
||||
}
|
||||
|
||||
var err error
|
||||
// report event for loading go plugin
|
||||
defer func() {
|
||||
event := ga.EventTracking{
|
||||
Category: "LoadGoPlugin",
|
||||
Action: "plugin.Open",
|
||||
}
|
||||
if err != nil {
|
||||
event.Value = 1 // failed
|
||||
}
|
||||
go ga.SendEvent(event)
|
||||
}()
|
||||
|
||||
p.Plugin, err = plugin.Open(path)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Str("path", path).Msg("load go plugin failed")
|
||||
return err
|
||||
}
|
||||
|
||||
p.cachedFunctions = make(map[string]reflect.Value)
|
||||
log.Info().Str("path", path).Msg("load go plugin success")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *goPlugin) Has(funcName string) bool {
|
||||
fn, ok := p.cachedFunctions[funcName]
|
||||
if ok {
|
||||
return fn.IsValid()
|
||||
}
|
||||
|
||||
sym, err := p.Plugin.Lookup(funcName)
|
||||
if err != nil {
|
||||
p.cachedFunctions[funcName] = reflect.Value{} // mark as invalid
|
||||
return false
|
||||
}
|
||||
fn = reflect.ValueOf(sym)
|
||||
|
||||
// check function type
|
||||
if fn.Kind() != reflect.Func {
|
||||
p.cachedFunctions[funcName] = reflect.Value{} // mark as invalid
|
||||
return false
|
||||
}
|
||||
|
||||
p.cachedFunctions[funcName] = fn
|
||||
return true
|
||||
}
|
||||
|
||||
func (p *goPlugin) Call(funcName string, args ...interface{}) (interface{}, error) {
|
||||
if !p.Has(funcName) {
|
||||
return nil, fmt.Errorf("function %s not found", funcName)
|
||||
}
|
||||
fn := p.cachedFunctions[funcName]
|
||||
return CallFunc(fn, args...)
|
||||
}
|
||||
|
||||
func (p *goPlugin) Quit() error {
|
||||
// no need to quit for go plugin
|
||||
return nil
|
||||
}
|
||||
|
||||
// hashicorpPlugin implements hashicorp/go-plugin
|
||||
type hashicorpPlugin struct {
|
||||
pluginShared.FuncCaller
|
||||
cachedFunctions map[string]bool // cache loaded functions to improve performance
|
||||
}
|
||||
|
||||
func (p *hashicorpPlugin) Init(path string) error {
|
||||
|
||||
f, err := pluginHost.Init(path)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Str("path", path).Msg("load go hashicorp plugin failed")
|
||||
return err
|
||||
}
|
||||
p.FuncCaller = f
|
||||
|
||||
p.cachedFunctions = make(map[string]bool)
|
||||
log.Info().Str("path", path).Msg("load hashicorp go plugin success")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *hashicorpPlugin) Has(funcName string) bool {
|
||||
flag, ok := p.cachedFunctions[funcName]
|
||||
if ok {
|
||||
return flag
|
||||
}
|
||||
|
||||
funcNames, err := p.GetNames()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, name := range funcNames {
|
||||
if name == funcName {
|
||||
p.cachedFunctions[funcName] = true // cache as exists
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
p.cachedFunctions[funcName] = false // cache as not exists
|
||||
return false
|
||||
}
|
||||
|
||||
func (p *hashicorpPlugin) Call(funcName string, args ...interface{}) (interface{}, error) {
|
||||
return p.FuncCaller.Call(funcName, args...)
|
||||
}
|
||||
|
||||
func (p *hashicorpPlugin) Quit() error {
|
||||
// kill hashicorp plugin process
|
||||
pluginHost.Quit()
|
||||
return nil
|
||||
}
|
||||
|
||||
func Init(path string) (Plugin, error) {
|
||||
if path == "" {
|
||||
return nil, nil
|
||||
}
|
||||
var plugin Plugin
|
||||
|
||||
// priority: hashicorp plugin > go plugin > builtin functions
|
||||
// locate hashicorp plugin file
|
||||
pluginPath, err := locateFile(path, hashicorpGoPluginFile)
|
||||
if err == nil {
|
||||
// found hashicorp go plugin file
|
||||
plugin = &hashicorpPlugin{}
|
||||
err = plugin.Init(pluginPath)
|
||||
return plugin, err
|
||||
}
|
||||
|
||||
// locate go plugin file
|
||||
pluginPath, err = locateFile(path, goPluginFile)
|
||||
if err == nil {
|
||||
// found go plugin file
|
||||
plugin = &goPlugin{}
|
||||
err = plugin.Init(pluginPath)
|
||||
return plugin, err
|
||||
}
|
||||
|
||||
// plugin not found
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// locateFile searches destFile upward recursively until current
|
||||
// working directory or system root dir.
|
||||
func locateFile(startPath string, destFile pluginFile) (string, error) {
|
||||
stat, err := os.Stat(startPath)
|
||||
if os.IsNotExist(err) {
|
||||
return "", err
|
||||
}
|
||||
|
||||
var startDir string
|
||||
if stat.IsDir() {
|
||||
startDir = startPath
|
||||
} else {
|
||||
startDir = filepath.Dir(startPath)
|
||||
}
|
||||
startDir, _ = filepath.Abs(startDir)
|
||||
|
||||
// convention over configuration
|
||||
pluginPath := filepath.Join(startDir, string(destFile))
|
||||
if _, err := os.Stat(pluginPath); err == nil {
|
||||
return pluginPath, nil
|
||||
}
|
||||
|
||||
// current working directory
|
||||
cwd, _ := os.Getwd()
|
||||
if startDir == cwd {
|
||||
return "", fmt.Errorf("searched to CWD, plugin file not found")
|
||||
}
|
||||
|
||||
// system root dir
|
||||
parentDir, _ := filepath.Abs(filepath.Dir(startDir))
|
||||
if parentDir == startDir {
|
||||
return "", fmt.Errorf("searched to system root dir, plugin file not found")
|
||||
}
|
||||
|
||||
return locateFile(parentDir, destFile)
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
hclog "github.com/hashicorp/go-hclog"
|
||||
"github.com/hashicorp/go-plugin"
|
||||
|
||||
"github.com/httprunner/hrp/plugin/common"
|
||||
pluginShared "github.com/httprunner/hrp/plugin/shared"
|
||||
)
|
||||
|
||||
@@ -36,7 +37,7 @@ func (p *functionPlugin) Call(funcName string, args ...interface{}) (interface{}
|
||||
return nil, fmt.Errorf("function %s not found", funcName)
|
||||
}
|
||||
|
||||
return pluginShared.CallFunc(fn, args...)
|
||||
return common.CallFunc(fn, args...)
|
||||
}
|
||||
|
||||
var functions = make(functionsMap)
|
||||
|
||||
Reference in New Issue
Block a user