Skip to content

Commit

Permalink
fix: use backoff exception to gracefully exit and fail (GoogleCloudPl…
Browse files Browse the repository at this point in the history
…atform#8257)

* fix: use backoff exception to gracefully exit and fail

* fix: using specific tuple of exceptions to track

* fix: update syntax

* fix: lint

* fix: lint2

* fix: update lint

* chore(deps): pin dependency for versions

Co-authored-by: Maciej Strzelczyk <strzelczyk@google.com>
  • Loading branch information
dandhlee and m-strzelczyk authored Aug 16, 2022
1 parent a61f2dc commit a86d380
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
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

0 comments on commit a86d380

Please sign in to comment.