1package rec 2 3import ( 4 "context" 5 "fotff/pkg" 6 "fotff/tester" 7 8 "github.com/sirupsen/logrus" 9) 10 11// FlashAndTestOptions specify which pkg.Manager and which tester to use to flash and test the specified version 12type FlashAndTestOptions struct { 13 M pkg.Manager 14 T tester.Tester 15 Version string 16 Device string 17 TestCase string 18} 19 20// FlashAndTest build and flash the given version to the specified device, then run the specified test cases 21func FlashAndTest(ctx context.Context, opt *FlashAndTestOptions) error { 22 // flash the specified version to the specified device 23 if err := opt.M.Flash(opt.Device, opt.Version, ctx); err != nil { 24 logrus.Errorf("Failed to flash version %s to device %s, error: %s", opt.Version, opt.Device, err.Error()) 25 return err 26 } 27 28 // prepare and run the specified test 29 if err := opt.T.Prepare(opt.M.PkgDir(opt.Version), opt.Device, ctx); err != nil { 30 logrus.Errorf("Failed to prepare test, error: %s", err.Error()) 31 return err 32 } 33 34 if opt.TestCase == "" { 35 // run all test cases if the --testcase argument was not present 36 results, err := opt.T.DoTestTask(opt.Device, ctx) 37 if err != nil { 38 logrus.Errorf("Failed to run all test cases on device %s, error: %s", opt.Device, err.Error()) 39 return err 40 } 41 42 // only mark test result as pass when all test cases passed 43 var result tester.ResultStatus = tester.ResultPass 44 for _, r := range results { 45 logrus.Infof("Result for test case %s is %s", r.TestCaseName, r.Status) 46 if r.Status == tester.ResultFail { 47 result = tester.ResultFail 48 } 49 } 50 51 logrus.Infof("Flash and test for all test cases done, result: %s", result) 52 return nil 53 } else { 54 // otherwise run the specified test case 55 result, err := opt.T.DoTestCase(opt.Device, opt.TestCase, ctx) 56 if err != nil { 57 logrus.Errorf("Failed to run test case %s on device %s, error: %s", opt.TestCase, opt.Device, err.Error()) 58 return err 59 } 60 61 logrus.Infof("Flash and test for test case %s done, result: %s", opt.TestCase, result) 62 return nil 63 } 64} 65