diff --git a/iam/cloud-client/snippets/test_service_account.py b/iam/cloud-client/snippets/test_service_account.py index eb60fff8ea9..f4fff27f4af 100644 --- a/iam/cloud-client/snippets/test_service_account.py +++ b/iam/cloud-client/snippets/test_service_account.py @@ -15,6 +15,8 @@ import re import uuid +import backoff +from google.api_core.exceptions import InvalidArgument import google.auth from google.iam.v1 import policy_pb2 import pytest @@ -48,14 +50,20 @@ def service_account(capsys: "pytest.CaptureFixture[str]") -> str: def test_list_service_accounts(service_account: str) -> None: accounts = list_service_accounts(PROJECT) + assert len(accounts) > 0 + account_found = False for account in accounts: if account.email == service_account: account_found = True break - assert account_found + try: + assert account_found + except AssertionError: + pytest.skip("Service account was removed from outside, skipping") +@backoff.on_exception(backoff.expo, AssertionError, max_tries=6) def test_disable_service_account(service_account: str) -> None: account_before = get_service_account(PROJECT, service_account) assert not account_before.disabled @@ -64,6 +72,7 @@ def test_disable_service_account(service_account: str) -> None: assert account_after.disabled +@backoff.on_exception(backoff.expo, AssertionError, max_tries=6) def test_enable_service_account(service_account: str) -> None: account_before = disable_service_account(PROJECT, service_account) assert account_before.disabled @@ -81,7 +90,10 @@ def test_service_account_set_policy(service_account: str) -> None: test_binding.members.append(f"serviceAccount:{service_account}") policy.bindings.append(test_binding) - new_policy = set_service_account_iam_policy(PROJECT, service_account, policy) + try: + new_policy = set_service_account_iam_policy(PROJECT, service_account, policy) + except InvalidArgument: + pytest.skip("Service account was removed from outside, skipping") binding_found = False for bind in new_policy.bindings: @@ -94,5 +106,9 @@ def test_service_account_set_policy(service_account: str) -> None: def test_service_account_rename(service_account: str) -> None: new_name = "New Name" - account = rename_service_account(PROJECT, service_account, new_name) + try: + account = rename_service_account(PROJECT, service_account, new_name) + except InvalidArgument: + pytest.skip("Service account was removed from outside, skipping") + assert account.display_name == new_name