refactor: remove IConfig

This commit is contained in:
debugtalk
2022-01-19 21:42:12 +08:00
parent 72d4a988aa
commit 743fc706c6
14 changed files with 39 additions and 64 deletions
+3 -3
View File
@@ -51,7 +51,7 @@ func (b *HRPBoomer) Run(testcases ...ITestCase) {
if err != nil { if err != nil {
panic(err) panic(err)
} }
cfg := testcase.Config.ToStruct() cfg := testcase.Config
err = initParameterIterator(cfg, "boomer") err = initParameterIterator(cfg, "boomer")
if err != nil { if err != nil {
panic(err) panic(err)
@@ -74,7 +74,7 @@ func (b *HRPBoomer) Quit() {
func (b *HRPBoomer) convertBoomerTask(testcase *TestCase) *boomer.Task { func (b *HRPBoomer) convertBoomerTask(testcase *TestCase) *boomer.Task {
hrpRunner := NewRunner(nil).SetDebug(b.debug) hrpRunner := NewRunner(nil).SetDebug(b.debug)
config := testcase.Config.ToStruct() config := testcase.Config
// each testcase has its own plugin process // each testcase has its own plugin process
plugin, _ := initPlugin(config.Path) plugin, _ := initPlugin(config.Path)
@@ -94,7 +94,7 @@ func (b *HRPBoomer) convertBoomerTask(testcase *TestCase) *boomer.Task {
testcaseSuccess := true // flag whole testcase result testcaseSuccess := true // flag whole testcase result
var transactionSuccess = true // flag current transaction result var transactionSuccess = true // flag current transaction result
cfg := testcase.Config.ToStruct() cfg := testcase.Config
caseConfig := &TConfig{} caseConfig := &TConfig{}
// copy config to avoid data racing // copy config to avoid data racing
if err := copier.Copy(caseConfig, cfg); err != nil { if err := copier.Copy(caseConfig, cfg); err != nil {
+1 -1
View File
@@ -96,7 +96,7 @@ func loadFromYAML(path string) (*TCase, error) {
func (tc *TCase) ToTestCase() (*TestCase, error) { func (tc *TCase) ToTestCase() (*TestCase, error) {
testCase := &TestCase{ testCase := &TestCase{
Config: &Config{cfg: tc.Config}, Config: tc.Config,
} }
for _, step := range tc.TestSteps { for _, step := range tc.TestSteps {
if step.Request != nil { if step.Request != nil {
+1 -1
View File
@@ -33,4 +33,4 @@ Copyright 2021 debugtalk
* [hrp run](hrp_run.md) - run API test * [hrp run](hrp_run.md) - run API test
* [hrp startproject](hrp_startproject.md) - create a scaffold project * [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
+1 -1
View File
@@ -39,4 +39,4 @@ hrp boom [flags]
* [hrp](hrp.md) - One-stop solution for HTTP(S) testing. * [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
+1 -1
View File
@@ -23,4 +23,4 @@ hrp har2case $har_path... [flags]
* [hrp](hrp.md) - One-stop solution for HTTP(S) testing. * [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
+1 -1
View File
@@ -31,4 +31,4 @@ hrp run $path... [flags]
* [hrp](hrp.md) - One-stop solution for HTTP(S) testing. * [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
+1 -1
View File
@@ -16,4 +16,4 @@ hrp startproject $project_name [flags]
* [hrp](hrp.md) - One-stop solution for HTTP(S) testing. * [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
+1 -1
View File
@@ -114,7 +114,7 @@ func (h *har) load() (*Har, error) {
func (h *har) prepareConfig() *hrp.TConfig { func (h *har) prepareConfig() *hrp.TConfig {
return hrp.NewConfig("testcase description"). return hrp.NewConfig("testcase description").
SetVerifySSL(false).ToStruct() SetVerifySSL(false)
} }
func (h *har) prepareTestSteps() ([]*hrp.TStep, error) { func (h *har) prepareTestSteps() ([]*hrp.TStep, error) {
+1 -1
View File
@@ -46,7 +46,7 @@ func TestExampleDemo(t *testing.T) {
buildHashicorpPlugin() buildHashicorpPlugin()
defer removeHashicorpPlugin() defer removeHashicorpPlugin()
demoTestCase.Config.ToStruct().Path = "../../examples/debugtalk.bin" demoTestCase.Config.Path = "../../examples/debugtalk.bin"
err := hrp.NewRunner(nil).Run(demoTestCase) // hrp.Run(demoTestCase) err := hrp.NewRunner(nil).Run(demoTestCase) // hrp.Run(demoTestCase)
if err != nil { if err != nil {
t.Fail() t.Fail()
+2 -9
View File
@@ -156,13 +156,6 @@ type TCase struct {
TestSteps []*TStep `json:"teststeps" yaml:"teststeps"` 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: // IStep represents interface for all types for teststeps, includes:
// StepRequest, StepRequestWithOptionalArgs, StepRequestValidation, StepRequestExtraction, // StepRequest, StepRequestWithOptionalArgs, StepRequestValidation, StepRequestExtraction,
// StepTestCaseWithOptionalArgs, // StepTestCaseWithOptionalArgs,
@@ -183,7 +176,7 @@ type ITestCase interface {
// TestCase is a container for one testcase, which is used for testcase runner. // TestCase is a container for one testcase, which is used for testcase runner.
// TestCase implements ITestCase interface. // TestCase implements ITestCase interface.
type TestCase struct { type TestCase struct {
Config IConfig Config *TConfig
TestSteps []IStep TestSteps []IStep
} }
@@ -193,7 +186,7 @@ func (tc *TestCase) ToTestCase() (*TestCase, error) {
func (tc *TestCase) ToTCase() (*TCase, error) { func (tc *TestCase) ToTCase() (*TCase, error) {
tCase := TCase{ tCase := TCase{
Config: tc.Config.ToStruct(), Config: tc.Config,
} }
for _, step := range tc.TestSteps { for _, step := range tc.TestSteps {
tCase.TestSteps = append(tCase.TestSteps, step.ToStruct()) tCase.TestSteps = append(tCase.TestSteps, step.ToStruct())
+1 -1
View File
@@ -137,7 +137,7 @@ func (p *HashicorpPlugin) Call(funcName string, args ...interface{}) (interface{
func (p *HashicorpPlugin) Quit() error { func (p *HashicorpPlugin) Quit() error {
// kill hashicorp plugin process // kill hashicorp plugin process
log.Warn().Msg("quit hashicorp plugin process") log.Info().Msg("quit hashicorp plugin process")
pluginHost.Quit() pluginHost.Quit()
return nil return nil
} }
+6 -7
View File
@@ -102,7 +102,7 @@ func (r *HRPRunner) Run(testcases ...ITestCase) error {
log.Error().Err(err).Msg("[Run] convert ITestCase interface to TestCase struct failed") log.Error().Err(err).Msg("[Run] convert ITestCase interface to TestCase struct failed")
return err return err
} }
cfg := testcase.Config.ToStruct() cfg := testcase.Config
// parse config parameters // parse config parameters
err = initParameterIterator(cfg, "runner") err = initParameterIterator(cfg, "runner")
if err != nil { if err != nil {
@@ -160,10 +160,9 @@ func (r *caseRunner) reset() *caseRunner {
func (r *caseRunner) run() error { func (r *caseRunner) run() error {
config := r.TestCase.Config config := r.TestCase.Config
cfg := config.ToStruct()
// init plugin // init plugin
var err error 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 return err
} }
defer func() { defer func() {
@@ -171,14 +170,14 @@ func (r *caseRunner) run() error {
r.parser.plugin.Quit() r.parser.plugin.Quit()
} }
}() }()
if err := r.parseConfig(cfg); err != nil { if err := r.parseConfig(config); err != nil {
return err 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() r.startTime = time.Now()
for index := range r.TestCase.TestSteps { for index := range r.TestCase.TestSteps {
_, err := r.runStep(index, cfg) _, err := r.runStep(index, config)
if err != nil { if err != nil {
if r.hrpRunner.failfast { if r.hrpRunner.failfast {
return errors.Wrap(err, "abort running due to failfast setting") 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 return nil
} }
+17 -34
View File
@@ -3,66 +3,49 @@ package hrp
import "fmt" import "fmt"
// NewConfig returns a new constructed testcase config with specified testcase name. // NewConfig returns a new constructed testcase config with specified testcase name.
func NewConfig(name string) *Config { func NewConfig(name string) *TConfig {
return &Config{ return &TConfig{
cfg: &TConfig{ Name: name,
Name: name, Variables: make(map[string]interface{}),
Variables: make(map[string]interface{}),
},
} }
} }
// Config implements IConfig interface.
type Config struct {
cfg *TConfig
}
// WithVariables sets variables for current testcase. // WithVariables sets variables for current testcase.
func (c *Config) WithVariables(variables map[string]interface{}) *Config { func (c *TConfig) WithVariables(variables map[string]interface{}) *TConfig {
c.cfg.Variables = variables c.Variables = variables
return c return c
} }
// SetBaseURL sets base URL for current testcase. // SetBaseURL sets base URL for current testcase.
func (c *Config) SetBaseURL(baseURL string) *Config { func (c *TConfig) SetBaseURL(baseURL string) *TConfig {
c.cfg.BaseURL = baseURL c.BaseURL = baseURL
return c return c
} }
// SetVerifySSL sets whether to verify SSL for current testcase. // SetVerifySSL sets whether to verify SSL for current testcase.
func (c *Config) SetVerifySSL(verify bool) *Config { func (c *TConfig) SetVerifySSL(verify bool) *TConfig {
c.cfg.Verify = verify c.Verify = verify
return c return c
} }
// WithParameters sets parameters for current testcase. // WithParameters sets parameters for current testcase.
func (c *Config) WithParameters(parameters map[string]interface{}) *Config { func (c *TConfig) WithParameters(parameters map[string]interface{}) *TConfig {
c.cfg.Parameters = parameters c.Parameters = parameters
return c return c
} }
// ExportVars specifies variable names to export for current testcase. // ExportVars specifies variable names to export for current testcase.
func (c *Config) ExportVars(vars ...string) *Config { func (c *TConfig) ExportVars(vars ...string) *TConfig {
c.cfg.Export = vars c.Export = vars
return c return c
} }
// SetWeight sets weight for current testcase, which is used in load testing. // SetWeight sets weight for current testcase, which is used in load testing.
func (c *Config) SetWeight(weight int) *Config { func (c *TConfig) SetWeight(weight int) *TConfig {
c.cfg.Weight = weight c.Weight = weight
return c 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. // NewStep returns a new constructed teststep with specified step name.
func NewStep(name string) *StepRequest { func NewStep(name string) *StepRequest {
return &StepRequest{ return &StepRequest{
@@ -312,7 +295,7 @@ func (s *StepTestCaseWithOptionalArgs) Name() string {
if s.step.Name != "" { if s.step.Name != "" {
return s.step.Name return s.step.Name
} }
return s.step.TestCase.Config.Name() return s.step.TestCase.Config.Name
} }
func (s *StepTestCaseWithOptionalArgs) Type() string { func (s *StepTestCaseWithOptionalArgs) Type() string {
+2 -2
View File
@@ -79,10 +79,10 @@ func TestRunRequestRun(t *testing.T) {
TestSteps: []IStep{stepGET, stepPOSTData}, TestSteps: []IStep{stepGET, stepPOSTData},
} }
runner := NewRunner(t).SetDebug(true).newCaseRunner(testcase) 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) 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) t.Fatalf("tStepPOSTData.Run() error: %s", err)
} }
} }