about summary refs log tree commit diff
path: root/tvix/store
diff options
context:
space:
mode:
authorFlorian Klink <flokli@flokli.de>2023-06-14T20·41+0300
committerclbot <clbot@tvl.fyi>2023-06-30T12·35+0000
commit7fbf874f1c530db159b24859050a6d3e5fcd8075 (patch)
tree8931c6f9340461fc090c2ffd8693577737ad1676 /tvix/store
parent3edc580e8070cff1cbfc1d93e78b9128b9b43ecc (diff)
feat(tvix/store/blobsvc): add more blobservice tests r/6370
Change-Id: I3e27dfb4ce3e52974d7614814abb7b5ae4a37f8c
Reviewed-on: https://cl.tvl.fyi/c/depot/+/8782
Tested-by: BuildkiteCI
Autosubmit: flokli <flokli@flokli.de>
Reviewed-by: tazjin <tazjin@tvl.su>
Diffstat (limited to 'tvix/store')
-rw-r--r--tvix/store/src/blobservice/mod.rs3
-rw-r--r--tvix/store/src/blobservice/tests.rs86
2 files changed, 89 insertions, 0 deletions
diff --git a/tvix/store/src/blobservice/mod.rs b/tvix/store/src/blobservice/mod.rs
index 622d058353a6..c159ce95a544 100644
--- a/tvix/store/src/blobservice/mod.rs
+++ b/tvix/store/src/blobservice/mod.rs
@@ -7,6 +7,9 @@ mod grpc;
 mod memory;
 mod sled;
 
+#[cfg(test)]
+mod tests;
+
 pub use self::from_addr::from_addr;
 pub use self::grpc::GRPCBlobService;
 pub use self::memory::MemoryBlobService;
diff --git a/tvix/store/src/blobservice/tests.rs b/tvix/store/src/blobservice/tests.rs
new file mode 100644
index 000000000000..2b1c2c166596
--- /dev/null
+++ b/tvix/store/src/blobservice/tests.rs
@@ -0,0 +1,86 @@
+use std::io;
+
+use test_case::test_case;
+
+use super::B3Digest;
+use super::BlobService;
+use super::MemoryBlobService;
+use super::SledBlobService;
+use crate::tests::fixtures;
+
+// TODO: avoid having to define all different services we test against for all functions.
+// maybe something like rstest can be used?
+
+fn gen_memory_blob_service() -> impl BlobService {
+    MemoryBlobService::default()
+}
+fn gen_sled_blob_service() -> impl BlobService {
+    SledBlobService::new_temporary().unwrap()
+}
+
+// TODO: add GRPC blob service here.
+
+/// Using [BlobService::has] on a non-existing blob should return false
+#[test_case(gen_memory_blob_service(); "memory")]
+#[test_case(gen_sled_blob_service(); "sled")]
+fn has_nonexistent_false(blob_service: impl BlobService) {
+    assert_eq!(
+        blob_service
+            .has(&fixtures::BLOB_A_DIGEST)
+            .expect("must not fail"),
+        false
+    );
+}
+
+/// Trying to read a non-existing blob should return a None instead of a reader.
+#[test_case(gen_memory_blob_service(); "memory")]
+#[test_case(gen_sled_blob_service(); "sled")]
+fn not_found_read(blob_service: impl BlobService) {
+    assert!(blob_service
+        .open_read(&fixtures::BLOB_A_DIGEST)
+        .expect("must not fail")
+        .is_none())
+}
+
+/// Put a blob in the store, check has, get it back.
+/// We test both with small and big blobs.
+#[test_case(gen_memory_blob_service(), &fixtures::BLOB_A, &fixtures::BLOB_A_DIGEST; "memory-small")]
+#[test_case(gen_sled_blob_service(), &fixtures::BLOB_A, &fixtures::BLOB_A_DIGEST; "sled-small")]
+#[test_case(gen_memory_blob_service(), &fixtures::BLOB_B, &fixtures::BLOB_B_DIGEST; "memory-big")]
+#[test_case(gen_sled_blob_service(), &fixtures::BLOB_B, &fixtures::BLOB_B_DIGEST; "sled-big")]
+fn put_has_get(blob_service: impl BlobService, blob_contents: &[u8], blob_digest: &B3Digest) {
+    let mut w = blob_service.open_write();
+
+    let l = io::copy(&mut io::Cursor::new(blob_contents), &mut w).expect("copy must succeed");
+    assert_eq!(
+        blob_contents.len(),
+        l as usize,
+        "written bytes must match blob length"
+    );
+
+    let digest = w.close().expect("close must succeed");
+
+    assert_eq!(*blob_digest, digest, "returned digest must be correct");
+
+    assert_eq!(
+        blob_service.has(blob_digest).expect("must not fail"),
+        true,
+        "blob service should now have the blob"
+    );
+
+    let mut r = blob_service
+        .open_read(blob_digest)
+        .expect("open_read must succeed")
+        .expect("must be some");
+
+    let mut buf: Vec<u8> = Vec::new();
+    let l = io::copy(&mut r, &mut buf).expect("copy must succeed");
+
+    assert_eq!(
+        blob_contents.len(),
+        l as usize,
+        "read bytes must match blob length"
+    );
+
+    assert_eq!(blob_contents, buf, "read blob contents must match");
+}