forked from aws/aws-lambda-builders
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_python_uv.py
More file actions
299 lines (255 loc) · 11.8 KB
/
Copy pathtest_python_uv.py
File metadata and controls
299 lines (255 loc) · 11.8 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
import os
import pathlib
import shutil
import sys
import platform
import tempfile
from unittest import TestCase, skipIf
from parameterized import parameterized_class
from aws_lambda_builders.builder import LambdaBuilder
from aws_lambda_builders.exceptions import WorkflowFailedError
from aws_lambda_builders.utils import which
from aws_lambda_builders.workflows.python_uv.utils import EXPERIMENTAL_FLAG_BUILD_PERFORMANCE
IS_WINDOWS = platform.system().lower() == "windows"
@parameterized_class(("experimental_flags",), [([]), ([EXPERIMENTAL_FLAG_BUILD_PERFORMANCE])])
class TestPythonUvWorkflow(TestCase):
"""
Verifies that `python_uv` workflow works by building a Lambda with simple dependencies
"""
TEST_DATA_FOLDER = os.path.join(os.path.dirname(__file__), "testdata")
experimental_flags = []
def setUp(self):
self.source_dir = self.TEST_DATA_FOLDER
self.artifacts_dir = tempfile.mkdtemp()
self.scratch_dir = tempfile.mkdtemp()
self.dependencies_dir = tempfile.mkdtemp()
self.manifest_path_requirements = os.path.join(self.TEST_DATA_FOLDER, "requirements-simple.txt")
self.manifest_path_pyproject = os.path.join(self.TEST_DATA_FOLDER, "pyproject.toml")
# PIP-equivalent test files for compatibility validation
self.manifest_path_numpy = os.path.join(self.TEST_DATA_FOLDER, "requirements-numpy.txt")
self.manifest_path_wrapt = os.path.join(self.TEST_DATA_FOLDER, "requirements-wrapt.txt")
self.manifest_path_invalid = os.path.join(self.TEST_DATA_FOLDER, "requirements-invalid.txt")
self.test_data_files = {
"__init__.py",
"main.py",
"requirements-simple.txt",
"pyproject.toml",
"requirements-numpy.txt",
"requirements-wrapt.txt",
"requirements-invalid.txt",
"pyproject-numpy.toml",
"pyproject-wrapt.toml",
}
def tearDown(self):
shutil.rmtree(self.artifacts_dir)
shutil.rmtree(self.scratch_dir)
shutil.rmtree(self.dependencies_dir)
@skipIf(which("uv") is None, "uv not available")
def test_workflow_uses_requirements_txt_file(self):
builder = LambdaBuilder(
language="python",
dependency_manager="uv",
application_framework=None,
)
builder.build(
self.source_dir,
self.artifacts_dir,
self.scratch_dir,
self.manifest_path_requirements,
runtime="python3.9",
experimental_flags=self.experimental_flags,
)
expected_files = self.test_data_files.union({"six.py", "six-1.16.0.dist-info"})
output_files = set(os.listdir(self.artifacts_dir))
self.assertEqual(expected_files.intersection(output_files), expected_files)
@skipIf(which("uv") is None, "uv not available")
def test_workflow_uses_pyproject_toml_file(self):
builder = LambdaBuilder(
language="python",
dependency_manager="uv",
application_framework=None,
)
builder.build(
self.source_dir,
self.artifacts_dir,
self.scratch_dir,
self.manifest_path_pyproject,
runtime="python3.9",
experimental_flags=self.experimental_flags,
)
expected_files = self.test_data_files.union({"six.py", "six-1.16.0.dist-info"})
output_files = set(os.listdir(self.artifacts_dir))
self.assertEqual(expected_files.intersection(output_files), expected_files)
@skipIf(which("uv") is None, "uv not available")
def test_workflow_with_dependencies_dir(self):
builder = LambdaBuilder(
language="python",
dependency_manager="uv",
application_framework=None,
)
builder.build(
self.source_dir,
self.artifacts_dir,
self.scratch_dir,
self.manifest_path_requirements,
runtime="python3.9",
dependencies_dir=self.dependencies_dir,
experimental_flags=self.experimental_flags,
)
expected_files = self.test_data_files
output_files = set(os.listdir(self.artifacts_dir))
self.assertEqual(expected_files.intersection(output_files), expected_files)
expected_dependencies = {"six.py", "six-1.16.0.dist-info"}
dependencies_files = set(os.listdir(self.dependencies_dir))
self.assertEqual(expected_dependencies.intersection(dependencies_files), expected_dependencies)
# combine_dependencies defaults to True, so dependencies must also be copied into the artifacts dir.
self.assertEqual(expected_dependencies.intersection(output_files), expected_dependencies)
@skipIf(which("uv") is None, "uv not available")
def test_workflow_builds_numpy_successfully(self):
"""Test that UV can build numpy (same as PIP workflow test)"""
builder = LambdaBuilder(language="python", dependency_manager="uv", application_framework=None)
builder.build(
self.source_dir,
self.artifacts_dir,
self.scratch_dir,
self.manifest_path_numpy,
runtime="python3.13",
experimental_flags=self.experimental_flags,
)
# Check that numpy was built successfully
output_files = set(os.listdir(self.artifacts_dir))
expected_numpy_files = {"numpy", "numpy-2.1.2.dist-info", "numpy.libs"}
# Verify numpy files are present
for expected_file in expected_numpy_files:
self.assertIn(expected_file, output_files, f"Expected {expected_file} in build output")
@skipIf(which("uv") is None, "uv not available")
def test_workflow_fails_with_wrapt_python313(self):
"""Test that UV fails with wrapt on Python 3.13 (same as PIP workflow)"""
builder = LambdaBuilder(language="python", dependency_manager="uv", application_framework=None)
# Should fail due to Python 3.13 incompatibility
with self.assertRaises(WorkflowFailedError):
builder.build(
self.source_dir,
self.artifacts_dir,
self.scratch_dir,
self.manifest_path_wrapt,
runtime="python3.13",
experimental_flags=self.experimental_flags,
)
@skipIf(which("uv") is None, "uv not available")
def test_workflow_fails_with_invalid_requirements(self):
"""Test that UV properly handles invalid requirements syntax (same as PIP workflow)"""
builder = LambdaBuilder(language="python", dependency_manager="uv", application_framework=None)
# Should fail due to invalid syntax (boto3=1.19.99 instead of boto3==1.19.99)
with self.assertRaises(WorkflowFailedError) as ctx:
builder.build(
self.source_dir,
self.artifacts_dir,
self.scratch_dir,
self.manifest_path_invalid,
runtime="python3.13",
experimental_flags=self.experimental_flags,
)
# Verify error message mentions the syntax issue
error_message = str(ctx.exception)
self.assertIn("no such comparison operator", error_message)
@skipIf(which("uv") is None, "uv not available")
def test_workflow_uses_pyproject_with_lock_file(self):
"""Test that UV uses existing uv.lock file for reproducible builds"""
builder = LambdaBuilder(
language="python",
dependency_manager="uv",
application_framework=None,
)
builder.build(
self.source_dir,
self.artifacts_dir,
self.scratch_dir,
self.manifest_path_pyproject,
runtime="python3.9",
experimental_flags=self.experimental_flags,
)
# uv.lock exists in testdata alongside pyproject.toml
# Verify dependencies were installed from lock file
expected_files = {"six.py", "six-1.16.0.dist-info"}
output_files = set(os.listdir(self.artifacts_dir))
for expected_file in expected_files:
self.assertIn(expected_file, output_files)
@skipIf(which("uv") is None, "uv not available")
def test_workflow_builds_with_arm64_architecture(self):
"""Test that UV builds for ARM64 architecture"""
builder = LambdaBuilder(
language="python",
dependency_manager="uv",
application_framework=None,
)
builder.build(
self.source_dir,
self.artifacts_dir,
self.scratch_dir,
self.manifest_path_numpy,
runtime="python3.12",
architecture="arm64",
experimental_flags=self.experimental_flags,
)
# Verify numpy was built for ARM64 by checking WHEEL metadata
wheel_path = os.path.join(self.artifacts_dir, "numpy-2.1.2.dist-info", "WHEEL")
self.assertTrue(os.path.exists(wheel_path), "WHEEL metadata file should exist")
with open(wheel_path) as f:
wheel_content = f.read()
self.assertIn("aarch64", wheel_content, "WHEEL should contain aarch64 platform tag")
@skipIf(which("uv") is None, "uv not available")
def test_workflow_builds_with_x86_64_architecture(self):
"""Test that UV builds for x86_64 architecture (default)"""
builder = LambdaBuilder(
language="python",
dependency_manager="uv",
application_framework=None,
)
builder.build(
self.source_dir,
self.artifacts_dir,
self.scratch_dir,
self.manifest_path_numpy,
runtime="python3.12",
architecture="x86_64",
experimental_flags=self.experimental_flags,
)
# Verify numpy was built for x86_64 by checking WHEEL metadata
wheel_path = os.path.join(self.artifacts_dir, "numpy-2.1.2.dist-info", "WHEEL")
self.assertTrue(os.path.exists(wheel_path), "WHEEL metadata file should exist")
with open(wheel_path) as f:
wheel_content = f.read()
self.assertIn("x86_64", wheel_content, "WHEEL should contain x86_64 platform tag")
@skipIf(which("uv") is None, "uv not available")
def test_workflow_builds_numpy_with_pyproject(self):
"""Test that UV can build numpy using pyproject.toml format"""
builder = LambdaBuilder(language="python", dependency_manager="uv", application_framework=None)
# Create a temporary directory with pyproject.toml (UV only recognizes exact name)
temp_source_dir = tempfile.mkdtemp()
try:
# Copy main.py to temp directory
shutil.copy(os.path.join(self.TEST_DATA_FOLDER, "main.py"), temp_source_dir)
shutil.copy(os.path.join(self.TEST_DATA_FOLDER, "__init__.py"), temp_source_dir)
# Copy pyproject-numpy.toml as pyproject.toml
shutil.copy(
os.path.join(self.TEST_DATA_FOLDER, "pyproject-numpy.toml"),
os.path.join(temp_source_dir, "pyproject.toml"),
)
pyproject_path = os.path.join(temp_source_dir, "pyproject.toml")
builder.build(
temp_source_dir,
self.artifacts_dir,
self.scratch_dir,
pyproject_path,
runtime="python3.13",
experimental_flags=self.experimental_flags,
)
# Check that numpy was built successfully
output_files = set(os.listdir(self.artifacts_dir))
expected_numpy_files = {"numpy", "numpy-2.1.2.dist-info", "numpy.libs"}
# Verify numpy files are present
for expected_file in expected_numpy_files:
self.assertIn(expected_file, output_files, f"Expected {expected_file} in build output")
finally:
shutil.rmtree(temp_source_dir)