forked from Aiven-Open/karapace
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
286 lines (237 loc) · 8.12 KB
/
Copy pathutils.py
File metadata and controls
286 lines (237 loc) · 8.12 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
"""
Copyright (c) 2023 Aiven Ltd
See LICENSE for details
"""
from aiohttp.client_exceptions import ClientOSError, ServerDisconnectedError
from kafka.errors import TopicAlreadyExistsError
from karapace.client import Client
from karapace.protobuf.kotlin_wrapper import trim_margin
from karapace.utils import Expiration
from pathlib import Path
from subprocess import Popen
from typing import Callable, IO, List, Union
from urllib.parse import quote
import asyncio
import copy
import json
import os
import ssl
import sys
import uuid
consumer_valid_payload = {
"format": "avro",
"auto.offset.reset": "earliest",
"consumer.request.timeout.ms": 11000,
"fetch.min.bytes": 100000,
"auto.commit.enable": "true",
}
schema_jsonschema_json = json.dumps(
{
"type": "object",
"properties": {
"foo": {"type": "integer"},
},
}
)
schema_avro_json = json.dumps(
{
"namespace": "example.avro",
"type": "record",
"name": "example.avro.User",
"fields": [
{"name": "name", "type": "string"},
{"name": "favorite_number", "type": "int"},
{"name": "favorite_color", "type": "string"},
],
}
)
test_objects_jsonschema = [{"foo": 100}, {"foo": 200}]
test_objects_avro = [
{"name": "First Foo", "favorite_number": 2, "favorite_color": "bar"},
{"name": "Second Foo", "favorite_number": 3, "favorite_color": "baz"},
{"name": "Third Foo", "favorite_number": 5, "favorite_color": "quux"},
]
# protobuf schemas in tests must be filtered by trim_margin() from kotlin_wrapper module
schema_protobuf = """
|syntax = "proto3";
|
|option java_package = "com.codingharbour.protobuf";
|option java_outer_classname = "TestEnumOrder";
|
|message Message {
| int32 query = 1;
| Enum speed = 2;
|}
|enum Enum {
| HIGH = 0;
| MIDDLE = 1;
| LOW = 2;
|}
|
"""
schema_protobuf = trim_margin(schema_protobuf)
test_objects_protobuf = [
{"query": 5, "speed": "HIGH"},
{"query": 10, "speed": "MIDDLE"},
]
test_fail_objects_protobuf = [
{"query": "STR", "speed": 99},
{"xx": 10, "bb": "MIDDLE"},
]
schema_data = {
"avro": (schema_avro_json, test_objects_avro),
"jsonschema": (schema_jsonschema_json, test_objects_jsonschema),
"protobuf": (schema_protobuf, test_objects_protobuf),
}
schema_protobuf_second = """
|syntax = "proto3";
|
|option java_package = "com.codingharbour.protobuf";
|option java_outer_classname = "TestEnumOrder";
|
|message SensorInfo {
| int32 q = 1;
| Enu sensor_type = 2;
| repeated int32 nums = 3;
| Order order = 4;
| message Order {
| string item = 1;
| }
|}
|enum Enu {
| H1 = 0;
| M1 = 1;
| L1 = 2;
|}
|
"""
schema_protobuf_second = trim_margin(schema_protobuf_second)
test_objects_protobuf_second = [
{"q": 1, "sensor_type": "H1", "nums": [3, 4], "order": {"item": "ABC01223"}},
{"q": 2, "sensor_type": "M1", "nums": [2], "order": {"item": "ABC01233"}},
{"q": 3, "sensor_type": "L1", "nums": [3, 4], "order": {"item": "ABC01223"}},
]
schema_data_second = {"protobuf": (schema_protobuf_second, test_objects_protobuf_second)}
second_schema_json = json.dumps(
{"namespace": "example.avro.other", "type": "record", "name": "Dude", "fields": [{"name": "name", "type": "string"}]}
)
second_obj = [{"name": "doe"}, {"name": "john"}]
REST_HEADERS = {
"json": {
"Content-Type": "application/vnd.kafka.json.v2+json",
"Accept": "application/vnd.kafka.json.v2+json, application/vnd.kafka.v2+json, application/json, */*",
},
"jsonschema": {
"Content-Type": "application/vnd.kafka.jsonschema.v2+json",
"Accept": "application/vnd.kafka.jsonschema.v2+json, application/vnd.kafka.v2+json, application/json, */*",
},
"binary": {
"Content-Type": "application/vnd.kafka.binary.v2+json",
"Accept": "application/vnd.kafka.binary.v2+json, application/vnd.kafka.v2+json, application/json, */*",
},
"avro": {
"Content-Type": "application/vnd.kafka.avro.v2+json",
"Accept": "application/vnd.kafka.avro.v2+json, application/vnd.kafka.v2+json, application/json, */*",
},
"protobuf": {
"Content-Type": "application/vnd.kafka.protobuf.v2+json",
"Accept": "application/vnd.kafka.protobuf.v2+json, application/vnd.kafka.v2+json, application/json, */*",
},
}
async def new_consumer(c, group, fmt="avro", trail=""):
payload = copy.copy(consumer_valid_payload)
payload["format"] = fmt
resp = await c.post(f"/consumers/{group}{trail}", json=payload, headers=REST_HEADERS[fmt])
assert resp.ok
return resp.json()["instance_id"]
def new_random_name(prefix: str) -> str:
# A hyphen is not a valid character for Avro identifiers. Use only the
# first 8 characters of the UUID.
suffix = str(uuid.uuid4())[:8]
return f"{prefix}{suffix}"
def create_subject_name_factory(prefix: str) -> Callable[[], str]:
return create_id_factory(f"subject_{prefix}")
def create_field_name_factory(prefix: str) -> Callable[[], str]:
return create_id_factory(f"field_{prefix}")
def create_schema_name_factory(prefix: str) -> Callable[[], str]:
return create_id_factory(f"schema_{prefix}")
def create_group_name_factory(prefix: str) -> Callable[[], str]:
return create_id_factory(f"group_{prefix}")
def create_id_factory(prefix: str) -> Callable[[], str]:
"""
Creates unique ids prefixed with prefix..
The resulting ids are safe to embed in URLs.
"""
index = 1
def create_name() -> str:
nonlocal index
return new_random_name(f"{quote(prefix).replace('/', '_')}_{index}_")
return create_name
def new_topic(admin_client, prefix="topic"):
tn = f"{new_random_name(prefix)}"
try:
admin_client.new_topic(tn)
except TopicAlreadyExistsError:
pass
return tn
async def wait_for_topics(rest_async_client: Client, topic_names: List[str], timeout: float, sleep: float) -> None:
for topic in topic_names:
expiration = Expiration.from_timeout(timeout=timeout)
topic_found = False
current_topics = None
while not topic_found:
await asyncio.sleep(sleep)
expiration.raise_timeout_if_expired(
msg_format="New topic {topic} must be in the result of /topics. Result={current_topics}",
topic=topic,
current_topics=current_topics,
)
res = await rest_async_client.get("/topics")
assert res.ok, f"Status code is not 200: {res.status_code}"
current_topics = res.json()
topic_found = topic in current_topics
async def repeat_until_successful_request(
callback,
path: str,
json_data,
headers,
error_msg: str,
timeout: float,
sleep: float,
):
expiration = Expiration.from_timeout(timeout=timeout)
ok = False
res = None
while not ok:
expiration.raise_timeout_if_expired(
msg_format=f"{error_msg} {res} after {timeout} secs",
error_msg=error_msg,
res=res,
timeout=timeout,
)
try:
res = await callback(path, json=json_data, headers=headers)
# SSLCertVerificationError: likely a configuration error, nothing to do
except ssl.SSLCertVerificationError:
raise
# ClientOSError: Raised when the listening socket is not yet open in the server
# ServerDisconnectedError: Wrong url
except (ClientOSError, ServerDisconnectedError):
await asyncio.sleep(sleep)
else:
ok = res.ok
return res
def write_ini(file_path: Path, ini_data: dict) -> None:
ini_contents = (f"{key}={value}" for key, value in ini_data.items())
file_contents = "\n".join(ini_contents)
with file_path.open("w") as fp:
fp.write(file_contents)
def python_exe() -> str:
python = sys.executable
if python is None:
python = os.environ.get("PYTHON", "python3")
return python
def popen_karapace_all(config_path: Union[Path, str], stdout: IO, stderr: IO, **kwargs) -> Popen:
kwargs["stdout"] = stdout
kwargs["stderr"] = stderr
return Popen([python_exe(), "-m", "karapace.karapace_all", str(config_path)], **kwargs)