forked from All-Hands-AI/docker-python-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_all.py
More file actions
253 lines (215 loc) · 7.37 KB
/
Copy pathtest_all.py
File metadata and controls
253 lines (215 loc) · 7.37 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
import dataclasses
import json
from pathlib import Path
from typing import Any
from unittest import mock
import pytest
import responses
from docker_python_nodejs.dockerfiles import render_dockerfile_with_context
from docker_python_nodejs.readme import update_dynamic_readme
from docker_python_nodejs.settings import BASE_PATH, DOCKERFILES_PATH
from docker_python_nodejs.versions import (
BuildVersion,
SupportedVersion,
decide_nodejs_versions,
decide_version_combinations,
fetch_supported_nodejs_versions,
scrape_supported_python_versions,
)
@pytest.fixture(name="build_version")
def build_version_fixture() -> BuildVersion:
return BuildVersion(
key="python3.11-nodejs20",
python="3.11",
python_canonical="3.11.3",
python_image="3.11.3-trixie",
nodejs="20",
nodejs_canonical="20.2.0",
distro="trixie",
platforms=[
"linux/amd64",
"linux/arm64",
],
)
@pytest.mark.enable_socket()
def test_scrape_supported_python_versions() -> None:
versions = scrape_supported_python_versions()
assert len(versions) > 0
first_version = versions[0]
assert first_version.version
assert first_version.start
assert first_version.end
@responses.activate
def test_render_dockerfile_with_context(build_version: BuildVersion) -> None:
file_path = DOCKERFILES_PATH / f"{build_version.key}.Dockerfile"
res_keys = responses.Response(
method="GET",
url="https://raw.githubusercontent.com/nodejs/docker-node/master/keys/node.keys",
body="a\nb\nc",
)
responses.add(res_keys)
render_dockerfile_with_context(json.dumps(dataclasses.asdict(build_version)))
assert file_path.exists()
with file_path.open() as fp:
dockerfile = fp.read()
assert f"# python: {build_version.python_canonical}" in dockerfile
assert f"# nodejs: {build_version.nodejs_canonical}" in dockerfile
class MockPath:
content: str = ""
def __init__(self: "MockPath", content: str) -> None:
self.content = content
def write_text(self: "MockPath", text: str) -> None:
self.content = text
def read_text(self: "MockPath") -> str:
return self.content
def test_update_dynamic_readme(build_version: BuildVersion) -> None:
ver = build_version
python_version = SupportedVersion(start="2022-10-24", end="2027-10", version="3.11")
node_version = SupportedVersion(start="2023-04-18", end="2026-04-30", version="v20")
initial_content = """
## Tags
<!-- TAGS_START -->
<!-- TAGS_END -->
## Supported Versions
<!-- SUPPORTED_VERSIONS_START -->
<!-- SUPPORTED_VERSIONS_END -->
"""
mock_path = MockPath(initial_content)
with mock.patch.multiple(
Path,
read_text=mock_path.read_text,
write_text=mock_path.write_text,
):
update_dynamic_readme([ver], [python_version], [node_version])
file_path = BASE_PATH / "README.md"
assert file_path.exists()
readme = file_path.read_text()
assert f"`{ver.key}` | {ver.python_canonical} | {ver.nodejs_canonical} | {ver.distro}" in readme
assert f"{python_version.version} | {python_version.start} | {python_version.end}" in readme
assert f"{node_version.version} | {node_version.start} | {node_version.end}" in readme
@pytest.fixture(name="python_tags")
def python_tags_fixture() -> dict[str, Any]:
return {
"count": 2,
"next": None,
"previous": None,
"results": [
{
"name": "3.11.4-trixie",
"images": [{"os": "linux", "architecture": "amd64"}, {"os": "linux", "architecture": "arm64"}],
},
{
"name": "3.11.4-alpine",
"images": [{"os": "linux", "architecture": "amd64"}],
},
],
}
@pytest.fixture(name="node_releases")
def node_releases_fixture() -> list[dict[str, Any]]:
return [
{
"version": "v20.3.0",
"date": "2023-09-07",
"files": [
"linux-arm64",
"linux-x64",
],
},
{
"version": "v22.0.0",
"date": "2024-04-23",
"files": [
"linux-arm64",
"linux-x64",
],
},
]
@pytest.fixture(name="node_unofficial_releases")
def node_unofficial_releases_fixture() -> list[dict[str, Any]]:
return [
{
"version": "v20.3.0",
"date": "2023-09-07",
"files": [
"linux-x64-musl",
],
},
{
"version": "v22.0.0",
"date": "2024-04-23",
"files": [
"linux-x64-musl",
],
},
]
@responses.activate
def test_decide_version_combinations(
python_tags: dict[str, Any],
node_releases: list[dict[str, Any]],
node_unofficial_releases: list[dict[str, Any]],
) -> None:
responses.add(
method="GET",
url="https://registry.hub.docker.com/v2/namespaces/library/repositories/python/tags?page=1&page_size=100",
json=python_tags,
)
responses.add(method="GET", url="https://nodejs.org/dist/index.json", json=node_releases)
responses.add(
method="GET",
url="https://unofficial-builds.nodejs.org/download/release/index.json",
json=node_unofficial_releases,
)
python_version = SupportedVersion(start="2022-10-24", end="2027-10", version="3.11")
node_version = SupportedVersion(start="2023-04-18", end="2026-04-30", version="v20")
versions = decide_version_combinations(["trixie", "alpine"], [python_version], [node_version])
assert versions
assert len(versions) == python_tags["count"]
assert versions[0].nodejs_canonical == "20.3.0"
assert versions[0].python_canonical == "3.11.4"
assert versions[0].distro == "trixie"
assert versions[1].nodejs_canonical == "20.3.0"
assert versions[1].python_canonical == "3.11.4"
assert versions[1].distro == "alpine"
@pytest.fixture(name="node_release_schedule")
def node_release_schedule_fixture() -> dict[str, dict[str, Any]]:
return {
"v20": {
"start": "2023-04-18",
"lts": "2023-10-24",
"maintenance": "2024-10-22",
"end": "2026-04-30",
"codename": "Iron",
},
"v22": {
"start": "2024-04-23",
"lts": "2024-10-29",
"maintenance": "2025-10-21",
"end": "2027-04-30",
"codename": "",
},
}
@responses.activate
def test_decide_nodejs_versions(
node_release_schedule: dict[str, dict[str, Any]],
node_releases: list[dict[str, Any]],
node_unofficial_releases: list[dict[str, Any]],
) -> None:
responses.add(
method="GET",
url="https://raw.githubusercontent.com/nodejs/Release/master/schedule.json",
json=node_release_schedule,
)
responses.add(
method="GET",
url="https://nodejs.org/dist/index.json",
json=node_releases,
)
responses.add(
method="GET",
url="https://unofficial-builds.nodejs.org/download/release/index.json",
json=node_unofficial_releases,
)
supported_node_versions = fetch_supported_nodejs_versions()
distros = ["trixie", "alpine"]
versions = decide_nodejs_versions(distros, supported_node_versions)
assert len(supported_node_versions) * len(distros) == len(versions)