Skip to content

Commit 4361f02

Browse files
parthealeahecole
andauthored
samples: fix flaky test teardown in servicedirectory (GoogleCloudPlatform#7023)
* samples: fix flaky test teardown in servicedirectory * Improve logging Co-authored-by: Leah E. Cole <[email protected]> Co-authored-by: Leah E. Cole <[email protected]>
1 parent cc5e834 commit 4361f02

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

servicedirectory/snippets_test.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from google.api_core import exceptions
2121
from google.cloud import servicedirectory_v1
2222

23+
import quickstart
2324
import snippets
2425

2526
PROJECT_ID = environ['GOOGLE_CLOUD_PROJECT']
@@ -34,11 +35,16 @@
3435
def teardown_module():
3536
client = servicedirectory_v1.RegistrationServiceClient()
3637
namespace_name = client.namespace_path(PROJECT_ID, LOCATION_ID, NAMESPACE_ID)
37-
try:
38-
namespace = client.get_namespace(name=namespace_name)
39-
client.delete_namespace(name=namespace.name)
40-
except exceptions.NotFound:
41-
pass
38+
all_namespaces = quickstart.list_namespaces(PROJECT_ID, LOCATION_ID).namespaces
39+
40+
# Delete namespace only if it exists
41+
for namespace in all_namespaces:
42+
if namespace_name in namespace.name:
43+
try:
44+
client.delete_namespace(name=namespace_name)
45+
except exceptions.NotFound:
46+
print("Namespace already deleted")
47+
break
4248

4349

4450
def test_create_namespace():

0 commit comments

Comments
 (0)