forked from aws/aws-lambda-builders
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_packager.py
More file actions
318 lines (256 loc) · 14 KB
/
Copy pathtest_packager.py
File metadata and controls
318 lines (256 loc) · 14 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import os
from unittest import TestCase
from unittest.mock import Mock, patch
from aws_lambda_builders.architecture import X86_64
from aws_lambda_builders.workflows.python_uv.exceptions import MissingUvError, UvBuildError, UvInstallationError
from aws_lambda_builders.workflows.python_uv.packager import PythonUvDependencyBuilder, SubprocessUv, UvRunner
from aws_lambda_builders.workflows.python_uv.utils import UvConfig
class TestSubprocessUv(TestCase):
@patch("aws_lambda_builders.workflows.python_uv.packager.OSUtils")
def test_subprocess_uv_init_success(self, mock_osutils_class):
mock_osutils = Mock()
mock_osutils.which.return_value = "/usr/bin/uv"
mock_osutils_class.return_value = mock_osutils
subprocess_uv = SubprocessUv()
self.assertEqual(subprocess_uv.uv_executable, "/usr/bin/uv")
@patch("aws_lambda_builders.workflows.python_uv.packager.OSUtils")
def test_subprocess_uv_init_success_windows_path(self, mock_osutils_class):
"""Test SubprocessUv initialization with Windows-style path."""
mock_osutils = Mock()
mock_osutils.which.return_value = "C:\\Users\\user\\AppData\\Local\\uv\\uv.exe"
mock_osutils_class.return_value = mock_osutils
subprocess_uv = SubprocessUv()
self.assertEqual(subprocess_uv.uv_executable, "C:\\Users\\user\\AppData\\Local\\uv\\uv.exe")
@patch("aws_lambda_builders.workflows.python_uv.packager.OSUtils")
def test_subprocess_uv_init_missing_uv(self, mock_osutils_class):
mock_osutils = Mock()
mock_osutils.which.return_value = None
mock_osutils_class.return_value = mock_osutils
with self.assertRaises(MissingUvError):
SubprocessUv()
@patch("aws_lambda_builders.workflows.python_uv.packager.OSUtils")
def test_run_uv_command_success(self, mock_osutils_class):
mock_osutils = Mock()
mock_osutils.which.return_value = "/usr/bin/uv"
mock_osutils.run_subprocess.return_value = (0, "success", "")
mock_osutils_class.return_value = mock_osutils
subprocess_uv = SubprocessUv()
rc, stdout, stderr = subprocess_uv.run_uv_command(["--version"])
self.assertEqual(rc, 0)
self.assertEqual(stdout, "success")
self.assertEqual(stderr, "")
mock_osutils.run_subprocess.assert_called_once_with(["/usr/bin/uv", "--version"], cwd=None, env=None)
@patch("aws_lambda_builders.workflows.python_uv.packager.OSUtils")
def test_run_uv_command_success_windows_path(self, mock_osutils_class):
"""Test run_uv_command with Windows-style path."""
mock_osutils = Mock()
windows_uv_path = "C:\\Users\\user\\AppData\\Local\\uv\\uv.exe"
mock_osutils.which.return_value = windows_uv_path
mock_osutils.run_subprocess.return_value = (0, "success", "")
mock_osutils_class.return_value = mock_osutils
subprocess_uv = SubprocessUv()
rc, stdout, stderr = subprocess_uv.run_uv_command(["--version"])
self.assertEqual(rc, 0)
mock_osutils.run_subprocess.assert_called_once_with([windows_uv_path, "--version"], cwd=None, env=None)
@patch("aws_lambda_builders.workflows.python_uv.packager.OSUtils")
def test_get_uv_version_success(self, mock_osutils_class):
mock_osutils = Mock()
mock_osutils.which.return_value = "/usr/bin/uv"
mock_osutils.run_subprocess.return_value = (0, "uv 0.6.16", "")
mock_osutils_class.return_value = mock_osutils
subprocess_uv = SubprocessUv()
version = subprocess_uv.get_uv_version()
self.assertEqual(version, "0.6.16")
@patch("aws_lambda_builders.workflows.python_uv.packager.OSUtils")
def test_get_uv_version_failure(self, mock_osutils_class):
mock_osutils = Mock()
mock_osutils.which.return_value = "/usr/bin/uv"
mock_osutils.run_subprocess.return_value = (1, "", "error")
mock_osutils_class.return_value = mock_osutils
subprocess_uv = SubprocessUv()
version = subprocess_uv.get_uv_version()
self.assertIsNone(version)
class TestUvRunner(TestCase):
def setUp(self):
self.mock_subprocess_uv = Mock()
self.mock_subprocess_uv.uv_executable = "/usr/bin/uv"
self.mock_osutils = Mock()
self.uv_runner = UvRunner(uv_subprocess=self.mock_subprocess_uv, osutils=self.mock_osutils)
def test_uv_version_property(self):
self.mock_subprocess_uv.get_uv_version.return_value = "0.6.16"
version = self.uv_runner.uv_version
self.assertEqual(version, "0.6.16")
self.mock_subprocess_uv.get_uv_version.assert_called_once()
def test_install_requirements_success(self):
self.mock_subprocess_uv.run_uv_command.return_value = (0, "success", "")
self.uv_runner.install_requirements(
requirements_path="/path/to/requirements.txt",
target_dir="/target",
scratch_dir="/scratch",
python_version="3.9",
platform="linux",
architecture=X86_64,
)
# Verify UV pip install command was called
args_called = self.mock_subprocess_uv.run_uv_command.call_args[0][0]
self.assertIn("pip", args_called)
self.assertIn("install", args_called)
self.assertIn("-r", args_called)
self.assertIn("/path/to/requirements.txt", args_called)
def test_install_requirements_failure(self):
self.mock_subprocess_uv.run_uv_command.return_value = (1, "", "error message")
with self.assertRaises(UvInstallationError):
self.uv_runner.install_requirements(
requirements_path="/path/to/requirements.txt", target_dir="/target", scratch_dir="/scratch"
)
class TestPythonUvDependencyBuilder(TestCase):
def setUp(self):
self.mock_osutils = Mock()
self.mock_uv_runner = Mock()
self.builder = PythonUvDependencyBuilder(
osutils=self.mock_osutils, runtime="python3.9", uv_runner=self.mock_uv_runner
)
def test_extract_python_version(self):
self.assertEqual(self.builder._extract_python_version("python3.9"), "3.9")
self.assertEqual(self.builder._extract_python_version("python3.11"), "3.11")
self.assertEqual(self.builder._extract_python_version("3.10"), "3.10")
# Runtime is required - should raise error when None
with self.assertRaises(UvBuildError) as context:
self.builder._extract_python_version(None)
self.assertIn("Runtime is required", str(context.exception))
# Empty string should also raise error
with self.assertRaises(UvBuildError) as context:
self.builder._extract_python_version("")
self.assertIn("Runtime is required", str(context.exception))
def test_build_from_lock_file(self):
# Mock the uv command for export
self.mock_uv_runner._uv.run_uv_command.return_value = (0, b"", b"")
self.builder._build_from_lock_file(
lock_path="/path/to/uv.lock",
target_dir="/target",
scratch_dir="/scratch",
python_version="3.9",
architecture=X86_64,
config=UvConfig(),
)
# Should call export then install_requirements
self.mock_uv_runner._uv.run_uv_command.assert_called_once()
self.mock_uv_runner.install_requirements.assert_called_once()
def test_build_from_requirements(self):
self.builder._build_from_requirements(
requirements_path="/path/to/requirements.txt",
target_dir="/target",
scratch_dir="/scratch",
python_version="3.9",
architecture=X86_64,
config=UvConfig(),
)
self.mock_uv_runner.install_requirements.assert_called_once()
def test_build_dependencies_with_requirements_txt(self):
with patch("os.path.basename", return_value="requirements.txt"):
self.builder.build_dependencies(
artifacts_dir_path="/artifacts",
scratch_dir_path="/scratch",
manifest_path="/path/to/requirements.txt",
architecture=X86_64,
)
self.mock_uv_runner.install_requirements.assert_called_once()
def test_build_dependencies_with_uv_lock_standalone_fails(self):
"""Test that uv.lock as standalone manifest fails (requires pyproject.toml)."""
with patch("os.path.basename", return_value="uv.lock"):
with self.assertRaises(UvBuildError) as context:
self.builder.build_dependencies(
artifacts_dir_path="/artifacts",
scratch_dir_path="/scratch",
manifest_path="/path/to/uv.lock",
architecture=X86_64,
)
self.assertIn("Unsupported manifest file: uv.lock", str(context.exception))
# Should not call any UV operations for unsupported manifest
self.mock_uv_runner.sync_dependencies.assert_not_called()
self.mock_uv_runner.install_requirements.assert_not_called()
def test_build_dependencies_pyproject_with_uv_lock(self):
"""Test that pyproject.toml with uv.lock present uses lock-based build."""
# Mock the uv export command
self.mock_uv_runner._uv.run_uv_command.return_value = (0, b"", b"")
with patch("os.path.basename", return_value="pyproject.toml"), patch(
"os.path.dirname", return_value=os.path.join("path", "to")
), patch("os.path.exists") as mock_exists:
# Mock that uv.lock exists alongside pyproject.toml
mock_exists.return_value = True
self.builder.build_dependencies(
artifacts_dir_path="/artifacts",
scratch_dir_path="/scratch",
manifest_path="/path/to/pyproject.toml",
architecture=X86_64,
)
# Should use export + install_requirements (for cross-platform support)
self.mock_uv_runner._uv.run_uv_command.assert_called_once() # export
self.mock_uv_runner.install_requirements.assert_called_once()
# Verify it checked for uv.lock in the right location
mock_exists.assert_called_with(os.path.join("path", "to", "uv.lock"))
def test_build_dependencies_pyproject_without_uv_lock(self):
"""Test that pyproject.toml without uv.lock uses standard pyproject build."""
with patch("os.path.basename", return_value="pyproject.toml"), patch(
"os.path.dirname", return_value=os.path.join("path", "to")
), patch("os.path.exists") as mock_exists:
# Mock that uv.lock does NOT exist alongside pyproject.toml
mock_exists.return_value = False
self.mock_uv_runner._uv.run_uv_command.return_value = (0, b"", b"")
self.builder.build_dependencies(
artifacts_dir_path="/artifacts",
scratch_dir_path="/scratch",
manifest_path="/path/to/pyproject.toml",
architecture=X86_64,
)
# Should use install_requirements (standard pyproject build)
self.mock_uv_runner.install_requirements.assert_called_once()
self.mock_uv_runner.sync_dependencies.assert_not_called()
# Verify it checked for uv.lock in the right location
mock_exists.assert_called_with(os.path.join("path", "to", "uv.lock"))
def test_build_dependencies_passes_scratch_dir(self):
"""Test that build_dependencies passes scratch_dir to UvRunner for cache configuration."""
with patch("os.path.basename", return_value="requirements.txt"):
self.builder.build_dependencies(
artifacts_dir_path="/artifacts",
scratch_dir_path="/scratch",
manifest_path="/path/to/requirements.txt",
architecture=X86_64,
)
# Verify that install_requirements was called with scratch_dir
# UvRunner._ensure_cache_dir() will use this to configure the cache
call_args = self.mock_uv_runner.install_requirements.call_args
self.assertEqual(call_args[1]["scratch_dir"], "/scratch")
def test_build_dependencies_respects_existing_cache_dir(self):
"""Test that existing cache_dir in config is respected."""
config = UvConfig(cache_dir="/custom/cache")
with patch("os.path.basename", return_value="requirements.txt"):
self.builder.build_dependencies(
artifacts_dir_path="/artifacts",
scratch_dir_path="/scratch",
manifest_path="/path/to/requirements.txt",
architecture=X86_64,
config=config,
)
# Verify that the custom cache directory is preserved
call_args = self.mock_uv_runner.install_requirements.call_args
passed_config = call_args[1]["config"]
self.assertEqual(passed_config.cache_dir, "/custom/cache")
def test_is_requirements_file_standard_names(self):
# Test standard requirements file name
self.assertTrue(self.builder._is_requirements_file("requirements.txt"))
def test_is_requirements_file_environment_specific(self):
# Test environment-specific requirements files
self.assertTrue(self.builder._is_requirements_file("requirements-dev.txt"))
self.assertTrue(self.builder._is_requirements_file("requirements-test.txt"))
self.assertTrue(self.builder._is_requirements_file("requirements-prod.txt"))
self.assertTrue(self.builder._is_requirements_file("requirements-staging.txt"))
def test_is_requirements_file_invalid_names(self):
# Test invalid requirements file names
self.assertFalse(self.builder._is_requirements_file("requirements"))
self.assertFalse(self.builder._is_requirements_file("requirements.in"))
self.assertFalse(self.builder._is_requirements_file("requirements.py"))
self.assertFalse(self.builder._is_requirements_file("my-requirements.txt"))
self.assertFalse(self.builder._is_requirements_file("requirements.txt.bak"))
self.assertFalse(self.builder._is_requirements_file("requirements-"))
self.assertFalse(self.builder._is_requirements_file("requirements-.txt"))