1#!/usr/bin/env python2 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 time 7import unittest 8import mock 9 10import common 11from autotest_lib.server.cros.storage import storage_validate 12from autotest_lib.server.cros.device_health_profile import device_health_profile 13from autotest_lib.server.cros.device_health_profile import profile_constants 14 15 16class MockHostInfoStore(object): 17 def __init__(self): 18 self.board = 'mock_board' 19 self.model = 'mock_model' 20 21 22class MockHost(object): 23 def __init__(self, hostname): 24 self.hostname = hostname 25 self.host_info_store = mock.Mock() 26 self.host_info_store.get.return_value = MockHostInfoStore() 27 self.job = None 28 29 def check_cached_up_status(self): 30 return True 31 32 def is_up(self): 33 return True 34 35 def send_file(self, source, dest): 36 return True 37 38 def get_file(self, source, dest): 39 return True 40 41 def is_file_exists(self, file_path): 42 return False 43 44 45def create_device_health_profile(): 46 servohost = MockHost('dummy_servohost_hostname') 47 dhp = device_health_profile.DeviceHealthProfile( 48 hostname='dummy_dut_hostname', 49 host_info=MockHostInfoStore(), 50 result_dir=None) 51 dhp.init_profile(servohost) 52 return dhp 53 54 55def _add_days_to_time(secs, days): 56 new_time = time.localtime(secs + (days * 24 * 60 * 60)) 57 return time.strftime(profile_constants.TIME_PATTERN, new_time) 58 59 60class BadblocksRunReadyTestCase(unittest.TestCase): 61 dhp = create_device_health_profile() 62 63 def test_is_time_to_run_badblocks_ro(self): 64 self.dhp.refresh_badblocks_ro_run_time() 65 last_time = self.dhp.get_badblocks_ro_run_time_epoch() 66 # sleep for a second to make difference from now to avoid flakiness 67 time.sleep(1) 68 self.assertFalse( 69 storage_validate._is_time_to_run_badblocks_ro(self.dhp)) 70 # set 5 days ago 71 self.dhp._update_profile( 72 profile_constants.LAST_BADBLOCKS_RO_RUN_TIME_KEY, 73 _add_days_to_time(last_time, -5)) 74 self.assertFalse( 75 storage_validate._is_time_to_run_badblocks_ro(self.dhp)) 76 # set 6 days ago 77 self.dhp._update_profile( 78 profile_constants.LAST_BADBLOCKS_RO_RUN_TIME_KEY, 79 _add_days_to_time(last_time, -6)) 80 self.assertTrue(storage_validate._is_time_to_run_badblocks_ro( 81 self.dhp)) 82 # set 7 days ago 83 self.dhp._update_profile( 84 profile_constants.LAST_BADBLOCKS_RO_RUN_TIME_KEY, 85 _add_days_to_time(last_time, -7)) 86 self.assertTrue(storage_validate._is_time_to_run_badblocks_ro( 87 self.dhp)) 88 89 def test_is_time_to_run_badblocks_rw(self): 90 self.dhp.refresh_badblocks_rw_run_time() 91 last_time = self.dhp.get_badblocks_rw_run_time_epoch() 92 # sleep for a second to make difference from now to avoid flakiness 93 time.sleep(1) 94 self.assertFalse( 95 storage_validate._is_time_to_run_badblocks_rw(self.dhp)) 96 # set 59 days ago 97 self.dhp._update_profile( 98 profile_constants.LAST_BADBLOCKS_RW_RUN_TIME_KEY, 99 _add_days_to_time(last_time, -59)) 100 self.assertFalse( 101 storage_validate._is_time_to_run_badblocks_rw(self.dhp)) 102 # set 60 days ago 103 self.dhp._update_profile( 104 profile_constants.LAST_BADBLOCKS_RW_RUN_TIME_KEY, 105 _add_days_to_time(last_time, -60)) 106 self.assertTrue(storage_validate._is_time_to_run_badblocks_rw( 107 self.dhp)) 108 # set 61 days ago 109 self.dhp._update_profile( 110 profile_constants.LAST_BADBLOCKS_RW_RUN_TIME_KEY, 111 _add_days_to_time(last_time, -61)) 112 self.assertTrue(storage_validate._is_time_to_run_badblocks_rw( 113 self.dhp)) 114 115 116if __name__ == '__main__': 117 unittest.main() 118