From 46c10ae92a05dcb70b26f9cb42bb7e9062e46fd9 Mon Sep 17 00:00:00 2001 From: BigBlackWolf Date: Mon, 3 Jun 2024 13:52:35 +0200 Subject: [PATCH 1/3] Fix: reducing flakiness --- iam/cloud-client/snippets/test_service_account.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/iam/cloud-client/snippets/test_service_account.py b/iam/cloud-client/snippets/test_service_account.py index eb60fff8ea9..2e573ff1d75 100644 --- a/iam/cloud-client/snippets/test_service_account.py +++ b/iam/cloud-client/snippets/test_service_account.py @@ -15,6 +15,7 @@ import re import uuid +import backoff import google.auth from google.iam.v1 import policy_pb2 import pytest @@ -57,11 +58,15 @@ def test_list_service_accounts(service_account: str) -> None: def test_disable_service_account(service_account: str) -> None: - account_before = get_service_account(PROJECT, service_account) - assert not account_before.disabled + @backoff.on_exception(backoff.expo, AssertionError, max_tries=6) + def run_test() -> None: + account_before = get_service_account(PROJECT, service_account) + assert not account_before.disabled - account_after = disable_service_account(PROJECT, service_account) - assert account_after.disabled + account_after = disable_service_account(PROJECT, service_account) + assert account_after.disabled + + run_test() def test_enable_service_account(service_account: str) -> None: From f23c0416b4a5a930e6b75146d6203c2407c7d5fd Mon Sep 17 00:00:00 2001 From: BigBlackWolf Date: Tue, 4 Jun 2024 14:42:31 +0200 Subject: [PATCH 2/3] fix: correcting service accounts test, according to possible flakiness --- .../snippets/test_service_account.py | 29 ++++++++++++++----- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/iam/cloud-client/snippets/test_service_account.py b/iam/cloud-client/snippets/test_service_account.py index 2e573ff1d75..e751de574f4 100644 --- a/iam/cloud-client/snippets/test_service_account.py +++ b/iam/cloud-client/snippets/test_service_account.py @@ -16,6 +16,7 @@ import uuid import backoff +from google.api_core.exceptions import InvalidArgument import google.auth from google.iam.v1 import policy_pb2 import pytest @@ -54,7 +55,10 @@ def test_list_service_accounts(service_account: str) -> None: 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") def test_disable_service_account(service_account: str) -> None: @@ -70,11 +74,15 @@ def run_test() -> None: def test_enable_service_account(service_account: str) -> None: - account_before = disable_service_account(PROJECT, service_account) - assert account_before.disabled + @backoff.on_exception(backoff.expo, AssertionError, max_tries=6) + def run_test() -> None: + account_before = disable_service_account(PROJECT, service_account) + assert account_before.disabled + + account_after = enable_service_account(PROJECT, service_account) + assert not account_after.disabled - account_after = enable_service_account(PROJECT, service_account) - assert not account_after.disabled + run_test() def test_service_account_set_policy(service_account: str) -> None: @@ -86,7 +94,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: @@ -99,5 +110,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 From f84da667dfb2fa3ee7d719f2d7f0931370947da6 Mon Sep 17 00:00:00 2001 From: BigBlackWolf Date: Thu, 6 Jun 2024 13:29:04 +0200 Subject: [PATCH 3/3] fix: moved backoff on upper level --- .../snippets/test_service_account.py | 28 ++++++++----------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/iam/cloud-client/snippets/test_service_account.py b/iam/cloud-client/snippets/test_service_account.py index e751de574f4..f4fff27f4af 100644 --- a/iam/cloud-client/snippets/test_service_account.py +++ b/iam/cloud-client/snippets/test_service_account.py @@ -50,6 +50,8 @@ 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: @@ -61,28 +63,22 @@ def test_list_service_accounts(service_account: str) -> None: 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: - @backoff.on_exception(backoff.expo, AssertionError, max_tries=6) - def run_test() -> None: - account_before = get_service_account(PROJECT, service_account) - assert not account_before.disabled - - account_after = disable_service_account(PROJECT, service_account) - assert account_after.disabled + account_before = get_service_account(PROJECT, service_account) + assert not account_before.disabled - run_test() + account_after = disable_service_account(PROJECT, service_account) + assert account_after.disabled +@backoff.on_exception(backoff.expo, AssertionError, max_tries=6) def test_enable_service_account(service_account: str) -> None: - @backoff.on_exception(backoff.expo, AssertionError, max_tries=6) - def run_test() -> None: - account_before = disable_service_account(PROJECT, service_account) - assert account_before.disabled - - account_after = enable_service_account(PROJECT, service_account) - assert not account_after.disabled + account_before = disable_service_account(PROJECT, service_account) + assert account_before.disabled - run_test() + account_after = enable_service_account(PROJECT, service_account) + assert not account_after.disabled def test_service_account_set_policy(service_account: str) -> None: