• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 datetime
6import logging
7import re
8
9import common
10from autotest_lib.client.common_lib import error, global_config
11from autotest_lib.server import test
12from autotest_lib.server.hosts import moblab_host
13
14
15DEFAULT_IMAGE_STORAGE_SERVER = global_config.global_config.get_config_value(
16        'CROS', 'image_storage_server')
17STORAGE_SERVER_REGEX = '(gs://|/).*/'
18DEFAULT_SERVICES_INIT_TIMEOUT_M = 5
19
20
21class MoblabTest(test.test):
22    """Base class for Moblab tests.
23    """
24
25    def initialize(
26            self,
27            host,
28            boto_path='',
29            image_storage_server=DEFAULT_IMAGE_STORAGE_SERVER,
30            services_init_timeout_m=DEFAULT_SERVICES_INIT_TIMEOUT_M,
31    ):
32        """Initialize the Moblab Host.
33
34        * Installs a boto file.
35        * Sets up the image storage server for this test.
36        * Finds and adds DUTs on the testing subnet.
37
38        @param boto_path: Path to the boto file we want to install.
39        @param image_storage_server: image storage server to use for grabbing
40                images from Google Storage.
41        @param services_init_timeout_m: Timeout (in minuts) for moblab DUT's
42                upstart service initialzation after boot.
43        """
44        super(MoblabTest, self).initialize()
45        self._start_time = datetime.datetime.now()
46        self._host = host
47        # When passed in from test_that or run_suite, all incoming arguments are
48        # str.
49        self._host.verify_moblab_services(
50                timeout_m=int(services_init_timeout_m))
51        self._host.wait_afe_up()
52        self._host.install_boto_file(boto_path)
53        self._set_image_storage_server(image_storage_server)
54        self._host.find_and_add_duts()
55        self._host.verify_duts()
56        self._host.verify_special_tasks_complete()
57
58
59    @property
60    def elapsed(self):
61        """A datetime.timedleta for time elapsed since start of test."""
62        return datetime.datetime.now() - self._start_time
63
64
65    def _set_image_storage_server(self, image_storage_server):
66        """Set the image storage server.
67
68        @param image_storage_server: Name of image storage server to use. Must
69                                     follow format or gs://bucket-name/
70                                     (Note trailing slash is required).
71
72        @raises error.TestError if the image_storage_server is incorrectly
73                                formatted.
74        """
75        if not re.match(STORAGE_SERVER_REGEX, image_storage_server):
76            raise error.TestError(
77                    'Image Storage Server supplied (%s) is not in the correct '
78                    'format. Remote paths must be of the form "gs://.*/" and '
79                    'local paths of the form "/.*/"' % image_storage_server)
80        logging.info('Setting image_storage_server to %s', image_storage_server)
81        # If the image_storage_server is already set, delete it.
82        self._host.run('sed -i /image_storage_server/d %s' %
83                       moblab_host.SHADOW_CONFIG_PATH, ignore_status=True)
84        self._host.run("sed -i '/\[CROS\]/ a\image_storage_server: "
85                       "%s' %s" %(image_storage_server,
86                                  moblab_host.SHADOW_CONFIG_PATH))
87