1# Lint as: python2, python3 2# Copyright 2022 The Chromium OS 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 6import unittest 7 8from unittest.mock import Mock, patch 9from autotest_lib.server.cros.tradefed import adb 10 11 12class AdbTest(unittest.TestCase): 13 """Tests for ADB module.""" 14 15 # Verify that ipv4 is put into IP:PORT format. 16 def test_get_adb_target_ipv4(self): 17 mock_host = Mock() 18 mock_host.port = 3467 19 mock_host.hostname = '123.76.0.29' 20 target = adb.get_adb_target(mock_host) 21 self.assertEqual(target, '123.76.0.29:3467') 22 23 # Verify that ipv6 is put into [IP]:PORT format. 24 def test_get_adb_target_ipv6(self): 25 mock_host = Mock() 26 mock_host.port = 1234 27 mock_host.hostname = '2409::3' 28 target = adb.get_adb_target(mock_host) 29 self.assertEqual(target, '[2409::3]:1234') 30 31 # Verify that a host name works. 32 def test_get_adb_target_hostname(self): 33 mock_host = Mock() 34 mock_host.port = 4792 35 mock_host.hostname = 'some.hostname.cros' 36 target = adb.get_adb_target(mock_host) 37 self.assertEqual(target, 'some.hostname.cros:4792') 38 39 # Verify that a list of hosts work. 40 def test_get_adb_targets(self): 41 mock_host1 = Mock() 42 mock_host2 = Mock() 43 mock_host3 = Mock() 44 mock_host1.port = 1111 45 mock_host2.port = 2222 46 mock_host3.port = 3333 47 mock_host1.hostname = 'host1' 48 mock_host2.hostname = 'host2' 49 mock_host3.hostname = 'host3' 50 51 targets = adb.get_adb_targets([mock_host1, mock_host2, mock_host3]) 52 self.assertEqual(targets, ['host1:1111', 'host2:2222', 'host3:3333']) 53 54 def test_add_paths(self): 55 instance = adb.Adb() 56 instance.add_path('/some/install/path') 57 instance.add_path('/another/directory') 58 59 self.assertEqual(set(['/some/install/path', '/another/directory']), 60 instance.get_paths()) 61 62 @patch('autotest_lib.server.utils.run') 63 def test_run(self, mock_run): 64 instance = adb.Adb() 65 instance.add_path('/some/install/path') 66 67 mock_host = Mock() 68 mock_host.port = 3467 69 mock_host.hostname = '123.76.0.29' 70 71 instance.run(mock_host, args=('some', 'command'), timeout=240) 72 mock_run.assert_called_with('adb', 73 args=('-s', '123.76.0.29:3467', 'some', 74 'command'), 75 timeout=240, 76 extra_paths=['/some/install/path']) 77 78 @patch('autotest_lib.server.utils.run') 79 def test_run_without_host(self, mock_run): 80 instance = adb.Adb() 81 instance.add_path('/some/install/path') 82 83 instance.run(None, args=('some', 'command'), timeout=240) 84 mock_run.assert_called_with('adb', 85 args=('-H', 'localhost', '-P', '5037', 86 'some', 'command'), 87 timeout=240, 88 extra_paths=['/some/install/path']) 89