|
16 | 16 |
|
17 | 17 | import pytest |
18 | 18 | import os |
| 19 | +import tarfile |
19 | 20 | import tempfile |
20 | 21 | from unittest.mock import Mock, patch |
21 | 22 |
|
@@ -318,6 +319,114 @@ def test_inject_repack_script_local_source_dir(self, mock_session, temp_entry_po |
318 | 319 | assert "#!/bin/bash" in content |
319 | 320 | assert "python _repack_model.py" in content |
320 | 321 |
|
| 322 | + # Regression for issue #3762: the launcher is a bash script that runs |
| 323 | + # in a Linux container, so it must use LF endings. NOTE: this raw-bytes |
| 324 | + # check only actually catches the bug on a Windows host (on Linux/mac the |
| 325 | + # old code already emitted LF); the host-independent guard is |
| 326 | + # test_inject_repack_launcher_opened_with_lf_newline below. |
| 327 | + with open(launcher_path, "rb") as f: |
| 328 | + raw = f.read() |
| 329 | + assert b"\r\n" not in raw |
| 330 | + |
| 331 | + def test_inject_repack_launcher_opened_with_lf_newline(self, mock_session, temp_entry_point): |
| 332 | + """Regression for #3762: launcher must be opened with newline="\\n". |
| 333 | +
|
| 334 | + On Windows, text-mode ``open(..., "w")`` translates ``\\n`` to ``\\r\\n``, which |
| 335 | + corrupts the bash launcher when it later runs in the Linux repack container. This |
| 336 | + asserts the fix (``newline="\\n"``) is in place independently of the test host OS. |
| 337 | + """ |
| 338 | + real_open = open |
| 339 | + launcher_open_calls = [] |
| 340 | + |
| 341 | + def tracking_open(file, mode="r", *args, **kwargs): |
| 342 | + if str(file).endswith(REPACK_SCRIPT_LAUNCHER) and "w" in mode: |
| 343 | + launcher_open_calls.append(kwargs.get("newline")) |
| 344 | + return real_open(file, mode, *args, **kwargs) |
| 345 | + |
| 346 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 347 | + with patch("sagemaker.mlops.workflow._utils.image_uris.retrieve") as mock_retrieve: |
| 348 | + with patch("sagemaker.train.ModelTrainer") as mock_trainer: |
| 349 | + with patch( |
| 350 | + "sagemaker.mlops.workflow._utils.TrainingStep.__init__" |
| 351 | + ) as mock_super: |
| 352 | + with patch("builtins.open", side_effect=tracking_open): |
| 353 | + mock_retrieve.return_value = "sklearn-image:latest" |
| 354 | + mock_trainer_instance = Mock() |
| 355 | + mock_trainer_instance.train = Mock(return_value=Mock()) |
| 356 | + mock_trainer.return_value = mock_trainer_instance |
| 357 | + mock_super.return_value = None |
| 358 | + |
| 359 | + _RepackModelStep( |
| 360 | + name="repack-step", |
| 361 | + sagemaker_session=mock_session, |
| 362 | + role="arn:aws:iam::123456789012:role/SageMakerRole", |
| 363 | + model_data="s3://bucket/model.tar.gz", |
| 364 | + entry_point=temp_entry_point, |
| 365 | + source_dir=temp_dir, |
| 366 | + ) |
| 367 | + |
| 368 | + assert launcher_open_calls, "launcher file was never opened for writing" |
| 369 | + assert all(nl == "\n" for nl in launcher_open_calls), ( |
| 370 | + "launcher must be opened with newline='\\n' to keep LF endings, " |
| 371 | + f"got {launcher_open_calls}" |
| 372 | + ) |
| 373 | + |
| 374 | + def test_inject_repack_launcher_opened_with_lf_newline_s3_source_dir( |
| 375 | + self, mock_session, temp_entry_point |
| 376 | + ): |
| 377 | + """Regression for #3762 on the S3 source_dir branch. |
| 378 | +
|
| 379 | + The S3 path downloads, extracts, and rewrites the source tarball; the launcher it |
| 380 | + writes into the extracted dir must also use ``newline="\\n"``. Mirrors |
| 381 | + ``test_inject_repack_launcher_opened_with_lf_newline`` for the other code branch. |
| 382 | + """ |
| 383 | + real_open = open |
| 384 | + launcher_open_calls = [] |
| 385 | + |
| 386 | + def tracking_open(file, mode="r", *args, **kwargs): |
| 387 | + if str(file).endswith(REPACK_SCRIPT_LAUNCHER) and "w" in mode: |
| 388 | + launcher_open_calls.append(kwargs.get("newline")) |
| 389 | + return real_open(file, mode, *args, **kwargs) |
| 390 | + |
| 391 | + def fake_download(source_dir, dest_path, session): |
| 392 | + # Write a minimal valid .tar.gz so the real tarfile extraction succeeds. |
| 393 | + with tempfile.TemporaryDirectory() as seed: |
| 394 | + seed_file = os.path.join(seed, "dummy.txt") |
| 395 | + with real_open(seed_file, "w") as fh: |
| 396 | + fh.write("seed") |
| 397 | + with tarfile.open(dest_path, "w:gz") as t: |
| 398 | + t.add(seed_file, arcname="dummy.txt") |
| 399 | + |
| 400 | + with patch("sagemaker.mlops.workflow._utils.image_uris.retrieve") as mock_retrieve: |
| 401 | + with patch("sagemaker.train.ModelTrainer") as mock_trainer: |
| 402 | + with patch("sagemaker.mlops.workflow._utils.TrainingStep.__init__") as mock_super: |
| 403 | + with patch( |
| 404 | + "sagemaker.mlops.workflow._utils.download_file_from_url", |
| 405 | + side_effect=fake_download, |
| 406 | + ): |
| 407 | + with patch("sagemaker.mlops.workflow._utils._save_model"): |
| 408 | + with patch("builtins.open", side_effect=tracking_open): |
| 409 | + mock_retrieve.return_value = "sklearn-image:latest" |
| 410 | + mock_trainer_instance = Mock() |
| 411 | + mock_trainer_instance.train = Mock(return_value=Mock()) |
| 412 | + mock_trainer.return_value = mock_trainer_instance |
| 413 | + mock_super.return_value = None |
| 414 | + |
| 415 | + _RepackModelStep( |
| 416 | + name="repack-step", |
| 417 | + sagemaker_session=mock_session, |
| 418 | + role="arn:aws:iam::123456789012:role/SageMakerRole", |
| 419 | + model_data="s3://bucket/model.tar.gz", |
| 420 | + entry_point=temp_entry_point, |
| 421 | + source_dir="s3://bucket/source.tar.gz", |
| 422 | + ) |
| 423 | + |
| 424 | + assert launcher_open_calls, "launcher file was never opened for writing (S3 branch)" |
| 425 | + assert all(nl == "\n" for nl in launcher_open_calls), ( |
| 426 | + "launcher must be opened with newline='\\n' to keep LF endings, " |
| 427 | + f"got {launcher_open_calls}" |
| 428 | + ) |
| 429 | + |
321 | 430 | def test_properties_returns_parent_properties(self, mock_session, temp_entry_point): |
322 | 431 | """Test properties returns parent class properties.""" |
323 | 432 | with patch("sagemaker.mlops.workflow._utils.image_uris.retrieve") as mock_retrieve: |
|
0 commit comments