From 044a40b2abbc29668fc4ec49c52cbd285b4275cd Mon Sep 17 00:00:00 2001 From: noellemu Date: Fri, 7 Apr 2023 16:10:16 +0800 Subject: [PATCH 1/2] Add common tester for fotff Signed-off-by: mujingrong --- tools/fotff/main.go | 7 +- tools/fotff/tester/common/common.go | 143 ++++++++++++++++++++++++++++ 2 files changed, 148 insertions(+), 2 deletions(-) create mode 100644 tools/fotff/tester/common/common.go diff --git a/tools/fotff/main.go b/tools/fotff/main.go index 2c7d73d..a5f4fde 100644 --- a/tools/fotff/main.go +++ b/tools/fotff/main.go @@ -23,15 +23,17 @@ import ( "fotff/rec" "fotff/res" "fotff/tester" + "fotff/tester/common" "fotff/tester/manual" testermock "fotff/tester/mock" "fotff/tester/smoke" "fotff/tester/xdevice" "fotff/utils" - "github.com/sirupsen/logrus" - "github.com/spf13/cobra" "os" "path/filepath" + + "github.com/sirupsen/logrus" + "github.com/spf13/cobra" ) var newPkgMgrFuncs = map[string]pkg.NewFunc{ @@ -42,6 +44,7 @@ var newPkgMgrFuncs = map[string]pkg.NewFunc{ var newTesterFuncs = map[string]tester.NewFunc{ "mock": testermock.NewTester, "manual": manual.NewTester, + "common": common.NewTester, "xdevice": xdevice.NewTester, "smoke": smoke.NewTester, } diff --git a/tools/fotff/tester/common/common.go b/tools/fotff/tester/common/common.go new file mode 100644 index 0000000..d4f28dc --- /dev/null +++ b/tools/fotff/tester/common/common.go @@ -0,0 +1,143 @@ +package common + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "fotff/tester" + "fotff/utils" + "os" + "path/filepath" + "strings" + + "github.com/sirupsen/logrus" +) + +const ( + testResultPass = "pass" + testCaseFlag = "--test-case" + deviceFlag = "--device" +) + +// Tester is the common tester for most kinds of tests +type Tester struct { + Tool string `key:"tool"` + Param string `key:"param"` + ResultPath string `key:"result_path"` + ResultFile string `key:"result_file"` +} + +// TestResult is the structure of the test result json file +type TestResult struct { + TestCase string `json:"test_case"` + Result string `json:"result"` +} + +func NewTester() tester.Tester { + t := &Tester{} + utils.ParseFromConfigFile("common", t) + return t +} + +func (t *Tester) TaskName() string { + return "common_tester" +} + +func (t *Tester) Prepare(version string, device string, ctx context.Context) error { + return nil +} + +// DoTestTask run all test cases on the specified device +func (t *Tester) DoTestTask(device string, ctx context.Context) ([]tester.Result, error) { + args := strings.Split(t.Param, " ") + if device != "" { + args = append(args, []string{deviceFlag, device}...) + } + if err := utils.ExecContext(ctx, t.Tool, args...); err != nil { + if errors.Is(err, context.Canceled) { + return nil, err + } + logrus.Errorf("Failed to do test task on device %s, error: %s", device, err.Error()) + return nil, err + } + + return t.processResult() +} + +// DoTestCase run the specified test case on the specified device +func (t *Tester) DoTestCase(device string, testCase string, ctx context.Context) (tester.Result, error) { + args := strings.Split(t.Param, " ") + args = append(args, testCaseFlag) + args = append(args, testCase) + if device != "" { + args = append(args, []string{deviceFlag, device}...) + } + defaultResult := tester.Result{} + if err := utils.ExecContext(ctx, t.Tool, args...); err != nil { + if errors.Is(err, context.Canceled) { + return defaultResult, err + } + logrus.Errorf("Failed to do test case %s on device %s, error: %s", testCase, device, err.Error()) + return defaultResult, err + } + + rs, err := t.processResult() + if err != nil { + return defaultResult, err + } + if len(rs) == 0 { + return defaultResult, fmt.Errorf("failed to process test result: no test result found") + } + if rs[0].TestCaseName != testCase { + return defaultResult, fmt.Errorf("failed to process test result: no matched test result found") + } + + logrus.Infof("test case %s on device %s finished, the result is %s", testCase, device, rs[0].Status) + return rs[0], nil +} + +// DoTestCases run the specified test cases on the specified device +func (t *Tester) DoTestCases(device string, testCases []string, ctx context.Context) ([]tester.Result, error) { + args := strings.Split(t.Param, " ") + args = append(args, testCaseFlag) + args = append(args, testCases...) + if device != "" { + args = append(args, []string{deviceFlag, device}...) + } + if err := utils.ExecContext(ctx, t.Tool, args...); err != nil { + if errors.Is(err, context.Canceled) { + return nil, err + } + logrus.Errorf("Failed to do test cases %v on device %s, error: %s", testCases, device, err.Error()) + return nil, err + } + + return t.processResult() +} + +// processResult parse the test result file +func (t *Tester) processResult() ([]tester.Result, error) { + resultFile := filepath.Join(t.ResultPath, t.ResultFile) + data, err := os.ReadFile(resultFile) + if err != nil { + logrus.Errorf("Failed to read from result file %s, error: %s", resultFile, err.Error()) + return nil, err + } + + var result []TestResult + if err := json.Unmarshal(data, &result); err != nil { + logrus.Errorf("Failed to unmarshal test result %s into json array, error: %s", string(data), err.Error()) + return nil, err + } + + var ret []tester.Result + for _, r := range result { + if r.Result == testResultPass { + ret = append(ret, tester.Result{TestCaseName: r.TestCase, Status: tester.ResultPass}) + } else { + ret = append(ret, tester.Result{TestCaseName: r.TestCase, Status: tester.ResultFail}) + } + } + return ret, nil +} -- Gitee From a2cfaa84be60cca19df31af3e55a0dd7903a17db Mon Sep 17 00:00:00 2001 From: mujingrong Date: Mon, 10 Apr 2023 11:53:52 +0800 Subject: [PATCH 2/2] standardize args for common tester Signed-off-by: mujingrong --- tools/fotff/tester/common/common.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tools/fotff/tester/common/common.go b/tools/fotff/tester/common/common.go index d4f28dc..d5fbd18 100644 --- a/tools/fotff/tester/common/common.go +++ b/tools/fotff/tester/common/common.go @@ -68,8 +68,7 @@ func (t *Tester) DoTestTask(device string, ctx context.Context) ([]tester.Result // DoTestCase run the specified test case on the specified device func (t *Tester) DoTestCase(device string, testCase string, ctx context.Context) (tester.Result, error) { args := strings.Split(t.Param, " ") - args = append(args, testCaseFlag) - args = append(args, testCase) + args = append(args, []string{testCaseFlag, testCase}...) if device != "" { args = append(args, []string{deviceFlag, device}...) } -- Gitee