From a4c84f3de25c412a4db0dba1137714403c8bd935 Mon Sep 17 00:00:00 2001 From: mujingrong Date: Thu, 20 Apr 2023 17:12:07 +0800 Subject: [PATCH 1/2] add test command for fotff Signed-off-by: mujingrong --- tools/fotff/main.go | 28 +++++++++++++- tools/fotff/rec/flashandtest.go | 66 +++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 tools/fotff/rec/flashandtest.go diff --git a/tools/fotff/main.go b/tools/fotff/main.go index 4bb156f..25f3d2f 100644 --- a/tools/fotff/main.go +++ b/tools/fotff/main.go @@ -64,7 +64,8 @@ func init() { } runCmd := initRunCmd(m, t) flashCmd := initFlashCmd(m) - rootCmd.AddCommand(runCmd, flashCmd) + testCmd := initTestCmd(m, t) + rootCmd.AddCommand(runCmd, flashCmd, testCmd) } func initRunCmd(m pkg.Manager, t tester.Tester) *cobra.Command { @@ -100,6 +101,31 @@ func initFlashCmd(m pkg.Manager) *cobra.Command { return flashCmd } +func initTestCmd(m pkg.Manager, t tester.Tester) *cobra.Command { + var targetPkg, device, testCase string + testCmd := &cobra.Command{ + Use: "test", + Short: "build and flash and test the given package on the specified device", + RunE: func(cmd *cobra.Command, args []string) error { + opt := &rec.FlashAndTestOptions{ + M: m, + T: t, + Version: targetPkg, + Device: device, + TestCase: testCase, + } + return rec.FlashAndTest(context.TODO(), opt) + }, + } + testCmd.PersistentFlags().StringVarP(&targetPkg, "package", "p", "", "package directory") + testCmd.PersistentFlags().StringVarP(&device, "device", "d", "", "target device sn") + testCmd.PersistentFlags().StringVarP(&testCase, "testcase", "t", "", "test case to run") + testCmd.MarkPersistentFlagRequired("package") + testCmd.MarkPersistentFlagRequired("device") + + return testCmd +} + func main() { utils.EnablePprof() if err := rootCmd.Execute(); err != nil { diff --git a/tools/fotff/rec/flashandtest.go b/tools/fotff/rec/flashandtest.go new file mode 100644 index 0000000..8eff793 --- /dev/null +++ b/tools/fotff/rec/flashandtest.go @@ -0,0 +1,66 @@ +package rec + +import ( + "context" + "fotff/pkg" + "fotff/tester" + + "github.com/sirupsen/logrus" +) + +// FlashAndTestOptions specify which pkg.Manager and which tester to use to flash and test the specified version +type FlashAndTestOptions struct { + M pkg.Manager + T tester.Tester + Version string + Device string + TestCase string +} + +// FlashAndTest build and flash the given version to the specified device, then run the specified test cases +func FlashAndTest(ctx context.Context, opt *FlashAndTestOptions) error { + result, err := doFlashAndTest(ctx, opt) + if err != nil { + return err + } + logrus.Infof("Flash and test for version %s and test case %s done, result: %s", opt.Version, opt.TestCase, result) + return nil +} + +func doFlashAndTest(ctx context.Context, opt *FlashAndTestOptions) (tester.ResultStatus, error) { + // flash the specified version to the specified device + if err := opt.M.Flash(opt.Device, opt.Version, ctx); err != nil { + logrus.Errorf("Failed to flash version %s to device %s, error: %s", opt.Version, opt.Device, err.Error()) + return tester.ResultFail, err + } + // prepare and run the specified test + if err := opt.T.Prepare(opt.M.PkgDir(opt.Version), opt.Device, ctx); err != nil { + logrus.Errorf("Failed to prepare test, error: %s", err.Error()) + return tester.ResultFail, err + } + if opt.TestCase == "" { + // run all test cases if the --testcase argument was not present + results, err := opt.T.DoTestTask(opt.Device, ctx) + if err != nil { + logrus.Errorf("Failed to run all test cases on device %s, error: %s", opt.Device, err.Error()) + return tester.ResultFail, err + } + // only mark test result as pass when all test cases passed + var ret tester.ResultStatus = tester.ResultPass + for _, r := range results { + logrus.Infof("Result for test case %s is %s", r.TestCaseName, r.Status) + if r.Status == tester.ResultFail { + ret = tester.ResultFail + } + } + return ret, nil + } else { + // otherwise run the specified test case + result, err := opt.T.DoTestCase(opt.Device, opt.TestCase, ctx) + if err != nil { + logrus.Errorf("Failed to run test case %s on device %s, error: %s", opt.TestCase, opt.Device, err.Error()) + return tester.ResultFail, err + } + return result.Status, nil + } +} -- Gitee From 1bedd225aa99345e3aff97d3266bae47aa95a1f3 Mon Sep 17 00:00:00 2001 From: mujingrong Date: Thu, 20 Apr 2023 17:35:33 +0800 Subject: [PATCH 2/2] refactor test command Signed-off-by: mujingrong --- tools/fotff/rec/flashandtest.go | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/tools/fotff/rec/flashandtest.go b/tools/fotff/rec/flashandtest.go index 8eff793..523270d 100644 --- a/tools/fotff/rec/flashandtest.go +++ b/tools/fotff/rec/flashandtest.go @@ -19,48 +19,46 @@ type FlashAndTestOptions struct { // FlashAndTest build and flash the given version to the specified device, then run the specified test cases func FlashAndTest(ctx context.Context, opt *FlashAndTestOptions) error { - result, err := doFlashAndTest(ctx, opt) - if err != nil { - return err - } - logrus.Infof("Flash and test for version %s and test case %s done, result: %s", opt.Version, opt.TestCase, result) - return nil -} - -func doFlashAndTest(ctx context.Context, opt *FlashAndTestOptions) (tester.ResultStatus, error) { // flash the specified version to the specified device if err := opt.M.Flash(opt.Device, opt.Version, ctx); err != nil { logrus.Errorf("Failed to flash version %s to device %s, error: %s", opt.Version, opt.Device, err.Error()) - return tester.ResultFail, err + return err } + // prepare and run the specified test if err := opt.T.Prepare(opt.M.PkgDir(opt.Version), opt.Device, ctx); err != nil { logrus.Errorf("Failed to prepare test, error: %s", err.Error()) - return tester.ResultFail, err + return err } + if opt.TestCase == "" { // run all test cases if the --testcase argument was not present results, err := opt.T.DoTestTask(opt.Device, ctx) if err != nil { logrus.Errorf("Failed to run all test cases on device %s, error: %s", opt.Device, err.Error()) - return tester.ResultFail, err + return err } + // only mark test result as pass when all test cases passed - var ret tester.ResultStatus = tester.ResultPass + var result tester.ResultStatus = tester.ResultPass for _, r := range results { logrus.Infof("Result for test case %s is %s", r.TestCaseName, r.Status) if r.Status == tester.ResultFail { - ret = tester.ResultFail + result = tester.ResultFail } } - return ret, nil + + logrus.Infof("Flash and test for all test cases done, result: %s", result) + return nil } else { // otherwise run the specified test case result, err := opt.T.DoTestCase(opt.Device, opt.TestCase, ctx) if err != nil { logrus.Errorf("Failed to run test case %s on device %s, error: %s", opt.TestCase, opt.Device, err.Error()) - return tester.ResultFail, err + return err } - return result.Status, nil + + logrus.Infof("Flash and test for test case %s done, result: %s", opt.TestCase, result) + return nil } } -- Gitee