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. 4 5import time 6 7from autotest_lib.client.bin import utils 8from autotest_lib.client.common_lib import error 9from autotest_lib.client.cros.enterprise import enterprise_policy_base 10 11 12class policy_DriveDisabled( 13 enterprise_policy_base.EnterprisePolicyTest): 14 """ 15 Test effect of policy_DriveDisabled policy on Chrome OS. 16 17 This test will set the policy, then check if the google drive is mounted 18 or not. 19 20 """ 21 version = 1 22 23 POLICY_NAME = 'DriveDisabled' 24 case_value_lookup = {'enable': False, 25 'disable': True, 26 'not_set': None} 27 28 def run_once(self, case=None): 29 """ 30 Setup and run the test configured for the specified test case. 31 32 @param case: Name of the test case to run. 33 34 """ 35 case = self.case_value_lookup[case] 36 37 self.setup_case(user_policies={self.POLICY_NAME: case}, 38 extra_chrome_flags=['--enable-features=DriveFS'], 39 real_gaia=True) 40 41 self.check_mount(case) 42 43 def check_mount(self, case): 44 """ 45 Poll for the drive setting. If the case is True (ie disabled), wait 46 another few seconds to ensure the drive doesn't start with a delay. 47 48 @param case: Value of the DriveDisabled setting. 49 50 """ 51 if case: 52 e_msg = 'Should not have found mountpoint but did!' 53 else: 54 e_msg = 'Should have found mountpoint but did not!' 55 # It may take some time until drivefs is started, so poll for the 56 # mountpoint until timeout. 57 utils.poll_for_condition( 58 lambda: self.is_drive_properly_set(case), 59 exception=error.TestFail(e_msg), 60 timeout=10, 61 sleep_interval=1, 62 desc='Polling for page to load.') 63 64 # Due to this being a negative case, and the poll_for would likely 65 # return True immediately, we should wait the maximum duration and do 66 # a final check for the mount. 67 if case: 68 time.sleep(10) 69 70 mountpoint = self._find_drivefs_mount() 71 72 if case and mountpoint: 73 raise error.TestFail(e_msg) 74 if not case and not mountpoint: 75 raise error.TestFail(e_msg) 76 77 def is_drive_properly_set(self, case): 78 """ 79 Checks if the drive status is proper vs the policy settings.policy 80 81 @param case: Value of the DriveDisabled setting. 82 83 """ 84 if case: 85 if not self._find_drivefs_mount(): 86 return True 87 else: 88 if self._find_drivefs_mount(): 89 return True 90 return False 91 92 def _find_drivefs_mount(self): 93 """Return the mount point of the drive if found, else return None.""" 94 for mount in utils.mounts(): 95 if mount['type'] == 'fuse.drivefs': 96 return mount['dest'] 97 return None 98