1# Copyright (c) 2013 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 logging 6import urllib2 7 8from autotest_lib.client.common_lib import error 9from autotest_lib.client.common_lib import global_config 10from autotest_lib.client.common_lib.cros import dev_server 11from autotest_lib.server import afe_utils 12from autotest_lib.server import test 13 14 15_CONFIG = global_config.global_config 16# pylint: disable-msg=E1120 17_IMAGE_URL_PATTERN = _CONFIG.get_config_value( 18 'CROS', 'image_url_pattern', type=str) 19 20 21class provision_AutoUpdate(test.test): 22 """A test that can provision a machine to the correct ChromeOS version.""" 23 version = 1 24 25 def initialize(self, host, value, force=False, is_test_na=False): 26 """Initialize. 27 28 @param host: The host object to update to |value|. 29 @param value: The build type and version to install on the host. 30 @param force: not used by initialize. 31 @param is_test_na: boolean, if True, will simply skip the test 32 and emit TestNAError. The control file 33 determines whether the test should be skipped 34 and passes the decision via this argument. Note 35 we can't raise TestNAError in control file as it won't 36 be caught and handled properly. 37 """ 38 if is_test_na: 39 raise error.TestNAError( 40 'Test not available for test_that. chroot detected, ' 41 'you are probably using test_that.') 42 # We check value in initialize so that it fails faster. 43 if not value: 44 raise error.TestFail('No build version specified.') 45 46 47 def run_once(self, host, value, force=False): 48 """The method called by the control file to start the test. 49 50 @param host: The host object to update to |value|. 51 @param value: The host object to provision with a build corresponding 52 to |value|. 53 @param force: True iff we should re-provision the machine regardless of 54 the current image version. If False and the image 55 version matches our expected image version, no 56 provisioning will be done. 57 58 """ 59 logging.debug('Start provisioning %s to %s', host, value) 60 image = value 61 62 # If the host is already on the correct build, we have nothing to do. 63 # Note that this means we're not doing any sort of stateful-only 64 # update, and that we're relying more on cleanup to do cleanup. 65 # We could just not pass |force_update=True| to |machine_install|, 66 # but I'd like the semantics that a provision test 'returns' TestNA 67 # if the machine is already properly provisioned. 68 if not force: 69 info = host.host_info_store.get() 70 if info.build == value: 71 # We can't raise a TestNA, as would make sense, as that makes 72 # job.run_test return False as if the job failed. However, it'd 73 # still be nice to get this into the status.log, so we manually 74 # emit an INFO line instead. 75 self.job.record('INFO', None, None, 76 'Host already running %s' % value) 77 return 78 79 # We're about to reimage a machine, so we need full_payload and 80 # stateful. If something happened where the devserver doesn't have one 81 # of these, then it's also likely that it'll be missing autotest. 82 # Therefore, we require the devserver to also have autotest staged, so 83 # that the test that runs after this provision finishes doesn't error 84 # out because the devserver that its job_repo_url is set to is missing 85 # autotest test code. 86 # TODO(milleral): http://crbug.com/249426 87 # Add an asynchronous staging call so that we can ask the devserver to 88 # fetch autotest in the background here, and then wait on it after 89 # reimaging finishes or at some other point in the provisioning. 90 ds = None 91 try: 92 ds = dev_server.ImageServer.resolve(image, host.hostname) 93 ds.stage_artifacts(image, ['full_payload', 'stateful', 94 'autotest_packages']) 95 except dev_server.DevServerException as e: 96 raise error.TestFail(str(e)) 97 finally: 98 # If a devserver is resolved, Log what has been downloaded so far. 99 if ds: 100 try: 101 ds.list_image_dir(image) 102 except (dev_server.DevServerException, urllib2.URLError) as e2: 103 logging.warning('Failed to list_image_dir for build %s. ' 104 'Error: %s', image, e2) 105 106 url = _IMAGE_URL_PATTERN % (ds.url(), image) 107 108 logging.debug('Installing image') 109 try: 110 afe_utils.machine_install_and_update_labels(host, 111 force_update=True, 112 update_url=url, 113 force_full_update=force) 114 except error.InstallError as e: 115 logging.error(e) 116 raise error.TestFail(str(e)) 117 logging.debug('Finished provisioning %s to %s', host, value) 118