diff --git a/tools/fotff/main.go b/tools/fotff/main.go index 4bb156fcab741f63635b392ee9dc462e172049cd..25f3d2fcc1aa5b8eb972302d4d00a44b8bc6e4be 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 0000000000000000000000000000000000000000..523270dd6831b198d67625b12fc4a7133c8de4b8 --- /dev/null +++ b/tools/fotff/rec/flashandtest.go @@ -0,0 +1,64 @@ +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 { + // 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 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 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 err + } + + // only mark test result as pass when all test cases passed + 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 { + result = tester.ResultFail + } + } + + 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 err + } + + logrus.Infof("Flash and test for test case %s done, result: %s", opt.TestCase, result) + return nil + } +}