test_archive_copy.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. """
  2. Tests for the 3MF archive copy path.
  3. Regression guards for #1032 where large 3MF files were silently truncated
  4. during archiving on Raspberry Pi OS / armv7l, leaving the archive row in
  5. place but the on-disk file no longer a valid ZIP.
  6. """
  7. import io
  8. import logging
  9. import os
  10. import zipfile
  11. from pathlib import Path
  12. import pytest
  13. from backend.app.services.archive import ThreeMFParser, _copy_and_fsync
  14. def _make_3mf(path: Path, payload_size: int = 0) -> None:
  15. """Write a minimal valid 3MF (ZIP) file with an optional large payload."""
  16. with zipfile.ZipFile(path, "w", zipfile.ZIP_DEFLATED) as zf:
  17. zf.writestr("Metadata/slice_info.config", "<config/>")
  18. if payload_size:
  19. # Uncompressible payload forces real bytes on disk.
  20. zf.writestr("blob.bin", os.urandom(payload_size))
  21. class TestCopyAndFsync:
  22. def test_copies_small_file_byte_for_byte(self, tmp_path: Path) -> None:
  23. src = tmp_path / "src.bin"
  24. dst = tmp_path / "dst.bin"
  25. src.write_bytes(b"hello world")
  26. _copy_and_fsync(src, dst)
  27. assert dst.read_bytes() == b"hello world"
  28. def test_copies_large_file_byte_for_byte(self, tmp_path: Path) -> None:
  29. """Spans multiple 1 MiB chunks to exercise the copy loop."""
  30. src = tmp_path / "src.bin"
  31. dst = tmp_path / "dst.bin"
  32. payload = os.urandom(5 * 1024 * 1024 + 123) # 5 MiB + change
  33. src.write_bytes(payload)
  34. _copy_and_fsync(src, dst)
  35. assert dst.stat().st_size == len(payload)
  36. assert dst.read_bytes() == payload
  37. def test_preserves_mtime_via_copystat(self, tmp_path: Path) -> None:
  38. src = tmp_path / "src.bin"
  39. dst = tmp_path / "dst.bin"
  40. src.write_bytes(b"x")
  41. os.utime(src, (1_700_000_000, 1_700_000_000))
  42. _copy_and_fsync(src, dst)
  43. assert int(dst.stat().st_mtime) == 1_700_000_000
  44. def test_overwrites_existing_destination(self, tmp_path: Path) -> None:
  45. src = tmp_path / "src.bin"
  46. dst = tmp_path / "dst.bin"
  47. src.write_bytes(b"new")
  48. dst.write_bytes(b"old old old")
  49. _copy_and_fsync(src, dst)
  50. assert dst.read_bytes() == b"new"
  51. def test_produces_valid_zip_on_3mf(self, tmp_path: Path) -> None:
  52. """The whole point of #1032: copy of a valid 3MF stays a valid ZIP."""
  53. src = tmp_path / "src.3mf"
  54. dst = tmp_path / "dst.3mf"
  55. _make_3mf(src, payload_size=2 * 1024 * 1024) # 2 MiB, multi-chunk
  56. assert zipfile.is_zipfile(src)
  57. _copy_and_fsync(src, dst)
  58. assert zipfile.is_zipfile(dst)
  59. class TestThreeMFParserErrorVisibility:
  60. def test_parse_logs_warning_on_corrupted_zip(
  61. self,
  62. tmp_path: Path,
  63. caplog: pytest.LogCaptureFixture,
  64. ) -> None:
  65. """Silent `except Exception: pass` was how #1032 escaped detection;
  66. parse() must now surface the failure at WARNING."""
  67. corrupted = tmp_path / "bad.3mf"
  68. corrupted.write_bytes(b"not a zip")
  69. with caplog.at_level(logging.WARNING, logger="backend.app.services.archive"):
  70. result = ThreeMFParser(corrupted).parse()
  71. assert result == {}
  72. assert any("failed to parse" in rec.message and str(corrupted) in rec.message for rec in caplog.records), (
  73. "Expected a WARNING mentioning the failed parse and file path"
  74. )
  75. def test_parse_returns_partial_metadata_without_raising(
  76. self,
  77. tmp_path: Path,
  78. ) -> None:
  79. """A valid-but-minimal 3MF must still parse without raising."""
  80. p = tmp_path / "ok.3mf"
  81. with zipfile.ZipFile(p, "w") as zf:
  82. zf.writestr("Metadata/slice_info.config", "<config/>")
  83. result = ThreeMFParser(p).parse()
  84. # No assertions about which keys are present — just that it didn't blow up.
  85. assert isinstance(result, dict)
  86. class TestZipFileSentinel:
  87. """Sanity check the sentinel the archive pipeline relies on."""
  88. def test_is_zipfile_on_truncated_zip_returns_false(self, tmp_path: Path) -> None:
  89. """Truncating a valid ZIP mid-stream must flip is_zipfile() to False.
  90. This is the exact post-condition archive_print now trusts."""
  91. src = tmp_path / "src.3mf"
  92. _make_3mf(src, payload_size=1024 * 1024)
  93. full = src.read_bytes()
  94. assert zipfile.is_zipfile(io.BytesIO(full))
  95. truncated = tmp_path / "truncated.3mf"
  96. # Strip the trailing end-of-central-directory record — exactly what a
  97. # short sendfile return would leave behind.
  98. truncated.write_bytes(full[: len(full) // 2])
  99. assert not zipfile.is_zipfile(truncated)