refactor: simplify ITestCase

This commit is contained in:
debugtalk
2022-03-28 20:36:35 +08:00
parent 1e91b8cb0f
commit f5e80989c4
4 changed files with 88 additions and 111 deletions

View File

@@ -90,90 +90,6 @@ func convertCheckExpr(checkExpr string) string {
return strings.Join(checkItems, ".")
}
func (tc *TCase) ToTestCase() (*TestCase, error) {
testCase := &TestCase{
Config: tc.Config,
}
// locate project root dir by plugin path
projectRootDir, err := getProjectRootDirPath(testCase.Config.Path)
if err != nil {
return nil, errors.Wrap(err, "failed to get project root dir")
}
log.Info().Str("dir", projectRootDir).Msg("located project root dir")
for _, step := range tc.TestSteps {
if step.API != nil {
if apiContent, ok := step.API.(*API); ok {
step.API = apiContent
testCase.TestSteps = append(testCase.TestSteps, &StepAPIWithOptionalArgs{
step: step,
})
return testCase, nil
}
// reference api path
var apiFullPath string
if apiPath, ok := step.API.(string); ok {
apiFullPath = filepath.Join(projectRootDir, apiPath)
} else if apiPath, ok := step.API.(*APIPath); ok {
apiFullPath = filepath.Join(projectRootDir, apiPath.GetPath())
} else {
return nil, errors.New("invalid api format")
}
if !builtin.IsFilePathExists(apiFullPath) {
return nil, errors.New("referenced api file not found: " + apiFullPath)
}
refAPI := APIPath(apiFullPath)
apiContent, err := refAPI.ToAPI()
if err != nil {
return nil, err
}
step.API = apiContent
testCase.TestSteps = append(testCase.TestSteps, &StepAPIWithOptionalArgs{
step: step,
})
} else if step.TestCase != nil {
path := filepath.Join(projectRootDir, step.TestCase.(string))
if !builtin.IsFilePathExists(path) {
return nil, errors.New("referenced testcase file not found: " + path)
}
refTestCase := TestCasePath(path)
tc, err := refTestCase.ToTestCase()
if err != nil {
return nil, err
}
step.TestCase = tc
testCase.TestSteps = append(testCase.TestSteps, &StepTestCaseWithOptionalArgs{
step: step,
})
} else if step.ThinkTime != nil {
testCase.TestSteps = append(testCase.TestSteps, &StepThinkTime{
step: step,
})
} else if step.Request != nil {
testCase.TestSteps = append(testCase.TestSteps, &StepRequestWithOptionalArgs{
step: step,
})
} else if step.Transaction != nil {
testCase.TestSteps = append(testCase.TestSteps, &StepTransaction{
step: step,
})
} else if step.Rendezvous != nil {
testCase.TestSteps = append(testCase.TestSteps, &StepRendezvous{
step: step,
})
} else {
log.Warn().Interface("step", step).Msg("[convertTestCase] unexpected step")
}
}
return testCase, nil
}
func getProjectRootDirPath(path string) (rootDir string, err error) {
pluginPath, err := locatePlugin(path)
if err == nil {
@@ -212,6 +128,7 @@ func (path *TestCasePath) GetPath() string {
return fmt.Sprintf("%v", *path)
}
// ToTestCase loads testcase path and convert to *TestCase
func (path *TestCasePath) ToTestCase() (*TestCase, error) {
tc := &TCase{}
casePath := path.GetPath()
@@ -225,17 +142,76 @@ func (path *TestCasePath) ToTestCase() (*TestCase, error) {
return nil, err
}
tc.Config.Path = casePath
testcase, err := tc.ToTestCase()
if err != nil {
return nil, err
}
return testcase, nil
}
func (path *TestCasePath) ToTCase() (*TCase, error) {
testcase, err := path.ToTestCase()
if err != nil {
return nil, err
testCase := &TestCase{
Config: tc.Config,
}
return testcase.ToTCase()
// locate project root dir by plugin path
projectRootDir, err := getProjectRootDirPath(casePath)
if err != nil {
return nil, errors.Wrap(err, "failed to get project root dir")
}
for _, step := range tc.TestSteps {
if step.API != nil {
apiPath, ok := step.API.(string)
if !ok {
return nil, fmt.Errorf("referenced api path should be string, got %v", step.API)
}
path := filepath.Join(projectRootDir, apiPath)
if !builtin.IsFilePathExists(path) {
return nil, errors.New("referenced api file not found: " + path)
}
refAPI := APIPath(path)
apiContent, err := refAPI.ToAPI()
if err != nil {
return nil, err
}
step.API = apiContent
testCase.TestSteps = append(testCase.TestSteps, &StepAPIWithOptionalArgs{
step: step,
})
} else if step.TestCase != nil {
casePath, ok := step.TestCase.(string)
if !ok {
return nil, fmt.Errorf("referenced testcase path should be string, got %v", step.TestCase)
}
path := filepath.Join(projectRootDir, casePath)
if !builtin.IsFilePathExists(path) {
return nil, errors.New("referenced testcase file not found: " + path)
}
refTestCase := TestCasePath(path)
tc, err := refTestCase.ToTestCase()
if err != nil {
return nil, err
}
step.TestCase = tc
testCase.TestSteps = append(testCase.TestSteps, &StepTestCaseWithOptionalArgs{
step: step,
})
} else if step.ThinkTime != nil {
testCase.TestSteps = append(testCase.TestSteps, &StepThinkTime{
step: step,
})
} else if step.Request != nil {
testCase.TestSteps = append(testCase.TestSteps, &StepRequestWithOptionalArgs{
step: step,
})
} else if step.Transaction != nil {
testCase.TestSteps = append(testCase.TestSteps, &StepTransaction{
step: step,
})
} else if step.Rendezvous != nil {
testCase.TestSteps = append(testCase.TestSteps, &StepRendezvous{
step: step,
})
} else {
log.Warn().Interface("step", step).Msg("[convertTestCase] unexpected step")
}
}
return testCase, nil
}

View File

@@ -153,7 +153,7 @@ var demoTestCaseWithoutPlugin = &TestCase{
}
func TestGenDemoTestCase(t *testing.T) {
tCase, _ := demoTestCaseWithPlugin.ToTCase()
tCase := convertTestCase(demoTestCaseWithPlugin)
err := builtin.Dump2JSON(tCase, demoTestCaseWithPluginJSONPath.GetPath())
if err != nil {
t.Fail()
@@ -163,7 +163,7 @@ func TestGenDemoTestCase(t *testing.T) {
t.Fail()
}
tCase, _ = demoTestCaseWithoutPlugin.ToTCase()
tCase = convertTestCase(demoTestCaseWithoutPlugin)
err = builtin.Dump2JSON(tCase, demoTestCaseWithoutPluginJSONPath.GetPath())
if err != nil {
t.Fail()

View File

@@ -291,10 +291,6 @@ type TCase struct {
TestSteps []*TStep `json:"teststeps" yaml:"teststeps"`
}
func (tc *TCase) Path() string {
return tc.Config.Path
}
// IStep represents interface for all types for teststeps, includes:
// StepRequest, StepRequestWithOptionalArgs, StepRequestValidation, StepRequestExtraction,
// StepTestCaseWithOptionalArgs,
@@ -310,7 +306,6 @@ type IStep interface {
type ITestCase interface {
GetPath() string
ToTestCase() (*TestCase, error)
ToTCase() (*TCase, error)
}
// TestCase is a container for one testcase, which is used for testcase runner.
@@ -328,14 +323,14 @@ func (tc *TestCase) ToTestCase() (*TestCase, error) {
return tc, nil
}
func (tc *TestCase) ToTCase() (*TCase, error) {
tCase := TCase{
Config: tc.Config,
func convertTestCase(testcase *TestCase) *TCase {
tCase := &TCase{
Config: testcase.Config,
}
for _, step := range tc.TestSteps {
for _, step := range testcase.TestSteps {
tCase.TestSteps = append(tCase.TestSteps, step.ToStruct())
}
return &tCase, nil
return tCase
}
type testCaseStat struct {

View File

@@ -230,9 +230,14 @@ func loadTestCases(iTestCases ...ITestCase) ([]*TestCase, error) {
// folder path
files, err := ioutil.ReadDir(casePath)
if err != nil {
return nil, errors.Wrap(err, "failed to read dir")
return nil, errors.Wrap(err, "read dir failed")
}
for _, f := range files {
ext := filepath.Ext(f.Name())
if ext != ".yml" && ext != ".yaml" && ext != ".json" {
// ignore non-testcase files
continue
}
path := TestCasePath(filepath.Join(casePath, f.Name()))
casePaths = append(casePaths, &path)
}
@@ -244,7 +249,8 @@ func loadTestCases(iTestCases ...ITestCase) ([]*TestCase, error) {
for _, path := range casePaths {
tc, err := path.ToTestCase()
if err != nil {
continue
log.Error().Err(err).Str("path", path.GetPath()).Msg("load testcase failed")
return nil, errors.Wrap(err, "load testcase failed")
}
testCases = append(testCases, tc)
}
@@ -561,7 +567,7 @@ func (r *caseRunner) runStepRendezvous(rendezvous *Rendezvous) (stepResult *step
}
func (r *caseRunner) isPreRendezvousAllReleased(rendezvous *Rendezvous) bool {
tCase, _ := r.ToTCase()
tCase := convertTestCase(r.TestCase)
for _, step := range tCase.TestSteps {
preRendezvous := step.Rendezvous
if preRendezvous == nil {
@@ -608,7 +614,7 @@ func (r *Rendezvous) setReleased() {
}
func initRendezvous(testcase *TestCase, total int64) []*Rendezvous {
tCase, _ := testcase.ToTCase()
tCase := convertTestCase(testcase)
var rendezvousList []*Rendezvous
for _, step := range tCase.TestSteps {
if step.Rendezvous == nil {