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