blob: b2c0a9558bee9eebb1168d9b1964563067915a23 [file] [log] [blame]
Simran Basi492242d2014-07-07 09:51:32 -07001# 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.
4import logging
5import os
6import re
7
8from autotest_lib.client.common_lib import error, global_config
9from autotest_lib.server import test
10from autotest_lib.server.hosts import moblab_host
11
12
13DEFAULT_IMAGE_STORAGE_SERVER = global_config.global_config.get_config_value(
14 'CROS', 'image_storage_server')
15MOBLAB_BOTO_FILE_DEST = '/home/moblab/.boto'
16STORAGE_SERVER_REGEX = 'gs://.*/'
17
18
19class MoblabTest(test.test):
20 """Base class for Moblab tests.
21 """
22
23 def initialize(self, host, boto_path='',
24 image_storage_server=DEFAULT_IMAGE_STORAGE_SERVER):
25 """Initialize the Moblab Host.
26
27 * Installs a boto file.
28 * Sets up the image storage server for this test.
29 * Finds and adds DUTs on the testing subnet.
30
31 @param boto_path: Path to the boto file we want to install.
32 @param image_storage_server: image storage server to use for grabbing
33 images from Google Storage.
34 """
35 super(MoblabTest, self).initialize()
36 self._host = host
37 self.install_boto_file(boto_path)
38 self.set_image_storage_server(image_storage_server)
39 self._host.wait_afe_up()
40 self._host.find_and_add_duts()
41
42
43 def install_boto_file(self, boto_path=''):
44 """Install a boto file on the Moblab device.
45
46 @param boto_path: Path to the boto file to install. If None, sends the
47 boto file in the current HOME directory.
48
49 @raises error.TestError if the boto file does not exist.
50 """
51 if not boto_path:
52 boto_path = os.path.join(os.getenv('HOME'), '.boto')
53 if not os.path.exists(boto_path):
54 raise error.TestError('Boto File:%s does not exist.' % boto_path)
55 self._host.send_file(boto_path, MOBLAB_BOTO_FILE_DEST)
56 self._host.run('chown moblab:moblab %s' % MOBLAB_BOTO_FILE_DEST)
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))