1# Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import logging
6import os
7
8from autotest_lib.client.common_lib import error
9from autotest_lib.client.common_lib import global_config
10from autotest_lib.server.cros import moblab_test
11from autotest_lib.server.hosts import moblab_host
12
13
14FAILURE_FOLDERS = ['/usr/local/autotest/results', '/usr/local/autotest/logs']
15
16
17class moblab_RunSuite(moblab_test.MoblabTest):
18    """
19    Moblab run suite test. Ensures that a Moblab can run a suite from start
20    to finish by kicking off a suite which will have the Moblab stage an
21    image, provision its DUTs and run the tests.
22    """
23    version = 1
24
25
26    def run_once(self, host, suite_name):
27        """Runs a suite on a Moblab Host against its test DUTS.
28
29        @param host: Moblab Host that will run the suite.
30        @param suite_name: Name of the suite to run.
31
32        @raises AutoservRunError if the suite does not complete successfully.
33        """
34        # Fetch the board of the DUT's assigned to this Moblab. There should
35        # only be one type.
36        board = host.afe.get_hosts()[0].platform
37        # TODO (crbug.com/399132) sbasi - Replace repair version with actual
38        # stable_version for the given board.
39        stable_version = host.afe.run('get_stable_version', board=board)
40        build_pattern = global_config.global_config.get_config_value(
41                'CROS', 'stable_build_pattern')
42        build = build_pattern % (board, stable_version)
43
44        logging.debug('Running suite: %s.', suite_name)
45        try:
46            result = host.run_as_moblab(
47                    "%s/site_utils/run_suite.py --pool='' "
48                    "--board=%s --build=%s --suite_name=%s" %
49                    (moblab_host.AUTOTEST_INSTALL_DIR, board, build,
50                     suite_name), timeout=10800)
51        except error.AutoservRunError as e:
52            # Collect the results and logs from the moblab device.
53            moblab_logs_dir = os.path.join(self.resultsdir, 'moblab_logs')
54            for folder in FAILURE_FOLDERS:
55                try:
56                    host.get_file(folder, moblab_logs_dir)
57                except error.AutoservRunError as e2:
58                    logging.error(e2)
59                    pass
60            raise e
61        logging.debug('Suite Run Output:\n%s', result.stdout)
62