Skip to content
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

fix: use backoff exception to gracefully exit and fail #8257

Merged
merged 8 commits into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion compute/oslogin/requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
backoff===1.11.1; python_version < "3.7"
backoff===2.0.0; python_version >= "3.7"
pytest==7.0.1
mock==4.0.3
retrying==1.3.3
21 changes: 11 additions & 10 deletions compute/oslogin/service_account_ssh_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
import json
import os
import random
from subprocess import CalledProcessError
import time

import backoff
from google.auth.exceptions import RefreshError
from google.oauth2 import service_account
import googleapiclient.discovery
from retrying import retry

from service_account_ssh import main

Expand Down Expand Up @@ -84,22 +86,21 @@ def test_main(capsys):
'oslogin', 'v1', cache_discovery=False, credentials=credentials)
account = 'users/' + account_email

@retry(wait_exponential_multiplier=1000, wait_exponential_max=300000,
stop_max_attempt_number=10)
# More exceptions could be raised, keeping track of ones I could
# find for now.
@backoff.on_exception(backoff.expo,
(CalledProcessError,
RefreshError),
max_tries=5)
def ssh_login():
main(cmd, project, test_id, zone, oslogin, account, hostname)
out, _ = capsys.readouterr()
assert_value = 'Linux {test_id}'.format(test_id=test_id)
assert assert_value in out

# Test SSH to the instance.
try:
ssh_login()
except Exception:
raise Exception('SSH to the test instance failed.')

finally:
cleanup_resources(compute, iam, project, test_id, zone, account_email)
ssh_login()
cleanup_resources(compute, iam, project, test_id, zone, account_email)


def setup_resources(
Expand Down