1425bb815Sopenharmony_ci#!/usr/bin/env python3
2425bb815Sopenharmony_ci#coding: utf-8
3425bb815Sopenharmony_ci
4425bb815Sopenharmony_ci"""
5425bb815Sopenharmony_ciCopyright (c) 2021-2022 Huawei Device Co., Ltd.
6425bb815Sopenharmony_ciLicensed under the Apache License, Version 2.0 (the "License");
7425bb815Sopenharmony_ciyou may not use this file except in compliance with the License.
8425bb815Sopenharmony_ciYou may obtain a copy of the License at
9425bb815Sopenharmony_ci
10425bb815Sopenharmony_ci    http://www.apache.org/licenses/LICENSE-2.0
11425bb815Sopenharmony_ci
12425bb815Sopenharmony_ciUnless required by applicable law or agreed to in writing, software
13425bb815Sopenharmony_cidistributed under the License is distributed on an "AS IS" BASIS,
14425bb815Sopenharmony_ciWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15425bb815Sopenharmony_ciSee the License for the specific language governing permissions and
16425bb815Sopenharmony_cilimitations under the License.
17425bb815Sopenharmony_ci
18425bb815Sopenharmony_ciDescription: run script
19425bb815Sopenharmony_ci    expect_output will get run result,
20425bb815Sopenharmony_ci    expect_sub_output will catch pivotal sub output,
21425bb815Sopenharmony_ci    expect_file will get print string
22425bb815Sopenharmony_ci"""
23425bb815Sopenharmony_ci
24425bb815Sopenharmony_ciimport argparse
25425bb815Sopenharmony_ciimport subprocess
26425bb815Sopenharmony_ciimport time
27425bb815Sopenharmony_ci
28425bb815Sopenharmony_ci
29425bb815Sopenharmony_cidef parse_args():
30425bb815Sopenharmony_ci    """parse arguments."""
31425bb815Sopenharmony_ci    parser = argparse.ArgumentParser()
32425bb815Sopenharmony_ci    parser.add_argument('--script-file', help='execute script file')
33425bb815Sopenharmony_ci    parser.add_argument('--script-options', help='execute script options')
34425bb815Sopenharmony_ci    parser.add_argument('--script-args', help='args of script')
35425bb815Sopenharmony_ci    parser.add_argument('--expect-output', help='expect output')
36425bb815Sopenharmony_ci    parser.add_argument('--expect-sub-output', help='expect sub output')
37425bb815Sopenharmony_ci    parser.add_argument('--expect-file', help='expect file')
38425bb815Sopenharmony_ci    parser.add_argument('--env-path', help='LD_LIBRARY_PATH env')
39425bb815Sopenharmony_ci    parser.add_argument('--timeout-limit', help='timeout limit')
40425bb815Sopenharmony_ci    args = parser.parse_args()
41425bb815Sopenharmony_ci    return args
42425bb815Sopenharmony_ci
43425bb815Sopenharmony_cidef judge_output(args):
44425bb815Sopenharmony_ci    """run testcase and judge is success or not."""
45425bb815Sopenharmony_ci    start_time = time.time()
46425bb815Sopenharmony_ci    cmd = input_args.script_file
47425bb815Sopenharmony_ci    if input_args.script_options:
48425bb815Sopenharmony_ci        cmd += input_args.script_options
49425bb815Sopenharmony_ci    if input_args.script_args:
50425bb815Sopenharmony_ci        cmd += " " + input_args.script_args
51425bb815Sopenharmony_ci    if input_args.timeout_limit:
52425bb815Sopenharmony_ci        timeout_limit = int(input_args.timeout_limit)
53425bb815Sopenharmony_ci    else:
54425bb815Sopenharmony_ci        timeout_limit = 120  # units: s
55425bb815Sopenharmony_ci    subp = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
56425bb815Sopenharmony_ci        env={'LD_LIBRARY_PATH': str(input_args.env_path)})
57425bb815Sopenharmony_ci    try:
58425bb815Sopenharmony_ci        out, err = subp.communicate(timeout=timeout_limit)
59425bb815Sopenharmony_ci    except subprocess.TimeoutExpired:
60425bb815Sopenharmony_ci        raise RuntimeError('Run [', cmd, '] timeout, timeout_limit = ', timeout_limit, 's')
61425bb815Sopenharmony_ci
62425bb815Sopenharmony_ci    if args.expect_output:
63425bb815Sopenharmony_ci        returncode = str(subp.returncode)
64425bb815Sopenharmony_ci        if returncode != args.expect_output:
65425bb815Sopenharmony_ci            out_str = out.decode('UTF-8')
66425bb815Sopenharmony_ci            err_str = err.decode('UTF-8')
67425bb815Sopenharmony_ci            print(out_str)
68425bb815Sopenharmony_ci            print(err_str)
69425bb815Sopenharmony_ci            print(">>>>> Expect return: [" + args.expect_output \
70425bb815Sopenharmony_ci                + "]\n>>>>> But got: [" + returncode + "]")
71425bb815Sopenharmony_ci            raise RuntimeError("Run [" + cmd + "] failed!")
72425bb815Sopenharmony_ci    elif args.expect_sub_output:
73425bb815Sopenharmony_ci        out_str = out.decode('UTF-8')
74425bb815Sopenharmony_ci        if out_str.find(args.expect_sub_output) == -1:
75425bb815Sopenharmony_ci            out_str = out.decode('UTF-8')
76425bb815Sopenharmony_ci            print(out_str)
77425bb815Sopenharmony_ci            print(">>>>> Expect contain: [" + args.expect_sub_output \
78425bb815Sopenharmony_ci                + "]\n>>>>> But got: [" + out_str + "]")
79425bb815Sopenharmony_ci            raise RuntimeError("Run [" + cmd + "] failed!")
80425bb815Sopenharmony_ci    elif args.expect_file:
81425bb815Sopenharmony_ci        with open(args.expect_file, mode='r') as file:
82425bb815Sopenharmony_ci            # skip license header
83425bb815Sopenharmony_ci            expect_output = ''.join(file.readlines()[13:])
84425bb815Sopenharmony_ci            file.close()
85425bb815Sopenharmony_ci            out_str = out.decode('UTF-8')
86425bb815Sopenharmony_ci            if out_str != expect_output:
87425bb815Sopenharmony_ci                err_str = err.decode('UTF-8')
88425bb815Sopenharmony_ci                print(err_str)
89425bb815Sopenharmony_ci                print(">>>>> Expect : [" + expect_output \
90425bb815Sopenharmony_ci                    + "]\n>>>>> But got: [" + out_str + "]")
91425bb815Sopenharmony_ci                raise RuntimeError("Run [" + cmd + "] failed!")
92425bb815Sopenharmony_ci    else:
93425bb815Sopenharmony_ci        raise RuntimeError("Run [" + cmd + "] with no expect !")
94425bb815Sopenharmony_ci
95425bb815Sopenharmony_ci    print("Run [" + cmd + "] success!")
96425bb815Sopenharmony_ci    print("used: %.5f seconds" % (time.time() - start_time))
97425bb815Sopenharmony_ci
98425bb815Sopenharmony_ci
99425bb815Sopenharmony_ciif __name__ == '__main__':
100425bb815Sopenharmony_ci    input_args = parse_args()
101425bb815Sopenharmony_ci    judge_output(input_args)
102