refactor: remove IConfig

This commit is contained in:
debugtalk
2022-01-19 21:42:12 +08:00
parent c024cb662d
commit 184a0e7296
14 changed files with 39 additions and 64 deletions

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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) {

View File

@@ -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()

View File

@@ -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())

View File

@@ -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
}

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")
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
}

51
step.go
View File

@@ -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 {

View File

@@ -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)
}
}