Skip to content

Commit fc9f776

Browse files
m-strzelczykkweinmeister
authored andcommitted
fix(oslogin_tests): Making the test check if the firewall rule got removed (#9167)
* fix(oslogin_tests): Making the test check if the firewall rule got removed * lint fix --------- Co-authored-by: Karl Weinmeister <[email protected]>
1 parent 3624ef0 commit fc9f776

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

compute/oslogin/oslogin_service_account_ssh.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,9 @@ def run_ssh(cmd: str, private_key_file: str, username: str, hostname: str) -> st
148148
text=True,
149149
check=True,
150150
env={'SSH_AUTH_SOCK': ''},
151+
timeout=10
151152
)
152-
except subprocess.CalledProcessError as err:
153+
except (subprocess.CalledProcessError, subprocess.TimeoutExpired) as err:
153154
time.sleep(30)
154155
tries += 1
155156
if tries == 3:

compute/oslogin/oslogin_service_account_ssh_test.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,7 @@ def oslogin_instance(ssh_firewall, oslogin_service_account):
187187

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

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

193192

194193
def test_oslogin_ssh(oslogin_instance, oslogin_service_account, capsys):
@@ -201,17 +200,17 @@ def test_oslogin_ssh(oslogin_instance, oslogin_service_account, capsys):
201200
hostname=oslogin_instance.network_interfaces[0].access_configs[0].nat_i_p,
202201
oslogin=oslogin_client)
203202

204-
delete_instance = True
205-
206203
out, _ = capsys.readouterr()
207204
assert_value = 'Linux {test_id}'.format(test_id=TEST_ID)
208205
try:
209206
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)
207+
except AssertionError as err:
208+
fw_client = compute_v1.FirewallsClient()
209+
try:
210+
fw_client.get(project=PROJECT, firewall=TEST_ID)
211+
except NotFound:
212+
# The test probably failed due to the firewall rule being removed too soon.
213+
pytest.skip("The test was interrupted by removal of SSH firewall rule.")
214+
else:
215+
# The test failed due to some other reason.
216+
raise err

0 commit comments

Comments
 (0)