Simran Basi | 492242d | 2014-07-07 09:51:32 -0700 | [diff] [blame] | 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 | import logging |
| 5 | import os |
| 6 | import re |
| 7 | |
| 8 | from autotest_lib.client.common_lib import error, global_config |
| 9 | from autotest_lib.server import test |
| 10 | from autotest_lib.server.hosts import moblab_host |
| 11 | |
| 12 | |
| 13 | DEFAULT_IMAGE_STORAGE_SERVER = global_config.global_config.get_config_value( |
| 14 | 'CROS', 'image_storage_server') |
Simran Basi | 492242d | 2014-07-07 09:51:32 -0700 | [diff] [blame] | 15 | STORAGE_SERVER_REGEX = 'gs://.*/' |
| 16 | |
| 17 | |
| 18 | class MoblabTest(test.test): |
| 19 | """Base class for Moblab tests. |
| 20 | """ |
| 21 | |
| 22 | def initialize(self, host, boto_path='', |
| 23 | image_storage_server=DEFAULT_IMAGE_STORAGE_SERVER): |
| 24 | """Initialize the Moblab Host. |
| 25 | |
| 26 | * Installs a boto file. |
| 27 | * Sets up the image storage server for this test. |
| 28 | * Finds and adds DUTs on the testing subnet. |
| 29 | |
| 30 | @param boto_path: Path to the boto file we want to install. |
| 31 | @param image_storage_server: image storage server to use for grabbing |
| 32 | images from Google Storage. |
| 33 | """ |
| 34 | super(MoblabTest, self).initialize() |
| 35 | self._host = host |
| 36 | self.install_boto_file(boto_path) |
| 37 | self.set_image_storage_server(image_storage_server) |
| 38 | self._host.wait_afe_up() |
| 39 | self._host.find_and_add_duts() |
| 40 | |
| 41 | |
| 42 | def install_boto_file(self, boto_path=''): |
| 43 | """Install a boto file on the Moblab device. |
| 44 | |
| 45 | @param boto_path: Path to the boto file to install. If None, sends the |
| 46 | boto file in the current HOME directory. |
| 47 | |
| 48 | @raises error.TestError if the boto file does not exist. |
| 49 | """ |
| 50 | if not boto_path: |
| 51 | boto_path = os.path.join(os.getenv('HOME'), '.boto') |
| 52 | if not os.path.exists(boto_path): |
| 53 | raise error.TestError('Boto File:%s does not exist.' % boto_path) |
Simran Basi | 71206ef | 2014-08-13 13:51:18 -0700 | [diff] [blame] | 54 | self._host.send_file(boto_path, moblab_host.MOBLAB_BOTO_LOCATION) |
| 55 | self._host.run('chown moblab:moblab %s' % |
| 56 | moblab_host.MOBLAB_BOTO_LOCATION) |
Simran Basi | 492242d | 2014-07-07 09:51:32 -0700 | [diff] [blame] | 57 | |
| 58 | |
| 59 | def set_image_storage_server(self, image_storage_server): |
| 60 | """Set the image storage server. |
| 61 | |
| 62 | @param image_storage_server: Name of image storage server to use. Must |
| 63 | follow format or gs://bucket-name/ |
| 64 | (Note trailing slash is required). |
| 65 | |
| 66 | @raises error.TestError if the image_storage_server is incorrectly |
| 67 | formatted. |
| 68 | """ |
| 69 | if not re.match(STORAGE_SERVER_REGEX, image_storage_server): |
| 70 | raise error.TestError( |
| 71 | 'Image Storage Server supplied is not in the correct ' |
| 72 | 'format. Must start with gs:// and end with a trailing ' |
| 73 | 'slash: %s' % image_storage_server) |
| 74 | logging.debug('Setting image_storage_server to %s', |
| 75 | image_storage_server) |
| 76 | # If the image_storage_server is already set, delete it. |
| 77 | self._host.run('sed -i /image_storage_server/d %s' % |
| 78 | moblab_host.SHADOW_CONFIG_PATH, ignore_status=True) |
| 79 | self._host.run("sed -i '/\[CROS\]/ a\image_storage_server: " |
| 80 | "%s' %s" %(image_storage_server, |
| 81 | moblab_host.SHADOW_CONFIG_PATH)) |