use bloock_storage::blob::blob_fs::FileSystem; use bloock_storage::blob::Blob; use bloock_storage::config::FsConfig; use tokio::runtime::Runtime; use std::any::type_name; use std::path::Path; use std::fs::read_dir; fn init_fs() -> FileSystem { let path = String::from("/tmp"); let config: FsConfig = FsConfig { path }; FileSystem::open(&config).unwrap() } fn check_empty_path(path: &str)->bool{ match read_dir(path) { Ok(contents) => { let len = contents.collect::>().len(); return len == 0; }, Err(e) => false } } fn type_of(_: T) -> &'static str { type_name::() } #[test] fn test_open() { let fs = init_fs(); assert_eq!( type_of(fs), "bloock_storage::blob::blob_fs::FileSystem", "FileSystem was not properly loaded" ); } #[test] fn test_read_exist() { let fs = init_fs(); let mut rt = Runtime::new().unwrap(); fs.write(&[0x46u8; 5], &[1; 32]).unwrap(); assert_eq!( rt.block_on(fs.read(&[0x46u8; 5])).unwrap(), Some(vec![1; 32]), "The value shouldn't be none" ); } #[test] fn test_read_not_exist() { let fs = init_fs(); let mut rt = Runtime::new().unwrap(); assert_eq!( rt.block_on(fs.read(&[0x46u8; 32])).is_err(), true, "The value should return error" ); } #[test] fn test_write() { let fs = init_fs(); fs.write(&[0x45u8; 32], &vec![0; 32]).unwrap(); let mut rt = Runtime::new().unwrap(); assert_eq!( rt.block_on(fs.read(&[0x45u8; 32])).unwrap().unwrap(), [0; 32], "The value read was not written or unable to read" ); } #[test] fn test_move_blobs_directory() { let path = String::from("tmp_blob/"); let config: FsConfig = FsConfig { path: path.clone() }; let fs =FileSystem::open(&config).unwrap(); fs.write(&[0x45u8; 32], &vec![0; 32]).unwrap(); let mut rt = Runtime::new().unwrap(); assert_eq!( rt.block_on(fs.read(&[0x45u8; 32])).unwrap().unwrap(), [0; 32], "The value read was not written or unable to read" ); let path_2 = String::from("tmp_blob_moved/"); let res = fs.move_to(path_2.clone()); //println!("{:?}", res.err().unwrap()); assert!(res.is_ok(), "error moving blobs"); assert!( Path::new("tmp_blob_moved").exists(), "blob folder was not moved" ); assert!(check_empty_path(&path.clone())); assert!(std::fs::remove_dir_all(path).is_ok()); assert!(std::fs::remove_dir_all(path_2).is_ok()); } #[test] fn test_move_blobs_directory_already_existing() { let path = String::from("tmp_blob_1/"); let config: FsConfig = FsConfig { path :path.clone() }; let fs =FileSystem::open(&config).unwrap(); let path_2 = String::from("tmp_blob_2/"); let config_2: FsConfig = FsConfig { path: path_2.clone()}; let fs_2 =FileSystem::open(&config_2).unwrap(); fs.write(&[0x45u8; 32], &vec![0; 32]).unwrap(); fs_2.write(&[0x45u8; 32], &vec![1;32]).unwrap(); let mut rt = Runtime::new().unwrap(); assert!(fs_2.move_to(path.clone()).is_ok(), "Error moving"); assert_eq!( rt.block_on(fs.read(&[0x45u8; 32])).unwrap().unwrap(), [1; 32], "The value read was not written or unable to read" ); assert!(check_empty_path(&path_2.clone())); assert!(std::fs::remove_dir_all(path).is_ok()); assert!(std::fs::remove_dir_all(path_2).is_ok()); } #[test] fn test_delete() { let fs = init_fs(); let mut rt = Runtime::new().unwrap(); fs.write(&[0x49u8; 5], &[1; 32]).unwrap(); assert_eq!( rt.block_on(fs.read(&[0x49u8; 5])).unwrap(), Some(vec![1; 32]), "The value shouldn't be none" ); assert!(rt.block_on(fs.delete([0x49u8; 5].to_vec())).is_ok(), "Expected ok"); assert_eq!( rt.block_on(fs.read(&[0x49u8; 5])).is_err(), true, "The value should return error" ); assert!(rt.block_on(fs.delete([0x49u8; 5].to_vec())).is_ok(), "Expected ok even if not existent"); }