blob: 7a83fdf9baa14a309d53c2f56b6dfa4f49444309 [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')
Simran Basi492242d2014-07-07 09:51:32 -070015STORAGE_SERVER_REGEX = 'gs://.*/'
16
17
18class 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 Basi71206ef2014-08-13 13:51:18 -070054 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 Basi492242d2014-07-07 09:51:32 -070057
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))