1# Copyright 2019 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.
4from autotest_lib.client.common_lib import error
5from autotest_lib.client.common_lib import utils
6from autotest_lib.client.cros.enterprise import enterprise_policy_base
7
8from telemetry.core import exceptions
9
10
11class policy_ReportUploadFrequency(
12        enterprise_policy_base.EnterprisePolicyTest):
13    """
14    Tests the ReportUploadFrequency policy in Chrome OS.
15
16    """
17
18    version = 1
19
20
21    def initialize(self, **kwargs):
22        super(policy_ReportUploadFrequency, self).initialize(**kwargs)
23
24        self.POLICY_NAME = 'ReportUploadFrequency'
25        self.POLICIES = {}
26        self.TEST_CASES = {
27            '60s': 60000,
28        }
29
30
31    def _check_report_upload_frequency(self):
32        """
33        Grep syslog for "Starting status upload: have_device_status = 1" line
34
35        @param case_value: policy value in milliseconds
36
37        """
38
39        def is_log_present():
40            """
41            Checks to see if logs have been written.
42
43            @returns True if written False if not.
44
45            """
46            try:
47                if 'Starting status upload: has_device_status = 1' in open(
48                    '/var/log/messages').read():
49                        return True
50            except exceptions.EvaluateException:
51                return False
52
53        utils.poll_for_condition(
54            lambda: is_log_present(),
55            exception=error.TestFail('No status upload sent.'),
56            timeout=60,
57            sleep_interval=5,
58            desc='Polling for logs to be written.')
59
60
61    def run_once(self, case):
62        """
63        Setup and run the test configured for the specified test case.
64
65        @param case: Name of the test case to run.
66
67        """
68
69        case_value = self.TEST_CASES[case]
70        self.POLICIES[self.POLICY_NAME] = case_value
71
72        self.setup_case(device_policies=self.POLICIES, enroll=True)
73        self._check_report_upload_frequency()
74