8000 fix: use backoff exception to gracefully exit and fail (#8257) · mhenc/python-docs-samples@a86d380 · GitHub
[go: up one dir, main page]

Skip to content

Commit a86d380

Browse files
fix: use backoff exception to gracefully exit and fail (GoogleCloudPlatform#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>
1 parent a61f2dc commit a86d380

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

compute/oslogin/requirements-test.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
backoff===1.11.1; python_version < "3.7"
2+
backoff===2.0.0; python_version >= "3.7"
13
pytest==7.0.1
24
mock==4.0.3
3-
retrying==1.3.3

compute/oslogin/service_account_ssh_test.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
import json
1717
import os
1818
import random
19+
from subprocess import CalledProcessError
1920
import time
2021

22+
import backoff
23+
from google.auth.exceptions import RefreshError
2124
from google.oauth2 import service_account
2225
import googleapiclient.discovery
23-
from retrying import retry
2426

2527
from service_account_ssh import main
2628

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

87-
@retry(wait_exponential_multiplier=1000, wait_exponential_max=300000,
88-
stop_max_attempt_number=10)
89+
# More exceptions could be raised, keeping track of ones I could
90+
# find for now.
91+
@backoff.on_exception(backoff.expo,
92+
(CalledProcessError,
93+
RefreshError),
94+
max_tries=5)
8995
def ssh_login():
9096
main(cmd, project, test_id, zone, oslogin, account, hostname)
9197
out, _ = capsys.readouterr()
9298
assert_value = 'Linux {test_id}'.format(test_id=test_id)
9399
assert assert_value in out
94100

95101
# Test SSH to the instance.
96-
try:
97-
ssh_login()
98-
except Exception:
99-
raise Exception('SSH to the test instance failed.')
100-
101-
finally:
102-
cleanup_resources(compute, iam, project, test_id, zone, account_email)
102+
ssh_login()
103+
cleanup_resources(compute, iam, project, test_id, zone, account_email)
103104

104105

105106
def setup_resources(

0 commit comments

Comments
 (0)
0