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 xdevice
17ba991379Sopenharmony_ci
18ba991379Sopenharmony_ciimport (
19ba991379Sopenharmony_ci	"context"
20ba991379Sopenharmony_ci	"crypto/md5"
21ba991379Sopenharmony_ci	"encoding/xml"
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	"strings"
31ba991379Sopenharmony_ci	"time"
32ba991379Sopenharmony_ci)
33ba991379Sopenharmony_ci
34ba991379Sopenharmony_ciconst enableTestModeScript = `mount -o rw,remount /; param set persist.ace.testmode.enabled 1; param set persist.sys.hilog.debug.on true; sed -i 's/enforcing/permissive/g' /system/etc/selinux/config; sync; reboot`
35ba991379Sopenharmony_ci
36ba991379Sopenharmony_citype Tester struct {
37ba991379Sopenharmony_ci	Task          string `key:"task" default:"acts"`
38ba991379Sopenharmony_ci	Config        string `key:"config" default:"./config/user_config.xml"`
39ba991379Sopenharmony_ci	TestCasesPath string `key:"test_cases_path" default:"./testcases"`
40ba991379Sopenharmony_ci	ResourcePath  string `key:"resource_path" default:"./resource"`
41ba991379Sopenharmony_ci}
42ba991379Sopenharmony_ci
43ba991379Sopenharmony_citype Report struct {
44ba991379Sopenharmony_ci	XMLName   xml.Name `xml:"testsuites"`
45ba991379Sopenharmony_ci	TestSuite []struct {
46ba991379Sopenharmony_ci		TestCase []struct {
47ba991379Sopenharmony_ci			Name   string `xml:"name,attr"`
48ba991379Sopenharmony_ci			Result string `xml:"result,attr"`
49ba991379Sopenharmony_ci		} `xml:"testcase"`
50ba991379Sopenharmony_ci	} `xml:"testsuite"`
51ba991379Sopenharmony_ci}
52ba991379Sopenharmony_ci
53ba991379Sopenharmony_cifunc init() {
54ba991379Sopenharmony_ci	rand.Seed(time.Now().UnixNano())
55ba991379Sopenharmony_ci}
56ba991379Sopenharmony_ci
57ba991379Sopenharmony_cifunc NewTester() tester.Tester {
58ba991379Sopenharmony_ci	ret := &Tester{}
59ba991379Sopenharmony_ci	utils.ParseFromConfigFile("xdevice", ret)
60ba991379Sopenharmony_ci	return ret
61ba991379Sopenharmony_ci}
62ba991379Sopenharmony_ci
63ba991379Sopenharmony_cifunc (t *Tester) TaskName() string {
64ba991379Sopenharmony_ci	return t.Task
65ba991379Sopenharmony_ci}
66ba991379Sopenharmony_ci
67ba991379Sopenharmony_cifunc (t *Tester) Prepare(pkgDir string, device string, ctx context.Context) (err error) {
68ba991379Sopenharmony_ci	logrus.Info("for xdevice test, try to enable test mode...")
69ba991379Sopenharmony_ci	if err := utils.HdcShell(enableTestModeScript, device, ctx); err != nil {
70ba991379Sopenharmony_ci		return err
71ba991379Sopenharmony_ci	}
72ba991379Sopenharmony_ci	time.Sleep(20 * time.Second) // usually, it takes about 20s to reboot into OpenHarmony
73ba991379Sopenharmony_ci	if connected := utils.WaitHDC(device, ctx); !connected {
74ba991379Sopenharmony_ci		logrus.Errorf("enable test mode at device %s done, but boot unnormally, hdc connection fail", device)
75ba991379Sopenharmony_ci		return fmt.Errorf("enable test mode at device %s done, but boot unnormally, hdc connection fail", device)
76ba991379Sopenharmony_ci	}
77ba991379Sopenharmony_ci	time.Sleep(10 * time.Second) // wait 10s more to ensure system has been started completely
78ba991379Sopenharmony_ci	logrus.Infof("enable test mode at device %s successfully", device)
79ba991379Sopenharmony_ci	return nil
80ba991379Sopenharmony_ci}
81ba991379Sopenharmony_ci
82ba991379Sopenharmony_cifunc (t *Tester) DoTestTask(deviceSN string, ctx context.Context) (ret []tester.Result, err error) {
83ba991379Sopenharmony_ci	reportDir := fmt.Sprintf("%X", md5.Sum([]byte(fmt.Sprintf("%d", rand.Int()))))
84ba991379Sopenharmony_ci	args := []string{"-m", "xdevice", "run", t.Task, "-c", t.Config, "-tcpath", t.TestCasesPath, "-respath", t.ResourcePath, "-rp", reportDir}
85ba991379Sopenharmony_ci	if deviceSN != "" {
86ba991379Sopenharmony_ci		args = append(args, "-sn", deviceSN)
87ba991379Sopenharmony_ci	}
88ba991379Sopenharmony_ci	if err := utils.ExecContext(ctx, "python", args...); err != nil {
89ba991379Sopenharmony_ci		if errors.Is(err, context.Canceled) {
90ba991379Sopenharmony_ci			return nil, err
91ba991379Sopenharmony_ci		}
92ba991379Sopenharmony_ci		logrus.Errorf("do test suite fail: %v", err)
93ba991379Sopenharmony_ci		return nil, err
94ba991379Sopenharmony_ci	}
95ba991379Sopenharmony_ci	return t.readReport(reportDir)
96ba991379Sopenharmony_ci}
97ba991379Sopenharmony_ci
98ba991379Sopenharmony_cifunc (t *Tester) DoTestCase(deviceSN, testCase string, ctx context.Context) (ret tester.Result, err error) {
99ba991379Sopenharmony_ci	reportDir := fmt.Sprintf("%X", md5.Sum([]byte(fmt.Sprintf("%d", rand.Int()))))
100ba991379Sopenharmony_ci	args := []string{"-m", "xdevice", "run", "-l", testCase, "-c", t.Config, "-tcpath", t.TestCasesPath, "-respath", t.ResourcePath, "-rp", reportDir}
101ba991379Sopenharmony_ci	if deviceSN != "" {
102ba991379Sopenharmony_ci		args = append(args, "-sn", deviceSN)
103ba991379Sopenharmony_ci	}
104ba991379Sopenharmony_ci	if err := utils.ExecContext(ctx, "python", args...); err != nil {
105ba991379Sopenharmony_ci		if errors.Is(err, context.Canceled) {
106ba991379Sopenharmony_ci			return ret, err
107ba991379Sopenharmony_ci		}
108ba991379Sopenharmony_ci		logrus.Errorf("do test case %s fail: %v", testCase, err)
109ba991379Sopenharmony_ci		return ret, err
110ba991379Sopenharmony_ci	}
111ba991379Sopenharmony_ci	r, err := t.readReport(reportDir)
112ba991379Sopenharmony_ci	if len(r) == 0 {
113ba991379Sopenharmony_ci		return ret, fmt.Errorf("read latest report err, no result found")
114ba991379Sopenharmony_ci	}
115ba991379Sopenharmony_ci	if r[0].TestCaseName != testCase {
116ba991379Sopenharmony_ci		return ret, fmt.Errorf("read latest report err, no matched result found")
117ba991379Sopenharmony_ci	}
118ba991379Sopenharmony_ci	logrus.Infof("do testcase %s at %s done, result is %s", r[0].TestCaseName, deviceSN, r[0].Status)
119ba991379Sopenharmony_ci	return r[0], nil
120ba991379Sopenharmony_ci}
121ba991379Sopenharmony_ci
122ba991379Sopenharmony_cifunc (t *Tester) DoTestCases(deviceSN string, testcases []string, ctx context.Context) (ret []tester.Result, err error) {
123ba991379Sopenharmony_ci	reportDir := fmt.Sprintf("%X", md5.Sum([]byte(fmt.Sprintf("%d", rand.Int()))))
124ba991379Sopenharmony_ci	args := []string{"-m", "xdevice", "run", "-l", strings.Join(testcases, ";"), "-c", t.Config, "-tcpath", t.TestCasesPath, "-respath", t.ResourcePath, "-rp", reportDir}
125ba991379Sopenharmony_ci	if deviceSN != "" {
126ba991379Sopenharmony_ci		args = append(args, "-sn", deviceSN)
127ba991379Sopenharmony_ci	}
128ba991379Sopenharmony_ci	if err := utils.ExecContext(ctx, "python", args...); err != nil {
129ba991379Sopenharmony_ci		if errors.Is(err, context.Canceled) {
130ba991379Sopenharmony_ci			return ret, err
131ba991379Sopenharmony_ci		}
132ba991379Sopenharmony_ci		logrus.Errorf("do test cases %v fail: %v", testcases, err)
133ba991379Sopenharmony_ci		return ret, err
134ba991379Sopenharmony_ci	}
135ba991379Sopenharmony_ci	return t.readReport(reportDir)
136ba991379Sopenharmony_ci}
137ba991379Sopenharmony_ci
138ba991379Sopenharmony_cifunc (t *Tester) readReport(reportDir string) (ret []tester.Result, err error) {
139ba991379Sopenharmony_ci	data, err := os.ReadFile(filepath.Join("reports", reportDir, "summary_report.xml"))
140ba991379Sopenharmony_ci	if err != nil {
141ba991379Sopenharmony_ci		logrus.Errorf("read report xml fail: %v", err)
142ba991379Sopenharmony_ci		return nil, err
143ba991379Sopenharmony_ci	}
144ba991379Sopenharmony_ci	var report Report
145ba991379Sopenharmony_ci	err = xml.Unmarshal(data, &report)
146ba991379Sopenharmony_ci	if err != nil {
147ba991379Sopenharmony_ci		logrus.Errorf("unmarshal report xml fail: %v", err)
148ba991379Sopenharmony_ci		return nil, err
149ba991379Sopenharmony_ci	}
150ba991379Sopenharmony_ci	for _, s := range report.TestSuite {
151ba991379Sopenharmony_ci		for _, c := range s.TestCase {
152ba991379Sopenharmony_ci			var status tester.ResultStatus
153ba991379Sopenharmony_ci			if c.Result == "true" {
154ba991379Sopenharmony_ci				status = tester.ResultPass
155ba991379Sopenharmony_ci			} else {
156ba991379Sopenharmony_ci				status = tester.ResultFail
157ba991379Sopenharmony_ci			}
158ba991379Sopenharmony_ci			ret = append(ret, tester.Result{TestCaseName: c.Name, Status: status})
159ba991379Sopenharmony_ci		}
160ba991379Sopenharmony_ci	}
161ba991379Sopenharmony_ci	return ret, err
162ba991379Sopenharmony_ci}
163