From 4361f02f07aec8a623c6fad0fe1327639d47ab20 Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Fri, 5 Nov 2021 16:39:34 -0400 Subject: [PATCH] samples: fix flaky test teardown in servicedirectory (#7023) * samples: fix flaky test teardown in servicedirectory * Improve logging Co-authored-by: Leah E. Cole <6719667+leahecole@users.noreply.github.com> Co-authored-by: Leah E. Cole <6719667+leahecole@users.noreply.github.com> --- servicedirectory/snippets_test.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/servicedirectory/snippets_test.py b/servicedirectory/snippets_test.py index b94a986eb383..5425d6bf42ff 100644 --- a/servicedirectory/snippets_test.py +++ b/servicedirectory/snippets_test.py @@ -20,6 +20,7 @@ from google.api_core import exceptions from google.cloud import servicedirectory_v1 +import quickstart import snippets PROJECT_ID = environ['GOOGLE_CLOUD_PROJECT'] @@ -34,11 +35,16 @@ def teardown_module(): client = servicedirectory_v1.RegistrationServiceClient() namespace_name = client.namespace_path(PROJECT_ID, LOCATION_ID, NAMESPACE_ID) - try: - namespace = client.get_namespace(name=namespace_name) - client.delete_namespace(name=namespace.name) - except exceptions.NotFound: - pass + all_namespaces = quickstart.list_namespaces(PROJECT_ID, LOCATION_ID).namespaces + + # Delete namespace only if it exists + for namespace in all_namespaces: + if namespace_name in namespace.name: + try: + client.delete_namespace(name=namespace_name) + except exceptions.NotFound: + print("Namespace already deleted") + break def test_create_namespace():