David Haddock | 95e7fbe | 2017-12-01 17:49:53 -0800 | [diff] [blame] | 1 | # Copyright 2017 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 json |
| 5 | import logging |
| 6 | import os |
| 7 | import time |
| 8 | |
David Haddock | 95e7fbe | 2017-12-01 17:49:53 -0800 | [diff] [blame] | 9 | from autotest_lib.client.common_lib import lsbrelease_utils |
David Haddock | 95e7fbe | 2017-12-01 17:49:53 -0800 | [diff] [blame] | 10 | from autotest_lib.server import autotest |
David Haddock | 95e7fbe | 2017-12-01 17:49:53 -0800 | [diff] [blame] | 11 | from autotest_lib.server.cros.update_engine import update_engine_test |
| 12 | |
| 13 | |
| 14 | class autoupdate_ForcedOOBEUpdate(update_engine_test.UpdateEngineTest): |
| 15 | """Runs a forced autoupdate during OOBE.""" |
| 16 | version = 1 |
| 17 | |
| 18 | # We override the default lsb-release file. |
| 19 | _CUSTOM_LSB_RELEASE = '/mnt/stateful_partition/etc/lsb-release' |
| 20 | |
| 21 | # Version we tell the DUT it is on before update. |
| 22 | _CUSTOM_LSB_VERSION = '0.0.0.0' |
| 23 | |
| 24 | # Expected hostlog events during update: 4 during rootfs |
| 25 | _ROOTFS_HOSTLOG_EVENTS = 4 |
| 26 | |
| 27 | |
David Haddock | 95e7fbe | 2017-12-01 17:49:53 -0800 | [diff] [blame] | 28 | def cleanup(self): |
David Haddock | 95e7fbe | 2017-12-01 17:49:53 -0800 | [diff] [blame] | 29 | self._host.run('rm %s' % self._CUSTOM_LSB_RELEASE, ignore_status=True) |
| 30 | |
| 31 | # Get the last two update_engine logs: before and after reboot. |
| 32 | files = self._host.run('ls -t -1 ' |
| 33 | '/var/log/update_engine/').stdout.splitlines() |
| 34 | for i in range(2): |
| 35 | self._host.get_file('/var/log/update_engine/%s' % files[i], |
| 36 | self.resultsdir) |
David Haddock | 50dbfee | 2018-01-12 12:43:12 -0800 | [diff] [blame^] | 37 | super(autoupdate_ForcedOOBEUpdate, self).cleanup() |
David Haddock | 95e7fbe | 2017-12-01 17:49:53 -0800 | [diff] [blame] | 38 | |
| 39 | def _get_chromeos_version(self): |
| 40 | """Read the ChromeOS version from /etc/lsb-release.""" |
| 41 | lsb = self._host.run('cat /etc/lsb-release').stdout |
| 42 | return lsbrelease_utils.get_chromeos_release_version(lsb) |
| 43 | |
| 44 | |
David Haddock | 95e7fbe | 2017-12-01 17:49:53 -0800 | [diff] [blame] | 45 | def _create_hostlog_files(self): |
| 46 | """Create the two hostlog files for the update. |
| 47 | |
| 48 | To ensure the update was succesful we need to compare the update |
| 49 | events against expected update events. There is a hostlog for the |
| 50 | rootfs update and for the post reboot update check. |
| 51 | """ |
David Haddock | 1433420 | 2017-12-11 13:50:47 -0800 | [diff] [blame] | 52 | hostlog = self._omaha_devserver.get_hostlog(self._host.ip, |
| 53 | wait_for_reboot_events=True) |
David Haddock | 95e7fbe | 2017-12-01 17:49:53 -0800 | [diff] [blame] | 54 | logging.info('Hostlog: %s', hostlog) |
| 55 | |
| 56 | # File names to save the hostlog events to. |
| 57 | rootfs_hostlog = os.path.join(self.resultsdir, 'hostlog_rootfs') |
| 58 | reboot_hostlog = os.path.join(self.resultsdir, 'hostlog_reboot') |
| 59 | |
| 60 | with open(rootfs_hostlog, 'w') as outfile: |
| 61 | json.dump(hostlog[:self._ROOTFS_HOSTLOG_EVENTS], outfile) |
| 62 | with open(reboot_hostlog, 'w') as outfile: |
| 63 | json.dump(hostlog[self._ROOTFS_HOSTLOG_EVENTS:], outfile) |
| 64 | return rootfs_hostlog, reboot_hostlog |
| 65 | |
| 66 | |
| 67 | def _wait_for_update_to_complete(self): |
| 68 | """Wait for the update that started to complete. |
| 69 | |
| 70 | Repeated check status of update. It should move from DOWNLOADING to |
| 71 | FINALIZING to COMPLETE to IDLE. |
| 72 | """ |
| 73 | while True: |
| 74 | status = self._host.run('update_engine_client --status', |
| 75 | ignore_timeout=True, |
| 76 | timeout=10) |
| 77 | |
| 78 | # During reboot, status will be None |
| 79 | if status is not None: |
| 80 | status = status.stdout.splitlines() |
| 81 | logging.debug(status) |
| 82 | if "UPDATE_STATUS_IDLE" in status[2]: |
| 83 | break |
| 84 | time.sleep(1) |
| 85 | |
| 86 | |
| 87 | def run_once(self, host, job_repo_url=None): |
| 88 | self._host = host |
David Haddock | 50dbfee | 2018-01-12 12:43:12 -0800 | [diff] [blame^] | 89 | update_url = self.get_update_url_for_test(job_repo_url, |
| 90 | full_payload=True, |
| 91 | critical_update=True) |
| 92 | logging.info('Update url: %s', update_url) |
David Haddock | 95e7fbe | 2017-12-01 17:49:53 -0800 | [diff] [blame] | 93 | before = self._get_chromeos_version() |
| 94 | |
| 95 | # Call client test to start the forced OOBE update. |
| 96 | client_at = autotest.Autotest(self._host) |
| 97 | client_at.run_test('autoupdate_StartOOBEUpdate', |
David Haddock | 50dbfee | 2018-01-12 12:43:12 -0800 | [diff] [blame^] | 98 | image_url=update_url) |
David Haddock | 95e7fbe | 2017-12-01 17:49:53 -0800 | [diff] [blame] | 99 | |
| 100 | # Don't continue the test if the client failed for any reason. |
| 101 | client_at._check_client_test_result(self._host, |
| 102 | 'autoupdate_StartOOBEUpdate') |
| 103 | |
| 104 | self._wait_for_update_to_complete() |
| 105 | |
| 106 | # Verify that the update completed successfully by checking hostlog. |
| 107 | rootfs_hostlog, reboot_hostlog = self._create_hostlog_files() |
| 108 | self.verify_update_events(self._CUSTOM_LSB_VERSION, rootfs_hostlog) |
| 109 | self.verify_update_events(self._CUSTOM_LSB_VERSION, reboot_hostlog, |
| 110 | self._CUSTOM_LSB_VERSION) |
| 111 | |
| 112 | after = self._get_chromeos_version() |
| 113 | logging.info('Successfully force updated from %s to %s.', before, after) |