From da455b14c6c5ba9b620a14f5cfeda35a4f92311c Mon Sep 17 00:00:00 2001 From: Mike <45373284+munkhuushmgl@users.noreply.github.com> Date: Thu, 30 Apr 2020 17:53:21 -0700 Subject: [PATCH] translate: fix glossary leak issue [(#3572)](https://github.com/GoogleCloudPlatform/python-docs-samples/issues/3572) * fix glossary leak issue * removed try/catch from teardown methods, removed sample_ prefix from all other methods * added specific exceptions to tests, added backoff tags to tests * fixed the lint issues * reordered imports * moved backoff inside methd and removed Retry * corrected import nit --- samples/snippets/requirements-test.txt | 3 ++- .../translate_v3_batch_translate_text_test.py | 8 ++++-- ...batch_translate_text_with_glossary_test.py | 27 ++++++++++++++----- .../translate_v3_create_glossary_test.py | 23 +++++++++++----- .../snippets/translate_v3_detect_language.py | 2 +- .../translate_v3_detect_language_test.py | 2 +- .../translate_v3_get_glossary_test.py | 25 +++++++++++++---- .../translate_v3_get_supported_languages.py | 2 +- ...anslate_v3_get_supported_languages_test.py | 2 +- .../snippets/translate_v3_list_glossary.py | 2 +- .../translate_v3_list_glossary_test.py | 27 ++++++++++++++----- .../snippets/translate_v3_translate_text.py | 2 +- .../translate_v3_translate_text_test.py | 2 +- ...te_v3_translate_text_with_glossary_test.py | 25 +++++++++++++---- 14 files changed, 114 insertions(+), 38 deletions(-) diff --git a/samples/snippets/requirements-test.txt b/samples/snippets/requirements-test.txt index c6f9b649..1fccf2dd 100644 --- a/samples/snippets/requirements-test.txt +++ b/samples/snippets/requirements-test.txt @@ -1,2 +1,3 @@ +backoff==1.10.0 flaky==3.6.1 -pytest==5.3.2 +pytest==5.3.2 \ No newline at end of file diff --git a/samples/snippets/translate_v3_batch_translate_text_test.py b/samples/snippets/translate_v3_batch_translate_text_test.py index 9dda7c04..4fa8626a 100644 --- a/samples/snippets/translate_v3_batch_translate_text_test.py +++ b/samples/snippets/translate_v3_batch_translate_text_test.py @@ -13,11 +13,15 @@ # limitations under the License. import os -import pytest -import translate_v3_batch_translate_text import uuid + +import pytest + from google.cloud import storage +import translate_v3_batch_translate_text + + PROJECT_ID = os.environ["GCLOUD_PROJECT"] diff --git a/samples/snippets/translate_v3_batch_translate_text_with_glossary_test.py b/samples/snippets/translate_v3_batch_translate_text_with_glossary_test.py index e5010521..9af648f7 100644 --- a/samples/snippets/translate_v3_batch_translate_text_with_glossary_test.py +++ b/samples/snippets/translate_v3_batch_translate_text_with_glossary_test.py @@ -13,12 +13,19 @@ # limitations under the License. import os -import pytest import uuid + +import backoff +import pytest + +from google.api_core.exceptions import DeadlineExceeded, GoogleAPICallError +from google.cloud.exceptions import NotFound +from google.cloud import storage + import translate_v3_batch_translate_text_with_glossary import translate_v3_create_glossary import translate_v3_delete_glossary -from google.cloud import storage + PROJECT_ID = os.environ["GCLOUD_PROJECT"] GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv" @@ -34,10 +41,18 @@ def glossary(): yield glossary_id - try: - translate_v3_delete_glossary.delete_glossary(PROJECT_ID, glossary_id) - except Exception: - pass + # cleanup + @backoff.on_exception( + backoff.expo, (DeadlineExceeded, GoogleAPICallError), max_time=60 + ) + def delete_glossary(): + try: + translate_v3_delete_glossary.delete_glossary( + PROJECT_ID, glossary_id) + except NotFound as e: + # Ignoring this case. + print("Got NotFound, detail: {}".format(str(e))) + delete_glossary() @pytest.fixture(scope="function") diff --git a/samples/snippets/translate_v3_create_glossary_test.py b/samples/snippets/translate_v3_create_glossary_test.py index b24488c8..4b1e08ed 100644 --- a/samples/snippets/translate_v3_create_glossary_test.py +++ b/samples/snippets/translate_v3_create_glossary_test.py @@ -15,11 +15,16 @@ import os import uuid +import backoff import pytest +from google.api_core.exceptions import DeadlineExceeded, GoogleAPICallError +from google.cloud.exceptions import NotFound + import translate_v3_create_glossary import translate_v3_delete_glossary + PROJECT_ID = os.environ["GCLOUD_PROJECT"] GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv" @@ -36,9 +41,15 @@ def test_create_glossary(capsys): assert "Created:" in out assert "gs://cloud-samples-data/translation/glossary_ja.csv" in out finally: - # clean up after use - try: - translate_v3_delete_glossary.delete_glossary( - PROJECT_ID, glossary_id) - except Exception: - pass + # cleanup + @backoff.on_exception( + backoff.expo, (DeadlineExceeded, GoogleAPICallError), max_time=60 + ) + def delete_glossary(): + try: + translate_v3_delete_glossary.delete_glossary( + PROJECT_ID, glossary_id) + except NotFound as e: + # Ignoring this case. + print("Got NotFound, detail: {}".format(str(e))) + delete_glossary() diff --git a/samples/snippets/translate_v3_detect_language.py b/samples/snippets/translate_v3_detect_language.py index 9b92853c..759eb41a 100644 --- a/samples/snippets/translate_v3_detect_language.py +++ b/samples/snippets/translate_v3_detect_language.py @@ -16,7 +16,7 @@ from google.cloud import translate -def sample_detect_language(project_id="YOUR_PROJECT_ID"): +def detect_language(project_id="YOUR_PROJECT_ID"): """Detecting the language of a text string.""" client = translate.TranslationServiceClient() diff --git a/samples/snippets/translate_v3_detect_language_test.py b/samples/snippets/translate_v3_detect_language_test.py index 82620bc5..a538fc68 100644 --- a/samples/snippets/translate_v3_detect_language_test.py +++ b/samples/snippets/translate_v3_detect_language_test.py @@ -19,6 +19,6 @@ def test_detect_language(capsys): - translate_v3_detect_language.sample_detect_language(PROJECT_ID) + translate_v3_detect_language.detect_language(PROJECT_ID) out, _ = capsys.readouterr() assert "en" in out diff --git a/samples/snippets/translate_v3_get_glossary_test.py b/samples/snippets/translate_v3_get_glossary_test.py index 7a4aa167..8880bf6d 100644 --- a/samples/snippets/translate_v3_get_glossary_test.py +++ b/samples/snippets/translate_v3_get_glossary_test.py @@ -13,11 +13,18 @@ # limitations under the License. import os +import uuid + +import backoff import pytest + +from google.api_core.exceptions import DeadlineExceeded, GoogleAPICallError +from google.cloud.exceptions import NotFound + import translate_v3_create_glossary import translate_v3_delete_glossary import translate_v3_get_glossary -import uuid + PROJECT_ID = os.environ["GCLOUD_PROJECT"] GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv" @@ -33,10 +40,18 @@ def glossary(): yield glossary_id - try: - translate_v3_delete_glossary.delete_glossary(PROJECT_ID, glossary_id) - except Exception: - pass + # cleanup + @backoff.on_exception( + backoff.expo, (DeadlineExceeded, GoogleAPICallError), max_time=60 + ) + def delete_glossary(): + try: + translate_v3_delete_glossary.delete_glossary( + PROJECT_ID, glossary_id) + except NotFound as e: + # Ignoring this case. + print("Got NotFound, detail: {}".format(str(e))) + delete_glossary() def test_get_glossary(capsys, glossary): diff --git a/samples/snippets/translate_v3_get_supported_languages.py b/samples/snippets/translate_v3_get_supported_languages.py index eb7d8366..6e15458d 100644 --- a/samples/snippets/translate_v3_get_supported_languages.py +++ b/samples/snippets/translate_v3_get_supported_languages.py @@ -16,7 +16,7 @@ from google.cloud import translate -def sample_get_supported_languages(project_id="YOUR_PROJECT_ID"): +def get_supported_languages(project_id="YOUR_PROJECT_ID"): """Getting a list of supported language codes.""" client = translate.TranslationServiceClient() diff --git a/samples/snippets/translate_v3_get_supported_languages_test.py b/samples/snippets/translate_v3_get_supported_languages_test.py index b4ba4cd2..cd4e44a4 100644 --- a/samples/snippets/translate_v3_get_supported_languages_test.py +++ b/samples/snippets/translate_v3_get_supported_languages_test.py @@ -19,6 +19,6 @@ def test_list_languages(capsys): - translate_v3_get_supported_languages.sample_get_supported_languages(PROJECT_ID) + translate_v3_get_supported_languages.get_supported_languages(PROJECT_ID) out, _ = capsys.readouterr() assert "zh-CN" in out diff --git a/samples/snippets/translate_v3_list_glossary.py b/samples/snippets/translate_v3_list_glossary.py index e52d6b26..53e51d85 100644 --- a/samples/snippets/translate_v3_list_glossary.py +++ b/samples/snippets/translate_v3_list_glossary.py @@ -16,7 +16,7 @@ from google.cloud import translate -def sample_list_glossaries(project_id="YOUR_PROJECT_ID"): +def list_glossaries(project_id="YOUR_PROJECT_ID"): """List Glossaries.""" client = translate.TranslationServiceClient() diff --git a/samples/snippets/translate_v3_list_glossary_test.py b/samples/snippets/translate_v3_list_glossary_test.py index de77daba..d0d38cde 100644 --- a/samples/snippets/translate_v3_list_glossary_test.py +++ b/samples/snippets/translate_v3_list_glossary_test.py @@ -13,11 +13,18 @@ # limitations under the License. import os +import uuid + +import backoff import pytest + +from google.api_core.exceptions import DeadlineExceeded, GoogleAPICallError +from google.cloud.exceptions import NotFound + import translate_v3_create_glossary import translate_v3_delete_glossary import translate_v3_list_glossary -import uuid + PROJECT_ID = os.environ["GCLOUD_PROJECT"] GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv" @@ -33,14 +40,22 @@ def glossary(): yield glossary_id - try: - translate_v3_delete_glossary.sample_delete_glossary(PROJECT_ID, glossary_id) - except Exception: - pass + # clean up + @backoff.on_exception( + backoff.expo, (DeadlineExceeded, GoogleAPICallError), max_time=60 + ) + def delete_glossary(): + try: + translate_v3_delete_glossary.delete_glossary( + PROJECT_ID, glossary_id) + except NotFound as e: + # Ignoring this case. + print("Got NotFound, detail: {}".format(str(e))) + delete_glossary() def test_list_glossary(capsys, glossary): - translate_v3_list_glossary.sample_list_glossaries(PROJECT_ID) + translate_v3_list_glossary.list_glossaries(PROJECT_ID) out, _ = capsys.readouterr() assert glossary in out assert "gs://cloud-samples-data/translation/glossary_ja.csv" in out diff --git a/samples/snippets/translate_v3_translate_text.py b/samples/snippets/translate_v3_translate_text.py index 77fcec26..086f775a 100644 --- a/samples/snippets/translate_v3_translate_text.py +++ b/samples/snippets/translate_v3_translate_text.py @@ -16,7 +16,7 @@ from google.cloud import translate -def sample_translate_text(text="YOUR_TEXT_TO_TRANSLATE", project_id="YOUR_PROJECT_ID"): +def translate_text(text="YOUR_TEXT_TO_TRANSLATE", project_id="YOUR_PROJECT_ID"): """Translating Text.""" client = translate.TranslationServiceClient() diff --git a/samples/snippets/translate_v3_translate_text_test.py b/samples/snippets/translate_v3_translate_text_test.py index a6319019..096b8dd8 100644 --- a/samples/snippets/translate_v3_translate_text_test.py +++ b/samples/snippets/translate_v3_translate_text_test.py @@ -19,7 +19,7 @@ def test_translate_text(capsys): - translate_v3_translate_text.sample_translate_text( + translate_v3_translate_text.translate_text( "Hello World!", PROJECT_ID) out, _ = capsys.readouterr() assert "Bonjour le monde" in out diff --git a/samples/snippets/translate_v3_translate_text_with_glossary_test.py b/samples/snippets/translate_v3_translate_text_with_glossary_test.py index 72f9d64e..adf66836 100644 --- a/samples/snippets/translate_v3_translate_text_with_glossary_test.py +++ b/samples/snippets/translate_v3_translate_text_with_glossary_test.py @@ -14,11 +14,18 @@ # limitations under the License. import os +import uuid + +import backoff import pytest + +from google.api_core.exceptions import DeadlineExceeded, GoogleAPICallError +from google.cloud.exceptions import NotFound + import translate_v3_create_glossary import translate_v3_delete_glossary import translate_v3_translate_text_with_glossary -import uuid + PROJECT_ID = os.environ["GCLOUD_PROJECT"] GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv" @@ -34,10 +41,18 @@ def glossary(): yield glossary_id - try: - translate_v3_delete_glossary.delete_glossary(PROJECT_ID, glossary_id) - except Exception: - pass + # cleanup + @backoff.on_exception( + backoff.expo, (DeadlineExceeded, GoogleAPICallError), max_time=60 + ) + def delete_glossary(): + try: + translate_v3_delete_glossary.delete_glossary( + PROJECT_ID, glossary_id) + except NotFound as e: + # Ignoring this case. + print("Got NotFound, detail: {}".format(str(e))) + delete_glossary() def test_translate_text_with_glossary(capsys, glossary):