about summary refs log tree commit diff
path: root/tvix/store
diff options
context:
space:
mode:
authorFlorian Klink <flokli@flokli.de>2023-02-27T07·59+0100
committerflokli <flokli@flokli.de>2023-03-10T10·58+0000
commit28a862976bd43912e0e5dc16e8919590c36f4cf0 (patch)
treef6e6fb7eac5145c9519ff8399a14ab1733a75c66 /tvix/store
parent0baaabc43e3027b1676874c536d5ade27abe14b8 (diff)
refactor(tvix/store/tests): move gen_*_service() into helper r/5928
This allows hiding to tests what exact implementation we're using, when
testing things that do something with a store, but don't care what's
used for underlying storage.

Change-Id: I7cdf60fd73c25d5050159cb31ec177db2bc2a7f1
Reviewed-on: https://cl.tvl.fyi/c/depot/+/8155
Tested-by: BuildkiteCI
Reviewed-by: raitobezarius <tvl@lahfa.xyz>
Diffstat (limited to 'tvix/store')
-rw-r--r--tvix/store/src/blobreader.rs19
-rw-r--r--tvix/store/src/proto/tests/grpc_blobservice.rs9
-rw-r--r--tvix/store/src/proto/tests/grpc_directoryservice.rs9
-rw-r--r--tvix/store/src/proto/tests/grpc_pathinfoservice.rs49
-rw-r--r--tvix/store/src/tests/mod.rs1
-rw-r--r--tvix/store/src/tests/nar_renderer.rs17
-rw-r--r--tvix/store/src/tests/utils.rs24
7 files changed, 57 insertions, 71 deletions
diff --git a/tvix/store/src/blobreader.rs b/tvix/store/src/blobreader.rs
index 47ddfae03c7b..5fb26228be65 100644
--- a/tvix/store/src/blobreader.rs
+++ b/tvix/store/src/blobreader.rs
@@ -133,13 +133,12 @@ impl<CS: ChunkService> std::io::Read for BlobReader<'_, CS> {
 mod tests {
     use super::BlobReader;
     use crate::chunkservice::ChunkService;
-    use crate::chunkservice::SledChunkService;
     use crate::proto;
+    use crate::tests::utils::gen_chunk_service;
     use lazy_static::lazy_static;
     use std::io::Cursor;
     use std::io::Read;
     use std::io::Write;
-    use std::path::PathBuf;
     use tempfile::TempDir;
 
     lazy_static! {
@@ -152,15 +151,11 @@ mod tests {
         static ref DUMMY_DATA_2: Vec<u8> = vec![0x04, 0x05];
     }
 
-    fn gen_chunk_service(p: PathBuf) -> impl ChunkService {
-        SledChunkService::new(p.join("chunks")).unwrap()
-    }
-
     #[test]
     /// reading from a blobmeta with zero chunks should produce zero bytes.
     fn empty_blobmeta() -> anyhow::Result<()> {
         let tmpdir = TempDir::new()?;
-        let chunk_service = gen_chunk_service(tmpdir.path().to_path_buf());
+        let chunk_service = gen_chunk_service(tmpdir.path());
 
         let blobmeta = proto::BlobMeta {
             chunks: vec![],
@@ -181,7 +176,7 @@ mod tests {
     /// trying to read something where the chunk doesn't exist should fail
     fn missing_chunk_fail() -> anyhow::Result<()> {
         let tmpdir = TempDir::new()?;
-        let chunk_service = gen_chunk_service(tmpdir.path().to_path_buf());
+        let chunk_service = gen_chunk_service(tmpdir.path());
 
         let blobmeta = proto::BlobMeta {
             chunks: vec![proto::blob_meta::ChunkMeta {
@@ -205,7 +200,7 @@ mod tests {
     /// read something containing the single (empty) chunk
     fn empty_chunk() -> anyhow::Result<()> {
         let tmpdir = TempDir::new()?;
-        let chunk_service = gen_chunk_service(tmpdir.path().to_path_buf());
+        let chunk_service = gen_chunk_service(tmpdir.path());
 
         // insert a single chunk
         let dgst = chunk_service.put(vec![]).expect("must succeed");
@@ -236,7 +231,7 @@ mod tests {
     #[test]
     fn single_chunk() -> anyhow::Result<()> {
         let tmpdir = TempDir::new()?;
-        let chunk_service = gen_chunk_service(tmpdir.path().to_path_buf());
+        let chunk_service = gen_chunk_service(tmpdir.path());
 
         // insert a single chunk
         let dgst = chunk_service
@@ -269,7 +264,7 @@ mod tests {
     #[test]
     fn wrong_size_fail() -> anyhow::Result<()> {
         let tmpdir = TempDir::new()?;
-        let chunk_service = gen_chunk_service(tmpdir.path().to_path_buf());
+        let chunk_service = gen_chunk_service(tmpdir.path());
 
         // insert chunks
         let dgst_1 = chunk_service
@@ -300,7 +295,7 @@ mod tests {
     #[test]
     fn multiple_chunks() -> anyhow::Result<()> {
         let tmpdir = TempDir::new()?;
-        let chunk_service = gen_chunk_service(tmpdir.path().to_path_buf());
+        let chunk_service = gen_chunk_service(tmpdir.path());
 
         // insert chunks
         let dgst_1 = chunk_service
diff --git a/tvix/store/src/proto/tests/grpc_blobservice.rs b/tvix/store/src/proto/tests/grpc_blobservice.rs
index cd8ae3e2bf8b..b409fc3ea3c0 100644
--- a/tvix/store/src/proto/tests/grpc_blobservice.rs
+++ b/tvix/store/src/proto/tests/grpc_blobservice.rs
@@ -1,8 +1,9 @@
-use crate::blobservice::{BlobService, SledBlobService};
-use crate::chunkservice::{ChunkService, SledChunkService};
+use crate::blobservice::BlobService;
+use crate::chunkservice::ChunkService;
 use crate::proto::blob_meta::ChunkMeta;
 use crate::proto::blob_service_server::BlobService as GRPCBlobService;
 use crate::proto::{BlobChunk, GRPCBlobServiceWrapper, ReadBlobRequest, StatBlobRequest};
+use crate::tests::utils::{gen_blob_service, gen_chunk_service};
 use lazy_static::lazy_static;
 use std::path::Path;
 use tempfile::TempDir;
@@ -23,8 +24,8 @@ fn gen_grpc_blob_service(
     impl BlobService + Send + Sync + Clone + 'static,
     impl ChunkService + Send + Sync + Clone + 'static,
 > {
-    let blob_service = SledBlobService::new(p.join("blobs")).unwrap();
-    let chunk_service = SledChunkService::new(p.join("chunks")).unwrap();
+    let blob_service = gen_blob_service(p);
+    let chunk_service = gen_chunk_service(p);
     GRPCBlobServiceWrapper::new(blob_service, chunk_service)
 }
 
diff --git a/tvix/store/src/proto/tests/grpc_directoryservice.rs b/tvix/store/src/proto/tests/grpc_directoryservice.rs
index 58d49811f081..b004c47c2d22 100644
--- a/tvix/store/src/proto/tests/grpc_directoryservice.rs
+++ b/tvix/store/src/proto/tests/grpc_directoryservice.rs
@@ -1,8 +1,9 @@
-use crate::directoryservice::SledDirectoryService;
+use crate::directoryservice::DirectoryService;
 use crate::proto::directory_service_server::DirectoryService as GRPCDirectoryService;
 use crate::proto::get_directory_request::ByWhat;
 use crate::proto::{Directory, DirectoryNode, SymlinkNode};
 use crate::proto::{GRPCDirectoryServiceWrapper, GetDirectoryRequest};
+use crate::tests::utils::gen_directory_service;
 use lazy_static::lazy_static;
 use std::path::Path;
 use tempfile::TempDir;
@@ -36,8 +37,10 @@ lazy_static! {
     };
 }
 
-fn gen_grpc_service(p: &Path) -> GRPCDirectoryServiceWrapper<SledDirectoryService> {
-    let directory_service = SledDirectoryService::new(p.join("directories")).unwrap();
+fn gen_grpc_service(
+    p: &Path,
+) -> GRPCDirectoryServiceWrapper<impl DirectoryService + Send + Sync + Clone + 'static> {
+    let directory_service = gen_directory_service(p);
     GRPCDirectoryServiceWrapper::from(directory_service)
 }
 
diff --git a/tvix/store/src/proto/tests/grpc_pathinfoservice.rs b/tvix/store/src/proto/tests/grpc_pathinfoservice.rs
index 3858909757b8..a6a1de9dc4da 100644
--- a/tvix/store/src/proto/tests/grpc_pathinfoservice.rs
+++ b/tvix/store/src/proto/tests/grpc_pathinfoservice.rs
@@ -1,21 +1,17 @@
-use std::path::Path;
-
-use tempfile::TempDir;
-use tonic::Request;
-
-use crate::blobservice::{BlobService, SledBlobService};
-use crate::chunkservice::{ChunkService, SledChunkService};
-use crate::directoryservice::{DirectoryService, SledDirectoryService};
 use crate::nar::NonCachingNARCalculationService;
-use crate::pathinfoservice::{PathInfoService, SledPathInfoService};
 use crate::proto::get_path_info_request::ByWhat::ByOutputHash;
 use crate::proto::node::Node::Symlink;
 use crate::proto::path_info_service_server::PathInfoService as GRPCPathInfoService;
 use crate::proto::GRPCPathInfoServiceWrapper;
 use crate::proto::PathInfo;
 use crate::proto::{GetPathInfoRequest, Node, SymlinkNode};
-
+use crate::tests::utils::{
+    gen_blob_service, gen_chunk_service, gen_directory_service, gen_pathinfo_service,
+};
 use lazy_static::lazy_static;
+use std::path::Path;
+use tempfile::TempDir;
+use tonic::Request;
 
 lazy_static! {
     static ref DUMMY_OUTPUT_HASH: Vec<u8> = vec![
@@ -24,38 +20,19 @@ lazy_static! {
     ];
 }
 
-// TODO: dedup these helpers
-fn gen_pathinfo_service(p: &Path) -> impl PathInfoService {
-    SledPathInfoService::new(p.join("pathinfo")).unwrap()
-}
-fn gen_blob_service(p: &Path) -> impl BlobService {
-    SledBlobService::new(p.join("blobs")).unwrap()
-}
-
-fn gen_chunk_service(p: &Path) -> impl ChunkService + Clone {
-    SledChunkService::new(p.join("chunks")).unwrap()
-}
-
-fn gen_directory_service(p: &Path) -> impl DirectoryService {
-    SledDirectoryService::new(p.join("directories")).unwrap()
-}
-
 /// generates a GRPCPathInfoService out of blob, chunk, directory and pathinfo services.
 ///
-/// It doesn't create underlying services on its own, as we don't need to
-/// preseed them with existing data for the test - we only interact with it via
-// the PathInfo GRPC interface.
+/// We only interact with it via the PathInfo GRPC interface.
 /// It uses the NonCachingNARCalculationService NARCalculationService to
 /// calculate NARs.
 fn gen_grpc_service(p: &Path) -> impl GRPCPathInfoService {
-    let blob_service = gen_blob_service(p);
-    let chunk_service = gen_chunk_service(p);
-    let directory_service = gen_directory_service(p);
-    let pathinfo_service = gen_pathinfo_service(p);
-
     GRPCPathInfoServiceWrapper::new(
-        pathinfo_service,
-        NonCachingNARCalculationService::new(blob_service, chunk_service, directory_service),
+        gen_pathinfo_service(p),
+        NonCachingNARCalculationService::new(
+            gen_blob_service(p),
+            gen_chunk_service(p),
+            gen_directory_service(p),
+        ),
     )
 }
 
diff --git a/tvix/store/src/tests/mod.rs b/tvix/store/src/tests/mod.rs
index 735b0789f80d..421858c03083 100644
--- a/tvix/store/src/tests/mod.rs
+++ b/tvix/store/src/tests/mod.rs
@@ -1 +1,2 @@
 mod nar_renderer;
+pub mod utils;
diff --git a/tvix/store/src/tests/nar_renderer.rs b/tvix/store/src/tests/nar_renderer.rs
index 7271a45f597d..4a72be5910fd 100644
--- a/tvix/store/src/tests/nar_renderer.rs
+++ b/tvix/store/src/tests/nar_renderer.rs
@@ -1,16 +1,13 @@
 use crate::blobservice::BlobService;
-use crate::blobservice::SledBlobService;
 use crate::chunkservice::ChunkService;
-use crate::chunkservice::SledChunkService;
 use crate::directoryservice::DirectoryService;
-use crate::directoryservice::SledDirectoryService;
 use crate::nar::NARRenderer;
 use crate::proto;
 use crate::proto::DirectoryNode;
 use crate::proto::FileNode;
 use crate::proto::SymlinkNode;
+use crate::tests::utils::*;
 use lazy_static::lazy_static;
-use std::path::Path;
 use tempfile::TempDir;
 
 const HELLOWORLD_BLOB_CONTENTS: &[u8] = b"Hello World!";
@@ -49,18 +46,6 @@ lazy_static! {
     };
 }
 
-fn gen_blob_service(p: &Path) -> impl BlobService {
-    SledBlobService::new(p.join("blobs")).unwrap()
-}
-
-fn gen_chunk_service(p: &Path) -> impl ChunkService + Clone {
-    SledChunkService::new(p.join("chunks")).unwrap()
-}
-
-fn gen_directory_service(p: &Path) -> impl DirectoryService {
-    SledDirectoryService::new(p.join("directories")).unwrap()
-}
-
 #[test]
 fn single_symlink() {
     let tmpdir = TempDir::new().unwrap();
diff --git a/tvix/store/src/tests/utils.rs b/tvix/store/src/tests/utils.rs
new file mode 100644
index 000000000000..4ddd4102fb97
--- /dev/null
+++ b/tvix/store/src/tests/utils.rs
@@ -0,0 +1,24 @@
+use std::path::Path;
+
+use crate::{
+    blobservice::{BlobService, SledBlobService},
+    chunkservice::{ChunkService, SledChunkService},
+    directoryservice::{DirectoryService, SledDirectoryService},
+    pathinfoservice::{PathInfoService, SledPathInfoService},
+};
+
+pub fn gen_blob_service(p: &Path) -> impl BlobService + Send + Sync + Clone + 'static {
+    SledBlobService::new(p.join("blobs")).unwrap()
+}
+
+pub fn gen_chunk_service(p: &Path) -> impl ChunkService + Clone {
+    SledChunkService::new(p.join("chunks")).unwrap()
+}
+
+pub fn gen_directory_service(p: &Path) -> impl DirectoryService + Send + Sync + Clone + 'static {
+    SledDirectoryService::new(p.join("directories")).unwrap()
+}
+
+pub fn gen_pathinfo_service(p: &Path) -> impl PathInfoService {
+    SledPathInfoService::new(p.join("pathinfo")).unwrap()
+}