From 6da517ff7b36c3b0507434734a6b1f4e8e28461b Mon Sep 17 00:00:00 2001 From: Takashi Matsuo Date: Thu, 23 Apr 2020 01:00:25 +0000 Subject: [PATCH 1/3] [monitoring] chore: remove gcp-devrel-py-tools --- .../api/v3/api-client/custom_metric_test.py | 64 ++++++++++--------- .../api/v3/api-client/requirements-test.txt | 3 +- .../api/v3/cloud-client/quickstart_test.py | 8 ++- .../api/v3/cloud-client/requirements-test.txt | 3 +- .../api/v3/cloud-client/snippets_test.py | 13 ++-- 5 files changed, 49 insertions(+), 42 deletions(-) diff --git a/monitoring/api/v3/api-client/custom_metric_test.py b/monitoring/api/v3/api-client/custom_metric_test.py index 5d51c15ec8f..2e8d4f4d4e0 100644 --- a/monitoring/api/v3/api-client/custom_metric_test.py +++ b/monitoring/api/v3/api-client/custom_metric_test.py @@ -24,8 +24,7 @@ import random import time -from gcp_devrel.testing import eventually_consistent -from flaky import flaky +import backoff import googleapiclient.discovery import pytest @@ -35,6 +34,7 @@ from custom_metric import read_timeseries from custom_metric import write_timeseries_value + PROJECT = os.environ['GCLOUD_PROJECT'] """ Custom metric domain for all custom metrics""" @@ -52,7 +52,7 @@ def client(): return googleapiclient.discovery.build('monitoring', 'v3') -@flaky +@pytest.mark.flaky def test_custom_metric(client): PROJECT_RESOURCE = "projects/{}".format(PROJECT) # Use a constant seed so psuedo random number is known ahead of time @@ -64,29 +64,35 @@ def test_custom_metric(client): INSTANCE_ID = "test_instance" METRIC_KIND = "GAUGE" - custom_metric_descriptor = create_custom_metric( - client, PROJECT_RESOURCE, METRIC_RESOURCE, METRIC_KIND) - - # wait until metric has been created, use the get call to wait until - # a response comes back with the new metric - custom_metric = None - while not custom_metric: - time.sleep(1) - custom_metric = get_custom_metric( - client, PROJECT_RESOURCE, METRIC_RESOURCE) - - write_timeseries_value(client, PROJECT_RESOURCE, - METRIC_RESOURCE, INSTANCE_ID, - METRIC_KIND) - - # Sometimes on new metric descriptors, writes have a delay in being - # read back. Use eventually_consistent to account for this. - @eventually_consistent.call - def _(): - response = read_timeseries(client, PROJECT_RESOURCE, METRIC_RESOURCE) - value = int( - response['timeSeries'][0]['points'][0]['value']['int64Value']) - # using seed of 1 will create a value of 1 - assert value == pseudo_random_value - - delete_metric_descriptor(client, custom_metric_descriptor['name']) + try: + custom_metric_descriptor = create_custom_metric( + client, PROJECT_RESOURCE, METRIC_RESOURCE, METRIC_KIND) + + # wait until metric has been created, use the get call to wait until + # a response comes back with the new metric + custom_metric = None + while not custom_metric: + time.sleep(1) + custom_metric = get_custom_metric( + client, PROJECT_RESOURCE, METRIC_RESOURCE) + + write_timeseries_value(client, PROJECT_RESOURCE, + METRIC_RESOURCE, INSTANCE_ID, + METRIC_KIND) + + # Sometimes on new metric descriptors, writes have a delay in being + # read back. Use eventually_consistent to account for this. + @backoff.on_exception(backoff.expo, AssertionError, max_time=120) + def eventually_consistent_test(): + response = read_timeseries( + client, PROJECT_RESOURCE, METRIC_RESOURCE) + value = int( + response['timeSeries'][0]['points'][0]['value']['int64Value']) + # using seed of 1 will create a value of 1 + assert value == pseudo_random_value + + eventually_consistent_test() + + finally: + # cleanup + delete_metric_descriptor(client, custom_metric_descriptor['name']) diff --git a/monitoring/api/v3/api-client/requirements-test.txt b/monitoring/api/v3/api-client/requirements-test.txt index 6e69b9d0715..40857e816e6 100644 --- a/monitoring/api/v3/api-client/requirements-test.txt +++ b/monitoring/api/v3/api-client/requirements-test.txt @@ -1,4 +1,3 @@ +backoff==1.10.0 pytest==5.3.2 -gcp-devrel-py-tools==0.0.15 -google-cloud-core==1.3.0 flaky==3.6.1 diff --git a/monitoring/api/v3/cloud-client/quickstart_test.py b/monitoring/api/v3/cloud-client/quickstart_test.py index 2befff9f07d..045ef3c443f 100644 --- a/monitoring/api/v3/cloud-client/quickstart_test.py +++ b/monitoring/api/v3/cloud-client/quickstart_test.py @@ -14,9 +14,9 @@ import os +import backoff import mock import pytest -from gcp_devrel.testing import eventually_consistent import quickstart @@ -37,8 +37,10 @@ def mock_project_path(): def test_quickstart(capsys, mock_project_path): - @eventually_consistent.call - def _(): + @backoff.on_exception(backoff.expo, AssertionError, max_time=60) + def eventually_consistent_test(): quickstart.run_quickstart() out, _ = capsys.readouterr() assert 'wrote' in out + + eventually_consistent_test() diff --git a/monitoring/api/v3/cloud-client/requirements-test.txt b/monitoring/api/v3/cloud-client/requirements-test.txt index 53eb762b994..adf26b9f98b 100644 --- a/monitoring/api/v3/cloud-client/requirements-test.txt +++ b/monitoring/api/v3/cloud-client/requirements-test.txt @@ -1,4 +1,3 @@ +backoff==1.10.0 pytest==5.3.2 mock==3.0.5 -gcp-devrel-py-tools==0.0.15 -google-cloud-core==1.3.0 diff --git a/monitoring/api/v3/cloud-client/snippets_test.py b/monitoring/api/v3/cloud-client/snippets_test.py index a6676cdf566..8ee986ce156 100644 --- a/monitoring/api/v3/cloud-client/snippets_test.py +++ b/monitoring/api/v3/cloud-client/snippets_test.py @@ -16,12 +16,12 @@ import re import pytest -from gcp_devrel.testing import eventually_consistent +import backoff from google.api_core.exceptions import NotFound - import snippets + PROJECT_ID = os.environ['GCLOUD_PROJECT'] @@ -48,12 +48,13 @@ def write_time_series(): def test_get_delete_metric_descriptor(capsys, custom_metric_descriptor): try: - @eventually_consistent.call - def __(): + @backoff.on_exception(backoff.expo, AssertionError, max_time=60) + def eventually_consistent_test(): snippets.get_metric_descriptor(custom_metric_descriptor) + out, _ = capsys.readouterr() + assert 'DOUBLE' in out - out, _ = capsys.readouterr() - assert 'DOUBLE' in out + eventually_consistent_test() finally: snippets.delete_metric_descriptor(custom_metric_descriptor) out, _ = capsys.readouterr() From 309e3e3716f39f1a08ae37259ee2edcb02adc6a5 Mon Sep 17 00:00:00 2001 From: Takashi Matsuo Date: Thu, 23 Apr 2020 18:06:00 +0000 Subject: [PATCH 2/3] fixed the infinite loop --- .../api/v3/api-client/custom_metric_test.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/monitoring/api/v3/api-client/custom_metric_test.py b/monitoring/api/v3/api-client/custom_metric_test.py index 2e8d4f4d4e0..1b2627a8d33 100644 --- a/monitoring/api/v3/api-client/custom_metric_test.py +++ b/monitoring/api/v3/api-client/custom_metric_test.py @@ -27,6 +27,7 @@ import backoff import googleapiclient.discovery import pytest +from googleapiclient.errors import HttpError from custom_metric import create_custom_metric from custom_metric import delete_metric_descriptor @@ -52,7 +53,6 @@ def client(): return googleapiclient.discovery.build('monitoring', 'v3') -@pytest.mark.flaky def test_custom_metric(client): PROJECT_RESOURCE = "projects/{}".format(PROJECT) # Use a constant seed so psuedo random number is known ahead of time @@ -69,12 +69,16 @@ def test_custom_metric(client): client, PROJECT_RESOURCE, METRIC_RESOURCE, METRIC_KIND) # wait until metric has been created, use the get call to wait until - # a response comes back with the new metric + # a response comes back with the new metric with 10 retries. custom_metric = None - while not custom_metric: + retry_count = 0 + while not custom_metric and retry_count < 10: time.sleep(1) + retry_count += 1 custom_metric = get_custom_metric( client, PROJECT_RESOURCE, METRIC_RESOURCE) + # Make sure we get the custom metric + assert custom_metric write_timeseries_value(client, PROJECT_RESOURCE, METRIC_RESOURCE, INSTANCE_ID, @@ -82,10 +86,13 @@ def test_custom_metric(client): # Sometimes on new metric descriptors, writes have a delay in being # read back. Use eventually_consistent to account for this. - @backoff.on_exception(backoff.expo, AssertionError, max_time=120) + @backoff.on_exception( + backoff.expo, (AssertionError, HttpError), max_time=120) def eventually_consistent_test(): response = read_timeseries( client, PROJECT_RESOURCE, METRIC_RESOURCE) + # Make sure the value is not empty. + assert 'timeSeries' in response value = int( response['timeSeries'][0]['points'][0]['value']['int64Value']) # using seed of 1 will create a value of 1 From 881f1f2059d7c12223d032e2fdd65bd5ef304902 Mon Sep 17 00:00:00 2001 From: Takashi Matsuo Date: Thu, 23 Apr 2020 18:53:58 +0000 Subject: [PATCH 3/3] fix 404 error --- monitoring/api/v3/cloud-client/snippets_test.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/monitoring/api/v3/cloud-client/snippets_test.py b/monitoring/api/v3/cloud-client/snippets_test.py index 8ee986ce156..117026959e3 100644 --- a/monitoring/api/v3/cloud-client/snippets_test.py +++ b/monitoring/api/v3/cloud-client/snippets_test.py @@ -48,7 +48,8 @@ def write_time_series(): def test_get_delete_metric_descriptor(capsys, custom_metric_descriptor): try: - @backoff.on_exception(backoff.expo, AssertionError, max_time=60) + @backoff.on_exception( + backoff.expo, (AssertionError, NotFound), max_time=60) def eventually_consistent_test(): snippets.get_metric_descriptor(custom_metric_descriptor) out, _ = capsys.readouterr()