def testNoNodeNameTwoDevicesRaiseExceptionAmbiguousTarget( self, mock_daemon_stop): with DeviceTarget.CreateFromArgs(self.args) as device_target_instance, \ mock.patch.object(FfxRunner, 'list_targets') as mock_list_targets, \ self.assertRaisesRegex(Exception, \ 'More than one device was discovered'): mock_list_targets.return_value = [{ "nodename": "device_name1", "rcs_state": "Y", "serial": "<unknown>", "target_type": "terminal.qemu-x64", "target_state": "Product", "addresses": ["address1"] }, { "nodename": "device_name2", "rcs_state": "Y", "serial": "<unknown>", "target_type": "terminal.qemu-x64", "target_state": "Product", "addresses": ["address2"] }] device_target_instance.Start() self.assertIsNone(device_target_instance._node_name) self.assertIsNone(device_target_instance._host) mock_daemon_stop.assert_called_once()
def testNoDevicesFoundRaiseExceptionCouldNotFind(self): with DeviceTarget.CreateFromArgs(self.args) as device_target_instance, \ mock.patch.object(FfxRunner, 'list_targets') as mock_list_targets, \ self.assertRaisesRegex(Exception, 'Could not find device.'): mock_list_targets.return_value = [] device_target_instance.Start() self.assertIsNone(device_target_instance._node_name) self.assertIsNone(device_target_instance._host)
def testNodeNameDefinedDeviceNotFoundRaiseExceptionCouldNotFind(self): self.args.node_name = 'wrong_device_name' with DeviceTarget.CreateFromArgs(self.args) as device_target_instance, \ mock.patch('subprocess.Popen') as mock_popen, \ self.assertRaisesRegex(Exception, 'Could not find device.'): mock_popen.returncode = ('', '') device_target_instance.Start() self.assertIsNone(device_target_instance._node_name) self.assertIsNone(device_target_instance._host)
def testProvisionIfOneNonDetectableDevice(self): self.args.os_check = 'update' self.args.node_name = 'mocknode' self.args.system_image_dir = 'mockdir' with DeviceTarget.CreateFromArgs(self.args) as device_target_instance, \ mock.patch('subprocess.Popen') as mock_popen, \ mock.patch.object(DeviceTarget, '_ProvisionDevice') as mock_provision: mock_popen.returncode = ('', '') device_target_instance.Start() self.assertEqual(mock_provision.call_count, 1)
def testNoDevicesFoundRaiseExceptionCouldNotFind(self): with DeviceTarget.CreateFromArgs(self.args) as device_target_instance, \ mock.patch.object(DeviceTarget, 'RunFFXCommand') as mock_ffx, \ self.assertRaisesRegex(Exception, 'Could not find device.'): mock_spec_popen = mock.create_autospec(subprocess.Popen, instance=True) mock_spec_popen.communicate.return_value = ('', '') mock_spec_popen.returncode = 0 mock_ffx.return_value = mock_spec_popen device_target_instance.Start() self.assertIsNone(device_target_instance._node_name) self.assertIsNone(device_target_instance._host)
def testProvisionIfOneNonDetectableDevice(self): self.args.os_check = 'update' self.args.node_name = 'mocknode' self.args.system_image_dir = 'mockdir' with DeviceTarget.CreateFromArgs(self.args) as device_target_instance, \ mock.patch.object( FfxTarget, 'get_ssh_address') as mock_get_ssh_address, \ mock.patch.object(DeviceTarget, '_ProvisionDevice') as mock_provision: mock_get_ssh_address.side_effect = subprocess.CalledProcessError( 1, 'ffx', 'Timeout attempting to reach target "mocknode"') device_target_instance.Start() self.assertEqual(mock_provision.call_count, 1)
def testNodeNameDefinedDeviceNotFoundRaiseExceptionCouldNotFind(self): self.args.node_name = 'wrong_device_name' with DeviceTarget.CreateFromArgs(self.args) as device_target_instance, \ mock.patch.object( FfxTarget, 'get_ssh_address') as mock_get_ssh_address, \ self.assertRaisesRegex(Exception, 'Could not find device.'): mock_get_ssh_address.side_effect = subprocess.CalledProcessError( 1, 'ffx', 'Timeout attempting to reach target "wrong_device_name"') device_target_instance.Start() self.assertIsNone(device_target_instance._node_name) self.assertIsNone(device_target_instance._host)
def testNodeNameDefinedDeviceFoundReturnNoneCheckNameAndHost(self): self.args.node_name = 'device_name' with DeviceTarget.CreateFromArgs(self.args) as device_target_instance, \ mock.patch('subprocess.Popen') as mock_popen, \ mock.patch.object(Target, '_WaitUntilReady') as mock_waituntilready: mock_popen.return_value.communicate.return_value = ('address', 'device_name') mock_popen.return_value.returncode = 0 mock_waituntilready.return_value = True self.assertIsNone(device_target_instance.Start()) self.assertEqual(device_target_instance._node_name, 'device_name') self.assertEqual(device_target_instance._host, 'address')
def testNodeNameDefinedDeviceFoundReturnNoneCheckNameAndHost(self): self.args.node_name = 'device_name' with DeviceTarget.CreateFromArgs(self.args) as device_target_instance, \ mock.patch.object( FfxTarget, 'get_ssh_address') as mock_get_ssh_address, \ mock.patch.object( DeviceTarget, '_ConnectToTarget') as mock_connecttotarget: mock_get_ssh_address.return_value = ('address', 12345) mock_connecttotarget.return_value = True self.assertIsNone(device_target_instance.Start()) self.assertEqual(device_target_instance._node_name, 'device_name') self.assertEqual(device_target_instance._host, 'address') self.assertEqual(device_target_instance._port, 12345)
def testNoNodeNameOneDeviceReturnNoneCheckNameAndAddress(self): with DeviceTarget.CreateFromArgs(self.args) as device_target_instance, \ mock.patch.object(DeviceTarget, 'RunFFXCommand') as mock_ffx, \ mock.patch.object(Target, '_WaitUntilReady') as mock_waituntilready: mock_spec_popen = mock.create_autospec(subprocess.Popen, instance=True) mock_spec_popen.communicate.return_value = ('address device_name', '') mock_spec_popen.returncode = 0 mock_ffx.return_value = mock_spec_popen mock_waituntilready.return_value = True self.assertIsNone(device_target_instance.Start()) self.assertEqual(device_target_instance._node_name, 'device_name') self.assertEqual(device_target_instance._host, 'address')
def testNoNodeNameDeviceDoesntHaveNameRaiseExceptionCouldNotFind(self): with (DeviceTarget.CreateFromArgs( self.args)) as device_target_instance: with mock.patch.object(DeviceTarget, 'RunFFXCommand') as mock_ffx: mock_spec_popen = mock.create_autospec(subprocess.Popen, instance=True) mock_spec_popen.communicate.return_value = ('address', '') mock_spec_popen.returncode = 0 mock_ffx.return_value = mock_spec_popen with self.assertRaisesRegex(Exception, 'Could not find device'): device_target_instance.Start() self.assertIsNone(device_target_instance._node_name) self.assertIsNone(device_target_instance._host)
def testNoNodeNameTwoDevicesRaiseExceptionAmbiguousTarget(self): with DeviceTarget.CreateFromArgs(self.args) as device_target_instance, \ mock.patch.object(DeviceTarget, 'RunFFXCommand') as mock_ffx, \ self.assertRaisesRegex(Exception, \ 'More than one device was discovered'): mock_spec_popen = mock.create_autospec(subprocess.Popen, instance=True) mock_spec_popen.communicate.return_value = ( 'address1 device_name1\n' 'address2 device_name2', '') mock_spec_popen.returncode = 0 mock_ffx.return_value = mock_spec_popen device_target_instance.Start() self.assertIsNone(device_target_instance._node_name) self.assertIsNone(device_target_instance._host)
def testNoNodeNameDeviceDoesntHaveNameRaiseExceptionCouldNotFind(self): with DeviceTarget.CreateFromArgs(self.args) as device_target_instance, \ mock.patch.object(FfxRunner, 'list_targets') as mock_list_targets, \ self.assertRaisesRegex(Exception, 'Could not find device.'): mock_list_targets.return_value = [{ "nodename": "<unknown>", "rcs_state": "Y", "serial": "<unknown>", "target_type": "terminal.qemu-x64", "target_state": "Product", "addresses": ["address"] }] device_target_instance.Start() self.assertIsNone(device_target_instance._node_name) self.assertIsNone(device_target_instance._host)
def testNoProvisionDeviceIfVersionsMatch(self): self.args.os_check = 'update' self.args.system_image_dir = 'mockdir' with DeviceTarget.CreateFromArgs(self.args) as device_target_instance, \ mock.patch.object(DeviceTarget, '_Discover') as mock_discover, \ mock.patch.object(DeviceTarget, '_WaitUntilReady') as mock_ready, \ mock.patch.object(DeviceTarget, '_GetSdkHash') as mock_hash, \ mock.patch.object( DeviceTarget, '_GetInstalledSdkVersion') as mock_version, \ mock.patch.object(DeviceTarget, '_ProvisionDevice') as mock_provision: mock_discover.return_value = True mock_hash.return_value = '1.0' mock_version.return_value = '1.0' device_target_instance.Start() self.assertEqual(mock_provision.call_count, 0)
def testRaiseExceptionIfCheckVersionsNoMatch(self): self.args.os_check = 'check' self.args.system_image_dir = 'mockdir' with DeviceTarget.CreateFromArgs(self.args) as device_target_instance, \ mock.patch.object(DeviceTarget, '_Discover') as mock_discover, \ mock.patch.object(DeviceTarget, '_WaitUntilReady') as mock_ready, \ mock.patch.object(DeviceTarget, '_GetSdkHash') as mock_hash, \ mock.patch.object( DeviceTarget, '_GetInstalledSdkVersion') as mock_version, \ mock.patch.object( DeviceTarget, '_ProvisionDevice') as mock_provision, \ self.assertRaisesRegex(Exception, 'Image and Fuchsia version'): mock_discover.return_value = True mock_hash.return_value = '2.0' mock_version.return_value = '1.0' device_target_instance.Start()
def testNoNodeNameTwoDevicesRaiseExceptionAmbiguousTarget(self): with (DeviceTarget.CreateFromArgs( self.args)) as device_target_instance: with mock.patch.object(DeviceTarget, 'RunFFXCommand') as mock_ffx: mock_spec_popen = mock.create_autospec(subprocess.Popen, instance=True) mock_spec_popen.communicate.return_value = ( 'address1 device_name1\n' 'address2 device_name2', '') mock_spec_popen.returncode = 0 mock_spec_popen.stdout = '' mock_ffx.return_value = mock_spec_popen with self.assertRaisesRegex( Exception, 'Ambiguous target device specification.'): device_target_instance.Start() self.assertIsNone(device_target_instance._node_name) self.assertIsNone(device_target_instance._host)
def testNoNodeNameOneDeviceReturnNoneCheckNameAndAddress(self): with DeviceTarget.CreateFromArgs(self.args) as device_target_instance, \ mock.patch.object(FfxRunner, 'list_targets') as mock_list_targets, \ mock.patch.object( FfxTarget, 'get_ssh_address') as mock_get_ssh_address, \ mock.patch.object( DeviceTarget, '_ConnectToTarget') as mock_connecttotarget: mock_list_targets.return_value = [{ "nodename": "device_name", "rcs_state": "Y", "serial": "<unknown>", "target_type": "terminal.qemu-x64", "target_state": "Product", "addresses": ["address"] }] mock_get_ssh_address.return_value = ('address', 12345) mock_connecttotarget.return_value = True self.assertIsNone(device_target_instance.Start()) self.assertEqual(device_target_instance._node_name, 'device_name') self.assertEqual(device_target_instance._host, 'address') self.assertEqual(device_target_instance._port, 12345)