-
Notifications
You must be signed in to change notification settings - Fork 12
/
quickstart_test.py
307 lines (237 loc) · 9.89 KB
/
quickstart_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/usr/bin/env python
# Copyright 2020 Google Inc. All Rights Reserved.
#
# 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 random
import uuid
import backoff
from google.api_core.exceptions import NotFound
from google.cloud.pubsublite import AdminClient
from google.cloud.pubsublite.types import (
CloudRegion,
CloudZone,
SubscriptionPath,
TopicPath,
)
import pytest
PROJECT_NUMBER = os.environ["GOOGLE_CLOUD_PROJECT_NUMBER"]
CLOUD_REGION = "us-central1"
ZONE_ID = ["a", "b", "c"][random.randint(0, 2)]
UUID = uuid.uuid4().hex
TOPIC_ID = "py-lite-topic-" + UUID
SUBSCRIPTION_ID = "py-lite-subscription-" + UUID
NUM_PARTITIONS = 1
NUM_MESSAGES = 10
# Allow 90s for tests to finish.
MAX_TIME = 90
@pytest.fixture(scope="module")
def client():
yield AdminClient(CLOUD_REGION)
@pytest.fixture(scope="module")
def topic_path(client):
location = CloudZone(CloudRegion(CLOUD_REGION), ZONE_ID)
topic_path = str(TopicPath(PROJECT_NUMBER, location, TOPIC_ID))
yield topic_path
try:
client.delete_topic(topic_path)
except NotFound:
pass
@pytest.fixture(scope="module")
def subscription_path(client):
location = CloudZone(CloudRegion(CLOUD_REGION), ZONE_ID)
subscription_path = str(SubscriptionPath(PROJECT_NUMBER, location, SUBSCRIPTION_ID))
yield subscription_path
try:
client.delete_subscription(subscription_path)
except NotFound:
pass
def test_create_lite_topic_example(topic_path, capsys):
import create_lite_topic_example
topic_path_object = TopicPath.parse(topic_path)
create_lite_topic_example.create_lite_topic(
topic_path_object.project,
topic_path_object.location.region.name,
topic_path_object.location.zone_id,
topic_path_object.name,
NUM_PARTITIONS,
)
out, _ = capsys.readouterr()
assert f"{topic_path} created successfully." in out
def test_update_lite_topic_example(topic_path, capsys):
import update_lite_topic_example
topic_path_object = TopicPath.parse(topic_path)
update_lite_topic_example.update_lite_topic(
topic_path_object.project,
topic_path_object.location.region.name,
topic_path_object.location.zone_id,
topic_path_object.name,
)
out, _ = capsys.readouterr()
assert f"{topic_path} updated successfully." in out
def test_get_lite_topic_example(topic_path, capsys):
import get_lite_topic_example
topic_path_object = TopicPath.parse(topic_path)
get_lite_topic_example.get_lite_topic(
topic_path_object.project,
topic_path_object.location.region.name,
topic_path_object.location.zone_id,
topic_path_object.name,
)
out, _ = capsys.readouterr()
assert f"{topic_path} has {NUM_PARTITIONS} partition(s)." in out
def test_list_lite_topics_example(topic_path, capsys):
import list_lite_topics_example
topic_path_object = TopicPath.parse(topic_path)
list_lite_topics_example.list_lite_topics(
topic_path_object.project,
topic_path_object.location.region.name,
topic_path_object.location.zone_id,
)
out, _ = capsys.readouterr()
assert "topic(s) listed in your project and location." in out
def test_create_lite_subscription(subscription_path, topic_path, capsys):
import create_lite_subscription_example
subscription_path_object = SubscriptionPath.parse(subscription_path)
topic_path_object = TopicPath.parse(topic_path)
create_lite_subscription_example.create_lite_subscription(
subscription_path_object.project,
subscription_path_object.location.region.name,
subscription_path_object.location.zone_id,
topic_path_object.name,
subscription_path_object.name,
)
out, _ = capsys.readouterr()
assert f"{subscription_path} created successfully." in out
def test_update_lite_subscription_example(subscription_path, capsys):
import update_lite_subscription_example
subscription_path_object = SubscriptionPath.parse(subscription_path)
update_lite_subscription_example.update_lite_subscription(
subscription_path_object.project,
subscription_path_object.location.region.name,
subscription_path_object.location.zone_id,
subscription_path_object.name,
)
out, _ = capsys.readouterr()
assert f"{subscription_path} updated successfully." in out
def test_get_lite_subscription(subscription_path, capsys):
import get_lite_subscription_example
subscription_path_object = SubscriptionPath.parse(subscription_path)
get_lite_subscription_example.get_lite_subscription(
subscription_path_object.project,
subscription_path_object.location.region.name,
subscription_path_object.location.zone_id,
subscription_path_object.name,
)
out, _ = capsys.readouterr()
assert f"{subscription_path} exists." in out
def test_list_lite_subscriptions_in_project(subscription_path, capsys):
import list_lite_subscriptions_in_project_example
subscription_path_object = SubscriptionPath.parse(subscription_path)
list_lite_subscriptions_in_project_example.list_lite_subscriptions_in_project(
subscription_path_object.project,
subscription_path_object.location.region.name,
subscription_path_object.location.zone_id,
)
out, _ = capsys.readouterr()
assert "subscription(s) listed in your project and location." in out
def test_list_lite_subscriptions_in_topic(topic_path, subscription_path, capsys):
import list_lite_subscriptions_in_topic_example
subscription_path_object = SubscriptionPath.parse(subscription_path)
topic_path_object = TopicPath.parse(topic_path)
list_lite_subscriptions_in_topic_example.list_lite_subscriptions_in_topic(
subscription_path_object.project,
subscription_path_object.location.region.name,
subscription_path_object.location.zone_id,
topic_path_object.name,
)
out, _ = capsys.readouterr()
assert "subscription(s) listed in your topic." in out
def test_publisher_example(topic_path, capsys):
import publisher_example
publisher_example.publish_messages(PROJECT_NUMBER, CLOUD_REGION, ZONE_ID, TOPIC_ID)
out, _ = capsys.readouterr()
assert "Published a message" in out
def test_publish_with_custom_attributes_example(topic_path, capsys):
import publish_with_custom_attributes_example
publish_with_custom_attributes_example.publish_with_custom_attributes(
PROJECT_NUMBER, CLOUD_REGION, ZONE_ID, TOPIC_ID
)
out, _ = capsys.readouterr()
assert (
f"Finished publishing a message with custom attributes to {topic_path}." in out
)
def test_publish_with_odering_key_example(topic_path, capsys):
import publish_with_ordering_key_example
publish_with_ordering_key_example.publish_with_odering_key(
PROJECT_NUMBER, CLOUD_REGION, ZONE_ID, TOPIC_ID, NUM_MESSAGES
)
out, _ = capsys.readouterr()
assert (
f"Finished publishing {NUM_MESSAGES} messages with an ordering key to {topic_path}."
in out
)
def test_publish_with_batch_settings_example(topic_path, capsys):
import publish_with_batch_settings_example
publish_with_batch_settings_example.publish_with_batch_settings(
PROJECT_NUMBER, CLOUD_REGION, ZONE_ID, TOPIC_ID, NUM_MESSAGES
)
out, _ = capsys.readouterr()
assert (
f"Finished publishing {NUM_MESSAGES} messages with batch settings to {topic_path}."
in out
)
def test_subscriber_example(topic_path, subscription_path, capsys):
import subscriber_example
subscriber_example.receive_messages(
PROJECT_NUMBER, CLOUD_REGION, ZONE_ID, SUBSCRIPTION_ID, 45
)
out, _ = capsys.readouterr()
assert f"Listening for messages on {subscription_path}..." in out
for message in range(NUM_MESSAGES):
assert f"Received {message}" in out
def test_seek_lite_subscription_example(capsys):
import seek_lite_subscription_example
seek_lite_subscription_example.seek_lite_subscription(
PROJECT_NUMBER, CLOUD_REGION, ZONE_ID, SUBSCRIPTION_ID, "BEGINNING", False
)
out, _ = capsys.readouterr()
assert "Seek operation" in out
def test_delete_lite_subscription_example(subscription_path, capsys):
import delete_lite_subscription_example
subscription_path_object = SubscriptionPath.parse(subscription_path)
@backoff.on_exception(backoff.expo, AssertionError, max_time=MAX_TIME)
def eventually_consistent_test():
delete_lite_subscription_example.delete_lite_subscription(
subscription_path_object.project,
subscription_path_object.location.region.name,
subscription_path_object.location.zone_id,
subscription_path_object.name,
)
out, _ = capsys.readouterr()
assert f"{subscription_path} deleted successfully." in out
eventually_consistent_test()
def test_delete_lite_topic_example(topic_path, capsys):
import delete_lite_topic_example
topic_path_object = TopicPath.parse(topic_path)
@backoff.on_exception(backoff.expo, AssertionError, max_time=MAX_TIME)
def eventually_consistent_test():
delete_lite_topic_example.delete_lite_topic(
topic_path_object.project,
topic_path_object.location.region.name,
topic_path_object.location.zone_id,
topic_path_object.name,
)
out, _ = capsys.readouterr()
assert f"{topic_path} deleted successfully." in out
eventually_consistent_test()