1#!/usr/bin/env python
2#
3# Copyright 2017 - The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18import argparse
19import os
20import re
21import sys
22
23from build.build_rule_gen import BuildRuleGen
24from build.vts_spec_parser import VtsSpecParser
25from configure.test_case_creator import TestCaseCreator
26from utils.const import Constant
27
28TEST_TIME_OUT_PATTERN = '(([0-9]+)(m|s|h))+'
29
30"""Generate Android.mk, Android.bp, and AndroidTest.xml files for given hal
31
32Usage:
33  python launch_hal_test.py [--options] hal_package name
34
35  where options are:
36  --test_type: Test type, currently support two types: target and host.
37  --time_out: Timeout for the test, default is 1m.
38  --package_root: Prefix of the HAL package. default is android.hardware
39  --path_root: Root path that stores the HAL definition. default is hardware/interfaces
40  --test_binary_file: Test binary file for target-side HAL test.
41  --test_script_file: Test script file for host-side HAL test.
42  --test_config_dir: Directory path to store the test configure files.
43  --replay: Whether this is a replay test.
44  --disable_stop_runtime: Whether to stop framework before the test.
45Example:
46  python launch_hal_test.py android.hardware.nfc@1.0
47  python launch_hal_test.py --test_type=host --time_out=5m android.hardware.nfc@1.0
48  python launch_hal_test.py --package_root com.qualcomm.qti
49  --path_root vendor/qcom/proprietary/interfaces/com/qualcomm/qti/
50  --test_configure_dir temp/ com.qualcomm.qti.qcril.qcrilhook@1.0
51"""
52
53
54def main():
55    parser = argparse.ArgumentParser(description='Initiate a test case.')
56    parser.add_argument(
57        '--test_type',
58        dest='test_type',
59        required=False,
60        default='target',
61        help='Test type, currently support two types: target and host.')
62    parser.add_argument(
63        '--time_out',
64        dest='time_out',
65        required=False,
66        default='1m',
67        help='Timeout for the test, default is 1m.')
68    parser.add_argument(
69        '--replay',
70        dest='is_replay',
71        action='store_true',
72        required=False,
73        help='Whether this is a replay test.')
74    parser.add_argument(
75        '--disable_stop_runtime',
76        dest='disable_stop_runtime',
77        action='store_true',
78        required=False,
79        help='Whether to stop framework before the test.')
80    parser.add_argument(
81        '--package_root',
82        dest='package_root',
83        required=False,
84        default=Constant.HAL_PACKAGE_PREFIX,
85        help='Prefix of the HAL package. e.g. android.hardware')
86    parser.add_argument(
87        '--path_root',
88        dest='path_root',
89        required=False,
90        default=Constant.HAL_INTERFACE_PATH,
91        help=
92        'Root path that stores the HAL definition. e.g. hardware/interfaces')
93    parser.add_argument(
94        '--test_binary_file',
95        dest='test_binary_file',
96        required=False,
97        help='Test binary file for target-side HAL test.')
98    parser.add_argument(
99        '--test_script_file',
100        dest='test_script_file',
101        required=False,
102        help='Test script file for host-side HAL test.')
103    parser.add_argument(
104        '--test_config_dir',
105        dest='test_config_dir',
106        required=False,
107        help='Directory path to store the test configure files.')
108    parser.add_argument(
109        'hal_package_name',
110        help='hal package name (e.g. android.hardware.nfc@1.0).')
111    args = parser.parse_args()
112
113    regex = re.compile(Constant.HAL_PACKAGE_NAME_PATTERN)
114    result = re.match(regex, args.hal_package_name)
115    if not result:
116        print 'Invalid hal package name. Exiting..'
117        sys.exit(1)
118
119    if not args.hal_package_name.startswith(args.package_root + '.'):
120        print 'hal_package_name does not start with package_root. Exiting...'
121        sys.exit(1)
122
123    if args.test_type != 'target' and args.test_type != 'host':
124        print 'Unsupported test type. Exiting...'
125        sys.exit(1)
126    elif args.test_type == 'host' and args.is_replay:
127        print 'Host side replay test is not supported yet. Exiting...'
128        sys.exit(1)
129
130    regex = re.compile(TEST_TIME_OUT_PATTERN)
131    result = re.match(regex, args.time_out)
132    if not result:
133        print 'Invalid test time out format. Exiting...'
134        sys.exit(1)
135
136    if not args.test_config_dir:
137        if args.package_root == Constant.HAL_PACKAGE_PREFIX:
138            args.test_config_dir = Constant.VTS_HAL_TEST_CASE_PATH
139        else:
140            args.test_config_dir = args.path_root
141
142    stop_runtime = False
143    if args.test_type == 'target' and not args.disable_stop_runtime:
144        stop_runtime = True
145
146    vts_spec_parser = VtsSpecParser(
147        package_root=args.package_root, path_root=args.path_root)
148    test_case_creater = TestCaseCreator(vts_spec_parser, args.hal_package_name)
149    if not test_case_creater.LaunchTestCase(
150            args.test_type,
151            args.time_out,
152            is_replay=args.is_replay,
153            stop_runtime=stop_runtime,
154            test_binary_file=args.test_binary_file,
155            test_script_file=args.test_script_file,
156            test_config_dir=args.test_config_dir,
157            package_root=args.package_root,
158            path_root=args.path_root):
159        print('Error: Failed to launch test for %s. Exiting...' %
160              args.hal_package_name)
161        sys.exit(1)
162
163if __name__ == '__main__':
164    main()
165