From 184a0e72965679c7fc8fe4db541bec13b8de1537 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Wed, 19 Jan 2022 21:42:12 +0800 Subject: [PATCH] refactor: remove IConfig --- boomer.go | 6 ++-- convert.go | 2 +- docs/cmd/hrp.md | 2 +- docs/cmd/hrp_boom.md | 2 +- docs/cmd/hrp_har2case.md | 2 +- docs/cmd/hrp_run.md | 2 +- docs/cmd/hrp_startproject.md | 2 +- internal/har2case/core.go | 2 +- internal/scaffold/demo_test.go | 2 +- models.go | 11 ++------ plugin/common/init.go | 2 +- runner.go | 13 ++++----- step.go | 51 ++++++++++++---------------------- step_test.go | 4 +-- 14 files changed, 39 insertions(+), 64 deletions(-) diff --git a/boomer.go b/boomer.go index df8e202f..49410898 100644 --- a/boomer.go +++ b/boomer.go @@ -51,7 +51,7 @@ func (b *HRPBoomer) Run(testcases ...ITestCase) { if err != nil { panic(err) } - cfg := testcase.Config.ToStruct() + cfg := testcase.Config err = initParameterIterator(cfg, "boomer") if err != nil { panic(err) @@ -74,7 +74,7 @@ func (b *HRPBoomer) Quit() { func (b *HRPBoomer) convertBoomerTask(testcase *TestCase) *boomer.Task { hrpRunner := NewRunner(nil).SetDebug(b.debug) - config := testcase.Config.ToStruct() + config := testcase.Config // each testcase has its own plugin process plugin, _ := initPlugin(config.Path) @@ -94,7 +94,7 @@ func (b *HRPBoomer) convertBoomerTask(testcase *TestCase) *boomer.Task { testcaseSuccess := true // flag whole testcase result var transactionSuccess = true // flag current transaction result - cfg := testcase.Config.ToStruct() + cfg := testcase.Config caseConfig := &TConfig{} // copy config to avoid data racing if err := copier.Copy(caseConfig, cfg); err != nil { diff --git a/convert.go b/convert.go index 2bfd6d08..593ecfdc 100644 --- a/convert.go +++ b/convert.go @@ -96,7 +96,7 @@ func loadFromYAML(path string) (*TCase, error) { func (tc *TCase) ToTestCase() (*TestCase, error) { testCase := &TestCase{ - Config: &Config{cfg: tc.Config}, + Config: tc.Config, } for _, step := range tc.TestSteps { if step.Request != nil { diff --git a/docs/cmd/hrp.md b/docs/cmd/hrp.md index 0a64f28f..e2d7e71f 100644 --- a/docs/cmd/hrp.md +++ b/docs/cmd/hrp.md @@ -33,4 +33,4 @@ Copyright 2021 debugtalk * [hrp run](hrp_run.md) - run API test * [hrp startproject](hrp_startproject.md) - create a scaffold project -###### Auto generated by spf13/cobra on 18-Jan-2022 +###### Auto generated by spf13/cobra on 19-Jan-2022 diff --git a/docs/cmd/hrp_boom.md b/docs/cmd/hrp_boom.md index 162ae0e3..72172651 100644 --- a/docs/cmd/hrp_boom.md +++ b/docs/cmd/hrp_boom.md @@ -39,4 +39,4 @@ hrp boom [flags] * [hrp](hrp.md) - One-stop solution for HTTP(S) testing. -###### Auto generated by spf13/cobra on 18-Jan-2022 +###### Auto generated by spf13/cobra on 19-Jan-2022 diff --git a/docs/cmd/hrp_har2case.md b/docs/cmd/hrp_har2case.md index 2d575c70..450d0788 100644 --- a/docs/cmd/hrp_har2case.md +++ b/docs/cmd/hrp_har2case.md @@ -23,4 +23,4 @@ hrp har2case $har_path... [flags] * [hrp](hrp.md) - One-stop solution for HTTP(S) testing. -###### Auto generated by spf13/cobra on 18-Jan-2022 +###### Auto generated by spf13/cobra on 19-Jan-2022 diff --git a/docs/cmd/hrp_run.md b/docs/cmd/hrp_run.md index 5c1f1c40..c23c6032 100644 --- a/docs/cmd/hrp_run.md +++ b/docs/cmd/hrp_run.md @@ -31,4 +31,4 @@ hrp run $path... [flags] * [hrp](hrp.md) - One-stop solution for HTTP(S) testing. -###### Auto generated by spf13/cobra on 18-Jan-2022 +###### Auto generated by spf13/cobra on 19-Jan-2022 diff --git a/docs/cmd/hrp_startproject.md b/docs/cmd/hrp_startproject.md index 96485c09..9395a577 100644 --- a/docs/cmd/hrp_startproject.md +++ b/docs/cmd/hrp_startproject.md @@ -16,4 +16,4 @@ hrp startproject $project_name [flags] * [hrp](hrp.md) - One-stop solution for HTTP(S) testing. -###### Auto generated by spf13/cobra on 18-Jan-2022 +###### Auto generated by spf13/cobra on 19-Jan-2022 diff --git a/internal/har2case/core.go b/internal/har2case/core.go index b385efc7..b2f33f10 100644 --- a/internal/har2case/core.go +++ b/internal/har2case/core.go @@ -114,7 +114,7 @@ func (h *har) load() (*Har, error) { func (h *har) prepareConfig() *hrp.TConfig { return hrp.NewConfig("testcase description"). - SetVerifySSL(false).ToStruct() + SetVerifySSL(false) } func (h *har) prepareTestSteps() ([]*hrp.TStep, error) { diff --git a/internal/scaffold/demo_test.go b/internal/scaffold/demo_test.go index be0b66f6..9e5dd5e9 100644 --- a/internal/scaffold/demo_test.go +++ b/internal/scaffold/demo_test.go @@ -46,7 +46,7 @@ func TestExampleDemo(t *testing.T) { buildHashicorpPlugin() defer removeHashicorpPlugin() - demoTestCase.Config.ToStruct().Path = "../../examples/debugtalk.bin" + demoTestCase.Config.Path = "../../examples/debugtalk.bin" err := hrp.NewRunner(nil).Run(demoTestCase) // hrp.Run(demoTestCase) if err != nil { t.Fail() diff --git a/models.go b/models.go index ebd254b1..e5af8c05 100644 --- a/models.go +++ b/models.go @@ -156,13 +156,6 @@ type TCase struct { TestSteps []*TStep `json:"teststeps" yaml:"teststeps"` } -// IConfig represents interface for testcase config, -// includes Config. -type IConfig interface { - Name() string - ToStruct() *TConfig -} - // IStep represents interface for all types for teststeps, includes: // StepRequest, StepRequestWithOptionalArgs, StepRequestValidation, StepRequestExtraction, // StepTestCaseWithOptionalArgs, @@ -183,7 +176,7 @@ type ITestCase interface { // TestCase is a container for one testcase, which is used for testcase runner. // TestCase implements ITestCase interface. type TestCase struct { - Config IConfig + Config *TConfig TestSteps []IStep } @@ -193,7 +186,7 @@ func (tc *TestCase) ToTestCase() (*TestCase, error) { func (tc *TestCase) ToTCase() (*TCase, error) { tCase := TCase{ - Config: tc.Config.ToStruct(), + Config: tc.Config, } for _, step := range tc.TestSteps { tCase.TestSteps = append(tCase.TestSteps, step.ToStruct()) diff --git a/plugin/common/init.go b/plugin/common/init.go index aa27856e..e8f757bf 100644 --- a/plugin/common/init.go +++ b/plugin/common/init.go @@ -137,7 +137,7 @@ func (p *HashicorpPlugin) Call(funcName string, args ...interface{}) (interface{ func (p *HashicorpPlugin) Quit() error { // kill hashicorp plugin process - log.Warn().Msg("quit hashicorp plugin process") + log.Info().Msg("quit hashicorp plugin process") pluginHost.Quit() return nil } diff --git a/runner.go b/runner.go index d995f297..0c1e01eb 100644 --- a/runner.go +++ b/runner.go @@ -102,7 +102,7 @@ func (r *HRPRunner) Run(testcases ...ITestCase) error { log.Error().Err(err).Msg("[Run] convert ITestCase interface to TestCase struct failed") return err } - cfg := testcase.Config.ToStruct() + cfg := testcase.Config // parse config parameters err = initParameterIterator(cfg, "runner") if err != nil { @@ -160,10 +160,9 @@ func (r *caseRunner) reset() *caseRunner { func (r *caseRunner) run() error { config := r.TestCase.Config - cfg := config.ToStruct() // init plugin var err error - if r.parser.plugin, err = initPlugin(cfg.Path); err != nil { + if r.parser.plugin, err = initPlugin(config.Path); err != nil { return err } defer func() { @@ -171,14 +170,14 @@ func (r *caseRunner) run() error { r.parser.plugin.Quit() } }() - if err := r.parseConfig(cfg); err != nil { + if err := r.parseConfig(config); err != nil { return err } - log.Info().Str("testcase", config.Name()).Msg("run testcase start") + log.Info().Str("testcase", config.Name).Msg("run testcase start") r.startTime = time.Now() for index := range r.TestCase.TestSteps { - _, err := r.runStep(index, cfg) + _, err := r.runStep(index, config) if err != nil { if r.hrpRunner.failfast { return errors.Wrap(err, "abort running due to failfast setting") @@ -186,7 +185,7 @@ func (r *caseRunner) run() error { } } - log.Info().Str("testcase", config.Name()).Msg("run testcase end") + log.Info().Str("testcase", config.Name).Msg("run testcase end") return nil } diff --git a/step.go b/step.go index dc7b3bc3..12fe3217 100644 --- a/step.go +++ b/step.go @@ -3,66 +3,49 @@ package hrp import "fmt" // NewConfig returns a new constructed testcase config with specified testcase name. -func NewConfig(name string) *Config { - return &Config{ - cfg: &TConfig{ - Name: name, - Variables: make(map[string]interface{}), - }, +func NewConfig(name string) *TConfig { + return &TConfig{ + Name: name, + Variables: make(map[string]interface{}), } } -// Config implements IConfig interface. -type Config struct { - cfg *TConfig -} - // WithVariables sets variables for current testcase. -func (c *Config) WithVariables(variables map[string]interface{}) *Config { - c.cfg.Variables = variables +func (c *TConfig) WithVariables(variables map[string]interface{}) *TConfig { + c.Variables = variables return c } // SetBaseURL sets base URL for current testcase. -func (c *Config) SetBaseURL(baseURL string) *Config { - c.cfg.BaseURL = baseURL +func (c *TConfig) SetBaseURL(baseURL string) *TConfig { + c.BaseURL = baseURL return c } // SetVerifySSL sets whether to verify SSL for current testcase. -func (c *Config) SetVerifySSL(verify bool) *Config { - c.cfg.Verify = verify +func (c *TConfig) SetVerifySSL(verify bool) *TConfig { + c.Verify = verify return c } // WithParameters sets parameters for current testcase. -func (c *Config) WithParameters(parameters map[string]interface{}) *Config { - c.cfg.Parameters = parameters +func (c *TConfig) WithParameters(parameters map[string]interface{}) *TConfig { + c.Parameters = parameters return c } // ExportVars specifies variable names to export for current testcase. -func (c *Config) ExportVars(vars ...string) *Config { - c.cfg.Export = vars +func (c *TConfig) ExportVars(vars ...string) *TConfig { + c.Export = vars return c } // SetWeight sets weight for current testcase, which is used in load testing. -func (c *Config) SetWeight(weight int) *Config { - c.cfg.Weight = weight +func (c *TConfig) SetWeight(weight int) *TConfig { + c.Weight = weight return c } -// Name returns config name, this implements IConfig interface. -func (c *Config) Name() string { - return c.cfg.Name -} - -// ToStruct returns *TConfig, this implements IConfig interface. -func (c *Config) ToStruct() *TConfig { - return c.cfg -} - // NewStep returns a new constructed teststep with specified step name. func NewStep(name string) *StepRequest { return &StepRequest{ @@ -312,7 +295,7 @@ func (s *StepTestCaseWithOptionalArgs) Name() string { if s.step.Name != "" { return s.step.Name } - return s.step.TestCase.Config.Name() + return s.step.TestCase.Config.Name } func (s *StepTestCaseWithOptionalArgs) Type() string { diff --git a/step_test.go b/step_test.go index d7e3ee17..5eb6427f 100644 --- a/step_test.go +++ b/step_test.go @@ -79,10 +79,10 @@ func TestRunRequestRun(t *testing.T) { TestSteps: []IStep{stepGET, stepPOSTData}, } runner := NewRunner(t).SetDebug(true).newCaseRunner(testcase) - if _, err := runner.runStep(0, testcase.Config.ToStruct()); err != nil { + if _, err := runner.runStep(0, testcase.Config); err != nil { t.Fatalf("tStep.Run() error: %s", err) } - if _, err := runner.runStep(1, testcase.Config.ToStruct()); err != nil { + if _, err := runner.runStep(1, testcase.Config); err != nil { t.Fatalf("tStepPOSTData.Run() error: %s", err) } }