1import mock 2import unittest 3 4import common 5 6from autotest_lib.server.hosts import servo_host 7 8 9class MockCmd(object): 10 """Simple mock command with base command and results""" 11 12 def __init__(self, cmd, exit_status, stdout): 13 self.cmd = cmd 14 self.stdout = stdout 15 self.exit_status = exit_status 16 17 18class MockHost(servo_host.ServoHost): 19 """Simple host for running mock'd host commands""" 20 21 def __init__(self, *args): 22 self._mock_cmds = {c.cmd: c for c in args} 23 self._init_attributes() 24 self.hostname = "some_hostname" 25 26 def run(self, command, **kwargs): 27 """Finds the matching result by command value""" 28 mock_cmd = self._mock_cmds[command] 29 file_out = kwargs.get('stdout_tee', None) 30 if file_out: 31 file_out.write(mock_cmd.stdout) 32 return mock_cmd 33 34 35class ServoHostServoStateTestCase(unittest.TestCase): 36 """Tests to verify changing the servo_state""" 37 def test_return_broken_if_state_not_defined(self): 38 host = MockHost() 39 self.assertIsNotNone(host) 40 self.assertIsNone(host._servo_state) 41 self.assertIsNotNone(host.get_servo_state()) 42 self.assertEqual(host.get_servo_state(), servo_host.SERVO_STATE_BROKEN) 43 44 def test_verify_set_state_broken_if_raised_error(self): 45 host = MockHost() 46 host._is_localhost = True 47 host._repair_strategy = mock.Mock() 48 host._repair_strategy.verify.side_effect = Exception('something_ex') 49 try: 50 host.verify(silent=True) 51 self.assertEqual("Should not be reached", 'expecting error') 52 except: 53 pass 54 self.assertEqual(host.get_servo_state(), servo_host.SERVO_STATE_BROKEN) 55 56 def test_verify_set_state_working_if_no_raised_error(self): 57 host = MockHost() 58 host._repair_strategy = mock.Mock() 59 host.verify(silent=True) 60 self.assertEqual(host.get_servo_state(), servo_host.SERVO_STATE_WORKING) 61 62 def test_repair_set_state_broken_if_raised_error(self): 63 host = MockHost() 64 host._is_localhost = True 65 host._repair_strategy = mock.Mock() 66 host._repair_strategy.repair.side_effect = Exception('something_ex') 67 try: 68 host.repair(silent=True) 69 self.assertEqual("Should not be reached", 'expecting error') 70 except: 71 pass 72 self.assertEqual(host.get_servo_state(), servo_host.SERVO_STATE_BROKEN) 73 74 def test_repair_set_state_working_if_no_raised_error(self): 75 host = MockHost() 76 host._is_labstation = False 77 host._repair_strategy = mock.Mock() 78 host.repair(silent=True) 79 self.assertEqual(host.get_servo_state(), servo_host.SERVO_STATE_WORKING) 80 81 82if __name__ == '__main__': 83 unittest.main() 84