From ff804a584ad5bdf772f7c9091090f8ae2246cf0d Mon Sep 17 00:00:00 2001 From: Takashi Matsuo Date: Mon, 6 Jul 2020 14:10:52 -0700 Subject: [PATCH] testing(asset): use conftest.py [(#4245)](https://github.com/GoogleCloudPlatform/python-docs-samples/issues/4245) fixes #4235 (by retrying upon InternalServerError) Co-authored-by: Leah E. Cole <6719667+leahecole@users.noreply.github.com> --- asset/snippets/snippets/conftest.py | 87 +++++++++++++++++++ .../snippets/quickstart_createfeed.py | 1 + .../snippets/quickstart_createfeed_test.py | 29 ++----- .../snippets/quickstart_deletefeed_test.py | 24 +---- .../snippets/quickstart_getfeed_test.py | 31 +------ .../snippets/quickstart_updatefeed_test.py | 36 +------- 6 files changed, 101 insertions(+), 107 deletions(-) create mode 100644 asset/snippets/snippets/conftest.py diff --git a/asset/snippets/snippets/conftest.py b/asset/snippets/snippets/conftest.py new file mode 100644 index 000000000000..c51b3631d724 --- /dev/null +++ b/asset/snippets/snippets/conftest.py @@ -0,0 +1,87 @@ +#!/usr/bin/env python +# +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os +import uuid + +import backoff +from google.api_core.exceptions import InternalServerError +from google.api_core.exceptions import NotFound +from google.cloud import pubsub_v1 +import pytest + +import quickstart_createfeed +import quickstart_deletefeed + + +PROJECT = os.environ['GOOGLE_CLOUD_PROJECT'] + + +@pytest.fixture(scope="module") +def test_topic(): + topic_id = f'topic-{uuid.uuid4().hex}' + publisher = pubsub_v1.PublisherClient() + topic_path = publisher.topic_path(PROJECT, topic_id) + topic = publisher.create_topic(topic_path) + + yield topic + + publisher.delete_topic(topic_path) + + +@pytest.fixture(scope="module") +def another_topic(): + topic_id = f'topic-{uuid.uuid4().hex}' + publisher = pubsub_v1.PublisherClient() + topic_path = publisher.topic_path(PROJECT, topic_id) + topic = publisher.create_topic(topic_path) + + yield topic + + publisher.delete_topic(topic_path) + + +@pytest.fixture(scope="module") +def test_feed(test_topic): + feed_id = f'feed-{uuid.uuid4().hex}' + asset_name = f'assets-{uuid.uuid4().hex}' + + @backoff.on_exception(backoff.expo, InternalServerError, max_time=60) + def create_feed(): + return quickstart_createfeed.create_feed( + PROJECT, feed_id, [asset_name, ], test_topic.name) + + feed = create_feed() + + yield feed + + try: + quickstart_deletefeed.delete_feed(feed.name) + except NotFound as e: + print(f"Ignoring NotFound: {e}") + + +@pytest.fixture(scope="module") +def deleter(): + feeds_to_delete = [] + + yield feeds_to_delete + + for feed_name in feeds_to_delete: + try: + quickstart_deletefeed.delete_feed(feed_name) + except NotFound as e: + print(f"Ignoring NotFound: {e}") diff --git a/asset/snippets/snippets/quickstart_createfeed.py b/asset/snippets/snippets/quickstart_createfeed.py index c77fb5c2cac4..7ee7b7e3e732 100644 --- a/asset/snippets/snippets/quickstart_createfeed.py +++ b/asset/snippets/snippets/quickstart_createfeed.py @@ -36,6 +36,7 @@ def create_feed(project_id, feed_id, asset_names, topic): response = client.create_feed(parent, feed_id, feed) print('feed: {}'.format(response)) # [END asset_quickstart_create_feed] + return response if __name__ == '__main__': diff --git a/asset/snippets/snippets/quickstart_createfeed_test.py b/asset/snippets/snippets/quickstart_createfeed_test.py index 956d957a5789..89e993a43b8e 100644 --- a/asset/snippets/snippets/quickstart_createfeed_test.py +++ b/asset/snippets/snippets/quickstart_createfeed_test.py @@ -14,37 +14,20 @@ # See the License for the specific language governing permissions and # limitations under the License. -import json import os import uuid -from google.cloud import pubsub_v1 -from google.cloud import resource_manager - import quickstart_createfeed -import quickstart_deletefeed -json_data = open(os.environ["GOOGLE_APPLICATION_CREDENTIALS"]).read() -data = json.loads(json_data) -PROJECT = data['project_id'] + +PROJECT = os.environ['GOOGLE_CLOUD_PROJECT'] ASSET_NAME = 'assets-{}'.format(uuid.uuid4().hex) FEED_ID = 'feed-{}'.format(uuid.uuid4().hex) -TOPIC = 'topic-{}'.format(uuid.uuid4().hex) -def test_create_feed(capsys): - client = resource_manager.Client() - project_number = client.fetch_project(PROJECT).number - full_topic_name = "projects/{}/topics/{}".format(PROJECT, TOPIC) - publisher = pubsub_v1.PublisherClient() - topic_path = publisher.topic_path(PROJECT, TOPIC) - publisher.create_topic(topic_path) - quickstart_createfeed.create_feed( - PROJECT, FEED_ID, [ASSET_NAME, ], full_topic_name) +def test_create_feed(capsys, test_topic, deleter): + feed = quickstart_createfeed.create_feed( + PROJECT, FEED_ID, [ASSET_NAME, ], test_topic.name) + deleter.append(feed.name) out, _ = capsys.readouterr() assert "feed" in out - - # Clean up, delete the feed - feed_name = "projects/{}/feeds/{}".format(project_number, FEED_ID) - quickstart_deletefeed.delete_feed(feed_name) - publisher.delete_topic(topic_path) diff --git a/asset/snippets/snippets/quickstart_deletefeed_test.py b/asset/snippets/snippets/quickstart_deletefeed_test.py index 5ec6f8021128..caa4dd0c94f6 100644 --- a/asset/snippets/snippets/quickstart_deletefeed_test.py +++ b/asset/snippets/snippets/quickstart_deletefeed_test.py @@ -15,34 +15,16 @@ # limitations under the License. import os -import uuid -from google.cloud import pubsub_v1 -from google.cloud import resource_manager - -import quickstart_createfeed import quickstart_deletefeed + PROJECT = os.environ['GOOGLE_CLOUD_PROJECT'] -ASSET_NAME = 'assets-{}'.format(uuid.uuid4().hex) -FEED_ID = 'feed-{}'.format(uuid.uuid4().hex) -TOPIC = 'topic-{}'.format(uuid.uuid4().hex) -def test_delete_feed(capsys): - client = resource_manager.Client() - project_number = client.fetch_project(PROJECT).number - # First create the feed, which will be deleted later - full_topic_name = "projects/{}/topics/{}".format(PROJECT, TOPIC) - publisher = pubsub_v1.PublisherClient() - topic_path = publisher.topic_path(PROJECT, TOPIC) - publisher.create_topic(topic_path) - quickstart_createfeed.create_feed( - PROJECT, FEED_ID, [ASSET_NAME, ], full_topic_name) +def test_delete_feed(capsys, test_feed): - feed_name = "projects/{}/feeds/{}".format(project_number, FEED_ID) - quickstart_deletefeed.delete_feed(feed_name) + quickstart_deletefeed.delete_feed(test_feed.name) out, _ = capsys.readouterr() assert "deleted_feed" in out - publisher.delete_topic(topic_path) diff --git a/asset/snippets/snippets/quickstart_getfeed_test.py b/asset/snippets/snippets/quickstart_getfeed_test.py index 940ce35557fd..dd2a92bd9bc6 100644 --- a/asset/snippets/snippets/quickstart_getfeed_test.py +++ b/asset/snippets/snippets/quickstart_getfeed_test.py @@ -14,38 +14,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os -import uuid - -from google.cloud import pubsub_v1 -from google.cloud import resource_manager - -import quickstart_createfeed -import quickstart_deletefeed import quickstart_getfeed -PROJECT = os.environ['GOOGLE_CLOUD_PROJECT'] -ASSET_NAME = 'assets-{}'.format(uuid.uuid4().hex) -FEED_ID = 'feed-{}'.format(uuid.uuid4().hex) -TOPIC = 'topic-{}'.format(uuid.uuid4().hex) - - -def test_get_feed(capsys): - client = resource_manager.Client() - project_number = client.fetch_project(PROJECT).number - # First create the feed, which will be gotten later - full_topic_name = "projects/{}/topics/{}".format(PROJECT, TOPIC) - publisher = pubsub_v1.PublisherClient() - topic_path = publisher.topic_path(PROJECT, TOPIC) - publisher.create_topic(topic_path) - quickstart_createfeed.create_feed( - PROJECT, FEED_ID, [ASSET_NAME, ], full_topic_name) - feed_name = "projects/{}/feeds/{}".format(project_number, FEED_ID) - quickstart_getfeed.get_feed(feed_name) +def test_get_feed(capsys, test_feed): + quickstart_getfeed.get_feed(test_feed.name) out, _ = capsys.readouterr() assert "gotten_feed" in out - # Clean up and delete the feed - quickstart_deletefeed.delete_feed(feed_name) - publisher.delete_topic(topic_path) diff --git a/asset/snippets/snippets/quickstart_updatefeed_test.py b/asset/snippets/snippets/quickstart_updatefeed_test.py index c996020ef82e..e09cb19102d5 100644 --- a/asset/snippets/snippets/quickstart_updatefeed_test.py +++ b/asset/snippets/snippets/quickstart_updatefeed_test.py @@ -14,43 +14,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os -import uuid - -from google.cloud import pubsub_v1 -from google.cloud import resource_manager - -import quickstart_createfeed -import quickstart_deletefeed import quickstart_updatefeed -PROJECT = os.environ['GOOGLE_CLOUD_PROJECT'] -ASSET_NAME = 'assets-{}'.format(uuid.uuid4().hex) -FEED_ID = 'feed-{}'.format(uuid.uuid4().hex) -TOPIC = 'topic-{}'.format(uuid.uuid4().hex) -NEW_TOPIC = 'new-topic-{}'.format(uuid.uuid4().hex) - - -def test_update_feed(capsys): - client = resource_manager.Client() - project_number = client.fetch_project(PROJECT).number - # First create the feed, which will be updated later - full_topic_name = "projects/{}/topics/{}".format(PROJECT, TOPIC) - publisher = pubsub_v1.PublisherClient() - topic_path = publisher.topic_path(PROJECT, TOPIC) - publisher.create_topic(topic_path) - quickstart_createfeed.create_feed( - PROJECT, FEED_ID, [ASSET_NAME, ], full_topic_name) - feed_name = "projects/{}/feeds/{}".format(project_number, FEED_ID) - new_full_topic_name = "projects/" + PROJECT + "/topics/" + NEW_TOPIC - new_topic_path = publisher.topic_path(PROJECT, NEW_TOPIC) - publisher.create_topic(new_topic_path) - quickstart_updatefeed.update_feed(feed_name, new_full_topic_name) +def test_update_feed(capsys, test_feed, another_topic): + quickstart_updatefeed.update_feed(test_feed.name, another_topic.name) out, _ = capsys.readouterr() assert "updated_feed" in out - # Clean up and delete the feed - quickstart_deletefeed.delete_feed(feed_name) - publisher.delete_topic(topic_path) - publisher.delete_topic(new_topic_path)