-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathtest_run_release.py
More file actions
455 lines (366 loc) · 14.2 KB
/
Copy pathtest_run_release.py
File metadata and controls
455 lines (366 loc) · 14.2 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
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
import builtins
import contextlib
import io
import tarfile
from contextlib import nullcontext as does_not_raise
from pathlib import Path
from typing import cast
import pytest
import run_release
from release import ReleaseShelf, Tag
from run_release import ReleaseException
@pytest.mark.parametrize(
"version",
["sigstore 3.6.2", "sigstore 3.6.6"],
)
def test_check_sigstore_version_success(version) -> None:
# Verify runs with no exceptions
run_release.check_sigstore_version(version)
@pytest.mark.parametrize(
"version",
["sigstore 3.4.0", "sigstore 3.6.0", "sigstore 4.0.0", ""],
)
def test_check_sigstore_version_exception(version) -> None:
with pytest.raises(
ReleaseException, match="Sigstore version not detected or not valid"
):
run_release.check_sigstore_version(version)
@pytest.mark.parametrize(
["url", "expected"],
[
("github.com/hugovk/cpython.git", "hugovk"),
("git@github.com:hugovk/cpython.git", "hugovk"),
("https://github.com/hugovk/cpython.git", "hugovk"),
],
)
def test_extract_github_owner(url: str, expected: str) -> None:
assert run_release.extract_github_owner(url) == expected
def test_invalid_extract_github_owner() -> None:
with pytest.raises(
ReleaseException,
match="Could not parse GitHub owner from 'origin' remote URL: "
"https://example.com",
):
run_release.extract_github_owner("https://example.com")
def test_join_remote_command_rejects_string_command() -> None:
assert (
run_release.join_remote_command(("echo", "hello world")) == "echo 'hello world'"
)
with pytest.raises(TypeError, match="remote command must be a list or tuple"):
run_release.join_remote_command("echo hello")
@pytest.mark.parametrize(
["release_tag", "git_current_branch", "expectation"],
[
# Success cases
("3.15.0rc1", "3.15\n", does_not_raise()),
("3.15.0b3", "3.15\n", does_not_raise()),
("3.15.0b2", "3.15\n", does_not_raise()),
("3.15.0b1", "main\n", does_not_raise()),
("3.15.0a6", "main\n", does_not_raise()),
("3.14.3", "3.14\n", does_not_raise()),
("3.13.12", "3.13\n", does_not_raise()),
# Failure cases
(
"3.15.0rc1",
"main\n",
pytest.raises(ReleaseException, match="on main branch, expected 3.15"),
),
(
"3.15.0b1",
"3.15\n",
pytest.raises(ReleaseException, match="on 3.15 branch, expected main"),
),
(
"3.15.0a6",
"3.14\n",
pytest.raises(ReleaseException, match="on 3.14 branch, expected main"),
),
(
"3.14.3",
"main\n",
pytest.raises(ReleaseException, match="on main branch, expected 3.14"),
),
],
)
def test_check_cpython_repo_branch(
monkeypatch, release_tag: str, git_current_branch: str, expectation
) -> None:
# Arrange
db = {"release": Tag(release_tag), "git_repo": "/fake/repo"}
monkeypatch.setattr(
run_release.subprocess,
"check_output",
lambda *args, **kwargs: git_current_branch,
)
# Act / Assert
with expectation:
run_release.check_cpython_repo_branch(cast(ReleaseShelf, db))
@pytest.mark.parametrize(
["age_seconds", "user_continues", "expectation"],
[
# Recent repo (< 1 day) - no question asked
(3600, None, does_not_raise()),
# Old repo (> 1 day) + user says yes
(90000, True, does_not_raise()),
# Old repo (> 1 day) + user says no
(90000, False, pytest.raises(ReleaseException, match="repository is old")),
],
)
def test_check_cpython_repo_age(
monkeypatch, age_seconds: int, user_continues: bool | None, expectation
) -> None:
# Arrange
db = {"release": Tag("3.15.0a6"), "git_repo": "/fake/repo"}
current_time = 1700000000
commit_timestamp = current_time - age_seconds
def fake_check_output(cmd, **kwargs):
cmd_str = " ".join(cmd)
if "%ct" in cmd_str:
return f"{commit_timestamp}\n"
if "%cr" in cmd_str:
return "some time ago\n"
return ""
monkeypatch.setattr(run_release.subprocess, "check_output", fake_check_output)
monkeypatch.setattr(run_release.time, "time", lambda: current_time)
if user_continues is not None:
monkeypatch.setattr(run_release, "ask_question", lambda _: user_continues)
# Act / Assert
with expectation:
run_release.check_cpython_repo_age(cast(ReleaseShelf, db))
def test_check_magic_number() -> None:
db = {
"release": Tag("3.14.0rc1"),
"git_repo": str(Path(__file__).parent / "magicdata"),
}
with pytest.raises(ReleaseException, match="Magic numbers in .* don't match"):
run_release.check_magic_number(cast(ReleaseShelf, db))
def prepare_fake_docs(tmp_path: Path, content: str) -> None:
docs_path = tmp_path / "3.13.0rc1/docs"
docs_path.mkdir(parents=True)
tarball = tarfile.open(docs_path / "python-3.13.0rc1-docs-html.tar.bz2", "w:bz2")
with tarball:
tarinfo = tarfile.TarInfo("index.html")
tarinfo.size = len(content)
tarball.addfile(tarinfo, io.BytesIO(content.encode()))
@contextlib.contextmanager
def fake_answers(monkeypatch: pytest.MonkeyPatch, answers: list[str]) -> None:
"""Monkey-patch input() to give the given answers. All must be consumed."""
answers_left = list(answers)
def fake_input(question):
print(question, "--", answers_left[0])
return answers_left.pop(0)
with monkeypatch.context() as ctx:
ctx.setattr(builtins, "input", fake_input)
yield
assert answers_left == []
def test_check_doc_unreleased_version_no_file(tmp_path: Path) -> None:
db = {
"release": Tag("3.13.0rc1"),
"git_repo": str(tmp_path),
}
with pytest.raises(AssertionError):
# There should be a docs artefact available
run_release.check_doc_unreleased_version(cast(ReleaseShelf, db))
def test_check_doc_unreleased_version_no_file_alpha(tmp_path: Path) -> None:
db = {
"release": Tag("3.13.0a1"),
"git_repo": str(tmp_path),
}
# No docs artefact needed for alphas
run_release.check_doc_unreleased_version(cast(ReleaseShelf, db))
def test_check_doc_unreleased_version_ok(tmp_path: Path) -> None:
prepare_fake_docs(
tmp_path,
"<div>New in 3.13</div>",
)
db = {
"release": Tag("3.13.0rc1"),
"git_repo": str(tmp_path),
}
run_release.check_doc_unreleased_version(cast(ReleaseShelf, db))
def test_check_doc_unreleased_version_not_ok(monkeypatch, tmp_path: Path) -> None:
prepare_fake_docs(
tmp_path,
"<div>New in 3.13.0rc1 (unreleased)</div>",
)
db = {
"release": Tag("3.13.0rc1"),
"git_repo": str(tmp_path),
}
with fake_answers(monkeypatch, ["no"]), pytest.raises(AssertionError):
run_release.check_doc_unreleased_version(cast(ReleaseShelf, db))
def test_check_doc_unreleased_version_waived(monkeypatch, tmp_path: Path) -> None:
prepare_fake_docs(
tmp_path,
"<div>New in 3.13.0rc1 (unreleased)</div>",
)
db = {
"release": Tag("3.13.0rc1"),
"git_repo": str(tmp_path),
}
with fake_answers(monkeypatch, ["yes"]):
run_release.check_doc_unreleased_version(cast(ReleaseShelf, db))
def test_update_whatsnew_toctree(tmp_path: Path) -> None:
# Arrange
# Only first beta triggers update
db = {"release": Tag("3.14.0b1")}
original_toctree_file = Path(__file__).parent / "whatsnew_index.rst"
toctree__file = tmp_path / "patchlevel.h"
toctree__file.write_text(original_toctree_file.read_text())
# Act
run_release.update_whatsnew_toctree(cast(ReleaseShelf, db), str(toctree__file))
# Assert
new_contents = toctree__file.read_text()
assert " 3.15.rst\n 3.14.rst\n" in new_contents
def test_run_add_to_python_dot_org_quotes_remote_environment(monkeypatch) -> None:
commands = []
class FakeSFTPClient:
def put(self, source: str, destination: str) -> None:
pass
def close(self) -> None:
pass
class FakeSSHClient:
def load_system_host_keys(self) -> None:
pass
def set_missing_host_key_policy(self, policy) -> None:
pass
def connect(self, *args, **kwargs) -> None:
pass
def get_transport(self):
return object()
def exec_command(self, command: str):
commands.append(command)
return None, io.BytesIO(b"ok"), io.BytesIO()
class FakeIssuer:
def __init__(self, issuer_url: str) -> None:
self.issuer_url = issuer_url
def identity_token(self) -> str:
return "token; touch /tmp/pwned"
monkeypatch.setattr(run_release.paramiko, "SSHClient", FakeSSHClient)
monkeypatch.setattr(
run_release.MySFTPClient,
"from_transport",
staticmethod(lambda transport: FakeSFTPClient()),
)
monkeypatch.setattr(run_release.sigstore.oidc, "Issuer", FakeIssuer)
db = {
"auth_info": "user:key; echo pwned",
"release": Tag("3.15.0a1"),
"ssh_key": None,
"ssh_user": "release-manager",
}
run_release.run_add_to_python_dot_org(cast(ReleaseShelf, db))
assert commands == [
"env 'AUTH_INFO=user:key; echo pwned' "
"'SIGSTORE_IDENTITY_TOKEN=token; touch /tmp/pwned' "
"python3 add_to_pydotorg.py 3.15.0a1"
]
def test_upload_files_to_server_quotes_remote_cleanup_path(
monkeypatch, tmp_path: Path
) -> None:
commands = []
class FakeSFTPClient:
def mkdir(self, path: str) -> None:
pass
def put_dir(self, source: Path, target: str, progress) -> None:
pass
def close(self) -> None:
pass
class FakeSSHClient:
def load_system_host_keys(self) -> None:
pass
def set_missing_host_key_policy(self, policy) -> None:
pass
def connect(self, *args, **kwargs) -> None:
pass
def get_transport(self):
return object()
def exec_command(self, command: str) -> None:
commands.append(command)
@contextlib.contextmanager
def fake_alive_bar(total: int):
yield lambda *args, **kwargs: None
release = Tag("3.15.0a1")
artifacts_path = tmp_path / str(release)
(artifacts_path / "downloads").mkdir(parents=True)
monkeypatch.setattr(run_release.paramiko, "SSHClient", FakeSSHClient)
monkeypatch.setattr(
run_release.MySFTPClient,
"from_transport",
staticmethod(lambda transport: FakeSFTPClient()),
)
monkeypatch.setattr(run_release, "alive_bar", fake_alive_bar)
db = {
"git_repo": tmp_path,
"release": release,
"ssh_key": None,
"ssh_user": "release-manager; touch /tmp/pwned #",
}
run_release.upload_files_to_server(
cast(ReleaseShelf, db), run_release.DOWNLOADS_SERVER
)
assert commands == [
"rm -rf '/home/psf-users/release-manager; touch /tmp/pwned #/3.15.0a1'"
]
def test_release_file_placement_quotes_remote_paths(monkeypatch) -> None:
commands = []
class FakeChannel:
def exec_command(self, command: str) -> None:
commands.append(command)
def recv_exit_status(self) -> int:
return 0
def recv_stderr(self, size: int) -> bytes:
return b""
class FakeTransport:
def open_session(self) -> FakeChannel:
return FakeChannel()
class FakeSSHClient:
def load_system_host_keys(self) -> None:
pass
def set_missing_host_key_policy(self, policy) -> None:
pass
def connect(self, *args, **kwargs) -> None:
pass
def get_transport(self) -> FakeTransport:
return FakeTransport()
monkeypatch.setattr(run_release.paramiko, "SSHClient", FakeSSHClient)
db = {
"release": Tag("3.15.0rc1"),
"ssh_key": None,
"ssh_user": "release-manager; touch /tmp/pwned #",
}
run_release.place_files_in_download_folder(cast(ReleaseShelf, db))
run_release.unpack_docs_in_the_docs_server(cast(ReleaseShelf, db))
assert commands == [
"mkdir -p /srv/www.python.org/ftp/python/3.15.0",
'sh -c \'cp "$1"/* "$2"\' sh '
"'/home/psf-users/release-manager; touch /tmp/pwned #/3.15.0rc1/downloads' "
"/srv/www.python.org/ftp/python/3.15.0",
"find /srv/www.python.org/ftp/python/3.15.0 -maxdepth 0 '!' "
"-group downloads -exec chgrp downloads '{}' +",
"find /srv/www.python.org/ftp/python/3.15.0 -maxdepth 0 '!' -perm 775 "
"-exec chmod 775 '{}' +",
"find /srv/www.python.org/ftp/python/3.15.0 -maxdepth 1 -type f -user "
"'release-manager; touch /tmp/pwned #' '!' -perm 664 -exec chmod 664 '{}' +",
"mkdir -p /srv/www.python.org/ftp/python/doc/3.15.0rc1",
'sh -c \'cp "$1"/* "$2"\' sh '
"'/home/psf-users/release-manager; touch /tmp/pwned #/3.15.0rc1/docs' "
"/srv/www.python.org/ftp/python/doc/3.15.0rc1",
"find /srv/www.python.org/ftp/python/doc/3.15.0rc1 -maxdepth 0 '!' "
"-group downloads -exec chgrp downloads '{}' +",
"find /srv/www.python.org/ftp/python/doc/3.15.0rc1 -maxdepth 0 '!' "
"-perm 775 -exec chmod 775 '{}' +",
"find /srv/www.python.org/ftp/python/doc/3.15.0rc1 -maxdepth 1 -type f "
"-user 'release-manager; touch /tmp/pwned #' '!' -perm 664 "
"-exec chmod 664 '{}' +",
"mkdir -p /srv/docs.python.org/release/3.15.0rc1",
"unzip '/home/psf-users/release-manager; touch /tmp/pwned #/3.15.0rc1/docs/"
"python-3.15.0rc1-docs-html.zip' -d /srv/docs.python.org/release/3.15.0rc1",
'sh -c \'mv "$1"/* "$2"\' sh '
"//srv/docs.python.org/release/3.15.0rc1/python-3.15.0rc1-docs-html "
"/srv/docs.python.org/release/3.15.0rc1",
"rm -rf //srv/docs.python.org/release/3.15.0rc1/python-3.15.0rc1-docs-html",
"chgrp -R docs /srv/docs.python.org/release/3.15.0rc1",
"chmod -R 775 /srv/docs.python.org/release/3.15.0rc1",
"find /srv/docs.python.org/release/3.15.0rc1 -type f -exec chmod 664 '{}' ';'",
]