From 613b4fabba88b09c4c278e3021d8ee181e2500ce Mon Sep 17 00:00:00 2001 From: Metin Kaya Date: Wed, 17 Jan 2024 10:45:52 +0000 Subject: [PATCH] ChromeOsTarget: Fix building SSH connection parameter list 'if list.get(elem, None)' like probing ignores list elements whose values are falsy. Here is a sample test code: ```Python connection_settings={'host': '127.0.0.1', 'port': 8022, 'username': 'root', 'password': 'root', 'strict_host_check': False} ssh_conn_params = ['host', 'username', 'password', 'port', 'strict_host_check'] print(f'connection_settings={connection_settings}') ssh_connection_settings = {} for setting in ssh_conn_params: if connection_settings.get(setting, None): print(f'1. setting "{setting}" to "{connection_settings[setting]}"...') ssh_connection_settings[setting] = connection_settings[setting] else: print(f'1. "{setting}" is None!') ssh_connection_settings = {} for setting in ssh_conn_params: if setting in connection_settings: print(f'2. setting "{setting}" to "{connection_settings[setting]}"...') ssh_connection_settings[setting] = connection_settings[setting] else: print(f'2. "{setting}" is None!') ``` And its output: ``` connection_settings={'host': '127.0.0.1', 'port': 8022, 'username': 'root', 'password': 'root', 'strict_host_check': False} 1. setting "host" to "127.0.0.1"... 1. setting "username" to "root"... 1. setting "password" to "root"... 1. setting "port" to "8022"... 1. "strict_host_check" is None! 2. setting "host" to "127.0.0.1"... 2. setting "username" to "root"... 2. setting "password" to "root"... 2. setting "port" to "8022"... 2. setting "strict_host_check" to "False"... ``` Also fix a typo in a log message. Signed-off-by: Metin Kaya --- devlib/target.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/devlib/target.py b/devlib/target.py index f50ae02..0b3f6ed 100644 --- a/devlib/target.py +++ b/devlib/target.py @@ -505,7 +505,7 @@ class Target(object): self.hard_reset() # pylint: disable=no-member else: if not self.is_connected: - message = 'Cannot reboot target becuase it is disconnected. ' +\ + message = 'Cannot reboot target because it is disconnected. ' +\ 'Either connect() first, or specify hard=True ' +\ '(in which case, a hard_reset module must be installed)' raise TargetTransientError(message) @@ -2970,9 +2970,11 @@ class ChromeOsTarget(LinuxTarget): 'total_transfer_timeout', 'poll_transfers', 'start_transfer_poll_delay'] self.ssh_connection_settings = {} - for setting in ssh_conn_params: - if connection_settings.get(setting, None): - self.ssh_connection_settings[setting] = connection_settings[setting] + self.ssh_connection_settings.update( + (key, value) + for key, value in connection_settings.items() + if key in ssh_conn_params + ) super(ChromeOsTarget, self).__init__(connection_settings=self.ssh_connection_settings, platform=platform, @@ -2991,9 +2993,11 @@ class ChromeOsTarget(LinuxTarget): # Pull out adb connection settings adb_conn_params = ['device', 'adb_server', 'timeout'] self.android_connection_settings = {} - for setting in adb_conn_params: - if connection_settings.get(setting, None): - self.android_connection_settings[setting] = connection_settings[setting] + self.android_connection_settings.update( + (key, value) + for key, value in connection_settings.items() + if key in adb_conn_params + ) # If adb device is not explicitly specified use same as ssh host if not connection_settings.get('device', None):