1ba991379Sopenharmony_ci/* 2ba991379Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 3ba991379Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4ba991379Sopenharmony_ci * you may not use this file except in compliance with the License. 5ba991379Sopenharmony_ci * You may obtain a copy of the License at 6ba991379Sopenharmony_ci * 7ba991379Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8ba991379Sopenharmony_ci * 9ba991379Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10ba991379Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11ba991379Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12ba991379Sopenharmony_ci * See the License for the specific language governing permissions and 13ba991379Sopenharmony_ci * limitations under the License. 14ba991379Sopenharmony_ci */ 15ba991379Sopenharmony_ci 16ba991379Sopenharmony_cipackage smoke 17ba991379Sopenharmony_ci 18ba991379Sopenharmony_ciimport ( 19ba991379Sopenharmony_ci "context" 20ba991379Sopenharmony_ci "crypto/md5" 21ba991379Sopenharmony_ci "encoding/json" 22ba991379Sopenharmony_ci "errors" 23ba991379Sopenharmony_ci "fmt" 24ba991379Sopenharmony_ci "fotff/tester" 25ba991379Sopenharmony_ci "fotff/utils" 26ba991379Sopenharmony_ci "github.com/sirupsen/logrus" 27ba991379Sopenharmony_ci "math/rand" 28ba991379Sopenharmony_ci "os" 29ba991379Sopenharmony_ci "path/filepath" 30ba991379Sopenharmony_ci "strconv" 31ba991379Sopenharmony_ci "strings" 32ba991379Sopenharmony_ci "time" 33ba991379Sopenharmony_ci) 34ba991379Sopenharmony_ci 35ba991379Sopenharmony_citype Tester struct { 36ba991379Sopenharmony_ci Py string `key:"py"` 37ba991379Sopenharmony_ci Config string `key:"config"` 38ba991379Sopenharmony_ci AnswerPath string `key:"answer_path"` 39ba991379Sopenharmony_ci SavePath string `key:"save_path"` 40ba991379Sopenharmony_ci ToolsPath string `key:"tools_path"` 41ba991379Sopenharmony_ci} 42ba991379Sopenharmony_ci 43ba991379Sopenharmony_cifunc init() { 44ba991379Sopenharmony_ci rand.Seed(time.Now().UnixNano()) 45ba991379Sopenharmony_ci} 46ba991379Sopenharmony_ci 47ba991379Sopenharmony_cifunc NewTester() tester.Tester { 48ba991379Sopenharmony_ci ret := &Tester{} 49ba991379Sopenharmony_ci utils.ParseFromConfigFile("smoke", ret) 50ba991379Sopenharmony_ci return ret 51ba991379Sopenharmony_ci} 52ba991379Sopenharmony_ci 53ba991379Sopenharmony_cifunc (t *Tester) TaskName() string { 54ba991379Sopenharmony_ci return "smoke_test" 55ba991379Sopenharmony_ci} 56ba991379Sopenharmony_ci 57ba991379Sopenharmony_cifunc (t *Tester) Prepare(pkgDir string, device string, ctx context.Context) error { 58ba991379Sopenharmony_ci return nil 59ba991379Sopenharmony_ci} 60ba991379Sopenharmony_ci 61ba991379Sopenharmony_cifunc (t *Tester) DoTestTask(deviceSN string, ctx context.Context) (ret []tester.Result, err error) { 62ba991379Sopenharmony_ci reportDir := fmt.Sprintf("%X", md5.Sum([]byte(fmt.Sprintf("%d", rand.Int())))) 63ba991379Sopenharmony_ci if err := os.MkdirAll(filepath.Join(t.SavePath, reportDir), 0755); err != nil { 64ba991379Sopenharmony_ci return nil, err 65ba991379Sopenharmony_ci } 66ba991379Sopenharmony_ci args := []string{t.Py, "--config", t.Config, "--answer_path", t.AnswerPath, "--save_path", filepath.Join(t.SavePath, reportDir), "--tools_path", t.ToolsPath} 67ba991379Sopenharmony_ci if deviceSN != "" { 68ba991379Sopenharmony_ci args = append(args, "--device_num", deviceSN) 69ba991379Sopenharmony_ci } 70ba991379Sopenharmony_ci if err := utils.ExecContext(ctx, "python", args...); err != nil { 71ba991379Sopenharmony_ci if errors.Is(err, context.Canceled) { 72ba991379Sopenharmony_ci return nil, err 73ba991379Sopenharmony_ci } 74ba991379Sopenharmony_ci logrus.Errorf("do test suite fail: %v", err) 75ba991379Sopenharmony_ci return nil, err 76ba991379Sopenharmony_ci } 77ba991379Sopenharmony_ci return t.readReport(reportDir) 78ba991379Sopenharmony_ci} 79ba991379Sopenharmony_ci 80ba991379Sopenharmony_cifunc (t *Tester) DoTestCase(deviceSN, testCase string, ctx context.Context) (ret tester.Result, err error) { 81ba991379Sopenharmony_ci reportDir := fmt.Sprintf("%X", md5.Sum([]byte(fmt.Sprintf("%d", rand.Int())))) 82ba991379Sopenharmony_ci if err := os.MkdirAll(filepath.Join(t.SavePath, reportDir), 0755); err != nil { 83ba991379Sopenharmony_ci return ret, err 84ba991379Sopenharmony_ci } 85ba991379Sopenharmony_ci args := []string{t.Py, "--config", t.Config, "--answer_path", t.AnswerPath, "--save_path", filepath.Join(t.SavePath, reportDir), "--tools_path", t.ToolsPath, "--test_num", testCase} 86ba991379Sopenharmony_ci if deviceSN != "" { 87ba991379Sopenharmony_ci args = append(args, "--device_num", deviceSN) 88ba991379Sopenharmony_ci } 89ba991379Sopenharmony_ci if err := utils.ExecContext(ctx, "python", args...); err != nil { 90ba991379Sopenharmony_ci if errors.Is(err, context.Canceled) { 91ba991379Sopenharmony_ci return ret, err 92ba991379Sopenharmony_ci } 93ba991379Sopenharmony_ci logrus.Errorf("do test case %s fail: %v", testCase, err) 94ba991379Sopenharmony_ci return ret, err 95ba991379Sopenharmony_ci } 96ba991379Sopenharmony_ci r, err := t.readReport(reportDir) 97ba991379Sopenharmony_ci if len(r) == 0 { 98ba991379Sopenharmony_ci return ret, fmt.Errorf("read latest report err, no result found") 99ba991379Sopenharmony_ci } 100ba991379Sopenharmony_ci if r[0].TestCaseName != testCase { 101ba991379Sopenharmony_ci return ret, fmt.Errorf("read latest report err, no matched result found") 102ba991379Sopenharmony_ci } 103ba991379Sopenharmony_ci logrus.Infof("do testcase %s at %s done, result is %s", r[0].TestCaseName, deviceSN, r[0].Status) 104ba991379Sopenharmony_ci return r[0], nil 105ba991379Sopenharmony_ci} 106ba991379Sopenharmony_ci 107ba991379Sopenharmony_cifunc (t *Tester) DoTestCases(deviceSN string, testcases []string, ctx context.Context) (ret []tester.Result, err error) { 108ba991379Sopenharmony_ci reportDir := fmt.Sprintf("%X", md5.Sum([]byte(fmt.Sprintf("%d", rand.Int())))) 109ba991379Sopenharmony_ci if err := os.MkdirAll(filepath.Join(t.SavePath, reportDir), 0755); err != nil { 110ba991379Sopenharmony_ci return nil, err 111ba991379Sopenharmony_ci } 112ba991379Sopenharmony_ci args := []string{t.Py, "--config", t.Config, "--answer_path", t.AnswerPath, "--save_path", filepath.Join(t.SavePath, reportDir), "--tools_path", t.ToolsPath, "--test_num", strings.Join(testcases, " ")} 113ba991379Sopenharmony_ci if deviceSN != "" { 114ba991379Sopenharmony_ci args = append(args, "--device_num", deviceSN) 115ba991379Sopenharmony_ci } 116ba991379Sopenharmony_ci if err := utils.ExecContext(ctx, "python", args...); err != nil { 117ba991379Sopenharmony_ci if errors.Is(err, context.Canceled) { 118ba991379Sopenharmony_ci return ret, err 119ba991379Sopenharmony_ci } 120ba991379Sopenharmony_ci logrus.Errorf("do test cases %v fail: %v", testcases, err) 121ba991379Sopenharmony_ci return ret, err 122ba991379Sopenharmony_ci } 123ba991379Sopenharmony_ci return t.readReport(reportDir) 124ba991379Sopenharmony_ci} 125ba991379Sopenharmony_ci 126ba991379Sopenharmony_cifunc (t *Tester) readReport(reportDir string) (ret []tester.Result, err error) { 127ba991379Sopenharmony_ci data, err := os.ReadFile(filepath.Join(t.SavePath, reportDir, "result.json")) 128ba991379Sopenharmony_ci if err != nil { 129ba991379Sopenharmony_ci logrus.Errorf("read report json fail: %v", err) 130ba991379Sopenharmony_ci return nil, err 131ba991379Sopenharmony_ci } 132ba991379Sopenharmony_ci var result []struct { 133ba991379Sopenharmony_ci TestCaseName int `json:"test_case_name"` 134ba991379Sopenharmony_ci Status string `json:"status"` 135ba991379Sopenharmony_ci } 136ba991379Sopenharmony_ci err = json.Unmarshal(data, &result) 137ba991379Sopenharmony_ci if err != nil { 138ba991379Sopenharmony_ci logrus.Errorf("unmarshal report xml fail: %v", err) 139ba991379Sopenharmony_ci return nil, err 140ba991379Sopenharmony_ci } 141ba991379Sopenharmony_ci for _, r := range result { 142ba991379Sopenharmony_ci if r.Status == "pass" { 143ba991379Sopenharmony_ci ret = append(ret, tester.Result{TestCaseName: strconv.Itoa(r.TestCaseName), Status: tester.ResultPass}) 144ba991379Sopenharmony_ci } else { 145ba991379Sopenharmony_ci ret = append(ret, tester.Result{TestCaseName: strconv.Itoa(r.TestCaseName), Status: tester.ResultFail}) 146ba991379Sopenharmony_ci } 147ba991379Sopenharmony_ci } 148ba991379Sopenharmony_ci return ret, err 149ba991379Sopenharmony_ci} 150