Home | History | Annotate | Download | only in provision_TestbedUpdate
      1 # Copyright 2016 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 
      5 import logging
      6 
      7 from autotest_lib.client.common_lib import error
      8 from autotest_lib.server import afe_utils
      9 from autotest_lib.server import test
     10 
     11 
     12 class provision_TestbedUpdate(test.test):
     13     """A test that can provision a machine to the correct Android version."""
     14     version = 1
     15 
     16     def _builds_to_set(self, builds):
     17         """Helper function to convert a build string into a set of builds.
     18 
     19         @param builds: Testbed build string to convert into a set.
     20 
     21         @returns: A set of the different builds in the build string.
     22         """
     23         result = set()
     24         if not builds:
     25             return result
     26         builds = builds.split(',')
     27         for build in builds:
     28             # Remove any build multipliers, i.e. <build>#2
     29             build = build.split('#')[0]
     30             result.add(build)
     31         return result
     32 
     33 
     34     def initialize(self, host, value, force=False, is_test_na=False,
     35                    repair=False):
     36         """Initialize.
     37 
     38         @param host: The testbed object to update to |value|.
     39                      NOTE: This arg must be called host to align with the other
     40                            provision actions.
     41         @param value: String of the image we want to install on the testbed.
     42         @param force: not used by initialize.
     43         @param is_test_na: boolean, if True, will simply skip the test
     44                            and emit TestNAError. The control file
     45                            determines whether the test should be skipped
     46                            and passes the decision via this argument. Note
     47                            we can't raise TestNAError in control file as it won't
     48                            be caught and handled properly.
     49         @param repair: not used by initialize.
     50         """
     51         if is_test_na:
     52             raise error.TestNAError('Provisioning not applicable.')
     53         # We check value in initialize so that it fails faster.
     54         if not (value or repair):
     55             raise error.TestFail('No build version specified.')
     56 
     57 
     58     def run_once(self, host, value=None, force=False, repair=False):
     59         """The method called by the control file to start the test.
     60 
     61         @param host: The testbed object to update to |value|.
     62                      NOTE: This arg must be called host to align with the other
     63                            provision actions.
     64         @param value: The testbed object to provision with a build
     65                       corresponding to |value|.
     66         @param force: True iff we should re-provision the machine regardless of
     67                       the current image version.  If False and the image
     68                       version matches our expected image version, no
     69                       provisioning will be done.
     70         @param repair: Not yet supported for testbeds.
     71 
     72         """
     73         testbed = host
     74         logging.debug('Start provisioning %s to %s', testbed, value)
     75 
     76         if not value and not repair:
     77             raise error.TestFail('No build provided and this is not a repair '
     78                                  ' job.')
     79 
     80         if not force:
     81             info = testbed.host_info_store.get()
     82             # If the host is already on the correct build, we have nothing to
     83             # do.
     84             if self._builds_to_set(info.build) == self._builds_to_set(value):
     85                 # We can't raise a TestNA, as would make sense, as that makes
     86                 # job.run_test return False as if the job failed.  However, it'd
     87                 # still be nice to get this into the status.log, so we manually
     88                 # emit an INFO line instead.
     89                 self.job.record('INFO', None, None,
     90                                 'Testbed already running %s' % value)
     91                 return
     92 
     93         try:
     94             afe_utils.machine_install_and_update_labels(
     95                     host, image=value)
     96         except error.InstallError as e:
     97             logging.exception(e)
     98             raise error.TestFail(str(e))
     99         logging.debug('Finished provisioning %s to %s', host, value)
    100