• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python3
2# pylint: disable=missing-docstring
3
4import time
5import unittest
6
7import common
8from autotest_lib.server.cros.device_health_profile import device_health_profile
9from autotest_lib.server.cros.device_health_profile import profile_constants
10
11
12class MockHostInfoStore(object):
13    def __init__(self):
14        self.board = 'mock_board'
15        self.model = 'mock_model'
16
17
18class MockHost(object):
19    def __init__(self, hostname):
20        self.hostname = hostname
21
22    def check_cached_up_status(self):
23        return True
24
25    def is_up(self):
26        return True
27
28    def send_file(self, source, dest):
29        return True
30
31    def get_file(self, source, dest):
32        return True
33
34    def is_file_exists(self, file_path):
35        return False
36
37    def is_containerized_servod(self):
38        return False
39
40
41def create_device_health_profile():
42    servohost = MockHost('placeholder_servohost_hostname')
43    host_info = MockHostInfoStore()
44    dhp = device_health_profile.DeviceHealthProfile(
45            hostname='placeholder_dut_hostname',
46            host_info=host_info,
47            result_dir=None)
48    dhp.init_profile(servohost)
49    return dhp
50
51
52class DeviceHealthProfileTestCase(unittest.TestCase):
53    dhp = create_device_health_profile()
54
55    def _sleep(self):
56        """Sleep to create a difference in timestamp between updates."""
57        # Set 2 seconds as 1 seconds brought the flakiness of the tests.
58        time.sleep(2)
59
60    def test_shows_not_loaded_till_profile_host_provided(self):
61        host_info = MockHostInfoStore()
62        dhp = device_health_profile.DeviceHealthProfile(
63                hostname='placeholder_dut_hostname',
64                host_info=host_info,
65                result_dir=None)
66        self.assertFalse(dhp.is_loaded())
67
68    def test_set_loaded_when_provide_profile_host_provided(self):
69        dhp = create_device_health_profile()
70        self.assertTrue(dhp.is_loaded())
71
72    def test_validate_device_health_profile_data(self):
73        profile_data = self.dhp.health_profile
74        self.assertEqual(self.dhp._validate_profile_data(profile_data), True)
75
76    def test_get_board(self):
77        self.assertEqual(self.dhp.get_board(), 'mock_board')
78
79    def test_get_model(self):
80        self.assertEqual(self.dhp.get_model(), 'mock_model')
81
82    def test_get_profile_version(self):
83        self.assertEqual(self.dhp.get_profile_version(),
84                         profile_constants.PROFILE_VERSION)
85
86    def test_dut_state(self):
87        self.assertEqual(self.dhp.get_dut_state(),
88                         profile_constants.DEFAULT_STRING_VALUE)
89        self.dhp.update_dut_state('test_state_1')
90        self.assertEqual(self.dhp.get_dut_state(), 'test_state_1')
91
92    def test_servo_state(self):
93        self.assertEqual(self.dhp.get_servo_state(),
94                         profile_constants.DEFAULT_STRING_VALUE)
95        self.dhp.update_servo_state('servod_issue')
96        self.assertEqual(self.dhp.get_servo_state(), 'servod_issue')
97
98    def test_cros_stable_version(self):
99        self.assertEqual(self.dhp.get_cros_stable_version(),
100                         profile_constants.DEFAULT_STRING_VALUE)
101        self.dhp.set_cros_stable_version('placeholder-release/R80-10000.0.0')
102        self.assertEqual(self.dhp.get_cros_stable_version(),
103                         'placeholder-release/R80-10000.0.0')
104
105    def test_firmware_stable_version(self):
106        self.assertEqual(self.dhp.get_firmware_stable_version(),
107                         profile_constants.DEFAULT_STRING_VALUE)
108        self.dhp.set_firmware_stable_version('placeholder_firmware_release')
109        self.assertEqual(self.dhp.get_firmware_stable_version(),
110                         'placeholder_firmware_release')
111
112    def test_last_update_time(self):
113        cached_time = self.dhp.get_last_update_time()
114        self.assertRegexpMatches(cached_time, r'\d{4}[-/]\d{2}[-/]\d{2}')
115        self._sleep()
116        self.dhp.refresh_update_time()
117        self.assertNotEqual(cached_time, self.dhp.get_last_update_time())
118
119    def test_last_update_time_epoch(self):
120        cached_time_epoch = self.dhp.get_last_update_time_epoch()
121        self.assertEqual(type(cached_time_epoch), int)
122        self._sleep()
123        self.dhp.refresh_update_time()
124        self.assertGreater(self.dhp.get_last_update_time_epoch(),
125                           cached_time_epoch)
126
127    def test_enter_current_state_time(self):
128        cached_time = self.dhp.get_enter_current_state_time()
129        self.assertRegexpMatches(cached_time, r'\d{4}[-/]\d{2}[-/]\d{2}')
130        self._sleep()
131        self.dhp.update_dut_state('test_state_2')
132        self.assertNotEqual(cached_time,
133                            self.dhp.get_enter_current_state_time())
134
135    def test_enter_current_state_time_epoch(self):
136        cached_time_epoch = self.dhp.get_enter_current_state_time_epoch()
137        self.assertEqual(type(cached_time_epoch), int)
138        self._sleep()
139        self.dhp.update_dut_state('test_state_3')
140        self.assertGreater(self.dhp.get_enter_current_state_time_epoch(),
141                           cached_time_epoch)
142
143    def test_repair_fail_count(self):
144        cached_count = self.dhp.get_repair_fail_count()
145        self.dhp.increase_repair_fail_count()
146        self.assertEqual(self.dhp.get_repair_fail_count(), cached_count + 1)
147
148    def test_provision_fail_count(self):
149        cached_count = self.dhp.get_provision_fail_count()
150        self.dhp.increase_provision_fail_count()
151        self.assertEqual(self.dhp.get_provision_fail_count(), cached_count + 1)
152
153    def test_failed_verifiers(self):
154        tag = 'placeholder_verifier'
155        self.assertEqual(self.dhp.get_failed_verifiers(), {})
156        self.assertEqual(self.dhp.get_failed_verifier(tag), 0)
157        self.dhp.insert_failed_verifier(tag)
158        self.assertEqual(self.dhp.get_failed_verifier(tag), 1)
159        self.assertEqual(self.dhp.get_failed_verifiers(),
160                         {'placeholder_verifier': 1})
161
162    def test_succeed_repair_action(self):
163        tag = 'placeholder_succeed_action'
164        self.assertEqual(self.dhp.get_succeed_repair_actions(), {})
165        self.assertEqual(self.dhp.get_succeed_repair_action(tag), 0)
166        self.dhp.insert_succeed_repair_action(tag)
167        self.assertEqual(self.dhp.get_succeed_repair_action(tag), 1)
168        self.assertEqual(self.dhp.get_succeed_repair_actions(),
169                         {'placeholder_succeed_action': 1})
170
171    def test_failed_repair_action(self):
172        tag = 'placeholder_failed_action'
173        self.assertEqual(self.dhp.get_failed_repair_actions(), {})
174        self.assertEqual(self.dhp.get_failed_repair_action(tag), 0)
175        self.dhp.insert_failed_repair_action(tag)
176        self.assertEqual(self.dhp.get_failed_repair_action(tag), 1)
177        self.assertEqual(self.dhp.get_failed_repair_actions(),
178                         {'placeholder_failed_action': 1})
179
180    def test_get_badblocks_ro_run_time(self):
181        cached_time = self.dhp.get_badblocks_ro_run_time()
182        self.assertRegexpMatches(cached_time, r'\d{4}[-/]\d{2}[-/]\d{2}')
183        self._sleep()
184        self.dhp.refresh_badblocks_ro_run_time()
185        self.assertNotEqual(cached_time, self.dhp.get_badblocks_ro_run_time())
186
187    def test_get_badblocks_ro_run_time_epoch(self):
188        cached_time_epoch = self.dhp.get_badblocks_ro_run_time_epoch()
189        self.assertEqual(type(cached_time_epoch), int)
190        self._sleep()
191        self.dhp.refresh_badblocks_ro_run_time()
192        self.assertGreater(self.dhp.get_badblocks_ro_run_time_epoch(),
193                           cached_time_epoch)
194
195    def test_get_badblocks_rw_run_time(self):
196        cached_time = self.dhp.get_badblocks_rw_run_time()
197        self.assertRegexpMatches(cached_time, r'\d{4}[-/]\d{2}[-/]\d{2}')
198        self._sleep()
199        self.dhp.refresh_badblocks_rw_run_time()
200        self.assertNotEqual(cached_time, self.dhp.get_badblocks_rw_run_time())
201
202    def test_get_badblocks_rw_run_time_epoch(self):
203        cached_time_epoch = self.dhp.get_badblocks_rw_run_time_epoch()
204        self.assertEqual(type(cached_time_epoch), int)
205        self._sleep()
206        self.dhp.refresh_badblocks_rw_run_time()
207        self.assertGreater(self.dhp.get_badblocks_rw_run_time_epoch(),
208                           cached_time_epoch)
209
210    def test_get_servo_micro_fw_update_time(self):
211        cached_time = self.dhp.get_servo_micro_fw_update_time()
212        self.assertRegexpMatches(cached_time, r'\d{4}[-/]\d{2}[-/]\d{2}')
213        self._sleep()
214        self.dhp.refresh_servo_miro_fw_update_run_time()
215        self.assertNotEqual(cached_time,
216                            self.dhp.get_servo_micro_fw_update_time())
217
218    def test_get_servo_micro_fw_update_time_epoch(self):
219        cached_time_epoch = self.dhp.get_servo_micro_fw_update_time_epoch()
220        self.assertEqual(type(cached_time_epoch), int)
221        self._sleep()
222        self.dhp.refresh_servo_miro_fw_update_run_time()
223        self.assertGreater(self.dhp.get_servo_micro_fw_update_time_epoch(),
224                           cached_time_epoch)
225
226
227if __name__ == '__main__':
228    unittest.main()
229