1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16package manual 17 18import ( 19 "context" 20 "fmt" 21 "fotff/tester" 22 "fotff/utils" 23 "github.com/sirupsen/logrus" 24 "math/rand" 25 "strings" 26 "sync" 27 "time" 28) 29 30type Tester struct { 31 ResultLock sync.Mutex 32} 33 34func init() { 35 rand.Seed(time.Now().UnixNano()) 36} 37 38func NewTester() tester.Tester { 39 ret := &Tester{} 40 utils.ParseFromConfigFile("manual", ret) 41 return ret 42} 43 44func (t *Tester) TaskName() string { 45 return "manual_test" 46} 47 48func (t *Tester) Prepare(pkgDir string, device string, ctx context.Context) error { 49 return nil 50} 51 52func (t *Tester) DoTestTask(deviceSN string, ctx context.Context) (ret []tester.Result, err error) { 53 return t.DoTestCases(deviceSN, []string{"default"}, ctx) 54} 55 56func (t *Tester) DoTestCase(deviceSN, testCase string, ctx context.Context) (ret tester.Result, err error) { 57 if deviceSN == "" { 58 deviceSN = "default" 59 } 60 t.ResultLock.Lock() 61 defer t.ResultLock.Unlock() 62 var answer string 63 for { 64 fmt.Printf("please do testcase %s on device %s manually and type the test result, 'pass' or 'fail':\n", testCase, deviceSN) 65 if _, err := fmt.Scanln(&answer); err != nil { 66 logrus.Errorf("failed to scan result: %v", err) 67 continue 68 } 69 switch strings.ToUpper(strings.TrimSpace(answer)) { 70 case "PASS": 71 return tester.Result{TestCaseName: testCase, Status: tester.ResultPass}, nil 72 case "FAIL": 73 return tester.Result{TestCaseName: testCase, Status: tester.ResultFail}, nil 74 default: 75 } 76 } 77} 78 79func (t *Tester) DoTestCases(deviceSN string, testcases []string, ctx context.Context) (ret []tester.Result, err error) { 80 for _, testcase := range testcases { 81 r, err := t.DoTestCase(deviceSN, testcase, ctx) 82 if err != nil { 83 return nil, err 84 } 85 ret = append(ret, r) 86 } 87 return ret, nil 88} 89