Skip to content

Commit 305a1ba

Browse files
Takashi Matsuoleahecole
Takashi Matsuo
andauthored
testing(asset): use conftest.py (#4245)
fixes #4235 (by retrying upon InternalServerError) Co-authored-by: Leah E. Cole <[email protected]>
1 parent 5431dad commit 305a1ba

6 files changed

+101
-107
lines changed

asset/cloud-client/conftest.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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}")

asset/cloud-client/quickstart_createfeed.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def create_feed(project_id, feed_id, asset_names, topic):
3636
response = client.create_feed(parent, feed_id, feed)
3737
print('feed: {}'.format(response))
3838
# [END asset_quickstart_create_feed]
39+
return response
3940

4041

4142
if __name__ == '__main__':

asset/cloud-client/quickstart_createfeed_test.py

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,20 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
import json
1817
import os
1918
import uuid
2019

21-
from google.cloud import pubsub_v1
22-
from google.cloud import resource_manager
23-
2420
import quickstart_createfeed
25-
import quickstart_deletefeed
2621

27-
json_data = open(os.environ["GOOGLE_APPLICATION_CREDENTIALS"]).read()
28-
data = json.loads(json_data)
29-
PROJECT = data['project_id']
22+
23+
PROJECT = os.environ['GOOGLE_CLOUD_PROJECT']
3024
ASSET_NAME = 'assets-{}'.format(uuid.uuid4().hex)
3125
FEED_ID = 'feed-{}'.format(uuid.uuid4().hex)
32-
TOPIC = 'topic-{}'.format(uuid.uuid4().hex)
3326

3427

35-
def test_create_feed(capsys):
36-
client = resource_manager.Client()
37-
project_number = client.fetch_project(PROJECT).number
38-
full_topic_name = "projects/{}/topics/{}".format(PROJECT, TOPIC)
39-
publisher = pubsub_v1.PublisherClient()
40-
topic_path = publisher.topic_path(PROJECT, TOPIC)
41-
publisher.create_topic(topic_path)
42-
quickstart_createfeed.create_feed(
43-
PROJECT, FEED_ID, [ASSET_NAME, ], full_topic_name)
28+
def test_create_feed(capsys, test_topic, deleter):
29+
feed = quickstart_createfeed.create_feed(
30+
PROJECT, FEED_ID, [ASSET_NAME, ], test_topic.name)
31+
deleter.append(feed.name)
4432
out, _ = capsys.readouterr()
4533
assert "feed" in out
46-
47-
# Clean up, delete the feed
48-
feed_name = "projects/{}/feeds/{}".format(project_number, FEED_ID)
49-
quickstart_deletefeed.delete_feed(feed_name)
50-
publisher.delete_topic(topic_path)

asset/cloud-client/quickstart_deletefeed_test.py

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,16 @@
1515
# limitations under the License.
1616

1717
import os
18-
import uuid
1918

20-
from google.cloud import pubsub_v1
21-
from google.cloud import resource_manager
22-
23-
import quickstart_createfeed
2419
import quickstart_deletefeed
2520

21+
2622
PROJECT = os.environ['GOOGLE_CLOUD_PROJECT']
27-
ASSET_NAME = 'assets-{}'.format(uuid.uuid4().hex)
28-
FEED_ID = 'feed-{}'.format(uuid.uuid4().hex)
29-
TOPIC = 'topic-{}'.format(uuid.uuid4().hex)
3023

3124

32-
def test_delete_feed(capsys):
33-
client = resource_manager.Client()
34-
project_number = client.fetch_project(PROJECT).number
35-
# First create the feed, which will be deleted later
36-
full_topic_name = "projects/{}/topics/{}".format(PROJECT, TOPIC)
37-
publisher = pubsub_v1.PublisherClient()
38-
topic_path = publisher.topic_path(PROJECT, TOPIC)
39-
publisher.create_topic(topic_path)
40-
quickstart_createfeed.create_feed(
41-
PROJECT, FEED_ID, [ASSET_NAME, ], full_topic_name)
25+
def test_delete_feed(capsys, test_feed):
4226

43-
feed_name = "projects/{}/feeds/{}".format(project_number, FEED_ID)
44-
quickstart_deletefeed.delete_feed(feed_name)
27+
quickstart_deletefeed.delete_feed(test_feed.name)
4528

4629
out, _ = capsys.readouterr()
4730
assert "deleted_feed" in out
48-
publisher.delete_topic(topic_path)

asset/cloud-client/quickstart_getfeed_test.py

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,11 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
import os
18-
import uuid
19-
20-
from google.cloud import pubsub_v1
21-
from google.cloud import resource_manager
22-
23-
import quickstart_createfeed
24-
import quickstart_deletefeed
2517
import quickstart_getfeed
2618

27-
PROJECT = os.environ['GOOGLE_CLOUD_PROJECT']
28-
ASSET_NAME = 'assets-{}'.format(uuid.uuid4().hex)
29-
FEED_ID = 'feed-{}'.format(uuid.uuid4().hex)
30-
TOPIC = 'topic-{}'.format(uuid.uuid4().hex)
31-
32-
33-
def test_get_feed(capsys):
34-
client = resource_manager.Client()
35-
project_number = client.fetch_project(PROJECT).number
36-
# First create the feed, which will be gotten later
37-
full_topic_name = "projects/{}/topics/{}".format(PROJECT, TOPIC)
38-
publisher = pubsub_v1.PublisherClient()
39-
topic_path = publisher.topic_path(PROJECT, TOPIC)
40-
publisher.create_topic(topic_path)
41-
quickstart_createfeed.create_feed(
42-
PROJECT, FEED_ID, [ASSET_NAME, ], full_topic_name)
4319

44-
feed_name = "projects/{}/feeds/{}".format(project_number, FEED_ID)
45-
quickstart_getfeed.get_feed(feed_name)
20+
def test_get_feed(capsys, test_feed):
21+
quickstart_getfeed.get_feed(test_feed.name)
4622
out, _ = capsys.readouterr()
4723

4824
assert "gotten_feed" in out
49-
# Clean up and delete the feed
50-
quickstart_deletefeed.delete_feed(feed_name)
51-
publisher.delete_topic(topic_path)

asset/cloud-client/quickstart_updatefeed_test.py

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,43 +14,11 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
import os
18-
import uuid
19-
20-
from google.cloud import pubsub_v1
21-
from google.cloud import resource_manager
22-
23-
import quickstart_createfeed
24-
import quickstart_deletefeed
2517
import quickstart_updatefeed
2618

27-
PROJECT = os.environ['GOOGLE_CLOUD_PROJECT']
28-
ASSET_NAME = 'assets-{}'.format(uuid.uuid4().hex)
29-
FEED_ID = 'feed-{}'.format(uuid.uuid4().hex)
30-
TOPIC = 'topic-{}'.format(uuid.uuid4().hex)
31-
NEW_TOPIC = 'new-topic-{}'.format(uuid.uuid4().hex)
32-
33-
34-
def test_update_feed(capsys):
35-
client = resource_manager.Client()
36-
project_number = client.fetch_project(PROJECT).number
37-
# First create the feed, which will be updated later
38-
full_topic_name = "projects/{}/topics/{}".format(PROJECT, TOPIC)
39-
publisher = pubsub_v1.PublisherClient()
40-
topic_path = publisher.topic_path(PROJECT, TOPIC)
41-
publisher.create_topic(topic_path)
42-
quickstart_createfeed.create_feed(
43-
PROJECT, FEED_ID, [ASSET_NAME, ], full_topic_name)
4419

45-
feed_name = "projects/{}/feeds/{}".format(project_number, FEED_ID)
46-
new_full_topic_name = "projects/" + PROJECT + "/topics/" + NEW_TOPIC
47-
new_topic_path = publisher.topic_path(PROJECT, NEW_TOPIC)
48-
publisher.create_topic(new_topic_path)
49-
quickstart_updatefeed.update_feed(feed_name, new_full_topic_name)
20+
def test_update_feed(capsys, test_feed, another_topic):
21+
quickstart_updatefeed.update_feed(test_feed.name, another_topic.name)
5022
out, _ = capsys.readouterr()
5123

5224
assert "updated_feed" in out
53-
# Clean up and delete the feed
54-
quickstart_deletefeed.delete_feed(feed_name)
55-
publisher.delete_topic(topic_path)
56-
publisher.delete_topic(new_topic_path)

0 commit comments

Comments
 (0)