Skip to content

chore(tests): Improving the tests for OSLogin SSH sample #8486

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 33 commits into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
73b461b
docs(samples): New version of the OSLogin SSH script
m-strzelczyk Oct 24, 2022
b93bf75
Adding tests for the new snippet
m-strzelczyk Oct 28, 2022
62066e1
Merge remote-tracking branch 'origin/main' into issue-8429
m-strzelczyk Oct 28, 2022
10c13ef
Fixing test
m-strzelczyk Oct 28, 2022
4a8ce62
Fixing lint problems
m-strzelczyk Oct 31, 2022
2652f6f
Adding region tag to new sample
m-strzelczyk Nov 2, 2022
437fd0b
Apply suggestions from code review
m-strzelczyk Nov 7, 2022
56a7a6a
Fixing some issues
m-strzelczyk Nov 7, 2022
7ce1a05
Better exception handling
m-strzelczyk Nov 7, 2022
a89f3c9
Merge branch 'main' into issue-8429
m-strzelczyk Nov 7, 2022
9df6e2c
Fixing typing
m-strzelczyk Nov 8, 2022
64fb566
Fixing typing
m-strzelczyk Nov 8, 2022
2ee3437
Making test more tolerant of ssh failures
m-strzelczyk Nov 8, 2022
7eea20b
Linting
m-strzelczyk Nov 8, 2022
0d314bc
Re-raising the exception if ssh fails too many times.
m-strzelczyk Nov 9, 2022
3798344
Improving error reporting
m-strzelczyk Nov 10, 2022
e58bc90
Merge remote-tracking branch 'origin/main' into issue-8429
m-strzelczyk Nov 10, 2022
3f8554e
Merge branch 'main' into issue-8429
m-strzelczyk Nov 14, 2022
27c4006
Making test more understanding with firewalls
m-strzelczyk Nov 14, 2022
bfdfc3d
Merge remote-tracking branch 'origin/main' into issue-8429
m-strzelczyk Nov 18, 2022
22f7624
Updating file name + linting.
m-strzelczyk Nov 18, 2022
5458925
Merge remote-tracking branch 'origin/main' into issue-8429
m-strzelczyk Jan 9, 2023
e921dcb
Trying a different approach
m-strzelczyk Jan 9, 2023
66c2bc3
Merge branch 'main' into issue-8429
m-strzelczyk Jan 10, 2023
fd9bf84
Merge branch 'main' into issue-8429
m-strzelczyk Jan 12, 2023
d3cbb51
Changing the behavior of test to preserve instances of failed tests.
m-strzelczyk Jan 12, 2023
76cf4e8
Merge branch 'main' into issue-8429
m-strzelczyk Jan 12, 2023
a8b1766
Merge branch 'main' into issue-8429
m-strzelczyk Jan 13, 2023
bf7d010
Merge branch 'main' into issue-8429
m-strzelczyk Jan 13, 2023
b1ddc09
Merge branch 'main' into issue-8429
m-strzelczyk Jan 16, 2023
1b268f7
Merge branch 'main' into issue-8429
m-strzelczyk Jan 17, 2023
78c154d
Merge branch 'main' into issue-8429
m-strzelczyk Jan 18, 2023
26af054
Merge branch 'main' into issue-8429
kweinmeister Jan 19, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,12 @@ def run_ssh(cmd: str, private_key_file: str, username: str, hostname: str) -> st
check=True,
env={'SSH_AUTH_SOCK': ''},
)
except subprocess.CalledProcessError:
except subprocess.CalledProcessError as err:
time.sleep(30)
tries += 1
if tries == 3:
print(f"Failed to execute SSH command (return code: {err.returncode}. Output received: {err.output}")
raise err
else:
return ssh.stdout

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import time
import uuid

from google.api_core.exceptions import NotFound
from google.api_core.exceptions import BadRequest, NotFound
import google.auth
from google.cloud import compute_v1
from google.cloud import oslogin_v1
Expand All @@ -35,7 +35,7 @@
import googleapiclient.errors
import pytest

from new_service_account_ssh import main
from oslogin_service_account_ssh import main

PROJECT = google.auth.default()[1]
ZONE = 'europe-north1-a'
Expand Down Expand Up @@ -119,7 +119,7 @@ def ssh_firewall():
yield firewall_client.get(project=PROJECT, firewall=TEST_ID)
try:
firewall_client.delete(project=PROJECT, firewall=TEST_ID)
except NotFound:
except (NotFound, BadRequest):
# That means the GCE Enforcer deleted it before us
pass

Expand Down Expand Up @@ -187,7 +187,8 @@ def oslogin_instance(ssh_firewall, oslogin_service_account):

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

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


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

delete_instance = True

out, _ = capsys.readouterr()
assert_value = 'Linux {test_id}'.format(test_id=TEST_ID)
assert assert_value in out
try:
assert assert_value in out
except AssertionError:
delete_instance = False
finally:
# If the assert passed, we can safely delete the instance. If it failed, we want to keep it around for
# manual inspection.
if delete_instance:
compute_client = compute_v1.InstancesClient()
compute_client.delete(project=PROJECT, zone=ZONE, instance=oslogin_instance.name)