mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-07 23:41:22 +08:00
feat: support creating and calling custom functions with go plugin
This commit is contained in:
@@ -146,6 +146,7 @@ func (path *TestCasePath) ToTestCase() (*TestCase, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
tc.Config.Path = path.Path
|
||||||
testcase, err := tc.ToTestCase()
|
testcase, err := tc.ToTestCase()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
# Release History
|
# 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: implement `transaction` mechanism for load test
|
||||||
- feat: continue running next step when failure occurs with `--continue-on-failure` flag, default to failfast
|
- feat: continue running next step when failure occurs with `--continue-on-failure` flag, default to failfast
|
||||||
|
|||||||
14
examples/plugin/debugtalk.go
Normal file
14
examples/plugin/debugtalk.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
@@ -20,6 +20,7 @@ type TConfig struct {
|
|||||||
Parameters map[string]interface{} `json:"parameters,omitempty" yaml:"parameters,omitempty"`
|
Parameters map[string]interface{} `json:"parameters,omitempty" yaml:"parameters,omitempty"`
|
||||||
Export []string `json:"export,omitempty" yaml:"export,omitempty"`
|
Export []string `json:"export,omitempty" yaml:"export,omitempty"`
|
||||||
Weight int `json:"weight,omitempty" yaml:"weight,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.
|
// Request represents HTTP request data structure.
|
||||||
|
|||||||
100
parser.go
100
parser.go
@@ -31,9 +31,9 @@ func buildURL(baseURL, stepURL string) string {
|
|||||||
return uStep.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)
|
parsedHeaders := make(map[string]string)
|
||||||
headers, err := parseData(rawHeaders, variablesMapping)
|
headers, err := parseData(rawHeaders, variablesMapping, pluginLoader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return rawHeaders, err
|
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)
|
rawValue := reflect.ValueOf(raw)
|
||||||
switch rawValue.Kind() {
|
switch rawValue.Kind() {
|
||||||
case reflect.String:
|
case reflect.String:
|
||||||
@@ -64,11 +64,11 @@ func parseData(raw interface{}, variablesMapping map[string]interface{}) (interf
|
|||||||
// other string
|
// other string
|
||||||
value := rawValue.String()
|
value := rawValue.String()
|
||||||
value = strings.TrimSpace(value)
|
value = strings.TrimSpace(value)
|
||||||
return parseString(value, variablesMapping)
|
return parseString(value, variablesMapping, pluginLoader)
|
||||||
case reflect.Slice:
|
case reflect.Slice:
|
||||||
parsedSlice := make([]interface{}, rawValue.Len())
|
parsedSlice := make([]interface{}, rawValue.Len())
|
||||||
for i := 0; i < rawValue.Len(); i++ {
|
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 {
|
if err != nil {
|
||||||
return raw, err
|
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{}
|
case reflect.Map: // convert any map to map[string]interface{}
|
||||||
parsedMap := make(map[string]interface{})
|
parsedMap := make(map[string]interface{})
|
||||||
for _, k := range rawValue.MapKeys() {
|
for _, k := range rawValue.MapKeys() {
|
||||||
parsedKey, err := parseString(k.String(), variablesMapping)
|
parsedKey, err := parseString(k.String(), variablesMapping, pluginLoader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return raw, err
|
return raw, err
|
||||||
}
|
}
|
||||||
v := rawValue.MapIndex(k)
|
v := rawValue.MapIndex(k)
|
||||||
parsedValue, err := parseData(v.Interface(), variablesMapping)
|
parsedValue, err := parseData(v.Interface(), variablesMapping, pluginLoader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return raw, err
|
return raw, err
|
||||||
}
|
}
|
||||||
@@ -121,7 +121,7 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// parseString parse string with variables
|
// 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
|
matchStartPosition := 0
|
||||||
parsedString := ""
|
parsedString := ""
|
||||||
remainedString := raw
|
remainedString := raw
|
||||||
@@ -160,16 +160,25 @@ func parseString(raw string, variablesMapping map[string]interface{}) (interface
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return raw, err
|
return raw, err
|
||||||
}
|
}
|
||||||
parsedArgs, err := parseData(arguments, variablesMapping)
|
parsedArgs, err := parseData(arguments, variablesMapping, pluginLoader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return raw, err
|
return raw, err
|
||||||
}
|
}
|
||||||
|
|
||||||
result, err := callFunc(funcName, parsedArgs.([]interface{})...)
|
fn, err := getMappingFunction(funcName, pluginLoader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return raw, err
|
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 {
|
if funcMatched[0] == raw {
|
||||||
// raw_string is a function, e.g. "${add_one(3)}", return its eval value directly
|
// raw_string is a function, e.g. "${add_one(3)}", return its eval value directly
|
||||||
return result, nil
|
return result, nil
|
||||||
@@ -247,30 +256,50 @@ func mergeVariables(variables, overriddenVariables map[string]interface{}) map[s
|
|||||||
return mergedVariables
|
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
|
// callFunc call function with arguments
|
||||||
// only support return at most one result value
|
// only support return at most one result value
|
||||||
func callFunc(funcName string, arguments ...interface{}) (interface{}, error) {
|
func callFunc(fn reflect.Value, arguments ...interface{}) (interface{}, error) {
|
||||||
function, ok := builtin.Functions[funcName]
|
if fn.Type().NumIn() != len(arguments) {
|
||||||
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) {
|
|
||||||
// function arguments not match
|
// 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))
|
argumentsValue := make([]reflect.Value, len(arguments))
|
||||||
for index, argument := range arguments {
|
for index, argument := range arguments {
|
||||||
argumentValue := reflect.ValueOf(argument)
|
argumentValue := reflect.ValueOf(argument)
|
||||||
expectArgumentType := funcValue.Type().In(index)
|
expectArgumentType := fn.Type().In(index)
|
||||||
actualArgumentType := reflect.TypeOf(argument)
|
actualArgumentType := reflect.TypeOf(argument)
|
||||||
|
|
||||||
// type match
|
// type match
|
||||||
@@ -282,20 +311,18 @@ func callFunc(funcName string, arguments ...interface{}) (interface{}, error) {
|
|||||||
// type not match, check if convertible
|
// type not match, check if convertible
|
||||||
if !actualArgumentType.ConvertibleTo(expectArgumentType) {
|
if !actualArgumentType.ConvertibleTo(expectArgumentType) {
|
||||||
// function argument type not match and not convertible
|
// 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",
|
err := fmt.Errorf("function argument %d's type is neither match nor convertible, expect %v, actual %v",
|
||||||
funcName, index, expectArgumentType, actualArgumentType)
|
index, expectArgumentType, actualArgumentType)
|
||||||
log.Error().Err(err).Msg("call function failed")
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
// convert argument to expect type
|
// convert argument to expect type
|
||||||
argumentsValue[index] = argumentValue.Convert(expectArgumentType)
|
argumentsValue[index] = argumentValue.Convert(expectArgumentType)
|
||||||
}
|
}
|
||||||
|
|
||||||
resultValues := funcValue.Call(argumentsValue)
|
resultValues := fn.Call(argumentsValue)
|
||||||
if len(resultValues) > 1 {
|
if len(resultValues) > 1 {
|
||||||
// function should return at most one value
|
// function should return at most one value
|
||||||
err := fmt.Errorf("function %s should return at most one value", funcName)
|
err := fmt.Errorf("function should return at most one value")
|
||||||
log.Error().Err(err).Msg("call function failed")
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -307,11 +334,6 @@ func callFunc(funcName string, arguments ...interface{}) (interface{}, error) {
|
|||||||
// return one value
|
// return one value
|
||||||
// convert reflect.Value to interface{}
|
// convert reflect.Value to interface{}
|
||||||
result := resultValues[0].Interface()
|
result := resultValues[0].Interface()
|
||||||
log.Info().
|
|
||||||
Str("funcName", funcName).
|
|
||||||
Interface("arguments", arguments).
|
|
||||||
Interface("output", result).
|
|
||||||
Msg("call function success")
|
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -361,7 +383,7 @@ func parseFunctionArguments(argsStr string) ([]interface{}, error) {
|
|||||||
return arguments, nil
|
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{})
|
parsedVariables := make(map[string]interface{})
|
||||||
var traverseRounds int
|
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)
|
return variables, fmt.Errorf("variable not defined: %v", undefinedVars)
|
||||||
}
|
}
|
||||||
|
|
||||||
parsedValue, err := parseData(varValue, parsedVariables)
|
parsedValue, err := parseData(varValue, parsedVariables, pluginLoader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package hrp
|
package hrp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"plugin"
|
||||||
"sort"
|
"sort"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@@ -161,7 +162,7 @@ func TestParseDataStringWithVariables(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, data := range testData {
|
for _, data := range testData {
|
||||||
parsedData, err := parseData(data.expr, variablesMapping)
|
parsedData, err := parseData(data.expr, variablesMapping, nil)
|
||||||
if !assert.NoError(t, err) {
|
if !assert.NoError(t, err) {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
@@ -185,7 +186,7 @@ func TestParseDataStringWithUndefinedVariables(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, data := range testData {
|
for _, data := range testData {
|
||||||
parsedData, err := parseData(data.expr, variablesMapping)
|
parsedData, err := parseData(data.expr, variablesMapping, nil)
|
||||||
if !assert.Error(t, err) {
|
if !assert.Error(t, err) {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
@@ -229,7 +230,7 @@ func TestParseDataStringWithVariablesAbnormal(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, data := range testData {
|
for _, data := range testData {
|
||||||
parsedData, err := parseData(data.expr, variablesMapping)
|
parsedData, err := parseData(data.expr, variablesMapping, nil)
|
||||||
if !assert.NoError(t, err) {
|
if !assert.NoError(t, err) {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
@@ -259,7 +260,7 @@ func TestParseDataMapWithVariables(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, data := range testData {
|
for _, data := range testData {
|
||||||
parsedData, err := parseData(data.expr, variablesMapping)
|
parsedData, err := parseData(data.expr, variablesMapping, nil)
|
||||||
if !assert.NoError(t, err) {
|
if !assert.NoError(t, err) {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
@@ -292,7 +293,7 @@ func TestParseHeaders(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, data := range testData {
|
for _, data := range testData {
|
||||||
parsedHeaders, err := parseHeaders(data.rawHeaders, variablesMapping)
|
parsedHeaders, err := parseHeaders(data.rawHeaders, variablesMapping, nil)
|
||||||
if !assert.NoError(t, err) {
|
if !assert.NoError(t, err) {
|
||||||
t.Fail()
|
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
|
// call function without arguments
|
||||||
_, err := callFunc("get_timestamp")
|
f1, _ := getMappingFunction("get_timestamp", nil)
|
||||||
|
_, err := callFunc(f1)
|
||||||
if !assert.NoError(t, err) {
|
if !assert.NoError(t, err) {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
|
|
||||||
// call function with one argument
|
// call function with one argument
|
||||||
timeStart := time.Now()
|
timeStart := time.Now()
|
||||||
_, err = callFunc("sleep", 1)
|
f2, _ := getMappingFunction("sleep", nil)
|
||||||
|
_, err = callFunc(f2, 1)
|
||||||
if !assert.NoError(t, err) {
|
if !assert.NoError(t, err) {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
@@ -346,7 +349,8 @@ func TestCallFunction(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// call function with one argument
|
// 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) {
|
if !assert.NoError(t, err) {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
@@ -355,7 +359,8 @@ func TestCallFunction(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// call function with two argument
|
// 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) {
|
if !assert.NoError(t, err) {
|
||||||
t.Fail()
|
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) {
|
func TestLiteralEval(t *testing.T) {
|
||||||
testData := []struct {
|
testData := []struct {
|
||||||
expr string
|
expr string
|
||||||
@@ -441,7 +464,7 @@ func TestParseDataStringWithFunctions(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, data := range testData1 {
|
for _, data := range testData1 {
|
||||||
value, err := parseData(data.expr, variablesMapping)
|
value, err := parseData(data.expr, variablesMapping, nil)
|
||||||
if !assert.NoError(t, err) {
|
if !assert.NoError(t, err) {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
@@ -460,7 +483,7 @@ func TestParseDataStringWithFunctions(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, data := range testData2 {
|
for _, data := range testData2 {
|
||||||
value, err := parseData(data.expr, variablesMapping)
|
value, err := parseData(data.expr, variablesMapping, nil)
|
||||||
if !assert.NoError(t, err) {
|
if !assert.NoError(t, err) {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
@@ -507,7 +530,7 @@ func TestParseVariables(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, data := range testData {
|
for _, data := range testData {
|
||||||
value, err := parseVariables(data.rawVars)
|
value, err := parseVariables(data.rawVars, nil)
|
||||||
if !assert.NoError(t, err) {
|
if !assert.NoError(t, err) {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
@@ -537,7 +560,7 @@ func TestParseVariablesAbnormal(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, data := range testData {
|
for _, data := range testData {
|
||||||
value, err := parseVariables(data.rawVars)
|
value, err := parseVariables(data.rawVars, nil)
|
||||||
if !assert.Error(t, err) {
|
if !assert.Error(t, err) {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
|
|||||||
12
response.go
12
response.go
@@ -13,7 +13,7 @@ import (
|
|||||||
"github.com/httprunner/hrp/internal/builtin"
|
"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
|
// prepare response headers
|
||||||
headers := make(map[string]string)
|
headers := make(map[string]string)
|
||||||
for k, v := range resp.Header {
|
for k, v := range resp.Header {
|
||||||
@@ -60,8 +60,9 @@ func newResponseObject(t *testing.T, resp *http.Response) (*responseObject, erro
|
|||||||
}
|
}
|
||||||
|
|
||||||
return &responseObject{
|
return &responseObject{
|
||||||
t: t,
|
t: t,
|
||||||
respObjMeta: data,
|
pluginLoader: pluginLoader,
|
||||||
|
respObjMeta: data,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,6 +75,7 @@ type respObjMeta struct {
|
|||||||
|
|
||||||
type responseObject struct {
|
type responseObject struct {
|
||||||
t *testing.T
|
t *testing.T
|
||||||
|
pluginLoader *pluginLoader
|
||||||
respObjMeta interface{}
|
respObjMeta interface{}
|
||||||
validationResults map[string]interface{}
|
validationResults map[string]interface{}
|
||||||
}
|
}
|
||||||
@@ -101,7 +103,7 @@ func (v *responseObject) Validate(validators []Validator, variablesMapping map[s
|
|||||||
var checkValue interface{}
|
var checkValue interface{}
|
||||||
if strings.Contains(checkItem, "$") {
|
if strings.Contains(checkItem, "$") {
|
||||||
// reference variable
|
// reference variable
|
||||||
checkValue, err = parseData(checkItem, variablesMapping)
|
checkValue, err = parseData(checkItem, variablesMapping, v.pluginLoader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -114,7 +116,7 @@ func (v *responseObject) Validate(validators []Validator, variablesMapping map[s
|
|||||||
assertFunc := builtin.Assertions[assertMethod]
|
assertFunc := builtin.Assertions[assertMethod]
|
||||||
|
|
||||||
// parse expected value
|
// parse expected value
|
||||||
expectValue, err := parseData(validator.Expect, variablesMapping)
|
expectValue, err := parseData(validator.Expect, variablesMapping, v.pluginLoader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
100
runner.go
100
runner.go
@@ -9,6 +9,9 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httputil"
|
"net/http/httputil"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"plugin"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"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.
|
// 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
|
transactions map[string]map[transactionType]time.Time
|
||||||
startTime time.Time // record start time of the testcase
|
startTime time.Time // record start time of the testcase
|
||||||
|
// pluginLoader stores loaded go plugins.
|
||||||
|
pluginLoader *pluginLoader
|
||||||
}
|
}
|
||||||
|
|
||||||
// reset clears runner session variables.
|
// reset clears runner session variables.
|
||||||
@@ -194,7 +199,7 @@ func (r *caseRunner) runStep(index int) (stepResult *stepData, err error) {
|
|||||||
stepVariables = mergeVariables(stepVariables, copiedConfig.Variables)
|
stepVariables = mergeVariables(stepVariables, copiedConfig.Variables)
|
||||||
|
|
||||||
// parse step variables
|
// parse step variables
|
||||||
parsedVariables, err := parseVariables(stepVariables)
|
parsedVariables, err := parseVariables(stepVariables, r.pluginLoader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Interface("variables", copiedConfig.Variables).Err(err).Msg("parse step variables failed")
|
log.Error().Interface("variables", copiedConfig.Variables).Err(err).Msg("parse step variables failed")
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -311,7 +316,7 @@ func (r *caseRunner) runStepRequest(step *TStep) (stepResult *stepData, err erro
|
|||||||
|
|
||||||
// prepare request headers
|
// prepare request headers
|
||||||
if len(step.Request.Headers) > 0 {
|
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 {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "parse headers failed")
|
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
|
// prepare request params
|
||||||
var queryParams url.Values
|
var queryParams url.Values
|
||||||
if len(step.Request.Params) > 0 {
|
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 {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "parse data failed")
|
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
|
// prepare request body
|
||||||
if step.Request.Body != nil {
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -434,7 +439,7 @@ func (r *caseRunner) runStepRequest(step *TStep) (stepResult *stepData, err erro
|
|||||||
}
|
}
|
||||||
|
|
||||||
// new response object
|
// new response object
|
||||||
respObj, err := newResponseObject(r.hrpRunner.t, resp)
|
respObj, err := newResponseObject(r.hrpRunner.t, r.pluginLoader, resp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = errors.Wrap(err, "init ResponseObject error")
|
err = errors.Wrap(err, "init ResponseObject error")
|
||||||
return
|
return
|
||||||
@@ -479,22 +484,28 @@ func (r *caseRunner) runStepTestCase(step *TStep) (stepResult *stepData, err err
|
|||||||
func (r *caseRunner) parseConfig(config IConfig) error {
|
func (r *caseRunner) parseConfig(config IConfig) error {
|
||||||
cfg := config.ToStruct()
|
cfg := config.ToStruct()
|
||||||
// parse config variables
|
// parse config variables
|
||||||
parsedVariables, err := parseVariables(cfg.Variables)
|
parsedVariables, err := parseVariables(cfg.Variables, r.pluginLoader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Interface("variables", cfg.Variables).Err(err).Msg("parse config variables failed")
|
log.Error().Interface("variables", cfg.Variables).Err(err).Msg("parse config variables failed")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
cfg.Variables = parsedVariables
|
cfg.Variables = parsedVariables
|
||||||
|
|
||||||
|
// load plugin variables and functions
|
||||||
|
err = r.loadPlugin(cfg.Path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// parse config name
|
// parse config name
|
||||||
parsedName, err := parseString(cfg.Name, cfg.Variables)
|
parsedName, err := parseString(cfg.Name, cfg.Variables, r.pluginLoader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
cfg.Name = convertString(parsedName)
|
cfg.Name = convertString(parsedName)
|
||||||
|
|
||||||
// parse config base url
|
// parse config base url
|
||||||
parsedBaseURL, err := parseString(cfg.BaseURL, cfg.Variables)
|
parsedBaseURL, err := parseString(cfg.BaseURL, cfg.Variables, r.pluginLoader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -503,10 +514,81 @@ func (r *caseRunner) parseConfig(config IConfig) error {
|
|||||||
return nil
|
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{}
|
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) {
|
func setBodyBytes(req *http.Request, data []byte) {
|
||||||
req.Body = ioutil.NopCloser(bytes.NewReader(data))
|
req.Body = ioutil.NopCloser(bytes.NewReader(data))
|
||||||
req.ContentLength = int64(len(data))
|
req.ContentLength = int64(len(data))
|
||||||
|
|||||||
@@ -1,9 +1,66 @@
|
|||||||
package hrp
|
package hrp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
"testing"
|
"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) {
|
func TestHttpRunner(t *testing.T) {
|
||||||
testcase1 := &TestCase{
|
testcase1 := &TestCase{
|
||||||
Config: NewConfig("TestCase1").
|
Config: NewConfig("TestCase1").
|
||||||
|
|||||||
Reference in New Issue
Block a user