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 pkg_available 17 18import ( 19 "context" 20 "fotff/tester" 21 "github.com/sirupsen/logrus" 22 "math/rand" 23 "os" 24 "strings" 25 "sync" 26 "time" 27) 28 29type Tester struct { 30 device2PkgDir sync.Map 31} 32 33func init() { 34 rand.Seed(time.Now().UnixNano()) 35} 36 37func NewTester() tester.Tester { 38 ret := &Tester{} 39 return ret 40} 41 42func (t *Tester) TaskName() string { 43 return "pkg_available" 44} 45 46func (t *Tester) Prepare(pkgDir string, device string, ctx context.Context) error { 47 t.device2PkgDir.Store(device, pkgDir) 48 return nil 49} 50 51func (t *Tester) DoTestTask(deviceSN string, ctx context.Context) (ret []tester.Result, err error) { 52 return t.DoTestCases(deviceSN, []string{"pkg_available"}, ctx) 53} 54 55func (t *Tester) DoTestCase(deviceSN, testCase string, ctx context.Context) (ret tester.Result, err error) { 56 pkgDir, _ := t.device2PkgDir.Load(deviceSN) 57 es, err := os.ReadDir(pkgDir.(string)) 58 if err != nil { 59 logrus.Errorf("can not read dir %s, testcase failed", pkgDir.(string)) 60 return tester.Result{TestCaseName: testCase, Status: tester.ResultFail}, nil 61 } 62 for _, e := range es { 63 if strings.HasSuffix(e.Name(), ".img") { 64 logrus.Infof("find image in dir %s, package is avaliable, testcase pass", pkgDir.(string)) 65 return tester.Result{TestCaseName: testCase, Status: tester.ResultPass}, nil 66 } 67 } 68 logrus.Infof("no images in dir %s, package is not avaliable, testcase failed", pkgDir.(string)) 69 return tester.Result{TestCaseName: testCase, Status: tester.ResultFail}, nil 70} 71 72func (t *Tester) DoTestCases(deviceSN string, testcases []string, ctx context.Context) (ret []tester.Result, err error) { 73 for _, testcase := range testcases { 74 r, err := t.DoTestCase(deviceSN, testcase, ctx) 75 if err != nil { 76 return nil, err 77 } 78 ret = append(ret, r) 79 } 80 return ret, nil 81} 82