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