-
Notifications
You must be signed in to change notification settings - Fork 35
fix: auto-detect image compression in StorageMux flashing #974
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,9 @@ | ||
| import bz2 | ||
| import gzip | ||
| import hashlib | ||
| import lzma | ||
| import os | ||
| import sys | ||
| from contextlib import contextmanager | ||
| from http.server import BaseHTTPRequestHandler, HTTPServer | ||
| from pathlib import Path | ||
|
|
@@ -11,6 +15,11 @@ | |
| import pytest | ||
| from opendal import Operator | ||
|
|
||
| if sys.version_info >= (3, 14): | ||
| from compression import zstd | ||
| else: | ||
|
Comment on lines
17
to
+20
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. import here but is not declared as a dev dependency in this package's own pyproject.toml |
||
| from backports import zstd | ||
|
|
||
| from .common import PresignedRequest | ||
| from .driver import MockFlasher, MockStorageMux, MockStorageMuxFlasher, Opendal | ||
| from jumpstarter.client.core import DriverError | ||
|
|
@@ -177,6 +186,58 @@ def test_driver_mock_storage_mux_flasher(tmp_path): | |
| assert (tmp_path / "dump.img").read_bytes() == b"hello" | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "compress", | ||
| [gzip.compress, lambda data: lzma.compress(data, format=lzma.FORMAT_XZ), bz2.compress, zstd.compress], | ||
| ids=["gzip", "xz", "bz2", "zstd"], | ||
| ) | ||
| def test_driver_mock_storage_mux_flasher_auto_decompress(tmp_path, compress): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are no tests for the explicit |
||
| original = b"hello compressed world" * 1024 | ||
| with serve(MockStorageMuxFlasher()) as flasher: | ||
| (tmp_path / "disk.img").write_bytes(compress(original)) | ||
|
|
||
| flasher.flash(tmp_path / "disk.img") | ||
| flasher.dump(tmp_path / "dump.img") | ||
|
|
||
| assert (tmp_path / "dump.img").read_bytes() == original | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
|
|
||
| def test_driver_mock_storage_mux_flasher_http_auto_decompress(tmp_path): | ||
| """Flashing a compressed image from a direct HTTP URL must auto-decompress (issue #54).""" | ||
| original = b"hello compressed world" * 1024 | ||
| compressed = lzma.compress(original, format=lzma.FORMAT_XZ) | ||
|
|
||
| class CompressedHandler(BaseHTTPRequestHandler): | ||
| def do_HEAD(self): | ||
| self.send_response(200) | ||
| self.send_header("content-length", str(len(compressed))) | ||
| self.end_headers() | ||
|
|
||
| def do_GET(self): | ||
| self.send_response(200) | ||
| self.send_header("content-length", str(len(compressed))) | ||
| self.end_headers() | ||
| self.wfile.write(compressed) | ||
|
|
||
| def log_message(self, format, *args): | ||
| pass | ||
|
|
||
| with serve(MockStorageMuxFlasher()) as flasher: | ||
| server = HTTPServer(("127.0.0.1", 0), CompressedHandler) | ||
| port = server.server_address[1] | ||
| server_thread = Thread(target=server.serve_forever) | ||
| server_thread.daemon = True | ||
| server_thread.start() | ||
| try: | ||
| flasher.flash(f"http://127.0.0.1:{port}/image.raw.xz") | ||
| flasher.dump(tmp_path / "dump.img") | ||
|
|
||
| assert (tmp_path / "dump.img").read_bytes() == original | ||
| finally: | ||
| server.shutdown() | ||
|
mmahut marked this conversation as resolved.
|
||
| server.server_close() | ||
|
|
||
|
|
||
| def test_drivers_mock_storage_mux_fs(monkeypatch: pytest.MonkeyPatch): | ||
| with serve(MockStorageMux()) as client: | ||
| with TemporaryDirectory() as tempdir: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import bz2 | ||
| import gzip | ||
| import lzma | ||
| import sys | ||
|
|
||
| import pytest | ||
|
|
||
| if sys.version_info >= (3, 14): | ||
| from compression import zstd | ||
| else: | ||
| from backports import zstd | ||
|
|
||
| from .storage import write_to_storage_device | ||
|
|
||
| pytestmark = pytest.mark.anyio | ||
|
|
||
|
|
||
| async def _chunks(data: bytes, chunk_size: int = 16): | ||
| for i in range(0, len(data), chunk_size): | ||
| yield data[i : i + chunk_size] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "compress", | ||
| [ | ||
| lambda data: data, | ||
| gzip.compress, | ||
| lambda data: lzma.compress(data, format=lzma.FORMAT_XZ), | ||
| bz2.compress, | ||
| zstd.compress, | ||
| ], | ||
| ids=["raw", "gzip", "xz", "bz2", "zstd"], | ||
| ) | ||
| async def test_write_to_storage_device_auto_decompress(tmp_path, compress): | ||
| original = b"jumpstarter" * 1024 | ||
| device = tmp_path / "device" | ||
| # simulate a present storage device: wait_for_storage_device requires a nonzero size | ||
| device.write_bytes(b"\x00" * len(original)) | ||
|
|
||
| await write_to_storage_device(device, _chunks(compress(original)), leeway=0) | ||
|
|
||
| assert device.read_bytes() == original |
Uh oh!
There was an error while loading. Please reload this page.