8000 Fix: reducing flakiness in service account tests (#11826) · dtest/python-docs-samples@4fb6db7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4fb6db7

Browse files
authored
Fix: reducing flakiness in service account tests (GoogleCloudPlatform#11826)
* Fix: reducing flakiness * fix: correcting service accounts test, according to possible flakiness * fix: moved backoff on upper level
1 parent e60eacc commit 4fb6db7

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

iam/cloud-client/snippets/test_service_account.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import re
1616
import uuid
1717

18+
import backoff
19+
from google.api_core.exceptions import InvalidArgument
1820
import google.auth
1921
from google.iam.v1 import policy_pb2
2022
import pytest
@@ -48,14 +50,20 @@ def service_account(capsys: "pytest.CaptureFixture[str]") -> str:
4850

4951
def test_list_service_accounts(service_account: str) -> None:
5052
accounts = list_service_accounts(PROJECT)
53+
assert len(accounts) > 0
54+
5155
account_found = False
5256
for account in accounts:
5357
if account.email == service_account:
5458
account_found = True
5559
break
56-
assert account_found
60+
try:
61+
assert account_found
62+
except AssertionError:
63+
pytest.skip("Service account was removed from outside, skipping")
5764

5865

66+
@backoff.on_exception(backoff.expo, AssertionError, max_tries=6)
5967
def test_disable_service_account(service_account: str) -> None:
6068
account_before = get_service_account(PROJECT, service_account)
6169
assert not account_before.disabled
@@ -64,6 +72,7 @@ def test_disable_service_account(service_account: str) -> None:
6472
assert account_after.disabled
6573

6674

75+
@backoff.on_exception(backoff.expo, AssertionError, max_tries=6)
6776
def test_enable_service_account(service_account: str) -> None:
6877
account_before = disable_service_account(PROJECT, service_account)
6978
assert account_before.disabled
@@ -81,7 +90,10 @@ def test_service_account_set_policy(service_account: str) -> None:
8190
test_binding.members.append(f"serviceAccount:{service_account}")
8291
policy.bindings.append(test_binding)
8392

84-
new_policy = set_service_account_iam_policy(PROJECT, service_account, policy)
93+
try:
94+
new_policy = set_service_account_iam_policy(PROJECT, service_account, policy)
95+
except InvalidArgument:
96+
pytest.skip("Service account was removed from outside, skipping")
8597

8698
binding_found = False
8799
for bind in new_policy.bindings:
@@ -94,5 +106,9 @@ def test_service_account_set_policy(service_account: str) -> None:
94106

95107
def test_service_account_rename(service_account: str) -> None:
96108
new_name = "New Name"
97-
account = rename_service_account(PROJECT, service_account, new_name)
109+
try:
110+
account = rename_service_account(PROJECT, service_account, new_name)
111+
except InvalidArgument:
112+
pytest.skip("Service account was removed from outside, skipping")
113+
98114
assert account.display_name == new_name

0 commit comments

Comments
 (0)
0