Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ee616fe

Browse filesBrowse files
fix(secretmanager): Use retry-wrapper for function calls to avoid flaky bot issues (GoogleCloudPlatform#10784)
## Description Note: This is WIP! Add fix flaky bot issues in snippet's test file. ### Fixes * https://togithub.com/GoogleCloudPlatform/python-docs-samples/issues/10763 * https://togithub.com/GoogleCloudPlatform/python-docs-samples/issues/10764 Note: Before submitting a pull request, please open an issue for discussion if you are not associated with Google. ## Checklist - [x] **Tests** pass: `nox -s py-3.9` (see [Test Environment Setup](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md#test-environment-setup)) - [x] **Lint** pass: `nox -s lint` (see [Test Environment Setup](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md#test-environment-setup)) - [x] Please **merge** this PR for me once it is approved
1 parent 9c41465 commit ee616fe
Copy full SHA for ee616fe

File tree

Expand file treeCollapse file tree

1 file changed

+44
-8
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+44
-8
lines changed

‎secretmanager/snippets/snippets_test.py

Copy file name to clipboardExpand all lines: secretmanager/snippets/snippets_test.py
+44-8Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
import base64
1515
import os
1616
import time
17-
from typing import Iterator, Tuple
17+
from typing import Iterator, Optional, Tuple, Union
1818
import uuid
1919

20-
from google.api_core import exceptions
20+
from google.api_core import exceptions, retry
2121
from google.cloud import secretmanager
2222
import pytest
2323

@@ -63,19 +63,54 @@ def iam_user() -> str:
6363
return "serviceAccount:" + os.environ["GCLOUD_SECRETS_SERVICE_ACCOUNT"]
6464

6565

66+
@retry.Retry()
67+
def retry_client_create_secret(
68+
client: secretmanager.SecretManagerServiceClient,
69+
request: Optional[Union[secretmanager.CreateSecretRequest, dict]],
70+
) -> secretmanager.Secret:
71+
# Retry to avoid 503 error & flaky issues
72+
return client.create_secret(request=request)
73+
74+
75+
@retry.Retry()
76+
def retry_client_access_secret_version(
77+
client: secretmanager.SecretManagerServiceClient,
78+
request: Optional[Union[secretmanager.AccessSecretVersionRequest, dict]],
79+
) -> secretmanager.AccessSecretVersionResponse:
80+
# Retry to avoid 503 error & flaky issues
81+
return client.access_secret_version(request=request)
82+
83+
84+
@retry.Retry()
85+
def retry_client_delete_secret(
86+
client: secretmanager.SecretManagerServiceClient,
87+
request: Optional[Union[secretmanager.DeleteSecretRequest, dict]],
88+
) -> None:
89+
# Retry to avoid 503 error & flaky issues
90+
return client.delete_secret(request=request)
91+
92+
93+
@retry.Retry()
94+
def retry_client_add_secret_version(
95+
client: secretmanager.SecretManagerServiceClient,
96+
request: Optional[Union[secretmanager.AddSecretVersionRequest, dict]],
97+
) -> secretmanager.SecretVersion:
98+
# Retry to avoid 503 error & flaky issues
99+
return client.add_secret_version(request=request)
100+
101+
66102
@pytest.fixture()
67103
def secret_id(
68104
client: secretmanager.SecretManagerServiceClient, project_id: str
69105
) -> Iterator[str]:
70106
secret_id = f"python-secret-{uuid.uuid4()}"
71107

72108
yield secret_id
73-
74109
secret_path = client.secret_path(project_id, secret_id)
75110
print(f"deleting secret {secret_id}")
76111
try:
77112
time.sleep(5)
78-
client.delete_secret(request={"name": secret_path})
113+
retry_client_delete_secret(client, request={"name": secret_path})
79114
except exceptions.NotFound:
80115
# Secret was already deleted, probably in the test
81116
print(f"Secret {secret_id} was not found.")
@@ -89,12 +124,13 @@ def secret(
89124

90125
parent = f"projects/{project_id}"
91126
time.sleep(5)
92-
secret = client.create_secret(
127+
secret = retry_client_create_secret(
128+
client,
93129
request={
94130
"parent": parent,
95131
"secret_id": secret_id,
96132
"secret": {"replication": {"automatic": {}}},
97-
}
133+
},
98134
)
99135

100136
yield project_id, secret_id, secret.etag
@@ -174,7 +210,7 @@ def test_delete_secret(
174210
with pytest.raises(exceptions.NotFound):
175211
print(f"{client}")
176212
name = f"projects/{project_id}/secrets/{secret_id}/versions/latest"
177-
client.access_secret_version(request={"name": name})
213+
retry_client_access_secret_version(client, request={"name": name})
178214

179215

180216
def test_delete_secret_with_etag(
@@ -185,7 +221,7 @@ def test_delete_secret_with_etag(
185221
with pytest.raises(exceptions.NotFound):
186222
print(f"{client}")
187223
name = f"projects/{project_id}/secrets/{secret_id}/versions/latest"
188-
client.access_secret_version(request={"name": name})
224+
retry_client_access_secret_version(client, request={"name": name})
189225

190226

191227
def test_destroy_secret_version(

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.