Skip to content
Merged
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
22 changes: 19 additions & 3 deletions iam/cloud-client/snippets/test_service_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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