1# Copyright (c) 2012 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
6from autotest_lib.server.cros.faft.firmware_test import FirmwareTest
7from autotest_lib.client.common_lib import error
8
9
10class firmware_UpdateKernelVersion(FirmwareTest):
11    """
12    This is a server based kernel update test which should run in developer
13    mode. On runtime, this test modifies the kernel version of kernel b and
14    modifies cgpt to reboot with kernel b. Check kernel version after reboot,
15    and then recover kernel b version to original version. Here also tries to
16    reboot with kernel b after recovery. If success, reboot with kernel a.
17    """
18    version = 1
19
20    def check_kernel_version(self, expected_ver):
21        actual_ver = self.faft_client.kernel.get_version('b')
22        if actual_ver != expected_ver:
23            raise error.TestFail(
24                'Kernel Version should be %s, but got %s.'
25                % (expected_ver, actual_ver))
26        else:
27            logging.info(
28                'Update success, now version is %s',
29                actual_ver)
30
31    def modify_kernel_b_and_set_cgpt_priority(self, delta, target_dev):
32        if delta == 1:
33            self.faft_client.kernel.move_version_forward('b')
34        elif delta == -1:
35            self.check_kernel_version(self._update_version)
36            self.faft_client.kernel.move_version_backward('b')
37
38        if target_dev == 'a':
39            self.reset_and_prioritize_kernel('a')
40        else:
41            self.reset_and_prioritize_kernel('b')
42
43    def initialize(self, host, cmdline_args, dev_mode=True):
44        super(firmware_UpdateKernelVersion, self).initialize(host, cmdline_args)
45
46        self.switcher.setup_mode('dev' if dev_mode else 'normal')
47
48        actual_ver = self.faft_client.kernel.get_version('b')
49        logging.info('Original Kernel Version of KERN-B is %s', actual_ver)
50
51        self._update_version = actual_ver + 1
52        logging.info('KERN-B will update to version %s', self._update_version)
53
54        self.setup_kernel('a')
55
56    def cleanup(self):
57        super(firmware_UpdateKernelVersion, self).cleanup()
58
59    def run_once(self):
60        logging.info("Update Kernel Version.")
61        self.check_state((self.check_root_part_on_non_recovery, 'a'))
62        self.modify_kernel_b_and_set_cgpt_priority(1, 'b')
63        self.switcher.mode_aware_reboot()
64
65        logging.info("Check kernel version and rollback.")
66        self.check_state((self.check_root_part_on_non_recovery, 'b'))
67        self.modify_kernel_b_and_set_cgpt_priority(-1, 'b')
68        self.switcher.mode_aware_reboot()
69
70        logging.info("Boot with rollback kernel and change boot priority.")
71        self.check_state((self.check_root_part_on_non_recovery, 'b'))
72        self.modify_kernel_b_and_set_cgpt_priority(0, 'a')
73        self.switcher.mode_aware_reboot()
74
75        logging.info("Check rollback version.")
76        self.check_state((self.check_root_part_on_non_recovery, 'a'))
77        self.check_kernel_version(self._update_version - 1)
78