1# Copyright 2018 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
6import math
7import time
8
9from autotest_lib.client.common_lib import error
10from autotest_lib.client.cros.enterprise import enterprise_au_context
11from autotest_lib.client.cros.enterprise import enterprise_policy_base
12
13
14class policy_DeviceAutoUpdateDisabled(
15        enterprise_policy_base.EnterprisePolicyTest):
16    """Test for the DeviceAutoUpdateDisabled policy."""
17    version = 1
18    _POLICY = 'DeviceAutoUpdateDisabled'
19
20
21    def _test_update_disabled(self, should_update):
22        """
23        Main test function.
24
25        Try to update and poll for start (or lack of start) to the update.
26        Check whether an update request was sent.
27
28        @param should_update: True or False whether the device should update.
29
30        """
31        # Log time is only in second accuracy.  Assume no update request has
32        # occured since the current whole second started.
33        start_time = math.floor(time.time())
34        logging.info('Update test start time: %s', start_time)
35
36        try:
37            self._au_context.update_and_poll_for_update_start()
38        except error.TestFail as e:
39            if should_update:
40                raise e
41        else:
42            if not should_update:
43                raise error.TestFail('Update started when it should not have!')
44
45        update_time = self._au_context.get_time_of_last_update_request()
46        logging.info('Last update time: %s', update_time)
47
48        if should_update and (not update_time or update_time < start_time):
49            raise error.TestFail('No update request was sent!')
50        if not should_update and update_time and update_time >= start_time:
51            raise error.TestFail('Update request was sent!')
52
53
54    def run_once(self, case, image_url, image_size, sha256, enroll=True):
55        """
56        Entry point of this test.
57
58        @param case: True, False, or None for the value of the update policy.
59        @param image_url: Url of update image (this build).
60        @param image_size: Size of the update.
61        @param sha256: Sha256 hash of the update.
62
63        """
64        self.setup_case(device_policies={self._POLICY: case}, enroll=enroll)
65
66        self._au_context = enterprise_au_context.NanoOmahaEnterpriseAUContext(
67                image_url=image_url, image_size=image_size, sha256=sha256)
68
69        # When policy is False or not set, user should update.
70        self._test_update_disabled(should_update=case is not True)
71