-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_upload_to_github_release_page.py
More file actions
181 lines (153 loc) · 5.32 KB
/
Copy pathtest_upload_to_github_release_page.py
File metadata and controls
181 lines (153 loc) · 5.32 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
import sys
from pathlib import Path
import pytest
import requests
import requests_mock
import gardenlinux.github.release.__main__ as gh
from gardenlinux.github.release import upload_to_github_release_page
from ..constants import TEST_GARDENLINUX_RELEASE
def test_upload_to_github_release_page_dryrun(
caplog: pytest.LogCaptureFixture, artifact_for_upload: Path
) -> None:
with requests_mock.Mocker():
assert (
upload_to_github_release_page( # type: ignore[func-returns-value]
"gardenlinux",
"gardenlinux",
TEST_GARDENLINUX_RELEASE,
str(artifact_for_upload),
dry_run=True,
)
is None
)
assert any(
"Dry run: would upload" in record.message for record in caplog.records
), "Expected a dry‑run log entry"
def test_upload_to_github_release_page_needs_github_token(
downloads_dir: None, artifact_for_upload: Path
) -> None:
with requests_mock.Mocker():
with pytest.raises(ValueError) as exn:
upload_to_github_release_page(
"gardenlinux",
"gardenlinux",
TEST_GARDENLINUX_RELEASE,
str(artifact_for_upload),
dry_run=False,
)
assert str(exn.value) == "GITHUB_TOKEN environment variable not set", (
"Expected an exception to be raised on missing GITHUB_TOKEN environment variable"
)
def test_upload_to_github_release_page(
downloads_dir: None,
caplog: pytest.LogCaptureFixture,
github_token: None,
artifact_for_upload: Path,
) -> None:
with requests_mock.Mocker(real_http=True) as m:
m.post(
f"https://uploads.github.com/repos/gardenlinux/gardenlinux/releases/{TEST_GARDENLINUX_RELEASE}/assets?name=artifact.log",
text="{}",
status_code=201,
)
upload_to_github_release_page(
"gardenlinux",
"gardenlinux",
TEST_GARDENLINUX_RELEASE,
str(artifact_for_upload),
dry_run=False,
)
assert any(
"Upload successful" in record.message for record in caplog.records
), "Expected an upload confirmation log entry"
def test_upload_to_github_release_page_unreadable_artifact(
downloads_dir: None,
caplog: pytest.LogCaptureFixture,
github_token: None,
artifact_for_upload: Path,
) -> None:
artifact_for_upload.chmod(0)
upload_to_github_release_page(
"gardenlinux",
"gardenlinux",
TEST_GARDENLINUX_RELEASE,
str(artifact_for_upload),
dry_run=False,
)
assert any("Error reading file" in record.message for record in caplog.records), (
"Expected an error message log entry"
)
def test_upload_to_github_release_page_failed(
downloads_dir: None,
caplog: pytest.LogCaptureFixture,
github_token: None,
artifact_for_upload: Path,
) -> None:
with requests_mock.Mocker(real_http=True) as m:
m.post(
f"https://uploads.github.com/repos/gardenlinux/gardenlinux/releases/{TEST_GARDENLINUX_RELEASE}/assets?name=artifact.log",
text="{}",
status_code=503,
)
with pytest.raises(requests.exceptions.HTTPError):
upload_to_github_release_page(
"gardenlinux",
"gardenlinux",
TEST_GARDENLINUX_RELEASE,
str(artifact_for_upload),
dry_run=False,
)
assert any(
"Upload failed with status code 503:" in record.message
for record in caplog.records
), "Expected an error HTTP status code to be logged"
def test_script_parse_args_wrong_command(
monkeypatch: pytest.MonkeyPatch, capfd: pytest.CaptureFixture[str]
) -> None:
monkeypatch.setattr(sys, "argv", ["gh", "rejoice"])
with pytest.raises(SystemExit):
gh.main()
captured = capfd.readouterr()
assert "argument command: invalid choice: 'rejoice'" in captured.err, (
"Expected help message printed"
)
def test_script_parse_args_upload_command_required_args(
monkeypatch: pytest.MonkeyPatch, capfd: pytest.CaptureFixture[str]
) -> None:
monkeypatch.setattr(
sys, "argv", ["gh", "upload", "--owner", "gardenlinux", "--repo", "gardenlinux"]
)
with pytest.raises(SystemExit):
gh.main()
captured = capfd.readouterr()
assert (
"the following arguments are required: --release_id, --file_path"
in captured.err
), "Expected help message on missing arguments for 'upload' command"
def test_script_upload_dry_run(
monkeypatch: pytest.MonkeyPatch, capfd: pytest.CaptureFixture[str]
) -> None:
monkeypatch.setattr(
sys,
"argv",
[
"gh",
"upload",
"--owner",
"gardenlinux",
"--repo",
"gardenlinux",
"--release_id",
TEST_GARDENLINUX_RELEASE,
"--file_path",
"foo",
"--dry-run",
],
)
monkeypatch.setattr(
"gardenlinux.github.release.__main__.upload_to_github_release_page",
lambda a1, a2, a3, a4, dry_run: print(f"dry-run: {dry_run}"),
)
gh.main()
captured = capfd.readouterr()
assert captured.out == "dry-run: True\n"