def test_is_reachable(self, mock_setup, mock_run_cmd): cm = machine_manager.CrosMachine('daisy.cros', '/usr/local/chromeos', 'average', self.mock_cmd_exec) self.mock_cmd_exec.CrosRunCommand = mock_run_cmd # Test 1. CrosRunCommand returns 1 (fail) mock_run_cmd.return_value = 1 result = cm.IsReachable() self.assertFalse(result) self.assertEqual(mock_setup.call_count, 1) self.assertEqual(mock_run_cmd.call_count, 1) # Test 2. CrosRunCommand returns 0 (success) mock_run_cmd.return_value = 0 result = cm.IsReachable() self.assertTrue(result) self.assertEqual(mock_run_cmd.call_count, 2) first_args = mock_run_cmd.call_args_list[0] second_args = mock_run_cmd.call_args_list[1] self.assertEqual(first_args[0], second_args[0]) self.assertEqual(first_args[1], second_args[1]) self.assertEqual(len(first_args[0]), 1) self.assertEqual(len(first_args[1]), 2) self.assertEqual(first_args[0][0], 'ls') args_dict = first_args[1] self.assertEqual(args_dict['machine'], 'daisy.cros') self.assertEqual(args_dict['chromeos_root'], '/usr/local/chromeos')
def test_setup_checksum_info(self, mock_md5sum, mock_machineid, mock_checkstring, mock_cpuinfo, mock_meminfo, mock_isreachable): # Test 1. Machine is not reachable; SetUpChecksumInfo is called via # __init__. mock_isreachable.return_value = False mock_md5sum.return_value = 'md5_checksum' cm = machine_manager.CrosMachine('daisy.cros', '/usr/local/chromeos', 'average', self.mock_cmd_exec) cm.checksum_string = 'This is a checksum string.' cm.machine_id = 'machine_id1' self.assertEqual(mock_isreachable.call_count, 1) self.assertIsNone(cm.machine_checksum) self.assertEqual(mock_meminfo.call_count, 0) # Test 2. Machine is reachable. Call explicitly. mock_isreachable.return_value = True cm.checksum_string = 'This is a checksum string.' cm.machine_id = 'machine_id1' cm.SetUpChecksumInfo() self.assertEqual(mock_isreachable.call_count, 2) self.assertEqual(mock_meminfo.call_count, 1) self.assertEqual(mock_cpuinfo.call_count, 1) self.assertEqual(mock_checkstring.call_count, 1) self.assertEqual(mock_machineid.call_count, 1) self.assertEqual(mock_md5sum.call_count, 2) self.assertEqual(cm.machine_checksum, 'md5_checksum') self.assertEqual(cm.machine_id_checksum, 'md5_checksum') self.assertEqual(mock_md5sum.call_args_list[0][0][0], 'This is a checksum string.') self.assertEqual(mock_md5sum.call_args_list[1][0][0], 'machine_id1')
def test_init(self, mock_setup): cm = machine_manager.CrosMachine('daisy.cros', '/usr/local/chromeos', 'average', self.mock_cmd_exec) self.assertEqual(mock_setup.call_count, 1) self.assertEqual(cm.chromeos_root, '/usr/local/chromeos') self.assertEqual(cm.log_level, 'average')
def test_compute_machine_checksum_string(self, _mock_setup): cm = machine_manager.CrosMachine('daisy.cros', '/usr/local/chromeos', 'average', self.mock_cmd_exec) cm.cpuinfo = CPUINFO_STRING cm.meminfo = MEMINFO_STRING cm._ParseMemoryInfo() cm._ComputeMachineChecksumString() self.assertEqual(cm.checksum_string, CHECKSUM_STRING)
def test_get_md5_checksum(self, _mock_setup): cm = machine_manager.CrosMachine('daisy.cros', '/usr/local/chromeos', 'average', self.mock_cmd_exec) temp_str = 'abcde' checksum_str = cm._GetMD5Checksum(temp_str) self.assertEqual(checksum_str, 'ab56b4d92b40713acc5af89985d4b786') temp_str = '' checksum_str = cm._GetMD5Checksum(temp_str) self.assertEqual(checksum_str, '')
def test_get_cpu_info(self, _mock_setup, mock_run_cmd): cm = machine_manager.CrosMachine('daisy.cros', '/usr/local/chromeos', 'average', self.mock_cmd_exec) self.mock_cmd_exec.CrosRunCommandWOutput = mock_run_cmd mock_run_cmd.return_value = [0, CPUINFO_STRING, ''] cm._GetCPUInfo() self.assertEqual(mock_run_cmd.call_count, 1) call_args = mock_run_cmd.call_args_list[0] self.assertEqual(call_args[0][0], 'cat /proc/cpuinfo') args_dict = call_args[1] self.assertEqual(args_dict['machine'], 'daisy.cros') self.assertEqual(args_dict['chromeos_root'], '/usr/local/chromeos') self.assertEqual(cm.cpuinfo, CPUINFO_STRING)
def test_get_memory_info(self, _mock_setup, mock_run_cmd): cm = machine_manager.CrosMachine('daisy.cros', '/usr/local/chromeos', 'average', self.mock_cmd_exec) self.mock_cmd_exec.CrosRunCommandWOutput = mock_run_cmd mock_run_cmd.return_value = [0, MEMINFO_STRING, ''] cm._GetMemoryInfo() self.assertEqual(mock_run_cmd.call_count, 1) call_args = mock_run_cmd.call_args_list[0] self.assertEqual(call_args[0][0], 'cat /proc/meminfo') args_dict = call_args[1] self.assertEqual(args_dict['machine'], 'daisy.cros') self.assertEqual(args_dict['chromeos_root'], '/usr/local/chromeos') self.assertEqual(cm.meminfo, MEMINFO_STRING) self.assertEqual(cm.phys_kbytes, 4194304) mock_run_cmd.return_value = [1, MEMINFO_STRING, ''] self.assertRaises(cm._GetMemoryInfo)
def test_get_machine_id(self, _mock_setup, mock_run_cmd): cm = machine_manager.CrosMachine('daisy.cros', '/usr/local/chromeos', 'average', self.mock_cmd_exec) self.mock_cmd_exec.CrosRunCommandWOutput = mock_run_cmd mock_run_cmd.return_value = [0, DUMP_VPD_STRING, ''] cm._GetMachineID() self.assertEqual(cm.machine_id, '"Product_S/N"="HT4L91SC300208"') mock_run_cmd.return_value = [0, IFCONFIG_STRING, ''] cm._GetMachineID() self.assertEqual( cm.machine_id, ' ether 00:50:b6:63:db:65 txqueuelen 1000 (Ethernet)_ ' 'ether e8:03:9a:9c:50:3d txqueuelen 1000 (Ethernet)_ ether ' '44:6d:57:20:4a:c5 txqueuelen 1000 (Ethernet)') mock_run_cmd.return_value = [0, 'invalid hardware config', ''] self.assertRaises(cm._GetMachineID)
def test_parse_memory_info(self, _mock_setup): cm = machine_manager.CrosMachine('daisy.cros', '/usr/local/chromeos', 'average', self.mock_cmd_exec) cm.meminfo = MEMINFO_STRING cm._ParseMemoryInfo() self.assertEqual(cm.phys_kbytes, 4194304)