-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathtest_server.py
More file actions
197 lines (149 loc) · 6.87 KB
/
Copy pathtest_server.py
File metadata and controls
197 lines (149 loc) · 6.87 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
from __future__ import annotations
import asyncio
import pathlib
import zipfile
from unittest.mock import Mock, patch
import pytest
from starlette.exceptions import HTTPException
from starlette.responses import FileResponse, HTMLResponse
from fromager import context, server
class _FakeRequest:
"""Minimal stand-in for starlette.requests.Request."""
def __init__(self, path_params: dict[str, str] | None = None) -> None:
self.path_params = path_params or {}
def _create_fake_wheel(directory: pathlib.Path, name: str) -> pathlib.Path:
"""Create a minimal valid wheel file for testing."""
wheel_path = directory.joinpath(name)
with zipfile.ZipFile(wheel_path, "w") as zf:
zf.writestr("dummy.txt", "fake wheel content")
return wheel_path
@pytest.fixture
def simple_dir(tmp_path: pathlib.Path) -> pathlib.Path:
"""Create a simple index directory with test data."""
basedir = tmp_path.joinpath("simple")
basedir.mkdir()
project_dir = basedir.joinpath("testpkg")
project_dir.mkdir()
_create_fake_wheel(project_dir, "testpkg-1.0-py3-none-any.whl")
project_dir.joinpath("testpkg-1.0-py3-none-any.whl.metadata").write_bytes(
b"Metadata-Version: 2.1\nName: testpkg\nVersion: 1.0\n"
)
project_dir.joinpath("testpkg-1.0.tar.gz").write_bytes(b"fake tarball")
return basedir
@pytest.fixture
def handler(simple_dir: pathlib.Path) -> server.SimpleHTMLIndex:
"""Create a SimpleHTMLIndex instance for testing."""
return server.SimpleHTMLIndex(simple_dir)
def test_update_wheel_mirror_moves_to_downloads(
tmp_context: context.WorkContext,
) -> None:
"""Verify wheels are moved from build dir to downloads dir."""
_create_fake_wheel(tmp_context.wheels_build, "foo-1.0-py3-none-any.whl")
server.update_wheel_mirror(tmp_context)
assert not tmp_context.wheels_build.joinpath("foo-1.0-py3-none-any.whl").exists()
assert tmp_context.wheels_downloads.joinpath("foo-1.0-py3-none-any.whl").exists()
def test_update_wheel_mirror_creates_symlink(
tmp_context: context.WorkContext,
) -> None:
"""Verify symlinks are created in the simple index structure."""
_create_fake_wheel(tmp_context.wheels_build, "foo-1.0-py3-none-any.whl")
server.update_wheel_mirror(tmp_context)
symlink = tmp_context.wheel_server_dir.joinpath("foo", "foo-1.0-py3-none-any.whl")
assert symlink.is_symlink()
assert symlink.is_file()
def test_update_wheel_mirror_prebuilt_wheels(
tmp_context: context.WorkContext,
) -> None:
"""Verify wheels in prebuilt directory get symlinked."""
_create_fake_wheel(tmp_context.wheels_prebuilt, "bar-2.0-py3-none-any.whl")
server.update_wheel_mirror(tmp_context)
symlink = tmp_context.wheel_server_dir.joinpath("bar", "bar-2.0-py3-none-any.whl")
assert symlink.is_symlink()
assert symlink.is_file()
def test_update_wheel_mirror_cleans_dangling_symlinks(
tmp_context: context.WorkContext,
) -> None:
"""Verify dangling symlinks are removed and recreated."""
wheel_path = _create_fake_wheel(
tmp_context.wheels_downloads, "foo-1.0-py3-none-any.whl"
)
server.update_wheel_mirror(tmp_context)
symlink = tmp_context.wheel_server_dir.joinpath("foo", "foo-1.0-py3-none-any.whl")
assert symlink.is_symlink()
# Remove the target to create a dangling symlink
wheel_path.unlink()
assert symlink.is_symlink()
assert not symlink.is_file() # dangling
# Re-create the wheel and update again
_create_fake_wheel(tmp_context.wheels_downloads, "foo-1.0-py3-none-any.whl")
server.update_wheel_mirror(tmp_context)
assert symlink.is_symlink()
assert symlink.is_file() # no longer dangling
def test_project_page_lists_files(handler: server.SimpleHTMLIndex) -> None:
"""Verify /simple/{project} lists wheel files."""
request = _FakeRequest({"project": "testpkg"})
response = asyncio.run(handler.project_page(request)) # type: ignore[arg-type]
assert isinstance(response, HTMLResponse)
assert response.status_code == 200
assert isinstance(response.body, bytes)
body = response.body.decode()
assert "testpkg-1.0-py3-none-any.whl" in body
assert "testpkg-1.0.tar.gz" in body
assert "testpkg-1.0-py3-none-any.whl.metadata" in body
def test_project_page_missing_project(handler: server.SimpleHTMLIndex) -> None:
"""Verify /simple/{project} raises 404 for unknown project."""
request = _FakeRequest({"project": "nonexistent"})
with pytest.raises(HTTPException) as exc_info:
asyncio.run(handler.project_page(request)) # type: ignore[arg-type]
assert exc_info.value.status_code == 404
def test_serve_wheel_file(handler: server.SimpleHTMLIndex) -> None:
"""Verify serving a .whl file with correct media type."""
request = _FakeRequest(
{"project": "testpkg", "filename": "testpkg-1.0-py3-none-any.whl"}
)
response = asyncio.run(handler.server_file(request)) # type: ignore[arg-type]
assert isinstance(response, FileResponse)
assert response.media_type == "application/zip"
def test_serve_file_not_found(handler: server.SimpleHTMLIndex) -> None:
"""Verify 404 for missing file."""
request = _FakeRequest(
{"project": "testpkg", "filename": "nonexistent-1.0-py3-none-any.whl"}
)
with pytest.raises(HTTPException) as exc_info:
asyncio.run(handler.server_file(request)) # type: ignore[arg-type]
assert exc_info.value.status_code == 404
def test_serve_file_bad_extension(
simple_dir: pathlib.Path,
handler: server.SimpleHTMLIndex,
) -> None:
"""Verify 400 for unsupported file extension."""
simple_dir.joinpath("testpkg", "bad.txt").write_text("bad")
request = _FakeRequest({"project": "testpkg", "filename": "bad.txt"})
with pytest.raises(HTTPException) as exc_info:
asyncio.run(handler.server_file(request)) # type: ignore[arg-type]
assert exc_info.value.status_code == 400
@patch("fromager.server.run_wheel_server")
@patch("fromager.server.update_wheel_mirror")
def test_start_wheel_server_uses_external_url(
mock_mirror: Mock,
mock_run: Mock,
tmp_context: context.WorkContext,
) -> None:
"""Verify no local server starts when external URL is configured."""
tmp_context.wheel_server_url = "http://external:8080/simple/"
server.start_wheel_server(tmp_context)
mock_mirror.assert_called_once_with(tmp_context)
mock_run.assert_not_called()
assert tmp_context.wheel_server_url == "http://external:8080/simple/"
@patch("fromager.server.run_wheel_server")
@patch("fromager.server.update_wheel_mirror")
def test_start_wheel_server_starts_local(
mock_mirror: Mock,
mock_run: Mock,
tmp_context: context.WorkContext,
) -> None:
"""Verify local server starts when no external URL is set."""
assert tmp_context.wheel_server_url == ""
server.start_wheel_server(tmp_context)
mock_mirror.assert_called_once_with(tmp_context)
mock_run.assert_called_once_with(tmp_context)