This commit is contained in:
Simone Scarduzio
2025-10-03 21:41:26 +02:00
parent 07f630d855
commit 8cea5a3527

View File

@@ -15,10 +15,19 @@ from deltaglider.app.cli.main import cli
def extract_json_from_cli_output(output: str) -> dict:
"""Extract JSON from CLI output that may contain log messages."""
lines = output.split("\n")
json_start = next(i for i, line in enumerate(lines) if line.strip().startswith("{"))
json_end = next(i for i in range(json_start, len(lines)) if lines[i].strip() == "}") + 1
json_text = "\n".join(lines[json_start:json_end])
return json.loads(json_text)
for i, line in enumerate(lines):
if line.strip().startswith("{"):
json_start = i
json_end = (
next(
(j for j in range(json_start, len(lines)) if lines[j].strip() == "}"),
len(lines) - 1,
)
+ 1
)
json_text = "\n".join(lines[json_start:json_end])
return json.loads(json_text)
raise ValueError("No JSON found in CLI output")
@pytest.mark.e2e
@@ -74,23 +83,25 @@ class TestLocalStackE2E:
# Upload first file (becomes reference)
result = runner.invoke(cli, ["cp", str(file1), f"s3://{test_bucket}/plugins/"])
assert result.exit_code == 0
output1 = extract_json_from_cli_output(result.output)
assert output1["operation"] == "create_reference"
assert output1["key"] == "plugins/reference.bin"
assert "reference" in result.output.lower() or "upload:" in result.output
# Verify reference was created
objects = s3_client.list_objects_v2(Bucket=test_bucket, Prefix="plugins/")
# Verify reference was created (deltaspace is root, files are at root level)
objects = s3_client.list_objects_v2(Bucket=test_bucket)
assert "Contents" in objects
keys = [obj["Key"] for obj in objects["Contents"]]
assert "plugins/reference.bin" in keys
assert "plugins/plugin-v1.0.0.zip.delta" in keys
# Files are stored at root level: reference.bin and plugin-v1.0.0.zip.delta
assert "reference.bin" in keys
assert "plugin-v1.0.0.zip.delta" in keys
# Upload second file (creates delta)
result = runner.invoke(cli, ["cp", str(file2), f"s3://{test_bucket}/plugins/"])
assert result.exit_code == 0
output2 = extract_json_from_cli_output(result.output)
assert output2["operation"] == "create_delta"
assert output2["key"] == "plugins/plugin-v1.0.1.zip.delta"
assert "delta_ratio" in output2
assert "upload:" in result.output
# Verify delta was created
objects = s3_client.list_objects_v2(Bucket=test_bucket)
keys = [obj["Key"] for obj in objects["Contents"]]
assert "plugin-v1.0.1.zip.delta" in keys
# Download and verify second file
output_file = tmpdir / "downloaded.zip"
@@ -98,7 +109,7 @@ class TestLocalStackE2E:
cli,
[
"cp",
f"s3://{test_bucket}/plugins/plugin-v1.0.1.zip.delta",
f"s3://{test_bucket}/plugin-v1.0.1.zip.delta",
str(output_file),
],
)
@@ -108,7 +119,7 @@ class TestLocalStackE2E:
# Verify integrity
result = runner.invoke(
cli,
["verify", f"s3://{test_bucket}/plugins/plugin-v1.0.1.zip.delta"],
["verify", f"s3://{test_bucket}/plugin-v1.0.1.zip.delta"],
)
assert result.exit_code == 0
verify_output = extract_json_from_cli_output(result.output)
@@ -128,21 +139,23 @@ class TestLocalStackE2E:
file_b1 = tmpdir / "app-b-v1.zip"
file_b1.write_text("Application B version 1")
# Upload to different deltaspaces
# Upload to different deltaspaces (each subdirectory is its own deltaspace)
result = runner.invoke(cli, ["cp", str(file_a1), f"s3://{test_bucket}/apps/app-a/"])
assert result.exit_code == 0
result = runner.invoke(cli, ["cp", str(file_b1), f"s3://{test_bucket}/apps/app-b/"])
assert result.exit_code == 0
# Verify each deltaspace has its own reference
objects_a = s3_client.list_objects_v2(Bucket=test_bucket, Prefix="apps/app-a/")
keys_a = [obj["Key"] for obj in objects_a["Contents"]]
assert "apps/app-a/reference.bin" in keys_a
objects_b = s3_client.list_objects_v2(Bucket=test_bucket, Prefix="apps/app-b/")
keys_b = [obj["Key"] for obj in objects_b["Contents"]]
assert "apps/app-b/reference.bin" in keys_b
# Verify each deltaspace has its own reference (different deltaspaces)
objects = s3_client.list_objects_v2(Bucket=test_bucket, Prefix="apps/")
assert "Contents" in objects
keys = [obj["Key"] for obj in objects["Contents"]]
# Should have: apps/app-a/reference.bin, apps/app-a/app-a-v1.zip.delta,
# apps/app-b/reference.bin, apps/app-b/app-b-v1.zip.delta
assert "apps/app-a/reference.bin" in keys
assert "apps/app-a/app-a-v1.zip.delta" in keys
assert "apps/app-b/reference.bin" in keys
assert "apps/app-b/app-b-v1.zip.delta" in keys
def test_large_delta_warning(self, test_bucket, s3_client):
"""Test delta compression with different content."""
@@ -174,9 +187,11 @@ class TestLocalStackE2E:
], # Very low threshold
)
assert result.exit_code == 0
# Even with completely different content, xdelta3 is efficient
output = extract_json_from_cli_output(result.output)
assert output["operation"] == "create_delta"
# Delta ratio should be small even for different files (xdelta3 is very efficient)
assert "delta_ratio" in output
assert output["delta_ratio"] > 0.01 # Should exceed the very low threshold we set
# Should still upload successfully even though delta exceeds threshold
assert "upload:" in result.output
# Verify delta was created
objects = s3_client.list_objects_v2(Bucket=test_bucket)
assert "Contents" in objects
keys = [obj["Key"] for obj in objects["Contents"]]
assert "file2.zip.delta" in keys