forked from googleapis/python-pubsub
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema_test.py
More file actions
307 lines (238 loc) · 9.42 KB
/
Copy pathschema_test.py
File metadata and controls
307 lines (238 loc) · 9.42 KB
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 2021 Google LLC. 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
from typing import Any, Callable, cast, Generator, TypeVar
import uuid
from _pytest.capture import CaptureFixture
from flaky import flaky
from google.api_core.exceptions import InternalServerError
from google.api_core.exceptions import NotFound
from google.cloud import pubsub_v1
from google.cloud.pubsub import PublisherClient
from google.cloud.pubsub import SchemaServiceClient
from google.cloud.pubsub import SubscriberClient
from google.pubsub_v1.types import Encoding
import pytest
import schema
# This uuid is shared across tests which run in parallel.
UUID = uuid.uuid4().hex
try:
PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"]
except KeyError:
raise KeyError("Need to set GOOGLE_CLOUD_PROJECT as an environment variable.")
AVRO_TOPIC_ID = f"schema-test-avro-topic-{UUID}"
PROTO_TOPIC_ID = f"schema-test-proto-topic-{UUID}"
AVRO_SUBSCRIPTION_ID = f"schema-test-avro-subscription-{UUID}"
PROTO_SUBSCRIPTION_ID = f"schema-test-proto-subscription-{UUID}"
AVRO_SCHEMA_ID = f"schema-test-avro-schema-{UUID}"
PROTO_SCHEMA_ID = f"schema-test-proto-schema-{UUID}"
AVSC_FILE = "resources/us-states.avsc"
PROTO_FILE = "resources/us-states.proto"
# These tests run in parallel if pytest-parallel is installed.
# Avoid modifying resources that are shared across tests,
# as this results in test flake.
@pytest.fixture(scope="module")
def schema_client() -> Generator[pubsub_v1.SchemaServiceClient, None, None]:
schema_client = SchemaServiceClient()
yield schema_client
@pytest.fixture(scope="module")
def avro_schema(
schema_client: pubsub_v1.SchemaServiceClient,
) -> Generator[str, None, None]:
avro_schema_path = schema_client.schema_path(PROJECT_ID, AVRO_SCHEMA_ID)
yield avro_schema_path
try:
schema_client.delete_schema(request={"name": avro_schema_path})
except (NotFound, InternalServerError):
pass
@pytest.fixture(scope="module")
def proto_schema(
schema_client: pubsub_v1.SchemaServiceClient,
) -> Generator[str, None, None]:
proto_schema_path = schema_client.schema_path(PROJECT_ID, PROTO_SCHEMA_ID)
yield proto_schema_path
try:
schema_client.delete_schema(request={"name": proto_schema_path})
except (NotFound, InternalServerError):
pass
@pytest.fixture(scope="module")
def publisher_client() -> Generator[pubsub_v1.PublisherClient, None, None]:
yield PublisherClient()
@pytest.fixture(scope="module")
def avro_topic(
publisher_client: pubsub_v1.PublisherClient, avro_schema: str
) -> Generator[str, None, None]:
from google.pubsub_v1.types import Encoding
avro_topic_path = publisher_client.topic_path(PROJECT_ID, AVRO_TOPIC_ID)
try:
avro_topic = publisher_client.get_topic(request={"topic": avro_topic_path})
except NotFound:
avro_topic = publisher_client.create_topic(
request={
"name": avro_topic_path,
"schema_settings": {
"schema": avro_schema,
"encoding": Encoding.BINARY,
},
}
)
yield avro_topic.name
publisher_client.delete_topic(request={"topic": avro_topic.name})
@pytest.fixture(scope="module")
def proto_topic(
publisher_client: pubsub_v1.PublisherClient, proto_schema: str
) -> Generator[str, None, None]:
proto_topic_path = publisher_client.topic_path(PROJECT_ID, PROTO_TOPIC_ID)
try:
proto_topic = publisher_client.get_topic(request={"topic": proto_topic_path})
except NotFound:
proto_topic = publisher_client.create_topic(
request={
"name": proto_topic_path,
"schema_settings": {
"schema": proto_schema,
"encoding": Encoding.BINARY,
},
}
)
yield proto_topic.name
publisher_client.delete_topic(request={"topic": proto_topic.name})
@pytest.fixture(scope="module")
def subscriber_client() -> Generator[pubsub_v1.SubscriberClient, None, None]:
subscriber_client = SubscriberClient()
yield subscriber_client
subscriber_client.close()
@pytest.fixture(scope="module")
def avro_subscription(
subscriber_client: pubsub_v1.SubscriberClient, avro_topic: str
) -> Generator[str, None, None]:
avro_subscription_path = subscriber_client.subscription_path(
PROJECT_ID, AVRO_SUBSCRIPTION_ID
)
try:
avro_subscription = subscriber_client.get_subscription(
request={"subscription": avro_subscription_path}
)
except NotFound:
avro_subscription = subscriber_client.create_subscription(
request={"name": avro_subscription_path, "topic": avro_topic}
)
yield avro_subscription.name
subscriber_client.delete_subscription(
request={"subscription": avro_subscription.name}
)
@pytest.fixture(scope="module")
def proto_subscription(
subscriber_client: pubsub_v1.SubscriberClient, proto_topic: str
) -> Generator[str, None, None]:
proto_subscription_path = subscriber_client.subscription_path(
PROJECT_ID, PROTO_SUBSCRIPTION_ID
)
try:
proto_subscription = subscriber_client.get_subscription(
request={"subscription": proto_subscription_path}
)
except NotFound:
proto_subscription = subscriber_client.create_subscription(
request={"name": proto_subscription_path, "topic": proto_topic}
)
yield proto_subscription.name
subscriber_client.delete_subscription(
request={"subscription": proto_subscription.name}
)
def test_create_avro_schema(
schema_client: pubsub_v1.SchemaServiceClient,
avro_schema: str,
capsys: CaptureFixture[str],
) -> None:
try:
schema_client.delete_schema(request={"name": avro_schema})
except NotFound:
pass
schema.create_avro_schema(PROJECT_ID, AVRO_SCHEMA_ID, AVSC_FILE)
out, _ = capsys.readouterr()
assert "Created a schema using an Avro schema file:" in out
assert f"{avro_schema}" in out
def test_create_proto_schema(
schema_client: pubsub_v1.SchemaServiceClient,
proto_schema: str,
capsys: CaptureFixture[str],
) -> None:
try:
schema_client.delete_schema(request={"name": proto_schema})
except NotFound:
pass
schema.create_proto_schema(PROJECT_ID, PROTO_SCHEMA_ID, PROTO_FILE)
out, _ = capsys.readouterr()
assert "Created a schema using a protobuf schema file:" in out
assert f"{proto_schema}" in out
def test_get_schema(avro_schema: str, capsys: CaptureFixture[str]) -> None:
schema.get_schema(PROJECT_ID, AVRO_SCHEMA_ID)
out, _ = capsys.readouterr()
assert "Got a schema" in out
assert f"{avro_schema}" in out
def test_list_schemas(capsys: CaptureFixture[str]) -> None:
schema.list_schemas(PROJECT_ID)
out, _ = capsys.readouterr()
assert "Listed schemas." in out
def test_create_topic_with_schema(
avro_schema: str, capsys: CaptureFixture[str]
) -> None:
schema.create_topic_with_schema(PROJECT_ID, AVRO_TOPIC_ID, AVRO_SCHEMA_ID, "BINARY")
out, _ = capsys.readouterr()
assert "Created a topic" in out
assert f"{AVRO_TOPIC_ID}" in out
assert f"{avro_schema}" in out
assert "BINARY" in out or "2" in out
def test_publish_avro_records(
avro_schema: str, avro_topic: str, capsys: CaptureFixture[str]
) -> None:
schema.publish_avro_records(PROJECT_ID, AVRO_TOPIC_ID, AVSC_FILE)
out, _ = capsys.readouterr()
assert "Preparing a binary-encoded message" in out
assert "Published message ID" in out
def test_subscribe_with_avro_schema(
avro_schema: str,
avro_topic: str,
avro_subscription: str,
capsys: CaptureFixture[str],
) -> None:
schema.publish_avro_records(PROJECT_ID, AVRO_TOPIC_ID, AVSC_FILE)
schema.subscribe_with_avro_schema(PROJECT_ID, AVRO_SUBSCRIPTION_ID, AVSC_FILE, 9)
out, _ = capsys.readouterr()
assert "Received a binary-encoded message:" in out
def test_publish_proto_records(proto_topic: str, capsys: CaptureFixture[str]) -> None:
schema.publish_proto_messages(PROJECT_ID, PROTO_TOPIC_ID)
out, _ = capsys.readouterr()
assert "Preparing a binary-encoded message" in out
assert "Published message ID" in out
def test_subscribe_with_proto_schema(
proto_schema: str,
proto_topic: str,
proto_subscription: str,
capsys: CaptureFixture[str],
) -> None:
schema.publish_proto_messages(PROJECT_ID, PROTO_TOPIC_ID)
schema.subscribe_with_proto_schema(PROJECT_ID, PROTO_SUBSCRIPTION_ID, 9)
out, _ = capsys.readouterr()
assert "Received a binary-encoded message" in out
C = TypeVar("C", bound=Callable[..., Any])
typed_flaky = cast(Callable[[C], C], flaky(max_runs=3, min_passes=1))
@typed_flaky
def test_delete_schema(proto_schema: str, capsys: CaptureFixture[str]) -> None:
schema.delete_schema(PROJECT_ID, PROTO_SCHEMA_ID)
out, _ = capsys.readouterr()
assert "Deleted a schema" in out
assert f"{proto_schema}" in out