From 2da666a4e6319917faf94257f74b53e159008fc0 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Tue, 28 Dec 2021 21:14:15 +0800 Subject: [PATCH] feat: support creating and calling custom functions with go plugin --- convert.go | 1 + docs/CHANGELOG.md | 6 ++- examples/plugin/debugtalk.go | 14 +++++ models.go | 1 + parser.go | 100 +++++++++++++++++++++-------------- parser_test.go | 51 +++++++++++++----- response.go | 12 +++-- runner.go | 100 +++++++++++++++++++++++++++++++---- runner_test.go | 57 ++++++++++++++++++++ 9 files changed, 274 insertions(+), 68 deletions(-) create mode 100644 examples/plugin/debugtalk.go diff --git a/convert.go b/convert.go index 7f1bc830..2bfd6d08 100644 --- a/convert.go +++ b/convert.go @@ -146,6 +146,7 @@ func (path *TestCasePath) ToTestCase() (*TestCase, error) { if err != nil { return nil, err } + tc.Config.Path = path.Path testcase, err := tc.ToTestCase() if err != nil { return nil, err diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 26a81198..9488bbd4 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,6 +1,10 @@ # Release History -## v0.3.0 (2021-12-22) +## v0.4.0 (2021-12-28) + +- feat: support creating and calling custom functions with `go plugin` + +## v0.3.0 (2021-12-24) - feat: implement `transaction` mechanism for load test - feat: continue running next step when failure occurs with `--continue-on-failure` flag, default to failfast diff --git a/examples/plugin/debugtalk.go b/examples/plugin/debugtalk.go new file mode 100644 index 00000000..ce98e20c --- /dev/null +++ b/examples/plugin/debugtalk.go @@ -0,0 +1,14 @@ +package main + +import ( + "fmt" + "log" +) + +func init() { + log.Println("plugin init function called") +} + +func Concatenate(a int, b string, c float64) string { + return fmt.Sprintf("%v_%v_%v", a, b, c) +} diff --git a/models.go b/models.go index 34924faf..9ee1fd89 100644 --- a/models.go +++ b/models.go @@ -20,6 +20,7 @@ type TConfig struct { Parameters map[string]interface{} `json:"parameters,omitempty" yaml:"parameters,omitempty"` Export []string `json:"export,omitempty" yaml:"export,omitempty"` Weight int `json:"weight,omitempty" yaml:"weight,omitempty"` + Path string `json:"path,omitempty" yaml:"path,omitempty"` // testcase file path } // Request represents HTTP request data structure. diff --git a/parser.go b/parser.go index e7398cd7..b9041d0c 100644 --- a/parser.go +++ b/parser.go @@ -31,9 +31,9 @@ func buildURL(baseURL, stepURL string) string { return uStep.String() } -func parseHeaders(rawHeaders map[string]string, variablesMapping map[string]interface{}) (map[string]string, error) { +func parseHeaders(rawHeaders map[string]string, variablesMapping map[string]interface{}, pluginLoader *pluginLoader) (map[string]string, error) { parsedHeaders := make(map[string]string) - headers, err := parseData(rawHeaders, variablesMapping) + headers, err := parseData(rawHeaders, variablesMapping, pluginLoader) if err != nil { return rawHeaders, err } @@ -53,7 +53,7 @@ func convertString(raw interface{}) string { } } -func parseData(raw interface{}, variablesMapping map[string]interface{}) (interface{}, error) { +func parseData(raw interface{}, variablesMapping map[string]interface{}, pluginLoader *pluginLoader) (interface{}, error) { rawValue := reflect.ValueOf(raw) switch rawValue.Kind() { case reflect.String: @@ -64,11 +64,11 @@ func parseData(raw interface{}, variablesMapping map[string]interface{}) (interf // other string value := rawValue.String() value = strings.TrimSpace(value) - return parseString(value, variablesMapping) + return parseString(value, variablesMapping, pluginLoader) case reflect.Slice: parsedSlice := make([]interface{}, rawValue.Len()) for i := 0; i < rawValue.Len(); i++ { - parsedValue, err := parseData(rawValue.Index(i).Interface(), variablesMapping) + parsedValue, err := parseData(rawValue.Index(i).Interface(), variablesMapping, pluginLoader) if err != nil { return raw, err } @@ -78,12 +78,12 @@ func parseData(raw interface{}, variablesMapping map[string]interface{}) (interf case reflect.Map: // convert any map to map[string]interface{} parsedMap := make(map[string]interface{}) for _, k := range rawValue.MapKeys() { - parsedKey, err := parseString(k.String(), variablesMapping) + parsedKey, err := parseString(k.String(), variablesMapping, pluginLoader) if err != nil { return raw, err } v := rawValue.MapIndex(k) - parsedValue, err := parseData(v.Interface(), variablesMapping) + parsedValue, err := parseData(v.Interface(), variablesMapping, pluginLoader) if err != nil { return raw, err } @@ -121,7 +121,7 @@ var ( ) // parseString parse string with variables -func parseString(raw string, variablesMapping map[string]interface{}) (interface{}, error) { +func parseString(raw string, variablesMapping map[string]interface{}, pluginLoader *pluginLoader) (interface{}, error) { matchStartPosition := 0 parsedString := "" remainedString := raw @@ -160,16 +160,25 @@ func parseString(raw string, variablesMapping map[string]interface{}) (interface if err != nil { return raw, err } - parsedArgs, err := parseData(arguments, variablesMapping) + parsedArgs, err := parseData(arguments, variablesMapping, pluginLoader) if err != nil { return raw, err } - result, err := callFunc(funcName, parsedArgs.([]interface{})...) + fn, err := getMappingFunction(funcName, pluginLoader) if err != nil { return raw, err } + result, err := callFunc(fn, parsedArgs.([]interface{})...) + if err != nil { + log.Error().Str("funcName", funcName).Interface("arguments", arguments). + Err(err).Msg("call function failed") + return raw, err + } + log.Info().Str("funcName", funcName).Interface("arguments", arguments). + Interface("output", result).Msg("call function success") + if funcMatched[0] == raw { // raw_string is a function, e.g. "${add_one(3)}", return its eval value directly return result, nil @@ -247,30 +256,50 @@ func mergeVariables(variables, overriddenVariables map[string]interface{}) map[s return mergedVariables } +func getMappingFunction(funcName string, pluginLoader *pluginLoader) (reflect.Value, error) { + var fn reflect.Value + var err error + + defer func() { + // check function type + if err == nil && fn.Kind() != reflect.Func { + // function not valid + err = fmt.Errorf("function %s is invalid", funcName) + return + } + }() + + // get function from plugin loader + if pluginLoader != nil { + sym, err := pluginLoader.Lookup(funcName) + if err == nil { + fn = reflect.ValueOf(sym) + return fn, nil + } + } + + // get builtin function + if function, ok := builtin.Functions[funcName]; ok { + fn = reflect.ValueOf(function) + return fn, nil + } + + // function not found + return reflect.Value{}, fmt.Errorf("function %s is not found", funcName) +} + // callFunc call function with arguments // only support return at most one result value -func callFunc(funcName string, arguments ...interface{}) (interface{}, error) { - function, ok := builtin.Functions[funcName] - if !ok { - // function not found - return nil, fmt.Errorf("function %s is not found", funcName) - } - - funcValue := reflect.ValueOf(function) - if funcValue.Kind() != reflect.Func { - // function not valid - return nil, fmt.Errorf("function %s is invalid", funcName) - } - - if funcValue.Type().NumIn() != len(arguments) { +func callFunc(fn reflect.Value, arguments ...interface{}) (interface{}, error) { + if fn.Type().NumIn() != len(arguments) { // function arguments not match - return nil, fmt.Errorf("function %s arguments number not match", funcName) + return nil, fmt.Errorf("function arguments number not match") } argumentsValue := make([]reflect.Value, len(arguments)) for index, argument := range arguments { argumentValue := reflect.ValueOf(argument) - expectArgumentType := funcValue.Type().In(index) + expectArgumentType := fn.Type().In(index) actualArgumentType := reflect.TypeOf(argument) // type match @@ -282,20 +311,18 @@ func callFunc(funcName string, arguments ...interface{}) (interface{}, error) { // type not match, check if convertible if !actualArgumentType.ConvertibleTo(expectArgumentType) { // function argument type not match and not convertible - err := fmt.Errorf("function %s argument %d type is neither match nor convertible, expect %v, actual %v", - funcName, index, expectArgumentType, actualArgumentType) - log.Error().Err(err).Msg("call function failed") + err := fmt.Errorf("function argument %d's type is neither match nor convertible, expect %v, actual %v", + index, expectArgumentType, actualArgumentType) return nil, err } // convert argument to expect type argumentsValue[index] = argumentValue.Convert(expectArgumentType) } - resultValues := funcValue.Call(argumentsValue) + resultValues := fn.Call(argumentsValue) if len(resultValues) > 1 { // function should return at most one value - err := fmt.Errorf("function %s should return at most one value", funcName) - log.Error().Err(err).Msg("call function failed") + err := fmt.Errorf("function should return at most one value") return nil, err } @@ -307,11 +334,6 @@ func callFunc(funcName string, arguments ...interface{}) (interface{}, error) { // return one value // convert reflect.Value to interface{} result := resultValues[0].Interface() - log.Info(). - Str("funcName", funcName). - Interface("arguments", arguments). - Interface("output", result). - Msg("call function success") return result, nil } @@ -361,7 +383,7 @@ func parseFunctionArguments(argsStr string) ([]interface{}, error) { return arguments, nil } -func parseVariables(variables map[string]interface{}) (map[string]interface{}, error) { +func parseVariables(variables map[string]interface{}, pluginLoader *pluginLoader) (map[string]interface{}, error) { parsedVariables := make(map[string]interface{}) var traverseRounds int @@ -399,7 +421,7 @@ func parseVariables(variables map[string]interface{}) (map[string]interface{}, e return variables, fmt.Errorf("variable not defined: %v", undefinedVars) } - parsedValue, err := parseData(varValue, parsedVariables) + parsedValue, err := parseData(varValue, parsedVariables, pluginLoader) if err != nil { continue } diff --git a/parser_test.go b/parser_test.go index 33f3ae76..4147bf5f 100644 --- a/parser_test.go +++ b/parser_test.go @@ -1,6 +1,7 @@ package hrp import ( + "plugin" "sort" "testing" "time" @@ -161,7 +162,7 @@ func TestParseDataStringWithVariables(t *testing.T) { } for _, data := range testData { - parsedData, err := parseData(data.expr, variablesMapping) + parsedData, err := parseData(data.expr, variablesMapping, nil) if !assert.NoError(t, err) { t.Fail() } @@ -185,7 +186,7 @@ func TestParseDataStringWithUndefinedVariables(t *testing.T) { } for _, data := range testData { - parsedData, err := parseData(data.expr, variablesMapping) + parsedData, err := parseData(data.expr, variablesMapping, nil) if !assert.Error(t, err) { t.Fail() } @@ -229,7 +230,7 @@ func TestParseDataStringWithVariablesAbnormal(t *testing.T) { } for _, data := range testData { - parsedData, err := parseData(data.expr, variablesMapping) + parsedData, err := parseData(data.expr, variablesMapping, nil) if !assert.NoError(t, err) { t.Fail() } @@ -259,7 +260,7 @@ func TestParseDataMapWithVariables(t *testing.T) { } for _, data := range testData { - parsedData, err := parseData(data.expr, variablesMapping) + parsedData, err := parseData(data.expr, variablesMapping, nil) if !assert.NoError(t, err) { t.Fail() } @@ -292,7 +293,7 @@ func TestParseHeaders(t *testing.T) { } for _, data := range testData { - parsedHeaders, err := parseHeaders(data.rawHeaders, variablesMapping) + parsedHeaders, err := parseHeaders(data.rawHeaders, variablesMapping, nil) if !assert.NoError(t, err) { t.Fail() } @@ -328,16 +329,18 @@ func TestMergeVariables(t *testing.T) { } } -func TestCallFunction(t *testing.T) { +func TestCallBuiltinFunction(t *testing.T) { // call function without arguments - _, err := callFunc("get_timestamp") + f1, _ := getMappingFunction("get_timestamp", nil) + _, err := callFunc(f1) if !assert.NoError(t, err) { t.Fail() } // call function with one argument timeStart := time.Now() - _, err = callFunc("sleep", 1) + f2, _ := getMappingFunction("sleep", nil) + _, err = callFunc(f2, 1) if !assert.NoError(t, err) { t.Fail() } @@ -346,7 +349,8 @@ func TestCallFunction(t *testing.T) { } // call function with one argument - result, err := callFunc("gen_random_string", 10) + f3, _ := getMappingFunction("gen_random_string", nil) + result, err := callFunc(f3, 10) if !assert.NoError(t, err) { t.Fail() } @@ -355,7 +359,8 @@ func TestCallFunction(t *testing.T) { } // call function with two argument - result, err = callFunc("max", float64(10), 9.99) + f4, _ := getMappingFunction("max", nil) + result, err = callFunc(f4, float64(10), 9.99) if !assert.NoError(t, err) { t.Fail() } @@ -364,6 +369,24 @@ func TestCallFunction(t *testing.T) { } } +func TestCallPluginFunction(t *testing.T) { + plugins, err := plugin.Open("examples/debugtalk.so") + if err != nil { + t.Fatalf(err.Error()) + } + pluginLoader := &pluginLoader{plugins} + + // call function without arguments + f1, _ := getMappingFunction("Concatenate", pluginLoader) + result, err := callFunc(f1, 1, "2", 3.14) + if !assert.NoError(t, err) { + t.Fail() + } + if !assert.Equal(t, result, "1_2_3.14") { + t.Fail() + } +} + func TestLiteralEval(t *testing.T) { testData := []struct { expr string @@ -441,7 +464,7 @@ func TestParseDataStringWithFunctions(t *testing.T) { } for _, data := range testData1 { - value, err := parseData(data.expr, variablesMapping) + value, err := parseData(data.expr, variablesMapping, nil) if !assert.NoError(t, err) { t.Fail() } @@ -460,7 +483,7 @@ func TestParseDataStringWithFunctions(t *testing.T) { } for _, data := range testData2 { - value, err := parseData(data.expr, variablesMapping) + value, err := parseData(data.expr, variablesMapping, nil) if !assert.NoError(t, err) { t.Fail() } @@ -507,7 +530,7 @@ func TestParseVariables(t *testing.T) { } for _, data := range testData { - value, err := parseVariables(data.rawVars) + value, err := parseVariables(data.rawVars, nil) if !assert.NoError(t, err) { t.Fail() } @@ -537,7 +560,7 @@ func TestParseVariablesAbnormal(t *testing.T) { } for _, data := range testData { - value, err := parseVariables(data.rawVars) + value, err := parseVariables(data.rawVars, nil) if !assert.Error(t, err) { t.Fail() } diff --git a/response.go b/response.go index f81de3f1..ce036164 100644 --- a/response.go +++ b/response.go @@ -13,7 +13,7 @@ import ( "github.com/httprunner/hrp/internal/builtin" ) -func newResponseObject(t *testing.T, resp *http.Response) (*responseObject, error) { +func newResponseObject(t *testing.T, pluginLoader *pluginLoader, resp *http.Response) (*responseObject, error) { // prepare response headers headers := make(map[string]string) for k, v := range resp.Header { @@ -60,8 +60,9 @@ func newResponseObject(t *testing.T, resp *http.Response) (*responseObject, erro } return &responseObject{ - t: t, - respObjMeta: data, + t: t, + pluginLoader: pluginLoader, + respObjMeta: data, }, nil } @@ -74,6 +75,7 @@ type respObjMeta struct { type responseObject struct { t *testing.T + pluginLoader *pluginLoader respObjMeta interface{} validationResults map[string]interface{} } @@ -101,7 +103,7 @@ func (v *responseObject) Validate(validators []Validator, variablesMapping map[s var checkValue interface{} if strings.Contains(checkItem, "$") { // reference variable - checkValue, err = parseData(checkItem, variablesMapping) + checkValue, err = parseData(checkItem, variablesMapping, v.pluginLoader) if err != nil { return err } @@ -114,7 +116,7 @@ func (v *responseObject) Validate(validators []Validator, variablesMapping map[s assertFunc := builtin.Assertions[assertMethod] // parse expected value - expectValue, err := parseData(validator.Expect, variablesMapping) + expectValue, err := parseData(validator.Expect, variablesMapping, v.pluginLoader) if err != nil { return err } diff --git a/runner.go b/runner.go index bca1900b..1b804911 100644 --- a/runner.go +++ b/runner.go @@ -9,6 +9,9 @@ import ( "net/http" "net/http/httputil" "net/url" + "os" + "path/filepath" + "plugin" "strconv" "strings" "testing" @@ -125,6 +128,8 @@ type caseRunner struct { // key is transaction name, value is map of transaction type and time, e.g. start time and end time. transactions map[string]map[transactionType]time.Time startTime time.Time // record start time of the testcase + // pluginLoader stores loaded go plugins. + pluginLoader *pluginLoader } // reset clears runner session variables. @@ -194,7 +199,7 @@ func (r *caseRunner) runStep(index int) (stepResult *stepData, err error) { stepVariables = mergeVariables(stepVariables, copiedConfig.Variables) // parse step variables - parsedVariables, err := parseVariables(stepVariables) + parsedVariables, err := parseVariables(stepVariables, r.pluginLoader) if err != nil { log.Error().Interface("variables", copiedConfig.Variables).Err(err).Msg("parse step variables failed") return nil, err @@ -311,7 +316,7 @@ func (r *caseRunner) runStepRequest(step *TStep) (stepResult *stepData, err erro // prepare request headers if len(step.Request.Headers) > 0 { - headers, err := parseHeaders(step.Request.Headers, step.Variables) + headers, err := parseHeaders(step.Request.Headers, step.Variables, r.pluginLoader) if err != nil { return nil, errors.Wrap(err, "parse headers failed") } @@ -328,7 +333,7 @@ func (r *caseRunner) runStepRequest(step *TStep) (stepResult *stepData, err erro // prepare request params var queryParams url.Values if len(step.Request.Params) > 0 { - params, err := parseData(step.Request.Params, step.Variables) + params, err := parseData(step.Request.Params, step.Variables, r.pluginLoader) if err != nil { return nil, errors.Wrap(err, "parse data failed") } @@ -360,7 +365,7 @@ func (r *caseRunner) runStepRequest(step *TStep) (stepResult *stepData, err erro // prepare request body if step.Request.Body != nil { - data, err := parseData(step.Request.Body, step.Variables) + data, err := parseData(step.Request.Body, step.Variables, r.pluginLoader) if err != nil { return nil, err } @@ -434,7 +439,7 @@ func (r *caseRunner) runStepRequest(step *TStep) (stepResult *stepData, err erro } // new response object - respObj, err := newResponseObject(r.hrpRunner.t, resp) + respObj, err := newResponseObject(r.hrpRunner.t, r.pluginLoader, resp) if err != nil { err = errors.Wrap(err, "init ResponseObject error") return @@ -479,22 +484,28 @@ func (r *caseRunner) runStepTestCase(step *TStep) (stepResult *stepData, err err func (r *caseRunner) parseConfig(config IConfig) error { cfg := config.ToStruct() // parse config variables - parsedVariables, err := parseVariables(cfg.Variables) + parsedVariables, err := parseVariables(cfg.Variables, r.pluginLoader) if err != nil { log.Error().Interface("variables", cfg.Variables).Err(err).Msg("parse config variables failed") return err } cfg.Variables = parsedVariables + // load plugin variables and functions + err = r.loadPlugin(cfg.Path) + if err != nil { + return err + } + // parse config name - parsedName, err := parseString(cfg.Name, cfg.Variables) + parsedName, err := parseString(cfg.Name, cfg.Variables, r.pluginLoader) if err != nil { return err } cfg.Name = convertString(parsedName) // parse config base url - parsedBaseURL, err := parseString(cfg.BaseURL, cfg.Variables) + parsedBaseURL, err := parseString(cfg.BaseURL, cfg.Variables, r.pluginLoader) if err != nil { return err } @@ -503,10 +514,81 @@ func (r *caseRunner) parseConfig(config IConfig) error { return nil } -func (r *caseRunner) getSummary() *testCaseSummary { +type pluginLoader struct { + *plugin.Plugin +} + +func (r *caseRunner) loadPlugin(path string) error { + if path == "" { + return nil + } + + // check if loaded before + if r.pluginLoader != nil { + return nil + } + + // locate plugin file + pluginPath, err := locatePlugin(path) + if err != nil { + // plugin not found + return nil + } + + // load plugin + plugins, err := plugin.Open(pluginPath) + if err != nil { + log.Error().Err(err).Str("path", path).Msg("load go plugin failed") + return err + } + r.pluginLoader = &pluginLoader{plugins} + + log.Info().Str("path", path).Msg("load go plugin success") + return nil +} + +func (r *hrpRunner) getSummary() *testCaseSummary { return &testCaseSummary{} } +// locatePlugin +// searching will be recursive upward until current working directory or system root dir. +func locatePlugin(startPath string) (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 + // target plugin file name is always debugtalk.so + pluginPath := filepath.Join(startDir, "debugtalk.so") + 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 locatePlugin(parentDir) +} + func setBodyBytes(req *http.Request, data []byte) { req.Body = ioutil.NopCloser(bytes.NewReader(data)) req.ContentLength = int64(len(data)) diff --git a/runner_test.go b/runner_test.go index 686d67e1..80770b4b 100644 --- a/runner_test.go +++ b/runner_test.go @@ -1,9 +1,66 @@ package hrp import ( + "fmt" + "os" + "os/exec" "testing" + + "github.com/stretchr/testify/assert" ) +func TestMain(m *testing.M) { + fmt.Println("[TestMain] build go plugin") + cmd := exec.Command("go", "build", "-buildmode=plugin", "-o=examples/debugtalk.so", "examples/plugin/debugtalk.go") + if err := cmd.Run(); err != nil { + panic(err) + } + os.Exit(m.Run()) +} + +func TestLocatePlugin(t *testing.T) { + cwd, _ := os.Getwd() + _, err := locatePlugin(cwd) + if !assert.Error(t, err) { + t.Fail() + } + + _, err = locatePlugin("") + if !assert.Error(t, err) { + t.Fail() + } + + startPath := "examples/debugtalk.so" + _, err = locatePlugin(startPath) + if !assert.Nil(t, err) { + t.Fail() + } + + startPath = "examples/demo.json" + _, err = locatePlugin(startPath) + if !assert.Nil(t, err) { + t.Fail() + } + + startPath = "examples/" + _, err = locatePlugin(startPath) + if !assert.Nil(t, err) { + t.Fail() + } + + startPath = "examples/plugin/debugtalk.go" + _, err = locatePlugin(startPath) + if !assert.Nil(t, err) { + t.Fail() + } + + startPath = "/abc" + _, err = locatePlugin(startPath) + if !assert.Error(t, err) { + t.Fail() + } +} + func TestHttpRunner(t *testing.T) { testcase1 := &TestCase{ Config: NewConfig("TestCase1").