Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: monitoring tests #2995

Merged
merged 5 commits into from
Feb 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions monitoring/api/v3/cloud-client/quickstart_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import mock
import pytest
from gcp_devrel.testing import eventually_consistent

import quickstart

Expand All @@ -36,6 +37,8 @@ def mock_project_path():


def test_quickstart(capsys, mock_project_path):
quickstart.run_quickstart()
out, _ = capsys.readouterr()
assert 'wrote' in out
@eventually_consistent.call
def _():
quickstart.run_quickstart()
out, _ = capsys.readouterr()
assert 'wrote' in out
50 changes: 13 additions & 37 deletions monitoring/api/v3/cloud-client/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,21 @@
import argparse
import os
import pprint
import random
import time
import uuid

from google.cloud import monitoring_v3


# Avoid collisions with other runs
RANDOM_SUFFIX = str(random.randint(1000, 9999))
PROJECT_ID = os.environ['GCLOUD_PROJECT']


def create_metric_descriptor(project_id):
# [START monitoring_create_metric]
client = monitoring_v3.MetricServiceClient()
project_name = client.project_path(project_id)
descriptor = monitoring_v3.types.MetricDescriptor()
descriptor.type = 'custom.googleapis.com/my_metric' + RANDOM_SUFFIX
descriptor.type = 'custom.googleapis.com/my_metric' + str(uuid.uuid4())
descriptor.metric_kind = (
monitoring_v3.enums.MetricDescriptor.MetricKind.GAUGE)
descriptor.value_type = (
Expand All @@ -55,7 +54,7 @@ def write_time_series(project_id):
project_name = client.project_path(project_id)

series = monitoring_v3.types.TimeSeries()
series.metric.type = 'custom.googleapis.com/my_metric' + RANDOM_SUFFIX
series.metric.type = 'custom.googleapis.com/my_metric' + str(uuid.uuid4())
series.resource.type = 'gce_instance'
series.resource.labels['instance_id'] = '1234567890123456789'
series.resource.labels['zone'] = 'us-central1-f'
Expand Down Expand Up @@ -205,29 +204,6 @@ def get_metric_descriptor(metric_name):
# [END monitoring_get_descriptor]


class MissingProjectIdError(Exception):
pass


def project_id():
"""Retreives the project id from the environment variable.

Raises:
MissingProjectIdError -- When not set.

Returns:
str -- the project name
"""
project_id = (os.environ['GOOGLE_CLOUD_PROJECT'] or
os.environ['GCLOUD_PROJECT'])

if not project_id:
raise MissingProjectIdError(
'Set the environment variable ' +
'GCLOUD_PROJECT to your Google Cloud Project Id.')
return project_id


if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Demonstrates Monitoring API operations.')
Expand Down Expand Up @@ -310,25 +286,25 @@ def project_id():
args = parser.parse_args()

if args.command == 'create-metric-descriptor':
create_metric_descriptor(project_id())
create_metric_descriptor(PROJECT_ID)
if args.command == 'list-metric-descriptors':
list_metric_descriptors(project_id())
list_metric_descriptors(PROJECT_ID)
if args.command == 'get-metric-descriptor':
get_metric_descriptor(args.metric_type_name)
if args.command == 'delete-metric-descriptor':
delete_metric_descriptor(args.metric_descriptor_name)
if args.command == 'list-resources':
list_monitored_resources(project_id())
list_monitored_resources(PROJECT_ID)
if args.command == 'get-resource':
get_monitored_resource_descriptor(
project_id(), args.resource_type_name)
PROJECT_ID, args.resource_type_name)
if args.command == 'write-time-series':
write_time_series(project_id())
write_time_series(PROJECT_ID)
if args.command == 'list-time-series':
list_time_series(project_id())
list_time_series(PROJECT_ID)
if args.command == 'list-time-series-header':
list_time_series_header(project_id())
list_time_series_header(PROJECT_ID)
if args.command == 'list-time-series-reduce':
list_time_series_reduce(project_id())
list_time_series_reduce(PROJECT_ID)
if args.command == 'list-time-series-aggregate':
list_time_series_aggregate(project_id())
list_time_series_aggregate(PROJECT_ID)
55 changes: 41 additions & 14 deletions monitoring/api/v3/cloud-client/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,69 +12,96 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import re
import pytest

from gcp_devrel.testing import eventually_consistent
from google.api_core.exceptions import NotFound


import snippets

PROJECT_ID = os.environ['GCLOUD_PROJECT']


def test_create_get_delete_metric_descriptor(capsys):
snippets.create_metric_descriptor(snippets.project_id())
@pytest.fixture(scope="function")
def custom_metric_descriptor(capsys):
snippets.create_metric_descriptor(PROJECT_ID)
out, _ = capsys.readouterr()
match = re.search(r'Created (.*)\.', out)
metric_name = match.group(1)
yield metric_name

# teardown
try:
snippets.delete_metric_descriptor(metric_name)
except NotFound:
print("Metric descriptor already deleted")


@pytest.fixture(scope="module")
def write_time_series():
snippets.write_time_series(PROJECT_ID)
yield


def test_get_delete_metric_descriptor(capsys, custom_metric_descriptor):
try:
@eventually_consistent.call
def __():
snippets.get_metric_descriptor(metric_name)
snippets.get_metric_descriptor(custom_metric_descriptor)

out, _ = capsys.readouterr()
assert 'DOUBLE' in out
finally:
snippets.delete_metric_descriptor(metric_name)
snippets.delete_metric_descriptor(custom_metric_descriptor)
out, _ = capsys.readouterr()
assert 'Deleted metric' in out


def test_list_metric_descriptors(capsys):
snippets.list_metric_descriptors(snippets.project_id())
snippets.list_metric_descriptors(PROJECT_ID)
out, _ = capsys.readouterr()
assert 'logging.googleapis.com/byte_count' in out


def test_list_resources(capsys):
snippets.list_monitored_resources(snippets.project_id())
snippets.list_monitored_resources(PROJECT_ID)
out, _ = capsys.readouterr()
assert 'pubsub_topic' in out


def test_get_resources(capsys):
snippets.get_monitored_resource_descriptor(
snippets.project_id(), 'pubsub_topic')
PROJECT_ID, 'pubsub_topic')
out, _ = capsys.readouterr()
assert 'A topic in Google Cloud Pub/Sub' in out


def test_time_series(capsys):
snippets.write_time_series(snippets.project_id())

snippets.list_time_series(snippets.project_id())
def test_list_time_series(capsys, write_time_series):
snippets.list_time_series(PROJECT_ID)
out, _ = capsys.readouterr()
assert 'gce_instance' in out

snippets.list_time_series_header(snippets.project_id())

def test_list_time_series_header(capsys, write_time_series):
snippets.list_time_series_header(PROJECT_ID)
out, _ = capsys.readouterr()
assert 'gce_instance' in out

snippets.list_time_series_aggregate(snippets.project_id())

def test_list_time_series_aggregate(capsys, write_time_series):
snippets.list_time_series_aggregate(PROJECT_ID)
out, _ = capsys.readouterr()
assert 'points' in out
assert 'interval' in out
assert 'start_time' in out
assert 'end_time' in out

snippets.list_time_series_reduce(snippets.project_id())

def test_list_time_series_reduce(capsys, write_time_series):
snippets.list_time_series_reduce(PROJECT_ID)
out, _ = capsys.readouterr()
assert 'points' in out
assert 'interval' in out
Expand Down