mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-12 02:21:29 +08:00
feat: replace log with zerolog
This commit is contained in:
28
convert.go
28
convert.go
@@ -7,7 +7,7 @@ import (
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/rs/zerolog/log"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
@@ -24,14 +24,14 @@ func (tc *TestCase) ToTCase() (*TCase, error) {
|
||||
func (tc *TCase) Dump2JSON(path string) error {
|
||||
path, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
log.Errorf("convert absolute path error: %v, path: %v", err, path)
|
||||
log.Error().Err(err).Msg("convert absolute path failed")
|
||||
return err
|
||||
}
|
||||
log.Infof("dump testcase to json path: %s", path)
|
||||
log.Info().Str("path", path).Msg("dump testcase to json")
|
||||
file, _ := json.MarshalIndent(tc, "", " ")
|
||||
err = ioutil.WriteFile(path, file, 0644)
|
||||
if err != nil {
|
||||
log.Errorf("dump json path error: %v", err)
|
||||
log.Error().Err(err).Msg("dump json path failed")
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
@@ -40,10 +40,10 @@ func (tc *TCase) Dump2JSON(path string) error {
|
||||
func (tc *TCase) Dump2YAML(path string) error {
|
||||
path, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
log.Errorf("convert absolute path error: %v, path: %v", err, path)
|
||||
log.Error().Err(err).Msg("convert absolute path failed")
|
||||
return err
|
||||
}
|
||||
log.Infof("dump testcase to yaml path: %s", path)
|
||||
log.Info().Str("path", path).Msg("dump testcase to yaml")
|
||||
|
||||
// init yaml encoder
|
||||
buffer := new(bytes.Buffer)
|
||||
@@ -58,7 +58,7 @@ func (tc *TCase) Dump2YAML(path string) error {
|
||||
|
||||
err = ioutil.WriteFile(path, buffer.Bytes(), 0644)
|
||||
if err != nil {
|
||||
log.Errorf("dump yaml path error: %v", err)
|
||||
log.Error().Err(err).Msg("dump yaml path failed")
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
@@ -67,14 +67,14 @@ func (tc *TCase) Dump2YAML(path string) error {
|
||||
func loadFromJSON(path string) (*TCase, error) {
|
||||
path, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
log.Errorf("convert absolute path error: %v, path: %v", err, path)
|
||||
log.Error().Str("path", path).Err(err).Msg("convert absolute path failed")
|
||||
return nil, err
|
||||
}
|
||||
log.WithField("path", path).Info("load json testcase")
|
||||
log.Info().Str("path", path).Msg("load json testcase")
|
||||
|
||||
file, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
log.Errorf("dump json path error: %v", err)
|
||||
log.Error().Err(err).Msg("load json path failed")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -88,14 +88,14 @@ func loadFromJSON(path string) (*TCase, error) {
|
||||
func loadFromYAML(path string) (*TCase, error) {
|
||||
path, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
log.Errorf("convert absolute path error: %v, path: %v", err, path)
|
||||
log.Error().Str("path", path).Err(err).Msg("convert absolute path failed")
|
||||
return nil, err
|
||||
}
|
||||
log.Infof("load testcase from yaml path: %s", path)
|
||||
log.Info().Str("path", path).Msg("load yaml testcase")
|
||||
|
||||
file, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
log.Errorf("dump yaml path error: %v", err)
|
||||
log.Error().Err(err).Msg("load yaml path failed")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -118,7 +118,7 @@ func (tc *TCase) ToTestCase() (*TestCase, error) {
|
||||
step: step,
|
||||
})
|
||||
} else {
|
||||
log.Warnf("[convertTestCase] unexpected step: %+v", step)
|
||||
log.Warn().Interface("step", step).Msg("[convertTestCase] unexpected step")
|
||||
}
|
||||
}
|
||||
return testCase, nil
|
||||
|
||||
2
go.mod
2
go.mod
@@ -12,8 +12,8 @@ require (
|
||||
github.com/myzhan/boomer v1.6.0
|
||||
github.com/olekukonko/tablewriter v0.0.5 // indirect
|
||||
github.com/pkg/errors v0.9.1
|
||||
github.com/rs/zerolog v1.25.0
|
||||
github.com/shirou/gopsutil v3.21.8+incompatible // indirect
|
||||
github.com/sirupsen/logrus v1.8.1
|
||||
github.com/spf13/cobra v1.2.1
|
||||
github.com/stretchr/testify v1.7.0
|
||||
github.com/tklauser/go-sysconf v0.3.9 // indirect
|
||||
|
||||
7
go.sum
7
go.sum
@@ -223,6 +223,9 @@ github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndr
|
||||
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
|
||||
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
|
||||
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
|
||||
github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
|
||||
github.com/rs/zerolog v1.25.0 h1:Rj7XygbUHKUlDPcVdoLyR91fJBsduXj5fRxyqIQj/II=
|
||||
github.com/rs/zerolog v1.25.0/go.mod h1:7KHcEGe0QZPOm2IE4Kpb5rTh6n1h2hIgS5OOnu1rUaI=
|
||||
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
|
||||
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
|
||||
@@ -231,8 +234,6 @@ github.com/shirou/gopsutil v3.21.8+incompatible h1:sh0foI8tMRlCidUJR+KzqWYWxrkuu
|
||||
github.com/shirou/gopsutil v3.21.8+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
|
||||
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
|
||||
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
|
||||
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
|
||||
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
|
||||
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
|
||||
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
|
||||
github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I=
|
||||
@@ -399,7 +400,6 @@ golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7w
|
||||
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20191001151750-bb3f8db39f24/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20191228213918-04cbcbbfeed8/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200113162924-86b910548bc1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
@@ -493,6 +493,7 @@ golang.org/x/tools v0.0.0-20210105154028-b0ab187a4818/go.mod h1:emZCQorbCU4vsT4f
|
||||
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
|
||||
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
|
||||
golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
|
||||
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
|
||||
@@ -12,7 +12,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/httprunner/hrp"
|
||||
)
|
||||
@@ -120,10 +120,10 @@ func (h *HAR) prepareTestSteps() ([]*hrp.TStep, error) {
|
||||
}
|
||||
|
||||
func (h *HAR) prepareTestStep(entry *Entry) (*hrp.TStep, error) {
|
||||
log.WithFields(log.Fields{
|
||||
"method": entry.Request.Method,
|
||||
"url": entry.Request.URL,
|
||||
}).Info("convert teststep")
|
||||
log.Info().
|
||||
Str("method", entry.Request.Method).
|
||||
Str("url", entry.Request.URL).
|
||||
Msg("convert teststep")
|
||||
|
||||
tStep := &TStep{
|
||||
TStep: hrp.TStep{
|
||||
@@ -168,7 +168,7 @@ func (s *TStep) makeRequestURL(entry *Entry) error {
|
||||
|
||||
u, err := url.Parse(entry.Request.URL)
|
||||
if err != nil {
|
||||
log.Errorf("makeRequestURL error: %v", err)
|
||||
log.Error().Err(err).Msg("make request url failed")
|
||||
return err
|
||||
}
|
||||
s.Request.URL = fmt.Sprintf("%s://%s", u.Scheme, u.Hostname()+u.Path)
|
||||
@@ -215,7 +215,7 @@ func (s *TStep) makeRequestBody(entry *Entry) error {
|
||||
var body interface{}
|
||||
err := json.Unmarshal([]byte(entry.Request.PostData.Text), &body)
|
||||
if err != nil {
|
||||
log.Errorf("makeRequestBody error: %v", err)
|
||||
log.Error().Err(err).Msg("make request body failed")
|
||||
return err
|
||||
}
|
||||
s.Request.Body = body
|
||||
@@ -231,7 +231,7 @@ func (s *TStep) makeRequestBody(entry *Entry) error {
|
||||
s.Request.Body = entry.Request.PostData.Text
|
||||
} else {
|
||||
// TODO
|
||||
log.Fatalf("makeRequestBody: Not implemented for mimeType %s", mimeType)
|
||||
log.Error().Msgf("makeRequestBody: Not implemented for mimeType %s", mimeType)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/httprunner/hrp/har2case"
|
||||
@@ -37,7 +37,7 @@ var har2caseCmd = &cobra.Command{
|
||||
}
|
||||
outputFiles = append(outputFiles, outputPath)
|
||||
}
|
||||
log.Infof("output: %v", outputFiles)
|
||||
log.Info().Strs("output", outputFiles).Msg("convert testcase success")
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
38
parser.go
38
parser.go
@@ -9,7 +9,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/maja42/goval"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/httprunner/hrp/builtin"
|
||||
)
|
||||
@@ -23,13 +23,13 @@ func parseStep(step IStep, config *TConfig) *TStep {
|
||||
func buildURL(baseURL, stepURL string) string {
|
||||
uConfig, err := url.Parse(baseURL)
|
||||
if err != nil {
|
||||
log.Errorf("[buildURL] baseURL: %v, error: %v", baseURL, err)
|
||||
log.Error().Str("baseURL", baseURL).Err(err).Msg("[buildURL] parse baseURL failed")
|
||||
return ""
|
||||
}
|
||||
|
||||
uStep, err := uConfig.Parse(stepURL)
|
||||
if err != nil {
|
||||
log.Errorf("[buildURL] baseURL: %v, error: %v", baseURL, err)
|
||||
log.Error().Str("stepURL", stepURL).Err(err).Msg("[buildURL] parse stepURL failed")
|
||||
return ""
|
||||
}
|
||||
|
||||
@@ -185,7 +185,10 @@ func parseString(raw string, variablesMapping map[string]interface{}) (interface
|
||||
matchStartPosition += len(funcMatched[0])
|
||||
parsedString += fmt.Sprintf("%v", result)
|
||||
remainedString = raw[matchStartPosition:]
|
||||
log.Infof("[parseString] parsedString: %v, matchStartPosition: %v", parsedString, matchStartPosition)
|
||||
log.Debug().
|
||||
Str("parsedString", parsedString).
|
||||
Int("matchStartPosition", matchStartPosition).
|
||||
Msg("[parseString] parse function")
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -211,7 +214,10 @@ func parseString(raw string, variablesMapping map[string]interface{}) (interface
|
||||
matchStartPosition += len(varMatched[0])
|
||||
parsedString += fmt.Sprintf("%v", varValue)
|
||||
remainedString = raw[matchStartPosition:]
|
||||
log.Infof("[parseString] parsedString: %v, matchStartPosition: %v", parsedString, matchStartPosition)
|
||||
log.Debug().
|
||||
Str("parsedString", parsedString).
|
||||
Int("matchStartPosition", matchStartPosition).
|
||||
Msg("[parseString] parse variable")
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -284,7 +290,7 @@ func callFunc(funcName string, arguments ...interface{}) (interface{}, error) {
|
||||
// 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.Errorf("[callFunction] error: %s", err.Error())
|
||||
log.Error().Err(err).Msg("call function failed")
|
||||
return nil, err
|
||||
}
|
||||
// convert argument to expect type
|
||||
@@ -295,7 +301,7 @@ func callFunc(funcName string, arguments ...interface{}) (interface{}, error) {
|
||||
if len(resultValues) > 1 {
|
||||
// function should return at most one value
|
||||
err := fmt.Errorf("function %s should return at most one value", funcName)
|
||||
log.Errorf("[callFunction] error: %s", err.Error())
|
||||
log.Error().Err(err).Msg("call function failed")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -307,11 +313,11 @@ func callFunc(funcName string, arguments ...interface{}) (interface{}, error) {
|
||||
// return one value
|
||||
// convert reflect.Value to interface{}
|
||||
result := resultValues[0].Interface()
|
||||
log.WithFields(log.Fields{
|
||||
"funcName": funcName,
|
||||
"arguments": arguments,
|
||||
"output": result,
|
||||
}).Info("call function")
|
||||
log.Info().
|
||||
Str("funcName", funcName).
|
||||
Interface("arguments", arguments).
|
||||
Interface("output", result).
|
||||
Msg("call function success")
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@@ -329,7 +335,7 @@ func literalEval(raw string) (interface{}, error) {
|
||||
// eval string to number
|
||||
result, err := eval.Evaluate(raw, nil, nil)
|
||||
if err != nil {
|
||||
log.Errorf("[literalEval] eval %s error: %s", raw, err.Error())
|
||||
log.Error().Err(err).Msgf("[literalEval] eval %s failed", raw)
|
||||
return raw, err
|
||||
}
|
||||
return result, nil
|
||||
@@ -380,7 +386,7 @@ func parseVariables(variables map[string]interface{}) (map[string]interface{}, e
|
||||
// variables = {"token": "abc$token"}
|
||||
// variables = {"key": ["$key", 2]}
|
||||
if _, ok := extractVarsSet[varName]; ok {
|
||||
log.Errorf("[parseVariables] variable self reference error: %v", variables)
|
||||
log.Error().Interface("variables", variables).Msg("[parseVariables] variable self reference error")
|
||||
return variables, fmt.Errorf("variable self reference: %v", varName)
|
||||
}
|
||||
|
||||
@@ -395,7 +401,7 @@ func parseVariables(variables map[string]interface{}) (map[string]interface{}, e
|
||||
}
|
||||
}
|
||||
if len(undefinedVars) > 0 {
|
||||
log.Errorf("[parseVariables] variable not defined error: %v", undefinedVars)
|
||||
log.Error().Interface("undefinedVars", undefinedVars).Msg("[parseVariables] variable not defined error")
|
||||
return variables, fmt.Errorf("variable not defined: %v", undefinedVars)
|
||||
}
|
||||
|
||||
@@ -408,7 +414,7 @@ func parseVariables(variables map[string]interface{}) (map[string]interface{}, e
|
||||
traverseRounds += 1
|
||||
// check if circular reference exists
|
||||
if traverseRounds > len(variables) {
|
||||
log.Errorf("[parseVariables] circular reference error, break infinite loop!")
|
||||
log.Error().Msg("[parseVariables] circular reference error, break infinite loop!")
|
||||
return variables, fmt.Errorf("circular reference")
|
||||
}
|
||||
}
|
||||
|
||||
26
response.go
26
response.go
@@ -7,7 +7,7 @@ import (
|
||||
|
||||
"github.com/imroc/req"
|
||||
"github.com/jmespath/go-jmespath"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/httprunner/hrp/builtin"
|
||||
)
|
||||
@@ -45,8 +45,10 @@ func NewResponseObject(t *testing.T, resp *req.Resp) (*ResponseObject, error) {
|
||||
respObjMetaBytes, _ := json.Marshal(respObjMeta)
|
||||
var data interface{}
|
||||
if err := json.Unmarshal(respObjMetaBytes, &data); err != nil {
|
||||
log.Errorf("[NewResponseObject] convert respObjMeta to interface{} error: %v, respObjMeta: %v",
|
||||
err, string(respObjMetaBytes))
|
||||
log.Error().
|
||||
Str("respObjMeta", string(respObjMetaBytes)).
|
||||
Err(err).
|
||||
Msg("[NewResponseObject] convert respObjMeta to interface{} failed")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -77,8 +79,8 @@ func (v *ResponseObject) Extract(extractors map[string]string) map[string]interf
|
||||
extractMapping := make(map[string]interface{})
|
||||
for key, value := range extractors {
|
||||
extractedValue := v.searchJmespath(value)
|
||||
log.WithField("value", extractedValue).Infof("extract value from %s", value)
|
||||
log.WithField("value", extractedValue).Infof("set variable %s", key)
|
||||
log.Info().Str("from", value).Interface("value", extractedValue).Msg("extract value")
|
||||
log.Info().Str("variable", key).Interface("value", extractedValue).Msg("set variable")
|
||||
extractMapping[key] = extractedValue
|
||||
}
|
||||
|
||||
@@ -112,12 +114,12 @@ func (v *ResponseObject) Validate(validators []TValidator, variablesMapping map[
|
||||
|
||||
// do assertion
|
||||
result := assertFunc(v.t, expectValue, checkValue)
|
||||
log.WithFields(log.Fields{
|
||||
"assertMethod": assertMethod,
|
||||
"expectValue": expectValue,
|
||||
"checkValue": checkValue,
|
||||
"result": result,
|
||||
}).Infof("validate %s", checkItem)
|
||||
log.Info().
|
||||
Str("assertMethod", assertMethod).
|
||||
Interface("expectValue", expectValue).
|
||||
Interface("checkValue", checkValue).
|
||||
Bool("result", result).
|
||||
Msgf("validate %s", checkItem)
|
||||
if !result {
|
||||
v.t.Fail()
|
||||
}
|
||||
@@ -128,7 +130,7 @@ func (v *ResponseObject) Validate(validators []TValidator, variablesMapping map[
|
||||
func (v *ResponseObject) searchJmespath(expr string) interface{} {
|
||||
checkValue, err := jmespath.Search(expr, v.respObjMeta)
|
||||
if err != nil {
|
||||
log.Errorf("[searchJmespath] jmespath.Search error: %v", err)
|
||||
log.Error().Str("expr", expr).Err(err).Msg("search jmespath failed")
|
||||
return expr // jmespath not found, return the expression
|
||||
}
|
||||
return checkValue
|
||||
|
||||
39
runner.go
39
runner.go
@@ -2,16 +2,17 @@ package hrp
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/imroc/req"
|
||||
"github.com/pkg/errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func init() {
|
||||
log.SetLevel(log.InfoLevel)
|
||||
log.SetFormatter(&log.TextFormatter{})
|
||||
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
|
||||
}
|
||||
|
||||
// run API test with default configs
|
||||
@@ -34,19 +35,19 @@ type Runner struct {
|
||||
}
|
||||
|
||||
func (r *Runner) WithTestingT(t *testing.T) *Runner {
|
||||
log.Info("[init] WithTestingT")
|
||||
log.Info().Msg("[init] WithTestingT")
|
||||
r.t = t
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *Runner) SetDebug(debug bool) *Runner {
|
||||
log.WithField("debug", debug).Info("[init] SetDebug")
|
||||
log.Info().Bool("debug", debug).Msg("[init] SetDebug")
|
||||
r.debug = debug
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *Runner) SetProxyUrl(proxyUrl string) *Runner {
|
||||
log.WithField("proxyUrl", proxyUrl).Info("[init] SetProxyUrl")
|
||||
log.Info().Str("proxyUrl", proxyUrl).Msg("[init] SetProxyUrl")
|
||||
r.client.SetProxyUrl(proxyUrl)
|
||||
return r
|
||||
}
|
||||
@@ -55,11 +56,11 @@ func (r *Runner) Run(testcases ...ITestCase) error {
|
||||
for _, iTestCase := range testcases {
|
||||
testcase, err := iTestCase.ToTestCase()
|
||||
if err != nil {
|
||||
log.Errorf("[Run] testcase.ToStruct() error: %v", err)
|
||||
log.Error().Err(err).Msg("[Run] convert ITestCase interface to TestCase struct failed")
|
||||
return err
|
||||
}
|
||||
if err := r.runCase(testcase); err != nil {
|
||||
log.Errorf("[Run] runCase error: %v", err)
|
||||
log.Error().Err(err).Msg("[Run] run testcase failed")
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -72,7 +73,7 @@ func (r *Runner) runCase(testcase *TestCase) error {
|
||||
return err
|
||||
}
|
||||
|
||||
log.WithField("testcase", config.Name).Info("run testcase start")
|
||||
log.Info().Str("testcase", config.Name).Msg("run testcase start")
|
||||
|
||||
extractedVariables := make(map[string]interface{})
|
||||
|
||||
@@ -86,7 +87,7 @@ func (r *Runner) runCase(testcase *TestCase) error {
|
||||
// parse step variables
|
||||
parsedVariables, err := parseVariables(stepVariables)
|
||||
if err != nil {
|
||||
log.Errorf("[parseConfig] parse variables: %v, error: %v", config.Variables, err)
|
||||
log.Error().Interface("variables", config.Variables).Err(err).Msg("parse step variables failed")
|
||||
return err
|
||||
}
|
||||
step.ToStruct().Variables = parsedVariables
|
||||
@@ -101,15 +102,15 @@ func (r *Runner) runCase(testcase *TestCase) error {
|
||||
}
|
||||
}
|
||||
|
||||
log.WithField("testcase", config.Name).Info("run testcase end")
|
||||
log.Info().Str("testcase", config.Name).Msg("run testcase end")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Runner) runStep(step IStep, config *TConfig) (stepData *StepData, err error) {
|
||||
log.WithField("step", step.Name()).Info("run step start")
|
||||
log.Info().Str("step", step.Name()).Msg("run step start")
|
||||
if tc, ok := step.(*testcaseWithOptionalArgs); ok {
|
||||
// run referenced testcase
|
||||
log.Infof("run referenced testcase: %v", tc.step.Name)
|
||||
log.Info().Str("testcase", tc.step.Name).Msg("run referenced testcase")
|
||||
// TODO: override testcase config
|
||||
stepData, err = r.runStepTestCase(tc.step)
|
||||
if err != nil {
|
||||
@@ -123,11 +124,11 @@ func (r *Runner) runStep(step IStep, config *TConfig) (stepData *StepData, err e
|
||||
return
|
||||
}
|
||||
}
|
||||
log.WithFields(log.Fields{
|
||||
"step": step.Name(),
|
||||
"success": stepData.Success,
|
||||
"exportVars": stepData.ExportVars,
|
||||
}).Info("run step end")
|
||||
log.Info().
|
||||
Str("step", step.Name()).
|
||||
Bool("success", stepData.Success).
|
||||
Interface("exportVars", stepData.ExportVars).
|
||||
Msg("run step end")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -222,7 +223,7 @@ func (r *Runner) parseConfig(config *TConfig) error {
|
||||
// parse config variables
|
||||
parsedVariables, err := parseVariables(config.Variables)
|
||||
if err != nil {
|
||||
log.Errorf("[parseConfig] parse variables: %v, error: %v", config.Variables, err)
|
||||
log.Error().Interface("variables", config.Variables).Err(err).Msg("parse config variables failed")
|
||||
return err
|
||||
}
|
||||
config.Variables = parsedVariables
|
||||
|
||||
Reference in New Issue
Block a user