1#!/usr/bin/env vpython3
2# Copyright 2021 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5"""Tests scenarios with number of devices and invalid devices"""
6import subprocess
7import unittest
8import unittest.mock as mock
9from argparse import Namespace
10from device_target import DeviceTarget
11from target import Target
12
13
14class TestDiscoverDeviceTarget(unittest.TestCase):
15  def setUp(self):
16    self.args = Namespace(out_dir='out/fuchsia',
17                          target_cpu='x64',
18                          host=None,
19                          node_name=None,
20                          port=None,
21                          ssh_config=None,
22                          fuchsia_out_dir=None,
23                          os_check='update',
24                          system_log_file=None)
25
26  def testNoNodeNameOneDeviceReturnNoneCheckNameAndAddress(self):
27    with (DeviceTarget.CreateFromArgs(self.args)) as device_target_instance:
28      with mock.patch.object(DeviceTarget, 'RunFFXCommand') as mock_ffx:
29        mock_spec_popen = mock.create_autospec(subprocess.Popen, instance=True)
30        mock_spec_popen.communicate.return_value = ('address device_name', '')
31        mock_spec_popen.returncode = 0
32        mock_ffx.return_value = mock_spec_popen
33        with mock.patch.object(Target,
34                               '_WaitUntilReady') as mock_waituntilready:
35          mock_waituntilready.return_value = True
36          self.assertIsNone(device_target_instance.Start())
37          self.assertEqual(device_target_instance._node_name, 'device_name')
38          self.assertEqual(device_target_instance._host, 'address')
39
40  def testNoNodeNameTwoDevicesRaiseExceptionAmbiguousTarget(self):
41    with (DeviceTarget.CreateFromArgs(self.args)) as device_target_instance:
42      with mock.patch.object(DeviceTarget, 'RunFFXCommand') as mock_ffx:
43        mock_spec_popen = mock.create_autospec(subprocess.Popen, instance=True)
44        mock_spec_popen.communicate.return_value = ('address1 device_name1\n'
45                                                    'address2 device_name2', '')
46        mock_spec_popen.returncode = 0
47        mock_spec_popen.stdout = ''
48        mock_ffx.return_value = mock_spec_popen
49        with self.assertRaisesRegex(Exception,
50                                    'Ambiguous target device specification.'):
51          device_target_instance.Start()
52          self.assertIsNone(device_target_instance._node_name)
53          self.assertIsNone(device_target_instance._host)
54
55  def testNoNodeNameDeviceDoesntHaveNameRaiseExceptionCouldNotFind(self):
56    with (DeviceTarget.CreateFromArgs(self.args)) as device_target_instance:
57      with mock.patch.object(DeviceTarget, 'RunFFXCommand') as mock_ffx:
58        mock_spec_popen = mock.create_autospec(subprocess.Popen, instance=True)
59        mock_spec_popen.communicate.return_value = ('address', '')
60        mock_spec_popen.returncode = 0
61        mock_ffx.return_value = mock_spec_popen
62        with self.assertRaisesRegex(Exception, 'Could not find device'):
63          device_target_instance.Start()
64          self.assertIsNone(device_target_instance._node_name)
65          self.assertIsNone(device_target_instance._host)
66
67  def testNodeNameDefinedDeviceFoundReturnNoneCheckNameAndHost(self):
68    self.args.node_name = 'device_name'
69    with (DeviceTarget.CreateFromArgs(self.args)) as device_target_instance:
70      with mock.patch('subprocess.Popen') as mock_popen:
71        mock_popen.returncode = ('address', 'device_name')
72        with mock.patch.object(Target,
73                               '_WaitUntilReady') as mock_waituntilready:
74          mock_waituntilready.return_value = True
75          self.assertIsNone(device_target_instance.Start())
76          self.assertEqual(device_target_instance._node_name, 'device_name')
77          self.assertEqual(device_target_instance._host, 'address')
78
79  def testNodeNameDefinedDeviceNotFoundRaiseExceptionCouldNotFind(self):
80    self.args.node_name = 'wrong_device_name'
81    with (DeviceTarget.CreateFromArgs(self.args)) as device_target_instance:
82      with mock.patch('subprocess.Popen') as mock_popen:
83        mock_popen.returncode = ('', '')
84        with self.assertRaisesRegex(Exception, 'Could not find device'):
85          device_target_instance.Start()
86          self.assertIsNone(device_target_instance._node_name)
87          self.assertIsNone(device_target_instance._host)
88
89  def testNoDevicesFoundRaiseExceptionCouldNotFind(self):
90    with (DeviceTarget.CreateFromArgs(self.args)) as device_target_instance:
91      with mock.patch.object(DeviceTarget, 'RunFFXCommand') as mock_ffx:
92        mock_spec_popen = mock.create_autospec(subprocess.Popen, instance=True)
93        mock_spec_popen.communicate.return_value = ('', '')
94        mock_spec_popen.returncode = 0
95        mock_ffx.return_value = mock_spec_popen
96        with self.assertRaisesRegex(Exception, 'Could not find device'):
97          device_target_instance.Start()
98          self.assertIsNone(device_target_instance._node_name)
99          self.assertIsNone(device_target_instance._host)
100
101
102if __name__ == '__main__':
103  unittest.main()
104