|
| 1 | +"""Tests for the upload API endpoints.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +UPLOAD_PAYLOAD = { |
| 6 | + "metadata": { |
| 7 | + "commit": { |
| 8 | + "hexsha": "b" * 40, |
| 9 | + "committed_date": "2025-06-16T10:00:00+00:00", |
| 10 | + "message": "Benchmark commit", |
| 11 | + "author": "Benchmark Author", |
| 12 | + }, |
| 13 | + "version": {"major": 3, "minor": 14, "micro": 1}, |
| 14 | + "configure_vars": { |
| 15 | + "CONFIG_ARGS": "'--enable-optimizations' '--prefix=/tmp/install'" |
| 16 | + }, |
| 17 | + }, |
| 18 | + "benchmark_results": [ |
| 19 | + { |
| 20 | + "benchmark_name": "test_bench", |
| 21 | + "stats_json": { |
| 22 | + "metadata": {"peak_memory": 2_000_000}, |
| 23 | + "allocation_size_histogram": [ |
| 24 | + {"min_bytes": 64, "count": 100}, |
| 25 | + {"min_bytes": 128, "count": 50}, |
| 26 | + ], |
| 27 | + "total_bytes_allocated": 3_000_000, |
| 28 | + "top_allocations_by_size": [ |
| 29 | + {"location": "test_func", "count": 10, "size": 100_000} |
| 30 | + ], |
| 31 | + }, |
| 32 | + "flamegraph_html": "<html>test flamegraph</html>", |
| 33 | + } |
| 34 | + ], |
| 35 | + "binary_id": "default", |
| 36 | + "environment_id": "linux-x86_64", |
| 37 | +} |
| 38 | + |
| 39 | + |
| 40 | +@pytest.mark.asyncio |
| 41 | +async def test_upload_requires_auth(client, sample_binary, sample_environment): |
| 42 | + response = await client.post("/api/upload-run", json=UPLOAD_PAYLOAD) |
| 43 | + assert response.status_code in (401, 403) |
| 44 | + |
| 45 | + |
| 46 | +@pytest.mark.asyncio |
| 47 | +async def test_upload_success( |
| 48 | + client, auth_headers, sample_binary, sample_environment |
| 49 | +): |
| 50 | + response = await client.post( |
| 51 | + "/api/upload-run", json=UPLOAD_PAYLOAD, headers=auth_headers |
| 52 | + ) |
| 53 | + assert response.status_code == 200 |
| 54 | + data = response.json() |
| 55 | + assert data["commit_sha"] == "b" * 40 |
| 56 | + assert data["binary_id"] == "default" |
| 57 | + assert data["environment_id"] == "linux-x86_64" |
| 58 | + assert data["results_created"] == 1 |
| 59 | + |
| 60 | + |
| 61 | +@pytest.mark.asyncio |
| 62 | +async def test_upload_missing_commit_sha( |
| 63 | + client, auth_headers, sample_binary, sample_environment |
| 64 | +): |
| 65 | + payload = { |
| 66 | + **UPLOAD_PAYLOAD, |
| 67 | + "metadata": {"commit": {}, "version": {"major": 3, "minor": 14, "micro": 0}}, |
| 68 | + } |
| 69 | + response = await client.post( |
| 70 | + "/api/upload-run", json=payload, headers=auth_headers |
| 71 | + ) |
| 72 | + assert response.status_code == 400 |
| 73 | + |
| 74 | + |
| 75 | +@pytest.mark.asyncio |
| 76 | +async def test_upload_invalid_binary(client, auth_headers, sample_environment): |
| 77 | + payload = {**UPLOAD_PAYLOAD, "binary_id": "nonexistent"} |
| 78 | + response = await client.post( |
| 79 | + "/api/upload-run", json=payload, headers=auth_headers |
| 80 | + ) |
| 81 | + assert response.status_code == 400 |
| 82 | + assert "not found" in response.json()["detail"].lower() |
| 83 | + |
| 84 | + |
| 85 | +@pytest.mark.asyncio |
| 86 | +async def test_upload_invalid_environment(client, auth_headers, sample_binary): |
| 87 | + payload = {**UPLOAD_PAYLOAD, "environment_id": "nonexistent"} |
| 88 | + response = await client.post( |
| 89 | + "/api/upload-run", json=payload, headers=auth_headers |
| 90 | + ) |
| 91 | + assert response.status_code == 400 |
| 92 | + assert "not found" in response.json()["detail"].lower() |
| 93 | + |
| 94 | + |
| 95 | +@pytest.mark.asyncio |
| 96 | +async def test_upload_flag_mismatch( |
| 97 | + client, auth_headers, sample_binary, sample_environment |
| 98 | +): |
| 99 | + payload = { |
| 100 | + **UPLOAD_PAYLOAD, |
| 101 | + "metadata": { |
| 102 | + **UPLOAD_PAYLOAD["metadata"], |
| 103 | + "configure_vars": {"CONFIG_ARGS": "'--with-pydebug'"}, |
| 104 | + }, |
| 105 | + } |
| 106 | + response = await client.post( |
| 107 | + "/api/upload-run", json=payload, headers=auth_headers |
| 108 | + ) |
| 109 | + assert response.status_code == 400 |
| 110 | + assert "configure flags" in response.json()["detail"].lower() |
| 111 | + |
| 112 | + |
| 113 | +@pytest.mark.asyncio |
| 114 | +async def test_report_memray_failure_requires_auth( |
| 115 | + client, sample_binary, sample_environment |
| 116 | +): |
| 117 | + payload = { |
| 118 | + "commit_sha": "c" * 40, |
| 119 | + "commit_timestamp": "2025-06-16T10:00:00", |
| 120 | + "binary_id": "default", |
| 121 | + "environment_id": "linux-x86_64", |
| 122 | + "error_message": "memray install failed", |
| 123 | + } |
| 124 | + response = await client.post("/api/report-memray-failure", json=payload) |
| 125 | + assert response.status_code in (401, 403) |
| 126 | + |
| 127 | + |
| 128 | +@pytest.mark.asyncio |
| 129 | +async def test_report_memray_failure_success( |
| 130 | + client, auth_headers, sample_binary, sample_environment |
| 131 | +): |
| 132 | + payload = { |
| 133 | + "commit_sha": "c" * 40, |
| 134 | + "commit_timestamp": "2025-06-16T10:00:00", |
| 135 | + "binary_id": "default", |
| 136 | + "environment_id": "linux-x86_64", |
| 137 | + "error_message": "memray install failed", |
| 138 | + } |
| 139 | + response = await client.post( |
| 140 | + "/api/report-memray-failure", json=payload, headers=auth_headers |
| 141 | + ) |
| 142 | + assert response.status_code == 200 |
| 143 | + data = response.json() |
| 144 | + assert data["message"] == "Memray failure reported successfully" |
0 commit comments