1# Lint as: python2, python3
2# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6import os, re
7
8from autotest_lib.client.bin import test, utils
9from autotest_lib.client.common_lib import error
10
11class hardware_DiskFirmwareUpgrade(test.test):
12    """
13    Run the disk firmware upgrade script.
14    """
15    TEST_SCRIPT = '/usr/sbin/chromeos-disk-firmware-update.sh'
16    UPGRADED_RE = r'^Upgraded.*'
17    version = 1
18
19    def run_once(self,
20                 disk_firmware_package='/opt/google/disk/firmware',
21                 expected_result=0,
22                 upgrade_required=True):
23        """
24        Runs the shell script that upgrade disk firmware.
25
26        @param disk_firmware_package: pre-installed firmware package location.
27        @param expected_result:       expected results of the upgrade.
28        @param upgrade_required:      if True, the firmware must change on the
29                                      device.
30        """
31        status_file = os.path.join(self.resultsdir, 'status')
32        cmd = [self.TEST_SCRIPT,
33               '--status %s' % (status_file),
34               '--fw_package_dir %s' % (disk_firmware_package)]
35        fw_upgrade = utils.run(' '.join(cmd), ignore_status=True)
36
37        # Check the result of the upgrade.
38        upgrade_happened = False
39        try:
40            with open(status_file) as sf:
41                for l in sf:
42                    if re.match(self.UPGRADED_RE, l):
43                        upgrade_happened = True
44        except IOError:
45            pass
46        if fw_upgrade.exit_status != expected_result:
47            raise error.TestError(
48                'Expected %d Result is %d' % (
49                    expected_result, fw_upgrade.exit_status))
50        if (fw_upgrade.exit_status == 0 and
51            upgrade_required and not upgrade_happened):
52            raise error.TestError('Expected upgrade did not happened')
53