//! The store against a real Garage, the version cnx01 runs: //! `bash scripts/dev-garage.sh` starts one in Docker and writes //! `.dev/garage.env`; `. .dev/garage.env` before `cargo test` runs this. //! Without VISA_TEST_S3_* set it is skipped (CI has no Garage; the test //! server in tests/common/fake_s3.rs covers the same calls there). use visa::files::{FileKey, Store}; use visa::s3::{Bucket, Put, S3Config}; use crate::common::pdf; fn garage() -> Option { let v = |n: &str| std::env::var(n).ok().filter(|s| !s.is_empty()); let secret = std::fs::read_to_string(v("VISA_TEST_S3_SECRET_FILE")?).ok()?; Some(S3Config { endpoint: v("VISA_TEST_S3_ENDPOINT")?, bucket: v("VISA_TEST_S3_BUCKET")?, region: v("VISA_TEST_S3_REGION").unwrap_or_else(|| "garage".into()), access_key_id: v("VISA_TEST_S3_ACCESS_KEY_ID")?, secret_access_key: secret.trim().to_string(), }) } /// Ids no other run uses at the same time: the bucket is shared. fn fresh_ids(n: i64) -> Vec { let nanos = std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) .unwrap() .subsec_nanos() as i64; let base = 1_000_000_000 + (std::process::id() as i64 % 1000) * 1_000_000 + nanos % 1_000_000; (0..n).map(|i| base + i * 7).collect() } #[tokio::test] async fn the_store_works_against_a_real_garage() { let Some(cfg) = garage() else { eprintln!("VISA_TEST_S3_* not set: skipped (scripts/dev-garage.sh)"); return; }; let key = FileKey::random(); let store = Store::in_bucket(&cfg, key.clone()).unwrap(); store.check().await.unwrap(); let ids = fresh_ids(3); for id in &ids { store .put(*id, pdf(&format!("document {id}"))) .await .unwrap(); } for id in &ids { assert_eq!( store.get(*id).await.unwrap(), pdf(&format!("document {id}")) ); } // the listing names them with their sizes, page after page let entries = store.entries().await.unwrap(); for id in &ids { let size = store.get_sealed(*id).await.unwrap().unwrap().len() as u64; assert!(entries.files.contains(&(*id, size)), "{id}: {entries:?}"); } // never replaced: the same bytes count as stored, others are refused let sealed = store.get_sealed(ids[0]).await.unwrap().unwrap(); assert_eq!( store.put_sealed(ids[0], sealed.clone()).await.unwrap(), Put::Same ); let e = store.put(ids[0], pdf("other")).await.unwrap_err(); assert!(format!("{e:#}").contains("other bytes"), "{e:#}"); assert_eq!(store.get_sealed(ids[0]).await.unwrap().unwrap(), sealed); // Garage 2.5 ignores If-None-Match on a PUT and writes (seen on Nov 19, // 2026): on a scratch object of this run, never on a document's let bucket = Bucket::new(&cfg).unwrap(); let scratch = format!("scratch/{}.probe", ids[0]); assert!(bucket.put_if_absent(&scratch, b"first").await.unwrap()); let honored = !bucket.put_if_absent(&scratch, b"second").await.unwrap(); let kept = bucket.get(&scratch).await.unwrap().unwrap(); assert_eq!( (honored, kept.as_slice()), (false, b"second".as_slice()), "Garage now honors If-None-Match: update the comment on Bucket::put_if_absent" ); bucket.delete(&scratch).await.unwrap(); // a wrong secret is refused, not taken for a missing file let mut wrong = cfg.clone(); wrong.secret_access_key = "wrong".into(); let e = Store::in_bucket(&wrong, key) .unwrap() .get(ids[0]) .await .unwrap_err(); assert!(format!("{e:#}").contains("HTTP 403"), "{e:#}"); for id in &ids { store.remove(*id).await.unwrap(); store.remove(*id).await.unwrap(); assert!(store.get_sealed(*id).await.unwrap().is_none()); } }