1*9c5db199SXin Liimport re 2*9c5db199SXin Liimport unittest 3*9c5db199SXin Lifrom unittest import mock 4*9c5db199SXin Li 5*9c5db199SXin Liimport common 6*9c5db199SXin Li 7*9c5db199SXin Lifrom autotest_lib.server.hosts import servo_host 8*9c5db199SXin Lifrom autotest_lib.server.hosts import servo_constants 9*9c5db199SXin Li 10*9c5db199SXin Li 11*9c5db199SXin Liclass MockCmd(object): 12*9c5db199SXin Li """Simple mock command with base command and results""" 13*9c5db199SXin Li 14*9c5db199SXin Li def __init__(self, cmd, exit_status, stdout): 15*9c5db199SXin Li self.cmd = cmd 16*9c5db199SXin Li self.stdout = stdout 17*9c5db199SXin Li self.exit_status = exit_status 18*9c5db199SXin Li 19*9c5db199SXin Li 20*9c5db199SXin Liclass MockHost(servo_host.ServoHost): 21*9c5db199SXin Li """Simple host for running mock'd host commands""" 22*9c5db199SXin Li 23*9c5db199SXin Li def __init__(self, *args): 24*9c5db199SXin Li self._mock_cmds = {c.cmd: c for c in args} 25*9c5db199SXin Li self._init_attributes() 26*9c5db199SXin Li self.hostname = "chromeos1-row1-rack1-host1" 27*9c5db199SXin Li self._dut_hostname = 'dut-' + self.hostname 28*9c5db199SXin Li self.servo_port = '9991' 29*9c5db199SXin Li self._is_localhost = False 30*9c5db199SXin Li self._use_icmp = True 31*9c5db199SXin Li self._is_containerized_servod = False 32*9c5db199SXin Li 33*9c5db199SXin Li def run(self, command, **kwargs): 34*9c5db199SXin Li """Finds the matching result by command value""" 35*9c5db199SXin Li mock_cmd = self._mock_cmds[command] 36*9c5db199SXin Li file_out = kwargs.get('stdout_tee', None) 37*9c5db199SXin Li if file_out: 38*9c5db199SXin Li file_out.write(mock_cmd.stdout) 39*9c5db199SXin Li return mock_cmd 40*9c5db199SXin Li 41*9c5db199SXin Li 42*9c5db199SXin Liclass ServoHostServoStateTestCase(unittest.TestCase): 43*9c5db199SXin Li """Tests to verify changing the servo_state""" 44*9c5db199SXin Li def test_return_none_if_state_not_defined(self): 45*9c5db199SXin Li host = MockHost() 46*9c5db199SXin Li self.assertIsNotNone(host) 47*9c5db199SXin Li self.assertIsNone(host._servo_state) 48*9c5db199SXin Li self.assertIsNone(host.get_servo_state()) 49*9c5db199SXin Li self.assertEqual(host._servo_state, None) 50*9c5db199SXin Li 51*9c5db199SXin Li def test_verify_set_state_broken_if_raised_error(self): 52*9c5db199SXin Li host = MockHost() 53*9c5db199SXin Li host._is_localhost = False 54*9c5db199SXin Li host._repair_strategy = mock.Mock() 55*9c5db199SXin Li host._repair_strategy.verify.side_effect = Exception('something_ex') 56*9c5db199SXin Li try: 57*9c5db199SXin Li host.verify(silent=True) 58*9c5db199SXin Li self.assertEqual("Should not be reached", 'expecting error') 59*9c5db199SXin Li except: 60*9c5db199SXin Li pass 61*9c5db199SXin Li self.assertEqual(host.get_servo_state(), 62*9c5db199SXin Li servo_constants.SERVO_STATE_BROKEN) 63*9c5db199SXin Li 64*9c5db199SXin Li def test_verify_set_state_working_if_no_raised_error(self): 65*9c5db199SXin Li host = MockHost() 66*9c5db199SXin Li host._repair_strategy = mock.Mock() 67*9c5db199SXin Li host.verify(silent=True) 68*9c5db199SXin Li self.assertEqual(host.get_servo_state(), 69*9c5db199SXin Li servo_constants.SERVO_STATE_WORKING) 70*9c5db199SXin Li 71*9c5db199SXin Li def test_repair_set_state_broken_if_raised_error(self): 72*9c5db199SXin Li host = MockHost() 73*9c5db199SXin Li host._is_localhost = False 74*9c5db199SXin Li host._repair_strategy = mock.Mock() 75*9c5db199SXin Li host._repair_strategy.repair.side_effect = Exception('something_ex') 76*9c5db199SXin Li try: 77*9c5db199SXin Li host.repair(silent=True) 78*9c5db199SXin Li self.assertEqual("Should not be reached", 'expecting error') 79*9c5db199SXin Li except: 80*9c5db199SXin Li pass 81*9c5db199SXin Li self.assertEqual(host.get_servo_state(), 82*9c5db199SXin Li servo_constants.SERVO_STATE_BROKEN) 83*9c5db199SXin Li 84*9c5db199SXin Li def test_repair_set_state_working_if_no_raised_error(self): 85*9c5db199SXin Li host = MockHost() 86*9c5db199SXin Li host._is_labstation = False 87*9c5db199SXin Li host._repair_strategy = mock.Mock() 88*9c5db199SXin Li host.repair(silent=True) 89*9c5db199SXin Li self.assertEqual(host.get_servo_state(), 90*9c5db199SXin Li servo_constants.SERVO_STATE_WORKING) 91*9c5db199SXin Li 92*9c5db199SXin Li 93*9c5db199SXin Liclass ServoHostInformationValidator(unittest.TestCase): 94*9c5db199SXin Li """Tests to verify logic in servo host data""" 95*9c5db199SXin Li def test_true_when_host_and_port_is_correct(self): 96*9c5db199SXin Li port = 9999 97*9c5db199SXin Li hostname = 'chromeos1-rack1-row1-host1-servo' 98*9c5db199SXin Li self.assertTrue(servo_host.is_servo_host_information_valid(hostname, port)) 99*9c5db199SXin Li hostname = 'CHROMEOS1-RACK1-ROW1-host1-SERVO' 100*9c5db199SXin Li self.assertTrue(servo_host.is_servo_host_information_valid(hostname, port)) 101*9c5db199SXin Li hostname = '96.120.0.567' 102*9c5db199SXin Li self.assertTrue(servo_host.is_servo_host_information_valid(hostname, port)) 103*9c5db199SXin Li hostname = 'locathost' 104*9c5db199SXin Li self.assertTrue(servo_host.is_servo_host_information_valid(hostname, port)) 105*9c5db199SXin Li hostname = 'my.dut-1' 106*9c5db199SXin Li self.assertTrue(servo_host.is_servo_host_information_valid(hostname, port)) 107*9c5db199SXin Li hostname = '192.168.0.1:8022' 108*9c5db199SXin Li self.assertTrue( 109*9c5db199SXin Li servo_host.is_servo_host_information_valid(hostname, port)) 110*9c5db199SXin Li # diff ports 111*9c5db199SXin Li self.assertTrue(servo_host.is_servo_host_information_valid(hostname, 7000)) 112*9c5db199SXin Li self.assertTrue(servo_host.is_servo_host_information_valid(hostname, 1234)) 113*9c5db199SXin Li self.assertTrue(servo_host.is_servo_host_information_valid(hostname, 1)) 114*9c5db199SXin Li self.assertTrue(servo_host.is_servo_host_information_valid(hostname, 15000)) 115*9c5db199SXin Li # port as string in case of local testing 116*9c5db199SXin Li self.assertTrue(servo_host.is_servo_host_information_valid(hostname, '7000')) 117*9c5db199SXin Li self.assertTrue(servo_host.is_servo_host_information_valid(hostname, '1234')) 118*9c5db199SXin Li self.assertTrue(servo_host.is_servo_host_information_valid(hostname, '1')) 119*9c5db199SXin Li self.assertTrue(servo_host.is_servo_host_information_valid(hostname, ' 15000')) 120*9c5db199SXin Li self.assertTrue(servo_host.is_servo_host_information_valid(hostname, ' 07000')) 121*9c5db199SXin Li self.assertTrue(servo_host.is_servo_host_information_valid(hostname, ' 01234 ')) 122*9c5db199SXin Li self.assertTrue(servo_host.is_servo_host_information_valid(hostname, ' 01 ')) 123*9c5db199SXin Li self.assertTrue(servo_host.is_servo_host_information_valid(hostname, '015000')) 124*9c5db199SXin Li 125*9c5db199SXin Li def test_false_when_host_is_incorrect_and_port_is_correct(self): 126*9c5db199SXin Li port = '9991' 127*9c5db199SXin Li self.assertFalse( 128*9c5db199SXin Li servo_host.is_servo_host_information_valid('ch1%ra1$r1.h1.servo', port)) 129*9c5db199SXin Li self.assertFalse( 130*9c5db199SXin Li servo_host.is_servo_host_information_valid('[undefined]', port)) 131*9c5db199SXin Li self.assertFalse( 132*9c5db199SXin Li servo_host.is_servo_host_information_valid('None', port)) 133*9c5db199SXin Li self.assertFalse( 134*9c5db199SXin Li servo_host.is_servo_host_information_valid('', port)) 135*9c5db199SXin Li self.assertFalse( 136*9c5db199SXin Li servo_host.is_servo_host_information_valid(None, port)) 137*9c5db199SXin Li 138*9c5db199SXin Li def test_false_when_port_is_incorrect_and_host_is_correct(self): 139*9c5db199SXin Li hostname = 'Some_host-my' 140*9c5db199SXin Li self.assertFalse(servo_host.is_servo_host_information_valid(hostname, None)) 141*9c5db199SXin Li self.assertFalse(servo_host.is_servo_host_information_valid(hostname, -1)) 142*9c5db199SXin Li self.assertFalse(servo_host.is_servo_host_information_valid(hostname, 0)) 143*9c5db199SXin Li self.assertFalse(servo_host.is_servo_host_information_valid(hostname, None)) 144*9c5db199SXin Li self.assertFalse(servo_host.is_servo_host_information_valid(hostname, 'a1234')) 145*9c5db199SXin Li self.assertFalse(servo_host.is_servo_host_information_valid(hostname, 'o1234')) 146*9c5db199SXin Li self.assertFalse(servo_host.is_servo_host_information_valid(hostname, '71234')) 147*9c5db199SXin Li self.assertFalse(servo_host.is_servo_host_information_valid(hostname, '71.24')) 148*9c5db199SXin Li self.assertFalse(servo_host.is_servo_host_information_valid(hostname, '71-24')) 149*9c5db199SXin Li self.assertFalse(servo_host.is_servo_host_information_valid(hostname, '-234')) 150*9c5db199SXin Li self.assertFalse(servo_host.is_servo_host_information_valid(hostname, '-234.9')) 151*9c5db199SXin Li 152*9c5db199SXin Li 153*9c5db199SXin Liclass ServoHostInformationExistor(unittest.TestCase): 154*9c5db199SXin Li """Tests to verify logic in servo host present""" 155*9c5db199SXin Li def test_true_when_host_is_correct(self): 156*9c5db199SXin Li port = 9999 157*9c5db199SXin Li hostname = 'chromeos1-rack1-row1-host1-servo' 158*9c5db199SXin Li self.assertTrue(servo_host._is_servo_host_information_exist(hostname, port)) 159*9c5db199SXin Li hostname = 'CHROMEOS1' 160*9c5db199SXin Li self.assertTrue(servo_host._is_servo_host_information_exist(hostname, port)) 161*9c5db199SXin Li hostname = '!@#$%^&*()' 162*9c5db199SXin Li self.assertTrue(servo_host._is_servo_host_information_exist(hostname, port)) 163*9c5db199SXin Li 164*9c5db199SXin Li def test_true_when_port_is_correct(self): 165*9c5db199SXin Li hostname = 'chromeos1-rack1-row1-host1-servo' 166*9c5db199SXin Li port = 9999 167*9c5db199SXin Li self.assertTrue(servo_host._is_servo_host_information_exist(hostname, 9999)) 168*9c5db199SXin Li self.assertTrue(servo_host._is_servo_host_information_exist(hostname, '9999')) 169*9c5db199SXin Li self.assertTrue(servo_host._is_servo_host_information_exist(hostname, 1234)) 170*9c5db199SXin Li self.assertTrue(servo_host._is_servo_host_information_exist(hostname, '1234')) 171*9c5db199SXin Li self.assertTrue(servo_host._is_servo_host_information_exist(hostname, ' 1234 ')) 172*9c5db199SXin Li self.assertTrue(servo_host._is_servo_host_information_exist(hostname, '01234')) 173*9c5db199SXin Li self.assertTrue(servo_host._is_servo_host_information_exist(hostname, '01234')) 174*9c5db199SXin Li 175*9c5db199SXin Li def test_false_when_port_was_not_set_up(self): 176*9c5db199SXin Li hostname = 'chromeos1%rack1$row1.host1.servo' 177*9c5db199SXin Li self.assertFalse(servo_host._is_servo_host_information_exist(hostname, '')) 178*9c5db199SXin Li self.assertFalse(servo_host._is_servo_host_information_exist(hostname, None)) 179*9c5db199SXin Li self.assertFalse(servo_host._is_servo_host_information_exist(hostname, "")) 180*9c5db199SXin Li 181*9c5db199SXin Li def test_false_when_host_was_not_set_up(self): 182*9c5db199SXin Li port = 1234 183*9c5db199SXin Li self.assertFalse(servo_host._is_servo_host_information_exist('', port)) 184*9c5db199SXin Li self.assertFalse(servo_host._is_servo_host_information_exist(None, port)) 185*9c5db199SXin Li self.assertFalse(servo_host._is_servo_host_information_exist(' ', port)) 186*9c5db199SXin Li 187*9c5db199SXin Li 188*9c5db199SXin Liclass ValidateUSBCPigtailRegex(unittest.TestCase): 189*9c5db199SXin Li """Tests to verify logic in servo host present""" 190*9c5db199SXin Li def test_good_cases(self): 191*9c5db199SXin Li host = MockHost() 192*9c5db199SXin Li message = "[475635.476044 PD TMOUT RX 1/1]" 193*9c5db199SXin Li self.assertTrue(bool(re.match(host.USBC_PIGTAIL_TIMEOUT_RE, message))) 194*9c5db199SXin Li message = "[475635.476044654 PD TMOUT RX 1/1]" 195*9c5db199SXin Li self.assertTrue(bool(re.match(host.USBC_PIGTAIL_TIMEOUT_RE, message))) 196*9c5db199SXin Li message = "475635.476044654 PD TMOUT RX 1/1" 197*9c5db199SXin Li self.assertFalse(bool(re.match(host.USBC_PIGTAIL_TIMEOUT_RE, message))) 198*9c5db199SXin Li 199*9c5db199SXin Li def test_bad_cases(self): 200*9c5db199SXin Li host = MockHost() 201*9c5db199SXin Li message = "PD TMOUT RX 1/1" 202*9c5db199SXin Li self.assertFalse(bool(re.match(host.USBC_PIGTAIL_TIMEOUT_RE, message))) 203*9c5db199SXin Li message = "[PD TMOUT RX 1/1]" 204*9c5db199SXin Li self.assertFalse(bool(re.match(host.USBC_PIGTAIL_TIMEOUT_RE, message))) 205*9c5db199SXin Li message = "PD TMOUT RX" 206*9c5db199SXin Li self.assertFalse(bool(re.match(host.USBC_PIGTAIL_TIMEOUT_RE, message))) 207*9c5db199SXin Li message = "something other" 208*9c5db199SXin Li self.assertFalse(bool(re.match(host.USBC_PIGTAIL_TIMEOUT_RE, message))) 209*9c5db199SXin Li 210*9c5db199SXin Li 211*9c5db199SXin Liif __name__ == '__main__': 212*9c5db199SXin Li unittest.main() 213