1212import logging
1313import os as operating_system
1414import re
15+ import stat
1516import subprocess
17+ from pathlib import Path
1618
1719import pytest
1820import requests
@@ -141,6 +143,14 @@ def _check_no_crashes(remote_command_executor, test_datadir):
141143 remote_command_executor .run_remote_script (str (test_datadir / "verify_no_core_files.sh" ))
142144
143145
146+ def _get_known_hosts_content (host_keys_file ):
147+ """Get content of known_hosts file, returning empty bytes if file doesn't exist or can't be read."""
148+ try :
149+ return subprocess .check_output (f"cat { host_keys_file } " , shell = True )
150+ except subprocess .CalledProcessError :
151+ return b""
152+
153+
144154def _check_error_cases (remote_command_executor , dcv_authenticator_port ):
145155 """Check DCV errors for both head and login nodes."""
146156 _check_auth_ko (
@@ -157,23 +167,32 @@ def _check_error_cases(remote_command_executor, dcv_authenticator_port):
157167 )
158168
159169
160- def _test_show_url (cluster , region , dcv_port , access_from , use_login_node = False ):
170+ def _test_show_url (cluster , region , dcv_port , access_from , use_login_node = False ): # noqa: C901
161171 """Test dcv-connect with --show-url."""
162172 env = operating_system .environ .copy ()
163173 env ["AWS_DEFAULT_REGION" ] = region
164174
165175 node_ip = cluster .get_login_node_public_ip () if use_login_node else cluster .head_node_ip
166176
167177 # add ssh key to jenkins user known hosts file to avoid ssh keychecking prompt
178+ # Ensure known_hosts path exists to avoid `cat` command returning non-zero exit when testing in ADC region.
168179 host_keys_file = operating_system .path .expanduser ("~/.ssh/known_hosts" )
169- logging .info (f"Add ip address { node_ip } to known hosts file { host_keys_file } " )
180+ host_keys_path = Path (host_keys_file )
181+ try :
182+ host_keys_path .parent .mkdir (parents = True , exist_ok = True )
183+ if not host_keys_path .exists ():
184+ host_keys_path .touch ()
185+ host_keys_path .chmod (stat .S_IRUSR | stat .S_IWUSR ) # 0600
186+ except Exception as e :
187+ logging .warning (f"Failed to prepare known_hosts file { host_keys_file } : { e } " )
170188
171- result = subprocess . check_output ( "cat {0}" . format ( host_keys_file ), shell = True )
172- logging .info (f"Original content of known hosts file { host_keys_file } : { result } " )
189+ before_content = _get_known_hosts_content ( host_keys_file )
190+ logging .info (f"Original content of known hosts file { host_keys_file } : { before_content } " )
173191
174192 add_keys_to_known_hosts (node_ip , host_keys_file )
175- result = subprocess .check_output ("cat {0}" .format (host_keys_file ), shell = True )
176- logging .info (f"New content of known hosts file { host_keys_file } : { result } " )
193+
194+ after_content = _get_known_hosts_content (host_keys_file )
195+ logging .info (f"New content of known hosts file { host_keys_file } : { after_content } " )
177196
178197 dcv_connect_args = ["pcluster" , "dcv-connect" , "--cluster-name" , cluster .name , "--show-url" ]
179198
0 commit comments