From e7279b6063b3a4675f8ed37c797254de8a0c79e9 Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Mon, 27 Feb 2023 09:12:09 +0100 Subject: [PATCH] refactor(tvix/store/tests): move fixtures into separate module Change-Id: I362dbf0899e4dc42114fd2e6a8fa7f537e9ea138 Reviewed-on: https://cl.tvl.fyi/c/depot/+/8156 Tested-by: BuildkiteCI Reviewed-by: raitobezarius --- tvix/store/src/blobreader.rs | 14 +-- .../store/src/proto/tests/grpc_blobservice.rs | 12 +-- .../src/proto/tests/grpc_directoryservice.rs | 29 +------ .../src/proto/tests/grpc_pathinfoservice.rs | 9 +- tvix/store/src/tests/fixtures.rs | 86 +++++++++++++++++++ tvix/store/src/tests/mod.rs | 1 + tvix/store/src/tests/nar_renderer.rs | 38 +------- 7 files changed, 94 insertions(+), 95 deletions(-) create mode 100644 tvix/store/src/tests/fixtures.rs diff --git a/tvix/store/src/blobreader.rs b/tvix/store/src/blobreader.rs index 5fb26228b..b4e56d909 100644 --- a/tvix/store/src/blobreader.rs +++ b/tvix/store/src/blobreader.rs @@ -134,23 +134,15 @@ mod tests { use super::BlobReader; use crate::chunkservice::ChunkService; use crate::proto; + use crate::tests::fixtures::DUMMY_DATA_1; + use crate::tests::fixtures::DUMMY_DATA_2; + use crate::tests::fixtures::DUMMY_DIGEST; 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 tempfile::TempDir; - lazy_static! { - static ref DUMMY_DIGEST: Vec = vec![ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - ]; - static ref DUMMY_DATA_1: Vec = vec![0x01, 0x02, 0x03]; - static ref DUMMY_DATA_2: Vec = vec![0x04, 0x05]; - } - #[test] /// reading from a blobmeta with zero chunks should produce zero bytes. fn empty_blobmeta() -> anyhow::Result<()> { diff --git a/tvix/store/src/proto/tests/grpc_blobservice.rs b/tvix/store/src/proto/tests/grpc_blobservice.rs index b409fc3ea..4ea367cbd 100644 --- a/tvix/store/src/proto/tests/grpc_blobservice.rs +++ b/tvix/store/src/proto/tests/grpc_blobservice.rs @@ -3,21 +3,11 @@ 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::fixtures::{BLOB_A, BLOB_A_DIGEST, BLOB_B, BLOB_B_DIGEST}; use crate::tests::utils::{gen_blob_service, gen_chunk_service}; -use lazy_static::lazy_static; use std::path::Path; use tempfile::TempDir; -lazy_static! { - // 2 bytes - static ref BLOB_A: Vec = vec![0x00, 0x01]; - static ref BLOB_A_DIGEST: Vec = blake3::hash(&BLOB_A).as_bytes().to_vec(); - - // 1MB - static ref BLOB_B: Vec = (0..255).collect::>().repeat(4 * 1024); - static ref BLOB_B_DIGEST: Vec = blake3::hash(&BLOB_B).as_bytes().to_vec(); -} - fn gen_grpc_blob_service( p: &Path, ) -> GRPCBlobServiceWrapper< diff --git a/tvix/store/src/proto/tests/grpc_directoryservice.rs b/tvix/store/src/proto/tests/grpc_directoryservice.rs index b004c47c2..6d9851111 100644 --- a/tvix/store/src/proto/tests/grpc_directoryservice.rs +++ b/tvix/store/src/proto/tests/grpc_directoryservice.rs @@ -3,40 +3,13 @@ use crate::proto::directory_service_server::DirectoryService as GRPCDirectorySer use crate::proto::get_directory_request::ByWhat; use crate::proto::{Directory, DirectoryNode, SymlinkNode}; use crate::proto::{GRPCDirectoryServiceWrapper, GetDirectoryRequest}; +use crate::tests::fixtures::{DIRECTORY_A, DIRECTORY_B, DIRECTORY_C}; use crate::tests::utils::gen_directory_service; -use lazy_static::lazy_static; use std::path::Path; use tempfile::TempDir; use tokio_stream::StreamExt; use tonic::Status; -lazy_static! { - static ref DIRECTORY_A: Directory = Directory::default(); - static ref DIRECTORY_B: Directory = Directory { - directories: vec![DirectoryNode { - name: "a".to_string(), - digest: DIRECTORY_A.digest(), - size: DIRECTORY_A.size(), - }], - ..Default::default() - }; - static ref DIRECTORY_C: Directory = Directory { - directories: vec![ - DirectoryNode { - name: "a".to_string(), - digest: DIRECTORY_A.digest(), - size: DIRECTORY_A.size(), - }, - DirectoryNode { - name: "a'".to_string(), - digest: DIRECTORY_A.digest(), - size: DIRECTORY_A.size(), - } - ], - ..Default::default() - }; -} - fn gen_grpc_service( p: &Path, ) -> GRPCDirectoryServiceWrapper { diff --git a/tvix/store/src/proto/tests/grpc_pathinfoservice.rs b/tvix/store/src/proto/tests/grpc_pathinfoservice.rs index a6a1de9dc..ce74711cb 100644 --- a/tvix/store/src/proto/tests/grpc_pathinfoservice.rs +++ b/tvix/store/src/proto/tests/grpc_pathinfoservice.rs @@ -5,21 +5,14 @@ use crate::proto::path_info_service_server::PathInfoService as GRPCPathInfoServi use crate::proto::GRPCPathInfoServiceWrapper; use crate::proto::PathInfo; use crate::proto::{GetPathInfoRequest, Node, SymlinkNode}; +use crate::tests::fixtures::DUMMY_OUTPUT_HASH; 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 = vec![ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00 - ]; -} - /// generates a GRPCPathInfoService out of blob, chunk, directory and pathinfo services. /// /// We only interact with it via the PathInfo GRPC interface. diff --git a/tvix/store/src/tests/fixtures.rs b/tvix/store/src/tests/fixtures.rs new file mode 100644 index 000000000..c6d80e279 --- /dev/null +++ b/tvix/store/src/tests/fixtures.rs @@ -0,0 +1,86 @@ +use crate::proto::{self, Directory, DirectoryNode, FileNode, SymlinkNode}; +use lazy_static::lazy_static; + +pub const HELLOWORLD_BLOB_CONTENTS: &[u8] = b"Hello World!"; +pub const EMPTY_BLOB_CONTENTS: &[u8] = b""; + +lazy_static! { + pub static ref DUMMY_DIGEST: Vec = vec![ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, + ]; + pub static ref DUMMY_DATA_1: Vec = vec![0x01, 0x02, 0x03]; + pub static ref DUMMY_DATA_2: Vec = vec![0x04, 0x05]; + pub static ref HELLOWORLD_BLOB_DIGEST: Vec = + blake3::hash(HELLOWORLD_BLOB_CONTENTS).as_bytes().to_vec(); + pub static ref EMPTY_BLOB_DIGEST: Vec = + blake3::hash(EMPTY_BLOB_CONTENTS).as_bytes().to_vec(); + + // 2 bytes + pub static ref BLOB_A: Vec = vec![0x00, 0x01]; + pub static ref BLOB_A_DIGEST: Vec = blake3::hash(&BLOB_A).as_bytes().to_vec(); + + // 1MB + pub static ref BLOB_B: Vec = (0..255).collect::>().repeat(4 * 1024); + pub static ref BLOB_B_DIGEST: Vec = blake3::hash(&BLOB_B).as_bytes().to_vec(); + + // Directories + pub static ref DIRECTORY_WITH_KEEP: proto::Directory = proto::Directory { + directories: vec![], + files: vec![FileNode { + name: ".keep".to_string(), + digest: EMPTY_BLOB_DIGEST.to_vec(), + size: 0, + executable: false, + }], + symlinks: vec![], + }; + pub static ref DIRECTORY_COMPLICATED: proto::Directory = proto::Directory { + directories: vec![DirectoryNode { + name: "keep".to_string(), + digest: DIRECTORY_WITH_KEEP.digest(), + size: DIRECTORY_WITH_KEEP.size(), + }], + files: vec![FileNode { + name: ".keep".to_string(), + digest: EMPTY_BLOB_DIGEST.to_vec(), + size: 0, + executable: false, + }], + symlinks: vec![SymlinkNode { + name: "aa".to_string(), + target: "/nix/store/somewhereelse".to_string(), + }], + }; + pub static ref DIRECTORY_A: Directory = Directory::default(); + pub static ref DIRECTORY_B: Directory = Directory { + directories: vec![DirectoryNode { + name: "a".to_string(), + digest: DIRECTORY_A.digest(), + size: DIRECTORY_A.size(), + }], + ..Default::default() + }; + pub static ref DIRECTORY_C: Directory = Directory { + directories: vec![ + DirectoryNode { + name: "a".to_string(), + digest: DIRECTORY_A.digest(), + size: DIRECTORY_A.size(), + }, + DirectoryNode { + name: "a'".to_string(), + digest: DIRECTORY_A.digest(), + size: DIRECTORY_A.size(), + } + ], + ..Default::default() + }; + + // output hash + pub static ref DUMMY_OUTPUT_HASH: Vec = vec![ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00 + ]; +} diff --git a/tvix/store/src/tests/mod.rs b/tvix/store/src/tests/mod.rs index 421858c03..daea048de 100644 --- a/tvix/store/src/tests/mod.rs +++ b/tvix/store/src/tests/mod.rs @@ -1,2 +1,3 @@ +pub mod fixtures; 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 4a72be591..52cf43e98 100644 --- a/tvix/store/src/tests/nar_renderer.rs +++ b/tvix/store/src/tests/nar_renderer.rs @@ -6,46 +6,10 @@ use crate::proto; use crate::proto::DirectoryNode; use crate::proto::FileNode; use crate::proto::SymlinkNode; +use crate::tests::fixtures::*; use crate::tests::utils::*; -use lazy_static::lazy_static; use tempfile::TempDir; -const HELLOWORLD_BLOB_CONTENTS: &[u8] = b"Hello World!"; -const EMPTY_BLOB_CONTENTS: &[u8] = b""; - -lazy_static! { - static ref HELLOWORLD_BLOB_DIGEST: Vec = - blake3::hash(HELLOWORLD_BLOB_CONTENTS).as_bytes().to_vec(); - static ref EMPTY_BLOB_DIGEST: Vec = blake3::hash(EMPTY_BLOB_CONTENTS).as_bytes().to_vec(); - static ref DIRECTORY_WITH_KEEP: proto::Directory = proto::Directory { - directories: vec![], - files: vec![FileNode { - name: ".keep".to_string(), - digest: EMPTY_BLOB_DIGEST.to_vec(), - size: 0, - executable: false, - }], - symlinks: vec![], - }; - static ref DIRECTORY_COMPLICATED: proto::Directory = proto::Directory { - directories: vec![DirectoryNode { - name: "keep".to_string(), - digest: DIRECTORY_WITH_KEEP.digest(), - size: DIRECTORY_WITH_KEEP.size(), - }], - files: vec![FileNode { - name: ".keep".to_string(), - digest: EMPTY_BLOB_DIGEST.to_vec(), - size: 0, - executable: false, - }], - symlinks: vec![SymlinkNode { - name: "aa".to_string(), - target: "/nix/store/somewhereelse".to_string(), - }], - }; -} - #[test] fn single_symlink() { let tmpdir = TempDir::new().unwrap();