Skip to content

Commit 375e9b0

Browse files
m-strzelczykdandhleekweinmeister
authored andcommitted
chore(tests): Improving the tests for OSLogin SSH sample (#8486)
* docs(samples): New version of the OSLogin SSH script * Adding tests for the new snippet * Fixing test * Fixing lint problems * Adding region tag to new sample * Apply suggestions from code review Co-authored-by: Dan Lee <[email protected]> * Fixing some issues * Better exception handling * Fixing typing * Fixing typing * Making test more tolerant of ssh failures * Linting * Re-raising the exception if ssh fails too many times. * Improving error reporting * Making test more understanding with firewalls * Updating file name + linting. * Trying a different approach Signed-off-by: Maciej Strzelczyk <[email protected]> * Changing the behavior of test to preserve instances of failed tests. Signed-off-by: Maciej Strzelczyk <[email protected]> Co-authored-by: Dan Lee <[email protected]> Co-authored-by: Karl Weinmeister <[email protected]>
1 parent 7b89edf commit 375e9b0

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

compute/oslogin/new_service_account_ssh.py renamed to compute/oslogin/oslogin_service_account_ssh.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,12 @@ def run_ssh(cmd: str, private_key_file: str, username: str, hostname: str) -> st
149149
check=True,
150150
env={'SSH_AUTH_SOCK': ''},
151151
)
152-
except subprocess.CalledProcessError:
152+
except subprocess.CalledProcessError as err:
153153
time.sleep(30)
154154
tries += 1
155+
if tries == 3:
156+
print(f"Failed to execute SSH command (return code: {err.returncode}. Output received: {err.output}")
157+
raise err
155158
else:
156159
return ssh.stdout
157160

compute/oslogin/new_service_account_ssh_test.py renamed to compute/oslogin/oslogin_service_account_ssh_test.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import time
2727
import uuid
2828

29-
from google.api_core.exceptions import NotFound
29+
from google.api_core.exceptions import BadRequest, NotFound
3030
import google.auth
3131
from google.cloud import compute_v1
3232
from google.cloud import oslogin_v1
@@ -35,7 +35,7 @@
3535
import googleapiclient.errors
3636
import pytest
3737

38-
from new_service_account_ssh import main
38+
from oslogin_service_account_ssh import main
3939

4040
PROJECT = google.auth.default()[1]
4141
ZONE = 'europe-north1-a'
@@ -119,7 +119,7 @@ def ssh_firewall():
119119
yield firewall_client.get(project=PROJECT, firewall=TEST_ID)
120120
try:
121121
firewall_client.delete(project=PROJECT, firewall=TEST_ID)
122-
except NotFound:
122+
except (NotFound, BadRequest):
123123
# That means the GCE Enforcer deleted it before us
124124
pass
125125

@@ -187,7 +187,8 @@ def oslogin_instance(ssh_firewall, oslogin_service_account):
187187

188188
yield client.get(project=PROJECT, zone=ZONE, instance=instance.name)
189189

190-
client.delete(project=PROJECT, zone=ZONE, instance=instance.name).result()
190+
# The deletion of the instance has been moved to the test itself.
191+
# client.delete(project=PROJECT, zone=ZONE, instance=instance.name).result()
191192

192193

193194
def test_oslogin_ssh(oslogin_instance, oslogin_service_account, capsys):
@@ -199,7 +200,18 @@ def test_oslogin_ssh(oslogin_instance, oslogin_service_account, capsys):
199200
main('uname -a', PROJECT, account=account,
200201
hostname=oslogin_instance.network_interfaces[0].access_configs[0].nat_i_p,
201202
oslogin=oslogin_client)
202-
out, _ = capsys.readouterr()
203203

204+
delete_instance = True
205+
206+
out, _ = capsys.readouterr()
204207
assert_value = 'Linux {test_id}'.format(test_id=TEST_ID)
205-
assert assert_value in out
208+
try:
209+
assert assert_value in out
210+
except AssertionError:
211+
delete_instance = False
212+
finally:
213+
# If the assert passed, we can safely delete the instance. If it failed, we want to keep it around for
214+
# manual inspection.
215+
if delete_instance:
216+
compute_client = compute_v1.InstancesClient()
217+
compute_client.delete(project=PROJECT, zone=ZONE, instance=oslogin_instance.name)

0 commit comments

Comments
 (0)