Skip to content

Commit

Permalink
testing(asset): use conftest.py [(#4245)](GoogleCloudPlatform/python-…
Browse files Browse the repository at this point in the history
…docs-samples#4245)

fixes #4235
(by retrying upon InternalServerError)

Co-authored-by: Leah E. Cole <[email protected]>
  • Loading branch information
2 people authored and busunkim96 committed Aug 4, 2020
1 parent 729d5ee commit acbc95b
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 107 deletions.
87 changes: 87 additions & 0 deletions packages/google-cloud-asset/samples/snippets/conftest.py
Original file line number Diff line number Diff line change
@@ -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}")
Original file line number Diff line number Diff line change
Expand Up @@ -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__':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit acbc95b

Please sign in to comment.