• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2#
3#   Copyright 2016 - 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
17from builtins import str
18
19import os
20import random
21import sys
22
23from acts import keys
24from acts import utils
25
26# An environment variable defining the base location for ACTS logs.
27_ENV_ACTS_LOGPATH = 'ACTS_LOGPATH'
28# An environment variable that enables test case failures to log stack traces.
29_ENV_TEST_FAILURE_TRACEBACKS = 'ACTS_TEST_FAILURE_TRACEBACKS'
30# An environment variable defining the test search paths for ACTS.
31_ENV_ACTS_TESTPATHS = 'ACTS_TESTPATHS'
32_PATH_SEPARATOR = ':'
33
34
35class ActsConfigError(Exception):
36    """Raised when there is a problem in test configuration file."""
37
38
39def _validate_test_config(test_config):
40    """Validates the raw configuration loaded from the config file.
41
42    Making sure all the required fields exist.
43    """
44    for k in keys.Config.reserved_keys.value:
45        if k not in test_config:
46            raise ActsConfigError(
47                "Required key %s missing in test config." % k)
48
49
50def _validate_testbed_name(name):
51    """Validates the name of a test bed.
52
53    Since test bed names are used as part of the test run id, it needs to meet
54    certain requirements.
55
56    Args:
57        name: The test bed's name specified in config file.
58
59    Raises:
60        If the name does not meet any criteria, ActsConfigError is raised.
61    """
62    if not name:
63        raise ActsConfigError("Test bed names can't be empty.")
64    if not isinstance(name, str):
65        raise ActsConfigError("Test bed names have to be string.")
66    for l in name:
67        if l not in utils.valid_filename_chars:
68            raise ActsConfigError(
69                "Char '%s' is not allowed in test bed names." % l)
70
71
72def _update_file_paths(config, config_path):
73    """ Checks if the path entries are valid.
74
75    If the file path is invalid, assume it is a relative path and append
76    that to the config file path.
77
78    Args:
79        config : the config object to verify.
80        config_path : The path to the config file, which can be used to
81                      generate absolute paths from relative paths in configs.
82
83    Raises:
84        If the file path is invalid, ActsConfigError is raised.
85    """
86    # Check the file_path_keys and update if it is a relative path.
87    for file_path_key in keys.Config.file_path_keys.value:
88        if file_path_key in config:
89            config_file = config[file_path_key]
90            if type(config_file) is str:
91                if not os.path.isfile(config_file):
92                    config_file = os.path.join(config_path, config_file)
93                if not os.path.isfile(config_file):
94                    raise ActsConfigError("Unable to load config %s from test "
95                                          "config file.", config_file)
96                config[file_path_key] = config_file
97
98
99def _validate_testbed_configs(testbed_configs, config_path):
100    """Validates the testbed configurations.
101
102    Args:
103        testbed_configs: A list of testbed configuration json objects.
104        config_path : The path to the config file, which can be used to
105                      generate absolute paths from relative paths in configs.
106
107    Raises:
108        If any part of the configuration is invalid, ActsConfigError is raised.
109    """
110    # Cross checks testbed configs for resource conflicts.
111    for name, config in testbed_configs.items():
112        _update_file_paths(config, config_path)
113        _validate_testbed_name(name)
114
115
116def gen_term_signal_handler(test_runners):
117    def termination_sig_handler(signal_num, frame):
118        print('Received sigterm %s.' % signal_num)
119        for t in test_runners:
120            t.stop()
121        sys.exit(1)
122
123    return termination_sig_handler
124
125
126def _parse_one_test_specifier(item):
127    """Parse one test specifier from command line input.
128
129    Args:
130        item: A string that specifies a test class or test cases in one test
131            class to run.
132
133    Returns:
134        A tuple of a string and a list of strings. The string is the test class
135        name, the list of strings is a list of test case names. The list can be
136        None.
137    """
138    tokens = item.split(':')
139    if len(tokens) > 2:
140        raise ActsConfigError("Syntax error in test specifier %s" % item)
141    if len(tokens) == 1:
142        # This should be considered a test class name
143        test_cls_name = tokens[0]
144        return test_cls_name, None
145    elif len(tokens) == 2:
146        # This should be considered a test class name followed by
147        # a list of test case names.
148        test_cls_name, test_case_names = tokens
149        clean_names = [elem.strip() for elem in test_case_names.split(',')]
150        return test_cls_name, clean_names
151
152
153def parse_test_list(test_list):
154    """Parse user provided test list into internal format for test_runner.
155
156    Args:
157        test_list: A list of test classes/cases.
158    """
159    result = []
160    for elem in test_list:
161        result.append(_parse_one_test_specifier(elem))
162    return result
163
164
165def test_randomizer(test_identifiers, test_case_iterations=10):
166    """Generate test lists by randomizing user provided test list.
167
168    Args:
169        test_identifiers: A list of test classes/cases.
170        test_case_iterations: The range of random iterations for each case.
171    Returns:
172        A list of randomized test cases.
173    """
174    random_tests = []
175    preflight_tests = []
176    postflight_tests = []
177    for test_class, test_cases in test_identifiers:
178        if "Preflight" in test_class:
179            preflight_tests.append((test_class, test_cases))
180        elif "Postflight" in test_class:
181            postflight_tests.append((test_class, test_cases))
182        else:
183            for test_case in test_cases:
184                random_tests.append((test_class,
185                                     [test_case] * random.randrange(
186                                         1, test_case_iterations + 1)))
187    random.shuffle(random_tests)
188    new_tests = []
189    previous_class = None
190    for test_class, test_cases in random_tests:
191        if test_class == previous_class:
192            previous_cases = new_tests[-1][1]
193            previous_cases.extend(test_cases)
194        else:
195            new_tests.append((test_class, test_cases))
196        previous_class = test_class
197    return preflight_tests + new_tests + postflight_tests
198
199
200def load_test_config_file(test_config_path,
201                          tb_filters=None,
202                          override_test_path=None,
203                          override_log_path=None,
204                          override_test_args=None,
205                          override_random=None,
206                          override_test_case_iterations=None):
207    """Processes the test configuration file provided by the user.
208
209    Loads the configuration file into a json object, unpacks each testbed
210    config into its own json object, and validate the configuration in the
211    process.
212
213    Args:
214        test_config_path: Path to the test configuration file.
215        tb_filters: A subset of test bed names to be pulled from the config
216                    file. If None, then all test beds will be selected.
217        override_test_path: If not none the test path to use instead.
218        override_log_path: If not none the log path to use instead.
219        override_test_args: If not none the test args to use instead.
220        override_random: If not None, override the config file value.
221        override_test_case_iterations: If not None, override the config file
222                                       value.
223
224    Returns:
225        A list of test configuration json objects to be passed to
226        test_runner.TestRunner.
227    """
228    configs = utils.load_config(test_config_path)
229    if override_test_path:
230        configs[keys.Config.key_test_paths.value] = override_test_path
231    if override_log_path:
232        configs[keys.Config.key_log_path.value] = override_log_path
233    if override_test_args:
234        configs[keys.Config.ikey_cli_args.value] = override_test_args
235    if override_random:
236        configs[keys.Config.key_random.value] = override_random
237    if override_test_case_iterations:
238        configs[keys.Config.key_test_case_iterations.value] = \
239            override_test_case_iterations
240
241    testbeds = configs[keys.Config.key_testbed.value]
242    if type(testbeds) is list:
243        tb_dict = dict()
244        for testbed in testbeds:
245            tb_dict[testbed[keys.Config.key_testbed_name.value]] = testbed
246        testbeds = tb_dict
247    elif type(testbeds) is dict:
248        # For compatibility, make sure the entry name is the same as
249        # the testbed's "name" entry
250        for name, testbed in testbeds.items():
251            testbed[keys.Config.key_testbed_name.value] = name
252
253    if tb_filters:
254        tbs = {}
255        for name in tb_filters:
256            if name in testbeds:
257                tbs[name] = testbeds[name]
258            else:
259                raise ActsConfigError(
260                    'Expected testbed named "%s", but none was found. Check'
261                    'if you have the correct testbed names.' % name)
262        testbeds = tbs
263
264    if (keys.Config.key_log_path.value not in configs
265            and _ENV_ACTS_LOGPATH in os.environ):
266        print('Using environment log path: %s' %
267              (os.environ[_ENV_ACTS_LOGPATH]))
268        configs[keys.Config.key_log_path.value] = os.environ[_ENV_ACTS_LOGPATH]
269    if (keys.Config.key_test_paths.value not in configs
270            and _ENV_ACTS_TESTPATHS in os.environ):
271        print('Using environment test paths: %s' %
272              (os.environ[_ENV_ACTS_TESTPATHS]))
273        configs[keys.Config.key_test_paths.value] = os.environ[
274            _ENV_ACTS_TESTPATHS].split(_PATH_SEPARATOR)
275    if (keys.Config.key_test_failure_tracebacks not in configs
276            and _ENV_TEST_FAILURE_TRACEBACKS in os.environ):
277        configs[keys.Config.key_test_failure_tracebacks.value] = os.environ[
278            _ENV_TEST_FAILURE_TRACEBACKS]
279
280    # Add the global paths to the global config.
281    k_log_path = keys.Config.key_log_path.value
282    configs[k_log_path] = utils.abs_path(configs[k_log_path])
283
284    # TODO: See if there is a better way to do this: b/29836695
285    config_path, _ = os.path.split(utils.abs_path(test_config_path))
286    configs[keys.Config.key_config_path] = config_path
287    _validate_test_config(configs)
288    _validate_testbed_configs(testbeds, config_path)
289    # Unpack testbeds into separate json objects.
290    configs.pop(keys.Config.key_testbed.value)
291    config_jsons = []
292
293    for _, original_bed_config in testbeds.items():
294        new_test_config = dict(configs)
295        new_test_config[keys.Config.key_testbed.value] = original_bed_config
296        # Keys in each test bed config will be copied to a level up to be
297        # picked up for user_params. If the key already exists in the upper
298        # level, the local one defined in test bed config overwrites the
299        # general one.
300        new_test_config.update(original_bed_config)
301        config_jsons.append(new_test_config)
302    return config_jsons
303
304
305def parse_test_file(fpath):
306    """Parses a test file that contains test specifiers.
307
308    Args:
309        fpath: A string that is the path to the test file to parse.
310
311    Returns:
312        A list of strings, each is a test specifier.
313    """
314    with open(fpath, 'r') as f:
315        tf = []
316        for line in f:
317            line = line.strip()
318            if not line:
319                continue
320            if len(tf) and (tf[-1].endswith(':') or tf[-1].endswith(',')):
321                tf[-1] += line
322            else:
323                tf.append(line)
324        return tf
325