1*6777b538SAndroid Build Coastguard Worker#!/usr/bin/env vpython3 2*6777b538SAndroid Build Coastguard Worker 3*6777b538SAndroid Build Coastguard Worker# Copyright 2023 The Chromium Authors 4*6777b538SAndroid Build Coastguard Worker# Use of this source code is governed by a BSD-style license that can be 5*6777b538SAndroid Build Coastguard Worker# found in the LICENSE file. 6*6777b538SAndroid Build Coastguard Worker"""Tests the connection of a target.""" 7*6777b538SAndroid Build Coastguard Worker 8*6777b538SAndroid Build Coastguard Worker# Note, this is a temporary tool and should be removed in favor of a better way 9*6777b538SAndroid Build Coastguard Worker# to expose the functionality or merge with other use cases of get_ssh_address. 10*6777b538SAndroid Build Coastguard Worker 11*6777b538SAndroid Build Coastguard Workerimport logging 12*6777b538SAndroid Build Coastguard Workerimport sys 13*6777b538SAndroid Build Coastguard Workerimport time 14*6777b538SAndroid Build Coastguard Worker 15*6777b538SAndroid Build Coastguard Workerfrom typing import Optional 16*6777b538SAndroid Build Coastguard Worker 17*6777b538SAndroid Build Coastguard Workerfrom boot_device import boot_device, BootMode 18*6777b538SAndroid Build Coastguard Workerfrom common import run_ffx_command 19*6777b538SAndroid Build Coastguard Worker 20*6777b538SAndroid Build Coastguard Worker 21*6777b538SAndroid Build Coastguard Workerdef test_connection(target_id: Optional[str], wait_sec: int = 60) -> None: 22*6777b538SAndroid Build Coastguard Worker """Runs echo tests to verify that the device can be connected to. 23*6777b538SAndroid Build Coastguard Worker 24*6777b538SAndroid Build Coastguard Worker Devices may not be connectable right after being discovered by ffx, e.g. 25*6777b538SAndroid Build Coastguard Worker after a `ffx target wait`, so this function retries up to |wait_sec| before 26*6777b538SAndroid Build Coastguard Worker throwing an exception. 27*6777b538SAndroid Build Coastguard Worker """ 28*6777b538SAndroid Build Coastguard Worker start_sec = time.time() 29*6777b538SAndroid Build Coastguard Worker while time.time() - start_sec < wait_sec: 30*6777b538SAndroid Build Coastguard Worker if run_ffx_command(cmd=('target', 'echo'), 31*6777b538SAndroid Build Coastguard Worker target_id=target_id, 32*6777b538SAndroid Build Coastguard Worker check=False).returncode == 0: 33*6777b538SAndroid Build Coastguard Worker return 34*6777b538SAndroid Build Coastguard Worker time.sleep(10) 35*6777b538SAndroid Build Coastguard Worker 36*6777b538SAndroid Build Coastguard Worker run_ffx_command(cmd=('target', 'echo'), target_id=target_id) 37*6777b538SAndroid Build Coastguard Worker 38*6777b538SAndroid Build Coastguard Worker 39*6777b538SAndroid Build Coastguard Workerdef test_device_connection(target_id: Optional[str]) -> None: 40*6777b538SAndroid Build Coastguard Worker """Runs test_connection against the target_id and restarts the device if 41*6777b538SAndroid Build Coastguard Worker it cannot be connected.""" 42*6777b538SAndroid Build Coastguard Worker start_sec = time.time() 43*6777b538SAndroid Build Coastguard Worker while time.time() - start_sec < 1800: 44*6777b538SAndroid Build Coastguard Worker # pylint: disable=bare-except 45*6777b538SAndroid Build Coastguard Worker # First, test_connection with ffx target echo. 46*6777b538SAndroid Build Coastguard Worker try: 47*6777b538SAndroid Build Coastguard Worker test_connection(target_id=target_id, wait_sec=600) 48*6777b538SAndroid Build Coastguard Worker return 49*6777b538SAndroid Build Coastguard Worker except: 50*6777b538SAndroid Build Coastguard Worker # If anything wrong, reboot the device and try again. 51*6777b538SAndroid Build Coastguard Worker try: 52*6777b538SAndroid Build Coastguard Worker boot_device(target_id, BootMode.REGULAR, must_boot=True) 53*6777b538SAndroid Build Coastguard Worker except: 54*6777b538SAndroid Build Coastguard Worker # If unfortunately, the reboot failed, it's still worth 55*6777b538SAndroid Build Coastguard Worker # continuing the test rather than failing here. 56*6777b538SAndroid Build Coastguard Worker pass 57*6777b538SAndroid Build Coastguard Worker logging.warning( 58*6777b538SAndroid Build Coastguard Worker run_ffx_command(cmd=('target', 'wait'), 59*6777b538SAndroid Build Coastguard Worker target_id=target_id, 60*6777b538SAndroid Build Coastguard Worker check=False, 61*6777b538SAndroid Build Coastguard Worker capture_output=True).stdout) 62*6777b538SAndroid Build Coastguard Worker 63*6777b538SAndroid Build Coastguard Workerdef main(): 64*6777b538SAndroid Build Coastguard Worker """Test a connection against a fuchsia target via ffx.""" 65*6777b538SAndroid Build Coastguard Worker if len(sys.argv) < 2: 66*6777b538SAndroid Build Coastguard Worker raise ValueError('test_connection.py target') 67*6777b538SAndroid Build Coastguard Worker test_connection(sys.argv[1]) 68*6777b538SAndroid Build Coastguard Worker 69*6777b538SAndroid Build Coastguard Worker 70*6777b538SAndroid Build Coastguard Workerif __name__ == '__main__': 71*6777b538SAndroid Build Coastguard Worker sys.exit(main()) 72