1# Lint as: python2, python3
2# Copyright (c) 2021 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
6from autotest_lib.client.common_lib import error
7from autotest_lib.client.cros.update_engine import nebraska_wrapper
8from autotest_lib.client.cros.update_engine import update_engine_test
9
10
11class autoupdate_InvalidateSuccessfulUpdate(update_engine_test.UpdateEngineTest
12                                            ):
13    """Tests installing an update and then invalidating it."""
14
15    version = 1
16
17    def _apply_update(self, update_url):
18        """
19        Performs the update and ensures it is successful.
20
21        @param update_url: The URL to get an update.
22
23        """
24        try:
25            self._check_for_update(update_url,
26                                   critical_update=True,
27                                   wait_for_completion=True)
28        except error.CmdError as e:
29            raise error.TestFail('Update attempt failed: %s' %
30                                 self._get_last_error_string())
31
32    def _check_invalidated_update(self, update_url):
33        """
34        Performs an update check and confirms that it results
35        in an invalidated update.
36
37        @param update_url: The URL to get an update.
38
39        """
40        try:
41            self._check_for_update(update_url,
42                                   critical_update=True,
43                                   wait_for_completion=False)
44            self._wait_for_update_to_idle(check_kernel_after_update=True,
45                                          inactive_kernel=False)
46        except error.CmdError as e:
47            raise error.TestFail('Invalidate attempt failed: %s' %
48                                 self._get_last_error_string())
49        self._check_update_engine_log_for_entry(
50                'Invalidating previous update.',
51                raise_error=True,
52                err_str='Failed to invalidate previous update')
53
54    def run_once(self, payload_url):
55        """
56        Runs an update and then invalidates it using Nebraska.
57
58        @param payload_url: Path to a payload on Google storage.
59
60        """
61        with nebraska_wrapper.NebraskaWrapper(
62                log_dir=self.resultsdir, payload_url=payload_url) as nebraska:
63            self._apply_update(nebraska.get_update_url())
64            nebraska.update_config(invalidate_last_update=True)
65            self._check_invalidated_update(nebraska.get_update_url())
66