1# Lint as: python2, python3
2# Copyright 2020 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 logging
7import os
8
9from autotest_lib.client.bin import utils
10from autotest_lib.client.common_lib import error
11from autotest_lib.client.cros.update_engine import nebraska_wrapper
12from autotest_lib.client.cros.update_engine import update_engine_test
13
14
15class autoupdate_PeriodicCheck(update_engine_test.UpdateEngineTest):
16    """Tests update_engine's periodic update check mechanism."""
17    version = 1
18
19    _PERIODIC_INTERVAL_PREF = 'test-update-check-interval-timeout'
20    _PERIODIC_LOG = ('Unofficial build, but periodic update check interval '
21                     'timeout is defined, so update is not blocked.')
22
23    def cleanup(self):
24        """Cleans up the state and extra files this test created."""
25        self._remove_update_engine_pref(self._PERIODIC_INTERVAL_PREF)
26        self._clear_custom_lsb_release()
27        super(autoupdate_PeriodicCheck, self).cleanup()
28
29    def run_once(self, payload_url, periodic_interval):
30        """
31        Tests update_engine's periodic update check.
32
33        @param payload_url: The payload url.
34        @param periodic_interval: Seconds between periodic update checks.
35
36        """
37        # Setup the DUT for the test.
38        pref_file = os.path.join(self._UPDATE_ENGINE_PREFS_DIR,
39                                 self._PERIODIC_INTERVAL_PREF)
40        utils.run(['echo', str(periodic_interval), '>', pref_file])
41        utils.run(['touch', '/home/chronos/.oobe_completed'])
42
43        with nebraska_wrapper.NebraskaWrapper(
44                log_dir=self.resultsdir, payload_url=payload_url) as nebraska:
45
46            logging.info('Setting first update response to return no update.')
47            nebraska.update_config(no_update=True)
48            self._create_custom_lsb_release(nebraska.get_update_url())
49            self._restart_update_engine()
50
51            # Wait for the first update check.
52            try:
53                utils.poll_for_condition(
54                    lambda: len(self._get_update_requests()) == 1,
55                    desc='1st periodic update check.',
56                    timeout=1.5 * periodic_interval)
57            except utils.TimeoutError:
58                raise error.TestFail('1st periodic check not found.')
59            self._check_update_engine_log_for_entry(self._PERIODIC_LOG,
60                                                    raise_error=True)
61            logging.info('First periodic update was initiated.')
62
63            logging.info('Setting the next update response to be an update.')
64            nebraska.update_config(no_update=False)
65
66            # Wait for the subsequent update checks.
67            try:
68                utils.poll_for_condition(lambda: len(self._get_update_requests(
69                )) > 1,
70                                         desc='2nd periodic update check.',
71                                         timeout=2 * periodic_interval)
72                logging.info(
73                        'Setting further update responses back to no update.')
74                nebraska.update_config(no_update=True)
75            except utils.TimeoutError:
76                raise error.TestFail('2nd periodic check not found.')
77            logging.info('Second periodic update was initiated.')
78            self._wait_for_update_to_complete()
79