forked from urllib3/urllib3
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
noxfile.py
511 lines (412 loc) · 14.7 KB
/
noxfile.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
from __future__ import annotations
import contextlib
import os
import platform
import shutil
import subprocess
import time
import typing
from http.client import RemoteDisconnected
from socket import timeout as SocketTimeout
from urllib.error import HTTPError, URLError
from urllib.request import Request, urlopen
import nox
@contextlib.contextmanager
def traefik_boot(
session: nox.Session, *args: str
) -> typing.Generator[None, None, None]:
"""
Start a server to reliably test HTTP/1.1, HTTP/2 and HTTP/3 over QUIC.
"""
# we may want to avoid starting the traefik server...
if os.environ.get("TRAEFIK_HTTPBIN_ENABLE", "true") != "true":
yield
return
# nox allows us to specify pos args
# if we detect any of them, we should check
# if the target tests requires Traefik or not.
detect_specific_traefik: bool | None = None
for arg in args:
if arg.startswith("test/"):
detect_specific_traefik = False
if arg.startswith("test/with_traefik") or arg == "test/":
detect_specific_traefik = True
break
if detect_specific_traefik is False:
yield
return
external_stack_started = False
is_windows = platform.system() == "Windows"
dc_v1_legacy = is_windows is False and shutil.which("docker-compose") is not None
traefik_ipv4 = os.environ.get("TRAEFIK_HTTPBIN_IPV4", "127.0.0.1")
if dc_v1_legacy:
dc_v2_probe = subprocess.Popen(["docker", "compose", "ps"])
dc_v2_probe.wait()
dc_v1_legacy = dc_v2_probe.returncode != 0
if not os.path.exists("./traefik/httpbin.local.pem"):
session.log("Prepare fake certificates for our Traefik server...")
addon_proc = subprocess.Popen(
[
"python",
"-m",
"pip",
"install",
"cffi==1.17.0rc1; python_version > '3.12'",
"trustme",
]
)
addon_proc.wait()
if addon_proc.returncode != 0:
yield
session.warn("Unable to install trustme outside of the nox Session")
return
trustme_proc = subprocess.Popen(
[
"python",
"-m",
"trustme",
"-i",
"httpbin.local",
"alt.httpbin.local",
"-d",
"./traefik",
]
)
trustme_proc.wait()
if trustme_proc.returncode != 0:
session.warn("Unable to issue required certificates for our Traefik stack")
yield
return
shutil.move("./traefik/server.pem", "./traefik/httpbin.local.pem")
if os.path.exists("./traefik/httpbin.local.key"):
os.unlink("./traefik/httpbin.local.key")
shutil.move("./traefik/server.key", "./traefik/httpbin.local.key")
if os.path.exists("./rootCA.pem"):
os.unlink("./rootCA.pem")
shutil.move("./traefik/client.pem", "./rootCA.pem")
try:
session.log("Attempt to start Traefik with go-httpbin[...]")
if is_windows:
if not os.path.exists("./go-httpbin"):
clone_proc = subprocess.Popen(
["git", "clone", "https://github.com/mccutchen/go-httpbin.git"]
)
clone_proc.wait()
shutil.copyfile(
"./traefik/patched.Dockerfile", "./go-httpbin/patched.Dockerfile"
)
pre_build = subprocess.Popen(
[
"docker",
"compose",
"-f",
"docker-compose.win.yaml",
"build",
"httpbin",
]
)
pre_build.wait()
if pre_build.returncode == 0:
dc_process = subprocess.Popen(
[
"docker",
"compose",
"-f",
"docker-compose.win.yaml",
"up",
"-d",
]
)
else:
raise OSError("Unable to build go-httpbin on Windows")
else:
if dc_v1_legacy:
dc_process = subprocess.Popen(["docker-compose", "up", "-d"])
else:
dc_process = subprocess.Popen(["docker", "compose", "up", "-d"])
dc_process.wait()
except OSError as e:
session.warn(
f"Traefik server cannot be run due to an error with containers: {e}"
)
else:
session.log("Traefik server is starting[...]")
i = 0
while True:
if i >= 120:
if not dc_v1_legacy:
subprocess.Popen(
[
"docker",
"compose",
"-f",
"docker-compose.win.yaml",
"logs",
"--tail=128",
]
)
raise TimeoutError(
"Error while waiting for the Traefik server (timeout/readiness)"
)
try:
r = urlopen(
Request(
f"http://{traefik_ipv4}:8888/get",
headers={"Host": "httpbin.local"},
),
timeout=1.0,
)
except (
HTTPError,
URLError,
RemoteDisconnected,
TimeoutError,
SocketTimeout,
) as e:
i += 1
time.sleep(1)
session.log(f"Waiting for the Traefik server: {e}...")
continue
if int(r.status) == 200:
break
session.log("Traefik server is ready to accept connections[...]")
external_stack_started = True
yield
if external_stack_started:
if dc_v1_legacy:
dc_process = subprocess.Popen(["docker-compose", "stop"])
else:
dc_process = subprocess.Popen(["docker", "compose", "stop"])
dc_process.wait()
def tests_impl(
session: nox.Session,
extras: str = "socks,brotli,zstd,ws",
byte_string_comparisons: bool = False,
tracemalloc_enable: bool = False,
) -> None:
with traefik_boot(session, *session.posargs):
# Install deps and the package itself.
session.install("-U", "pip", "setuptools", silent=False)
session.install("-r", "dev-requirements.txt", silent=False)
session.install(f".[{extras}]", silent=False)
# Show the pip version.
session.run("pip", "--version")
# Print the Python version and bytesize.
session.run("python", "--version")
session.run("python", "-c", "import struct; print(struct.calcsize('P') * 8)")
session.run("python", "-c", "import ssl; print(ssl.OPENSSL_VERSION)")
# Inspired from https://hynek.me/articles/ditch-codecov-python/
# We use parallel mode and then combine in a later CI step
session.run(
"python",
*(("-bb",) if byte_string_comparisons else ()),
"-m",
"coverage",
"run",
"--parallel-mode",
"-m",
"pytest",
"-v",
"-ra",
f"--color={'yes' if 'GITHUB_ACTIONS' in os.environ else 'auto'}",
"--tb=native",
"--durations=10",
"--strict-config",
"--strict-markers",
*(session.posargs or ("test/",)),
env={
"PYTHONWARNINGS": "always::DeprecationWarning",
"COVERAGE_CORE": "sysmon",
"PYTHONTRACEMALLOC": "25" if tracemalloc_enable else "",
},
)
@nox.session(
python=["3.7", "3.8", "3.9", "3.10", "3.11", "3.12", "3.13", "3.14", "pypy"]
)
def test(session: nox.Session) -> None:
tests_impl(session)
@nox.session(python=["3.7", "3.8", "3.9", "3.10", "3.11", "3.12", "3.13", "3.14"])
def tracemalloc(session: nox.Session) -> None:
tests_impl(session, tracemalloc_enable=True)
@nox.session(python=["3"])
def test_brotlipy(session: nox.Session) -> None:
"""Check that if 'brotlipy' is installed instead of 'brotli' or
'brotlicffi' that we still don't blow up.
"""
session.install("brotlipy")
tests_impl(session, extras="socks", byte_string_comparisons=False)
def git_clone(session: nox.Session, git_url: str) -> None:
"""We either clone the target repository or if already exist
simply reset the state and pull.
"""
expected_directory = git_url.split("/")[-1]
if expected_directory.endswith(".git"):
expected_directory = expected_directory[:-4]
if not os.path.isdir(expected_directory):
session.run("git", "clone", "--depth", "1", git_url, external=True)
else:
session.run(
"git", "-C", expected_directory, "reset", "--hard", "HEAD", external=True
)
session.run("git", "-C", expected_directory, "pull", external=True)
@nox.session()
def downstream_botocore(session: nox.Session) -> None:
root = os.getcwd()
tmp_dir = session.create_tmp()
session.cd(tmp_dir)
git_clone(session, "https://github.com/boto/botocore")
session.chdir("botocore")
for patch in [
"0001-Mark-100-Continue-tests-as-failing.patch",
"0003-Mark-HttpConn-bypass-internals-as-xfail.patch",
]:
session.run("git", "apply", f"{root}/ci/{patch}", external=True)
session.run("git", "rev-parse", "HEAD", external=True)
session.run("python", "scripts/ci/install")
session.cd(root)
session.install("setuptools<71")
session.install(".", silent=False)
session.cd(f"{tmp_dir}/botocore")
session.run("python", "-c", "import urllib3; print(urllib3.__version__)")
session.run("python", "scripts/ci/run-tests")
@nox.session()
def downstream_niquests(session: nox.Session) -> None:
root = os.getcwd()
tmp_dir = session.create_tmp()
session.cd(tmp_dir)
git_clone(session, "https://github.com/jawah/niquests")
session.chdir("niquests")
session.run("git", "rev-parse", "HEAD", external=True)
session.install(".[socks]", silent=False)
session.install("-r", "requirements-dev.txt", silent=False)
session.cd(root)
session.install(".", silent=False)
session.cd(f"{tmp_dir}/niquests")
session.run("python", "-c", "import urllib3; print(urllib3.__version__)")
session.run(
"python",
"-m",
"pytest",
"-v",
f"--color={'yes' if 'GITHUB_ACTIONS' in os.environ else 'auto'}",
*(session.posargs or ("tests/",)),
)
@nox.session()
def downstream_requests(session: nox.Session) -> None:
root = os.getcwd()
tmp_dir = session.create_tmp()
session.cd(tmp_dir)
git_clone(session, "https://github.com/psf/requests")
session.chdir("requests")
for patch in [
"0004-Requests-ChunkedEncodingError.patch",
]:
session.run("git", "apply", f"{root}/ci/{patch}", external=True)
session.run("git", "rev-parse", "HEAD", external=True)
session.install(".[socks]", silent=False)
session.install("-r", "requirements-dev.txt", silent=False)
session.cd(root)
session.install(".", silent=False)
session.cd(f"{tmp_dir}/requests")
session.run("python", "-c", "import urllib3; print(urllib3.__version__)")
session.run(
"python",
"-m",
"pytest",
"-v",
f"--color={'yes' if 'GITHUB_ACTIONS' in os.environ else 'auto'}",
*(session.posargs or ("tests/",)),
)
@nox.session()
def downstream_boto3(session: nox.Session) -> None:
root = os.getcwd()
tmp_dir = session.create_tmp()
session.cd(tmp_dir)
git_clone(session, "https://github.com/boto/boto3")
session.chdir("boto3")
session.run("git", "rev-parse", "HEAD", external=True)
session.install(".", silent=False)
session.install("-r", "requirements-dev.txt", silent=False)
session.cd(root)
session.install(".", silent=False)
session.cd(f"{tmp_dir}/boto3")
session.run("python", "-c", "import urllib3; print(urllib3.__version__)")
session.run(
"python",
"scripts/ci/run-tests",
)
@nox.session()
def downstream_sphinx(session: nox.Session) -> None:
root = os.getcwd()
tmp_dir = session.create_tmp()
session.cd(tmp_dir)
git_clone(session, "https://github.com/sphinx-doc/sphinx")
session.chdir("sphinx")
session.run("git", "rev-parse", "HEAD", external=True)
session.install(".[test]", silent=False)
session.cd(root)
session.install(".", silent=False)
session.cd(f"{tmp_dir}/sphinx")
session.run("python", "-c", "import urllib3; print(urllib3.__version__)")
session.run(
"python",
"-m",
"pytest",
"-v",
f"--color={'yes' if 'GITHUB_ACTIONS' in os.environ else 'auto'}",
*(session.posargs or ("tests/",)),
)
@nox.session()
def downstream_docker(session: nox.Session) -> None:
root = os.getcwd()
tmp_dir = session.create_tmp()
session.cd(tmp_dir)
git_clone(session, "https://github.com/docker/docker-py")
session.chdir("docker-py")
for patch in [
"0005-DockerPy-FixBadChunk.patch",
]:
session.run("git", "apply", f"{root}/ci/{patch}", external=True)
session.run("git", "rev-parse", "HEAD", external=True)
session.install(".[ssh,dev]", silent=False)
session.cd(root)
session.install(".", silent=False)
session.cd(f"{tmp_dir}/docker-py")
session.run("python", "-c", "import urllib3; print(urllib3.__version__)")
session.run(
"python",
"-m",
"pytest",
"-v",
f"--color={'yes' if 'GITHUB_ACTIONS' in os.environ else 'auto'}",
*(session.posargs or ("tests/unit",)),
)
@nox.session()
def format(session: nox.Session) -> None:
"""Run code formatters."""
lint(session)
@nox.session
def lint(session: nox.Session) -> None:
session.install("pre-commit")
session.run("pre-commit", "run", "--all-files")
mypy(session)
@nox.session
def mypy(session: nox.Session) -> None:
"""Run mypy."""
session.install("-r", "mypy-requirements.txt")
session.run("mypy", "--version")
session.run(
"mypy",
"dummyserver",
"noxfile.py",
"src/urllib3",
"test",
)
@nox.session
def docs(session: nox.Session) -> None:
session.install("-r", "docs/requirements.txt")
session.install(".[socks,brotli,zstd,ws]")
session.chdir("docs")
if os.path.exists("_build"):
shutil.rmtree("_build")
session.run("sphinx-build", "-b", "html", "-W", ".", "_build/html")