-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_create_github_release_notes.py
More file actions
222 lines (185 loc) · 7.66 KB
/
test_create_github_release_notes.py
File metadata and controls
222 lines (185 loc) · 7.66 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
import pytest
import requests_mock
from git import Repo
from moto import mock_aws
from gardenlinux.constants import GLVD_BASE_URL
from gardenlinux.distro_version import UnsupportedDistroVersion
from gardenlinux.github.release_notes import release_notes_changes_section # type: ignore[attr-defined]
from gardenlinux.github.release_notes import ( # type: ignore[attr-defined]
release_notes_compare_package_versions_section,
)
from gardenlinux.github.release_notes.deployment_platform import DeploymentPlatform
from gardenlinux.github.release_notes.helpers import get_variant_from_flavor
from gardenlinux.s3.bucket import Bucket
from ..constants import (
RELEASE_ARTIFACTS_METADATA_FILES,
RELEASE_NOTES_S3_ARTIFACTS_DIR,
RELEASE_NOTES_TEST_DATA_DIR,
TEST_GARDENLINUX_COMMIT,
TEST_GARDENLINUX_RELEASE,
)
TEST_FLAVORS = [
("foo_bar_baz", "legacy"),
("aws-gardener_prod_trustedboot_tpm2-amd64", "legacy"),
("openstack-gardener_prod_tpm2_trustedboot-arm64", "tpm2_trustedboot"),
("azure-gardener_prod_usi-amd64", "usi"),
("", "legacy"),
]
def test_release_notes_changes_section_empty_packagelist() -> None:
with requests_mock.Mocker() as m:
m.get(
f"{GLVD_BASE_URL}/releaseNotes/{TEST_GARDENLINUX_RELEASE}",
text='{"packageList": []}',
status_code=200,
)
assert release_notes_changes_section(TEST_GARDENLINUX_RELEASE) == "", (
"Expected an empty result if GLVD returns an empty package list"
)
def test_release_notes_changes_section_broken_glvd_response() -> None:
with requests_mock.Mocker() as m:
m.get(
f"{GLVD_BASE_URL}/releaseNotes/{TEST_GARDENLINUX_RELEASE}",
text="<html><body><h1>Personal Home Page</h1></body></html>",
status_code=200,
)
assert "fill this in" in release_notes_changes_section(
TEST_GARDENLINUX_RELEASE
), (
"Expected a placeholder message to be generated if GVLD response is not valid"
)
def test_release_notes_compare_package_versions_section_legacy_versioning_is_recognized() -> (
None
):
with requests_mock.Mocker() as m:
m.get(
f"{GLVD_BASE_URL}/releaseNotes/1.0",
json={
"packageList": [
{
"sourcePackageName": "gardenlinux-release-example",
"oldVersion": "0.9pre1",
"newVersion": "1.0",
"fixedCves": [],
}
]
},
status_code=200,
)
assert "upgrade 'gardenlinux-release-example'" in release_notes_changes_section(
"1.0"
), "Legacy versioning is supported"
def test_release_notes_compare_package_versions_section_legacy_versioning_patch_release_is_recognized(
monkeypatch: pytest.MonkeyPatch,
) -> None:
def mock_compare_apt_repo_versions(
previous_version: str, current_version: str
) -> str:
output = f"| Package | {previous_version} | {current_version} |\n"
output += "|---------|--------------------|-------------------|\n"
output += "|containerd|1.0|1.1|\n"
return output
monkeypatch.setattr(
"gardenlinux.github.release_notes.sections.compare_apt_repo_versions",
mock_compare_apt_repo_versions,
)
assert "|containerd|1.0|1.1|" in release_notes_compare_package_versions_section(
"1.1", {}
), "Legacy versioning patch releases are supported"
def test_release_notes_compare_package_versions_section_semver_is_recognized() -> None:
with requests_mock.Mocker() as m:
m.get(
f"{GLVD_BASE_URL}/releaseNotes/1.20.0",
json={
"packageList": [
{
"sourcePackageName": "gardenlinux-release-example",
"oldVersion": "1.19.0",
"newVersion": "1.20.0",
"fixedCves": [],
}
]
},
status_code=200,
)
assert "upgrade 'gardenlinux-release-example'" in release_notes_changes_section(
"1.20.0"
), "Semver is supported"
def test_release_notes_compare_package_versions_section_semver_patch_release_is_recognized(
monkeypatch: pytest.MonkeyPatch,
) -> None:
def mock_compare_apt_repo_versions(
previous_version: str, current_version: str
) -> str:
output = f"| Package | {previous_version} | {current_version} |\n"
output += "|---------|--------------------|-------------------|\n"
output += "|containerd|1.0|1.1|\n"
return output
monkeypatch.setattr(
"gardenlinux.github.release_notes.sections.compare_apt_repo_versions",
mock_compare_apt_repo_versions,
)
assert "|containerd|1.0|1.1|" in release_notes_compare_package_versions_section(
"1.20.1", {}
), "Semver patch releases are supported"
def test_release_notes_compare_package_versions_section_unrecognizable_version() -> (
None
):
with pytest.raises(UnsupportedDistroVersion):
release_notes_compare_package_versions_section("garden.linux", {})
@pytest.mark.parametrize("flavor", TEST_FLAVORS)
def test_get_variant_from_flavor(flavor: str) -> None:
assert get_variant_from_flavor(flavor[0]) == flavor[1]
def test_default_get_file_extension_for_deployment_platform() -> None:
assert DeploymentPlatform().image_extension() == "raw"
@mock_aws
def test_github_release_page(
monkeypatch: pytest.MonkeyPatch, downloads_dir: None, release_s3_bucket: Bucket
) -> None:
class SubmoduleAsRepo(Repo): # type: ignore[misc]
"""This will fake a git submodule as a git repository object."""
def __new__(cls, *args, **kwargs): # type: ignore[no-untyped-def]
r = super().__new__(Repo) # pyright: ignore[reportArgumentType]
r.__init__(*args, **kwargs)
maybe_gl_submodule = [
submodule
for submodule in r.submodules
if submodule.name.endswith("/gardenlinux")
]
if not maybe_gl_submodule:
return r
else:
gl = maybe_gl_submodule[0]
sr = gl.module()
sr.remotes.origin.pull("main")
return sr
monkeypatch.setattr(
"gardenlinux.github.release_notes.helpers.Repo", # pyright: ignore[reportAttributeAccessIssue]
SubmoduleAsRepo,
)
import gardenlinux.github
release_fixture_path = (
RELEASE_NOTES_TEST_DATA_DIR
/ f"github_release_notes_{TEST_GARDENLINUX_RELEASE}.md"
)
glvd_response_fixture_path = (
RELEASE_NOTES_TEST_DATA_DIR / f"glvd_{TEST_GARDENLINUX_RELEASE}.json"
)
with requests_mock.Mocker(real_http=True) as m:
for yaml_file in RELEASE_ARTIFACTS_METADATA_FILES:
filepath = f"{RELEASE_NOTES_S3_ARTIFACTS_DIR}/{yaml_file}"
base = yaml_file[: -len(".s3_metadata.yaml")]
key = f"meta/singles/{base}-{TEST_GARDENLINUX_RELEASE}-{TEST_GARDENLINUX_COMMIT}"
release_s3_bucket.upload_file(filepath, key)
m.get(
f"{GLVD_BASE_URL}/releaseNotes/{TEST_GARDENLINUX_RELEASE}",
text=glvd_response_fixture_path.read_text(),
status_code=200,
)
generated_release_notes = (
gardenlinux.github.release_notes.create_github_release_notes( # pyright: ignore[reportAttributeAccessIssue]
TEST_GARDENLINUX_RELEASE,
TEST_GARDENLINUX_COMMIT,
release_s3_bucket.name,
)
)
assert generated_release_notes == release_fixture_path.read_text()