Files
crank/crates/crank-registry/tests/integration/import_plan/cleanup.rs
T
bsodfather e93de7015a
CI / Rust Checks (pull_request) Failing after 1s
CI / UI Checks (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Deploy (pull_request) Skipped
feat(import): complete OpenAPI onboarding and verify delivery
2026-10-09 17:50:38 +03:00

290 lines
9.2 KiB
Rust

use super::super::common::TestDatabase;
use super::{create_pending_openapi_job, create_workspace};
use crank_artifacts::ArtifactRef;
use crank_core::WorkspaceId;
use crank_registry::{
ArtifactSourceId, CreateImportJobRequest, ImportJobId, ImportJobKind, ImportJobSourceEnvelope,
ImportJobStatus,
};
use serde_json::json;
use time::{Duration, OffsetDateTime};
async fn insert_active_source(
raw: &sqlx::PgPool,
workspace_id: &WorkspaceId,
source_id: &str,
blob_digest: &str,
created_at: OffsetDateTime,
) {
sqlx::query(
"insert into artifact_blobs (digest, artifact_ref, size_bytes, created_at, updated_at)
values ($1, 'sha256:' || $1, 16, $2, $2)
on conflict (digest) do nothing",
)
.bind(blob_digest)
.bind(created_at)
.execute(raw)
.await
.unwrap();
sqlx::query(
"insert into artifact_sources (
workspace_id, source_id, blob_digest, mime_type, sensitivity, lifecycle,
created_at, updated_at
) values ($1, $2, $3, 'application/yaml', 'public', 'active', $4, $4)",
)
.bind(workspace_id.as_str())
.bind(source_id)
.bind(blob_digest)
.bind(created_at)
.execute(raw)
.await
.unwrap();
}
async fn source_lifecycle(
raw: &sqlx::PgPool,
workspace_id: &WorkspaceId,
source_id: &str,
) -> String {
sqlx::query_scalar(
"select lifecycle from artifact_sources where workspace_id = $1 and source_id = $2",
)
.bind(workspace_id.as_str())
.bind(source_id)
.fetch_one(raw)
.await
.unwrap()
}
#[tokio::test]
async fn cleanup_ignores_non_array_dependencies_and_preserves_referenced_source() {
let database = TestDatabase::new().await;
let registry = database.registry().await;
let raw = database.raw_pool().await;
let now = OffsetDateTime::now_utc();
let ws = create_workspace(&registry, "ws_cleanup_object_deps").await;
// A non-expired job whose live primary source is old enough to be a
// dangling candidate, plus an unreferenced peer so the dangling sweep must
// evaluate the malformed `dependencies` branch for a non-matching source.
let job = create_pending_openapi_job(
&registry,
&ws,
"job_object_deps",
now - Duration::hours(2),
now + Duration::hours(1),
)
.await;
let old = now - Duration::minutes(10);
insert_active_source(&raw, &ws, "src_openapi_test", &"ab".repeat(32), old).await;
insert_active_source(&raw, &ws, "src_openapi_orphan", &"ac".repeat(32), old).await;
// Corrupt the active job's payload so `dependencies` is an object, not an
// array; the sweep must not abort on it.
sqlx::query(
"update import_jobs
set preview_payload = jsonb_set(preview_payload, '{dependencies}', '{}'::jsonb)
where id = $1 and workspace_id = $2",
)
.bind(job.as_str())
.bind(ws.as_str())
.execute(&raw)
.await
.unwrap();
let cleanup = registry
.cleanup_expired_import_jobs(8)
.await
.expect("dangling cleanup must not abort on a non-array dependencies payload");
assert_eq!(
cleanup.deleted_jobs, 0,
"the active job must survive cleanup"
);
assert_eq!(
cleanup.detached_sources, 1,
"the dangling sweep must still run and detach exactly the unreferenced peer"
);
assert_eq!(
source_lifecycle(&raw, &ws, "src_openapi_test").await,
"active",
"the live primary source referenced by the job must be preserved"
);
assert_eq!(
source_lifecycle(&raw, &ws, "src_openapi_orphan").await,
"detached",
"the unreferenced peer must be swept once the malformed branch is guarded"
);
database.cleanup().await;
}
#[tokio::test]
async fn cleanup_ignores_non_openapi_sources_named_by_expired_jobs() {
let database = TestDatabase::new().await;
let registry = database.registry().await;
let raw = database.raw_pool().await;
let now = OffsetDateTime::now_utc();
let ws = create_workspace(&registry, "ws_cleanup_foreign_source").await;
let old = now - Duration::hours(2);
let blob_digest = "cd".repeat(32);
insert_active_source(&raw, &ws, "src_confidential_leak", &blob_digest, old).await;
// Expired OpenAPI job naming a same-workspace source outside the OpenAPI
// namespace.
let job = ImportJobId::new("job_foreign_source");
let source_id = ArtifactSourceId::new("src_confidential_leak");
let digest = ArtifactRef::from_digest_hex(&blob_digest).unwrap();
let preview_payload = json!({
"source": {
"source_id": source_id.as_str(),
"digest": digest.as_str(),
},
"preview": { "ops": [] }
});
registry
.create_import_job(CreateImportJobRequest {
id: &job,
workspace_id: &ws,
kind: ImportJobKind::OpenApi,
source_format: "openapi",
source_version: Some("3.1.0"),
status: ImportJobStatus::Pending,
source: &ImportJobSourceEnvelope {
source_id: source_id.clone(),
digest: digest.clone(),
},
preview_payload: &preview_payload,
created_at: &old,
expires_at: &(now - Duration::minutes(10)),
})
.await
.unwrap();
let cleanup = registry
.cleanup_expired_import_jobs(8)
.await
.expect("expired OpenAPI cleanup must succeed");
assert_eq!(cleanup.deleted_jobs, 1, "the expired job must be deleted");
assert_eq!(
source_lifecycle(&raw, &ws, "src_confidential_leak").await,
"active",
"an OpenAPI job must not detach a source outside its namespace"
);
database.cleanup().await;
}
#[tokio::test]
async fn cleanup_keeps_source_referenced_by_nonexpired_peer_job() {
let database = TestDatabase::new().await;
let registry = database.registry().await;
let raw = database.raw_pool().await;
let now = OffsetDateTime::now_utc();
let ws = create_workspace(&registry, "ws_cleanup_shared_source").await;
let old = now - Duration::hours(2);
let shared_source = "src_openapi_shared";
let expired = create_pending_openapi_job(
&registry,
&ws,
"job_expired_shared_source",
old,
now - Duration::minutes(10),
)
.await;
let peer = create_pending_openapi_job(
&registry,
&ws,
"job_live_shared_source",
old,
now + Duration::hours(1),
)
.await;
insert_active_source(&raw, &ws, shared_source, &"ab".repeat(32), old).await;
for job_id in [expired.as_str(), peer.as_str()] {
sqlx::query(
"update import_jobs
set preview_payload = jsonb_set(
preview_payload, '{source,source_id}', to_jsonb($2::text)
)
where id = $1 and workspace_id = $3",
)
.bind(job_id)
.bind(shared_source)
.bind(ws.as_str())
.execute(&raw)
.await
.unwrap();
}
let cleanup = registry
.cleanup_expired_import_jobs(8)
.await
.expect("expired job cleanup must preserve a source referenced by a live peer");
assert_eq!(cleanup.deleted_jobs, 1);
assert_eq!(
source_lifecycle(&raw, &ws, shared_source).await,
"active",
"the source remains owned by the non-expired peer job"
);
database.cleanup().await;
}
#[tokio::test]
async fn failed_job_finalization_keeps_source_referenced_by_peer_job() {
let database = TestDatabase::new().await;
let registry = database.registry().await;
let raw = database.raw_pool().await;
let now = OffsetDateTime::now_utc();
let ws = create_workspace(&registry, "ws_failed_shared_source").await;
let source_id = "src_openapi_shared";
let failing = create_pending_openapi_job(
&registry,
&ws,
"job_failing_shared_source",
now - Duration::hours(1),
now + Duration::hours(1),
)
.await;
let peer = create_pending_openapi_job(
&registry,
&ws,
"job_peer_shared_source",
now - Duration::hours(1),
now + Duration::hours(1),
)
.await;
insert_active_source(&raw, &ws, source_id, &"ab".repeat(32), now).await;
for job_id in [failing.as_str(), peer.as_str()] {
sqlx::query(
"update import_jobs
set preview_payload = jsonb_set(
preview_payload, '{source,source_id}', to_jsonb($2::text)
)
where id = $1 and workspace_id = $3",
)
.bind(job_id)
.bind(source_id)
.bind(ws.as_str())
.execute(&raw)
.await
.unwrap();
}
registry
.finish_import_job(crank_registry::FinishImportJobRequest {
id: &failing,
status: ImportJobStatus::Failed,
created_operation_ids: &json!([]),
error_text: Some("test_failure"),
finished_at: &now,
})
.await
.unwrap();
assert_eq!(
source_lifecycle(&raw, &ws, source_id).await,
"active",
"failed finalization must not detach a source still referenced by a peer job"
);
database.cleanup().await;
}