• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2#coding: utf-8
3
4"""
5Copyright (c) 2021-2022 Huawei Device Co., Ltd.
6Licensed under the Apache License, Version 2.0 (the "License");
7you may not use this file except in compliance with the License.
8You may obtain a copy of the License at
9
10    http://www.apache.org/licenses/LICENSE-2.0
11
12Unless required by applicable law or agreed to in writing, software
13distributed under the License is distributed on an "AS IS" BASIS,
14WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15See the License for the specific language governing permissions and
16limitations under the License.
17
18Description: run script
19    expect_output will get run result,
20    expect_sub_output will catch pivotal sub output,
21    expect_file will get print string
22"""
23
24import argparse
25import subprocess
26import time
27
28
29def parse_args():
30    """parse arguments."""
31    parser = argparse.ArgumentParser()
32    parser.add_argument('--script-file', help='execute script file')
33    parser.add_argument('--script-options', help='execute script options')
34    parser.add_argument('--script-args', help='args of script')
35    parser.add_argument('--expect-output', help='expect output')
36    parser.add_argument('--expect-sub-output', help='expect sub output')
37    parser.add_argument('--expect-file', help='expect file')
38    parser.add_argument('--env-path', help='LD_LIBRARY_PATH env')
39    parser.add_argument('--timeout-limit', help='timeout limit')
40    args = parser.parse_args()
41    return args
42
43def judge_output(args):
44    """run testcase and judge is success or not."""
45    start_time = time.time()
46    cmd = input_args.script_file
47    if input_args.script_options:
48        cmd += input_args.script_options
49    if input_args.script_args:
50        cmd += " " + input_args.script_args
51    if input_args.timeout_limit:
52        timeout_limit = int(input_args.timeout_limit)
53    else:
54        timeout_limit = 120  # units: s
55    subp = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
56        env={'LD_LIBRARY_PATH': str(input_args.env_path)})
57    try:
58        out, err = subp.communicate(timeout=timeout_limit)
59    except subprocess.TimeoutExpired:
60        raise RuntimeError('Run [', cmd, '] timeout, timeout_limit = ', timeout_limit, 's')
61
62    if args.expect_output:
63        returncode = str(subp.returncode)
64        if returncode != args.expect_output:
65            out_str = out.decode('UTF-8')
66            err_str = err.decode('UTF-8')
67            print(out_str)
68            print(err_str)
69            print(">>>>> Expect return: [" + args.expect_output \
70                + "]\n>>>>> But got: [" + returncode + "]")
71            raise RuntimeError("Run [" + cmd + "] failed!")
72    elif args.expect_sub_output:
73        out_str = out.decode('UTF-8')
74        if out_str.find(args.expect_sub_output) == -1:
75            out_str = out.decode('UTF-8')
76            print(out_str)
77            print(">>>>> Expect contain: [" + args.expect_sub_output \
78                + "]\n>>>>> But got: [" + out_str + "]")
79            raise RuntimeError("Run [" + cmd + "] failed!")
80    elif args.expect_file:
81        with open(args.expect_file, mode='r') as file:
82            # skip license header
83            expect_output = ''.join(file.readlines()[13:])
84            file.close()
85            out_str = out.decode('UTF-8')
86            if out_str != expect_output:
87                err_str = err.decode('UTF-8')
88                print(err_str)
89                print(">>>>> Expect : [" + expect_output \
90                    + "]\n>>>>> But got: [" + out_str + "]")
91                raise RuntimeError("Run [" + cmd + "] failed!")
92    else:
93        raise RuntimeError("Run [" + cmd + "] with no expect !")
94
95    print("Run [" + cmd + "] success!")
96    print("used: %.5f seconds" % (time.time() - start_time))
97
98
99if __name__ == '__main__':
100    input_args = parse_args()
101    judge_output(input_args)
102