|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# Copyright 2020 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +import os |
| 18 | +import uuid |
| 19 | + |
| 20 | +import backoff |
| 21 | +from google.api_core.exceptions import InternalServerError |
| 22 | +from google.api_core.exceptions import NotFound |
| 23 | +from google.cloud import pubsub_v1 |
| 24 | +import pytest |
| 25 | + |
| 26 | +import quickstart_createfeed |
| 27 | +import quickstart_deletefeed |
| 28 | + |
| 29 | + |
| 30 | +PROJECT = os.environ['GOOGLE_CLOUD_PROJECT'] |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture(scope="module") |
| 34 | +def test_topic(): |
| 35 | + topic_id = f'topic-{uuid.uuid4().hex}' |
| 36 | + publisher = pubsub_v1.PublisherClient() |
| 37 | + topic_path = publisher.topic_path(PROJECT, topic_id) |
| 38 | + topic = publisher.create_topic(topic_path) |
| 39 | + |
| 40 | + yield topic |
| 41 | + |
| 42 | + publisher.delete_topic(topic_path) |
| 43 | + |
| 44 | + |
| 45 | +@pytest.fixture(scope="module") |
| 46 | +def another_topic(): |
| 47 | + topic_id = f'topic-{uuid.uuid4().hex}' |
| 48 | + publisher = pubsub_v1.PublisherClient() |
| 49 | + topic_path = publisher.topic_path(PROJECT, topic_id) |
| 50 | + topic = publisher.create_topic(topic_path) |
| 51 | + |
| 52 | + yield topic |
| 53 | + |
| 54 | + publisher.delete_topic(topic_path) |
| 55 | + |
| 56 | + |
| 57 | +@pytest.fixture(scope="module") |
| 58 | +def test_feed(test_topic): |
| 59 | + feed_id = f'feed-{uuid.uuid4().hex}' |
| 60 | + asset_name = f'assets-{uuid.uuid4().hex}' |
| 61 | + |
| 62 | + @backoff.on_exception(backoff.expo, InternalServerError, max_time=60) |
| 63 | + def create_feed(): |
| 64 | + return quickstart_createfeed.create_feed( |
| 65 | + PROJECT, feed_id, [asset_name, ], test_topic.name) |
| 66 | + |
| 67 | + feed = create_feed() |
| 68 | + |
| 69 | + yield feed |
| 70 | + |
| 71 | + try: |
| 72 | + quickstart_deletefeed.delete_feed(feed.name) |
| 73 | + except NotFound as e: |
| 74 | + print(f"Ignoring NotFound: {e}") |
| 75 | + |
| 76 | + |
| 77 | +@pytest.fixture(scope="module") |
| 78 | +def deleter(): |
| 79 | + feeds_to_delete = [] |
| 80 | + |
| 81 | + yield feeds_to_delete |
| 82 | + |
| 83 | + for feed_name in feeds_to_delete: |
| 84 | + try: |
| 85 | + quickstart_deletefeed.delete_feed(feed_name) |
| 86 | + except NotFound as e: |
| 87 | + print(f"Ignoring NotFound: {e}") |
0 commit comments