Skip to content

Commit 4f88d47

Browse files
author
Takashi Matsuo
authored
[monitoring] chore: remove gcp-devrel-py-tools (#3480)
* [monitoring] chore: remove gcp-devrel-py-tools * fixed the infinite loop * fix 404 error
1 parent 3fc0208 commit 4f88d47

File tree

5 files changed

+57
-42
lines changed

5 files changed

+57
-42
lines changed

monitoring/api/v3/api-client/custom_metric_test.py

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,18 @@
2424
import random
2525
import time
2626

27-
from gcp_devrel.testing import eventually_consistent
28-
from flaky import flaky
27+
import backoff
2928
import googleapiclient.discovery
3029
import pytest
30+
from googleapiclient.errors import HttpError
3131

3232
from custom_metric import create_custom_metric
3333
from custom_metric import delete_metric_descriptor
3434
from custom_metric import get_custom_metric
3535
from custom_metric import read_timeseries
3636
from custom_metric import write_timeseries_value
3737

38+
3839
PROJECT = os.environ['GCLOUD_PROJECT']
3940

4041
""" Custom metric domain for all custom metrics"""
@@ -52,7 +53,6 @@ def client():
5253
return googleapiclient.discovery.build('monitoring', 'v3')
5354

5455

55-
@flaky
5656
def test_custom_metric(client):
5757
PROJECT_RESOURCE = "projects/{}".format(PROJECT)
5858
# Use a constant seed so psuedo random number is known ahead of time
@@ -64,29 +64,42 @@ def test_custom_metric(client):
6464
INSTANCE_ID = "test_instance"
6565
METRIC_KIND = "GAUGE"
6666

67-
custom_metric_descriptor = create_custom_metric(
68-
client, PROJECT_RESOURCE, METRIC_RESOURCE, METRIC_KIND)
69-
70-
# wait until metric has been created, use the get call to wait until
71-
# a response comes back with the new metric
72-
custom_metric = None
73-
while not custom_metric:
74-
time.sleep(1)
75-
custom_metric = get_custom_metric(
76-
client, PROJECT_RESOURCE, METRIC_RESOURCE)
77-
78-
write_timeseries_value(client, PROJECT_RESOURCE,
79-
METRIC_RESOURCE, INSTANCE_ID,
80-
METRIC_KIND)
81-
82-
# Sometimes on new metric descriptors, writes have a delay in being
83-
# read back. Use eventually_consistent to account for this.
84-
@eventually_consistent.call
85-
def _():
86-
response = read_timeseries(client, PROJECT_RESOURCE, METRIC_RESOURCE)
87-
value = int(
88-
response['timeSeries'][0]['points'][0]['value']['int64Value'])
89-
# using seed of 1 will create a value of 1
90-
assert value == pseudo_random_value
91-
92-
delete_metric_descriptor(client, custom_metric_descriptor['name'])
67+
try:
68+
custom_metric_descriptor = create_custom_metric(
69+
client, PROJECT_RESOURCE, METRIC_RESOURCE, METRIC_KIND)
70+
71+
# wait until metric has been created, use the get call to wait until
72+
# a response comes back with the new metric with 10 retries.
73+
custom_metric = None
74+
retry_count = 0
75+
while not custom_metric and retry_count < 10:
76+
time.sleep(1)
77+
retry_count += 1
78+
custom_metric = get_custom_metric(
79+
client, PROJECT_RESOURCE, METRIC_RESOURCE)
80+
# Make sure we get the custom metric
81+
assert custom_metric
82+
83+
write_timeseries_value(client, PROJECT_RESOURCE,
84+
METRIC_RESOURCE, INSTANCE_ID,
85+
METRIC_KIND)
86+
87+
# Sometimes on new metric descriptors, writes have a delay in being
88+
# read back. Use eventually_consistent to account for this.
89+
@backoff.on_exception(
90+
backoff.expo, (AssertionError, HttpError), max_time=120)
91+
def eventually_consistent_test():
92+
response = read_timeseries(
93+
client, PROJECT_RESOURCE, METRIC_RESOURCE)
94+
# Make sure the value is not empty.
95+
assert 'timeSeries' in response
96+
value = int(
97+
response['timeSeries'][0]['points'][0]['value']['int64Value'])
98+
# using seed of 1 will create a value of 1
99+
assert value == pseudo_random_value
100+
101+
eventually_consistent_test()
102+
103+
finally:
104+
# cleanup
105+
delete_metric_descriptor(client, custom_metric_descriptor['name'])
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1+
backoff==1.10.0
12
pytest==5.3.2
2-
gcp-devrel-py-tools==0.0.15
3-
google-cloud-core==1.3.0
43
flaky==3.6.1

monitoring/api/v3/cloud-client/quickstart_test.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414

1515
import os
1616

17+
import backoff
1718
import mock
1819
import pytest
19-
from gcp_devrel.testing import eventually_consistent
2020

2121
import quickstart
2222

@@ -37,8 +37,10 @@ def mock_project_path():
3737

3838

3939
def test_quickstart(capsys, mock_project_path):
40-
@eventually_consistent.call
41-
def _():
40+
@backoff.on_exception(backoff.expo, AssertionError, max_time=60)
41+
def eventually_consistent_test():
4242
quickstart.run_quickstart()
4343
out, _ = capsys.readouterr()
4444
assert 'wrote' in out
45+
46+
eventually_consistent_test()
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1+
backoff==1.10.0
12
pytest==5.3.2
23
mock==3.0.5
3-
gcp-devrel-py-tools==0.0.15
4-
google-cloud-core==1.3.0

monitoring/api/v3/cloud-client/snippets_test.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
import re
1717
import pytest
1818

19-
from gcp_devrel.testing import eventually_consistent
19+
import backoff
2020
from google.api_core.exceptions import NotFound
2121

22-
2322
import snippets
2423

24+
2525
PROJECT_ID = os.environ['GCLOUD_PROJECT']
2626

2727

@@ -48,12 +48,14 @@ def write_time_series():
4848

4949
def test_get_delete_metric_descriptor(capsys, custom_metric_descriptor):
5050
try:
51-
@eventually_consistent.call
52-
def __():
51+
@backoff.on_exception(
52+
backoff.expo, (AssertionError, NotFound), max_time=60)
53+
def eventually_consistent_test():
5354
snippets.get_metric_descriptor(custom_metric_descriptor)
55+
out, _ = capsys.readouterr()
56+
assert 'DOUBLE' in out
5457

55-
out, _ = capsys.readouterr()
56-
assert 'DOUBLE' in out
58+
eventually_consistent_test()
5759
finally:
5860
snippets.delete_metric_descriptor(custom_metric_descriptor)
5961
out, _ = capsys.readouterr()

0 commit comments

Comments
 (0)