|
1 | 1 | """Tests for Cargo pull-through caching via the sparse index proxy.""" |
2 | 2 |
|
3 | 3 | import hashlib |
| 4 | +import json |
4 | 5 | from urllib.parse import urljoin |
5 | 6 |
|
6 | 7 | import pytest |
7 | 8 |
|
8 | | -from pulp_rust.tests.functional.utils import download_file |
9 | | - |
10 | | -CRATES_IO_URL = "sparse+https://index.crates.io/" |
| 9 | +from pulp_rust.tests.functional.utils import ( |
| 10 | + CRATES_IO_URL, |
| 11 | + assert_index_entry_matches_upstream, |
| 12 | + download_file, |
| 13 | + get_index_entry, |
| 14 | +) |
11 | 15 |
|
12 | 16 |
|
13 | 17 | def test_pull_through_sparse_index( |
@@ -241,3 +245,157 @@ def test_pull_through_multiple_crates_on_demand( |
241 | 245 |
|
242 | 246 | repository = rust_repo_api_client.read(repository.pulp_href) |
243 | 247 | assert not repository.latest_version_href.endswith("/versions/0/") |
| 248 | + |
| 249 | + |
| 250 | +def test_pull_through_on_demand_preserves_metadata( |
| 251 | + delete_orphans_pre, |
| 252 | + rust_remote_factory, |
| 253 | + rust_repo_factory, |
| 254 | + rust_distribution_factory, |
| 255 | + rust_repo_api_client, |
| 256 | + rust_content_api_client, |
| 257 | + monitor_task, |
| 258 | + cargo_registry_url, |
| 259 | +): |
| 260 | + """on_demand: cached content should have full metadata (deps, features). |
| 261 | +
|
| 262 | + Ensure that all metadata is saved appropriately when a package is created via |
| 263 | + pull-through caching. |
| 264 | + """ |
| 265 | + remote = rust_remote_factory(url=CRATES_IO_URL, policy="on_demand") |
| 266 | + repository = rust_repo_factory(remote=remote.pulp_href) |
| 267 | + distribution = rust_distribution_factory( |
| 268 | + remote=remote.pulp_href, repository=repository.pulp_href |
| 269 | + ) |
| 270 | + |
| 271 | + # serde has dependencies (serde_derive) and features in most versions |
| 272 | + crate_name, crate_version = "serde", "1.0.210" |
| 273 | + unit_path = f"api/v1/crates/{crate_name}/{crate_version}/download" |
| 274 | + pulp_unit_url = urljoin(cargo_registry_url(distribution.base_path), unit_path) |
| 275 | + download_file(pulp_unit_url) |
| 276 | + |
| 277 | + # Verify the content record was created with full metadata |
| 278 | + content_response = rust_content_api_client.list(name=crate_name, vers=crate_version) |
| 279 | + assert content_response.count == 1 |
| 280 | + content = content_response.results[0] |
| 281 | + |
| 282 | + # Should have dependencies (serde has serde_derive as an optional dep) |
| 283 | + assert len(content.dependencies) > 0 |
| 284 | + dep_names = [d.name for d in content.dependencies] |
| 285 | + assert "serde_derive" in dep_names |
| 286 | + |
| 287 | + # Should have features |
| 288 | + assert len(content.features) > 0 |
| 289 | + assert "derive" in content.features |
| 290 | + |
| 291 | + # Add cached content and verify the locally-served index includes deps |
| 292 | + monitor_task( |
| 293 | + rust_repo_api_client.add_cached_content( |
| 294 | + repository.pulp_href, {"remote": remote.pulp_href} |
| 295 | + ).task |
| 296 | + ) |
| 297 | + |
| 298 | + # Fetch the sparse index from Pulp (now served from local data) |
| 299 | + index_url = urljoin(cargo_registry_url(distribution.base_path), f"se/rd/{crate_name}") |
| 300 | + index_response = download_file(index_url) |
| 301 | + assert index_response.response_obj.status == 200 |
| 302 | + |
| 303 | + body = index_response.body.decode("utf-8") |
| 304 | + lines = body.strip().split("\n") |
| 305 | + # Find the line for our version |
| 306 | + version_entry = None |
| 307 | + for line in lines: |
| 308 | + entry = json.loads(line) |
| 309 | + if entry["vers"] == crate_version: |
| 310 | + version_entry = entry |
| 311 | + break |
| 312 | + |
| 313 | + assert version_entry is not None, f"Version {crate_version} not found in index" |
| 314 | + assert len(version_entry["deps"]) > 0, "Index entry has no dependencies" |
| 315 | + index_dep_names = [d["name"] for d in version_entry["deps"]] |
| 316 | + assert "serde_derive" in index_dep_names |
| 317 | + assert len(version_entry["features"]) > 0, "Index entry has no features" |
| 318 | + |
| 319 | + |
| 320 | +# --------------------------------------------------------------------------- |
| 321 | +# Index fidelity tests: compare Pulp output against crates.io for each mode |
| 322 | +# --------------------------------------------------------------------------- |
| 323 | + |
| 324 | + |
| 325 | +def test_index_fidelity_streamed( |
| 326 | + rust_remote_factory, |
| 327 | + rust_repo_factory, |
| 328 | + rust_distribution_factory, |
| 329 | + cargo_registry_url, |
| 330 | + upstream_index_entry, |
| 331 | +): |
| 332 | + """streamed: proxied sparse index entry should match crates.io exactly.""" |
| 333 | + remote = rust_remote_factory(url=CRATES_IO_URL, policy="streamed") |
| 334 | + repository = rust_repo_factory(remote=remote.pulp_href) |
| 335 | + distribution = rust_distribution_factory( |
| 336 | + remote=remote.pulp_href, repository=repository.pulp_href |
| 337 | + ) |
| 338 | + |
| 339 | + base = cargo_registry_url(distribution.base_path) |
| 340 | + pulp_entry = get_index_entry(base, "se/rd/serde", "1.0.210") |
| 341 | + assert_index_entry_matches_upstream(pulp_entry, upstream_index_entry) |
| 342 | + |
| 343 | + |
| 344 | +def test_index_fidelity_on_demand_proxied( |
| 345 | + rust_remote_factory, |
| 346 | + rust_repo_factory, |
| 347 | + rust_distribution_factory, |
| 348 | + cargo_registry_url, |
| 349 | + upstream_index_entry, |
| 350 | +): |
| 351 | + """on_demand: the first fetch (before caching) proxies upstream and should match.""" |
| 352 | + remote = rust_remote_factory(url=CRATES_IO_URL, policy="on_demand") |
| 353 | + repository = rust_repo_factory(remote=remote.pulp_href) |
| 354 | + distribution = rust_distribution_factory( |
| 355 | + remote=remote.pulp_href, repository=repository.pulp_href |
| 356 | + ) |
| 357 | + |
| 358 | + base = cargo_registry_url(distribution.base_path) |
| 359 | + |
| 360 | + # First fetch — proxied from upstream (no local content yet) |
| 361 | + pulp_entry = get_index_entry(base, "se/rd/serde", "1.0.210") |
| 362 | + assert_index_entry_matches_upstream(pulp_entry, upstream_index_entry) |
| 363 | + |
| 364 | + |
| 365 | +def test_index_fidelity_on_demand_cached( |
| 366 | + delete_orphans_pre, |
| 367 | + rust_remote_factory, |
| 368 | + rust_repo_factory, |
| 369 | + rust_distribution_factory, |
| 370 | + rust_repo_api_client, |
| 371 | + rust_content_api_client, |
| 372 | + monitor_task, |
| 373 | + cargo_registry_url, |
| 374 | + upstream_index_entry, |
| 375 | +): |
| 376 | + """on_demand: after caching, the locally-served index entry should match crates.io.""" |
| 377 | + remote = rust_remote_factory(url=CRATES_IO_URL, policy="on_demand") |
| 378 | + repository = rust_repo_factory(remote=remote.pulp_href) |
| 379 | + distribution = rust_distribution_factory( |
| 380 | + remote=remote.pulp_href, repository=repository.pulp_href |
| 381 | + ) |
| 382 | + |
| 383 | + base = cargo_registry_url(distribution.base_path) |
| 384 | + |
| 385 | + # Download the .crate to trigger content creation |
| 386 | + download_file(urljoin(base, "api/v1/crates/serde/1.0.210/download")) |
| 387 | + |
| 388 | + # Verify content was cached |
| 389 | + content = rust_content_api_client.list(name="serde", vers="1.0.210") |
| 390 | + assert content.count == 1, "Content was not cached after on_demand download" |
| 391 | + |
| 392 | + # Promote cached content into the repository |
| 393 | + monitor_task( |
| 394 | + rust_repo_api_client.add_cached_content( |
| 395 | + repository.pulp_href, {"remote": remote.pulp_href} |
| 396 | + ).task |
| 397 | + ) |
| 398 | + |
| 399 | + # Now the index is served from local data — compare against upstream |
| 400 | + pulp_entry = get_index_entry(base, "se/rd/serde", "1.0.210") |
| 401 | + assert_index_entry_matches_upstream(pulp_entry, upstream_index_entry) |
0 commit comments