Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: replace hardcoding /tmp/xxx path #1018

Merged
merged 4 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ uuid = { version = "1", features = ["v4"] }
workspace-hack = { version = "0.1", path = "../../workspace-hack" }

[dev-dependencies]
tempfile = "3"
test-macros = { path = "../test-macros" }
57 changes: 30 additions & 27 deletions crates/engine/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,15 +335,18 @@ mod test {
use clippy_utilities::NumericCast;
use test_macros::abort_on_panic;

use tempfile::TempDir;

use super::*;
use crate::api::snapshot_api::SnapshotApi;

const TESTTABLES: [&'static str; 3] = ["kv", "lease", "auth"];

#[test]
fn write_batch_into_a_non_existing_table_should_fail() {
let dir = PathBuf::from("/tmp/write_batch_into_a_non_existing_table_should_fail");
let rocks_engine_path = dir.join("rocks_engine");
let dir =
TempDir::with_prefix("/tmp/write_batch_into_a_non_existing_table_should_fail").unwrap();
let rocks_engine_path = dir.path().join("rocks_engine");
let engines = vec![
Engine::new(EngineType::Memory, &TESTTABLES).unwrap(),
Engine::new(EngineType::Rocks(rocks_engine_path), &TESTTABLES).unwrap(),
Expand All @@ -363,13 +366,13 @@ mod test {
let delete_range = WriteOperation::new_delete_range("hello", b"hello", b"world");
assert!(engine.write_multi(vec![delete_range], false).is_err());
}
std::fs::remove_dir_all(dir).unwrap();
dir.close().unwrap();
}

#[test]
fn write_batch_should_success() {
let dir = PathBuf::from("/tmp/write_batch_should_success");
let rocks_engine_path = dir.join("rocks_engine");
let dir = TempDir::with_prefix("/tmp/write_batch_should_success").unwrap();
let rocks_engine_path = dir.path().join("rocks_engine");
let engines = vec![
Engine::new(EngineType::Memory, &TESTTABLES).unwrap(),
Engine::new(EngineType::Rocks(rocks_engine_path), &TESTTABLES).unwrap(),
Expand Down Expand Up @@ -409,13 +412,13 @@ mod test {
assert!(engine.get("kv", &get_key_1).unwrap().is_some());
assert!(engine.get("kv", &get_key_2).unwrap().is_none());
}
std::fs::remove_dir_all(dir).unwrap();
dir.close().unwrap();
}

#[test]
fn get_operation_should_success() {
let dir = PathBuf::from("/tmp/get_operation_should_success");
let rocks_engine_path = dir.join("rocks_engine");
let dir = TempDir::with_prefix("/tmp/get_operation_should_success").unwrap();
let rocks_engine_path = dir.path().join("rocks_engine");
let engines = vec![
Engine::new(EngineType::Memory, &TESTTABLES).unwrap(),
Engine::new(EngineType::Rocks(rocks_engine_path), &TESTTABLES).unwrap(),
Expand Down Expand Up @@ -447,17 +450,17 @@ mod test {
.collect::<Vec<(Vec<u8>, Vec<u8>)>>();
assert_eq!(res_3.sort(), expected_all_values.sort());
}
std::fs::remove_dir_all(dir).unwrap();
dir.close().unwrap();
}

#[tokio::test]
#[abort_on_panic]
async fn snapshot_should_work() {
let dir = PathBuf::from("/tmp/snapshot_should_work");
let origin_data_dir = dir.join("origin");
let recover_data_dir = dir.join("recover");
let snapshot_dir = dir.join("snapshot");
let snapshot_bak_dir = dir.join("snapshot_bak");
let dir = TempDir::with_prefix("/tmp/snapshot_should_work").unwrap();
let origin_data_dir = dir.path().join("origin");
let recover_data_dir = dir.path().join("recover");
let snapshot_dir = dir.path().join("snapshot");
let snapshot_bak_dir = dir.path().join("snapshot_bak");
let engines = vec![
Engine::new(EngineType::Memory, &TESTTABLES).unwrap(),
Engine::new(EngineType::Rocks(origin_data_dir), &TESTTABLES).unwrap(),
Expand Down Expand Up @@ -508,14 +511,14 @@ mod test {
assert!(value2.is_none());
}

std::fs::remove_dir_all(&dir).unwrap();
dir.close().unwrap();
}

#[tokio::test]
#[abort_on_panic]
async fn txn_write_multi_should_success() {
let dir = PathBuf::from("/tmp/txn_write_multi_should_success");
let rocks_engine_path = dir.join("rocks_engine");
let dir = TempDir::with_prefix("/tmp/txn_write_multi_should_success").unwrap();
let rocks_engine_path = dir.path().join("rocks_engine");
let engines = vec![
Engine::new(EngineType::Memory, &TESTTABLES).unwrap(),
Engine::new(EngineType::Rocks(rocks_engine_path), &TESTTABLES).unwrap(),
Expand Down Expand Up @@ -558,14 +561,14 @@ mod test {

txn.commit().unwrap();
}
std::fs::remove_dir_all(dir).unwrap();
dir.close().unwrap();
}

#[tokio::test]
#[abort_on_panic]
async fn txn_get_operation_should_success() {
let dir = PathBuf::from("/tmp/txn_get_operation_should_success");
let rocks_engine_path = dir.join("rocks_engine");
let dir = TempDir::with_prefix("/tmp/txn_get_operation_should_success").unwrap();
let rocks_engine_path = dir.path().join("rocks_engine");
let engines = vec![
Engine::new(EngineType::Memory, &TESTTABLES).unwrap(),
Engine::new(EngineType::Rocks(rocks_engine_path), &TESTTABLES).unwrap(),
Expand All @@ -592,14 +595,14 @@ mod test {
assert_eq!(res_2, expected_multi_values);
txn.commit().unwrap();
}
std::fs::remove_dir_all(dir).unwrap();
dir.close().unwrap();
}

#[tokio::test]
#[abort_on_panic]
async fn txn_operation_is_atomic() {
let dir = PathBuf::from("/tmp/txn_operation_should_be_atomic");
let rocks_engine_path = dir.join("rocks_engine");
let dir = TempDir::with_prefix("/tmp/txn_operation_should_be_atomic").unwrap();
let rocks_engine_path = dir.path().join("rocks_engine");
let engines = vec![
Engine::new(EngineType::Memory, &TESTTABLES).unwrap(),
Engine::new(EngineType::Rocks(rocks_engine_path), &TESTTABLES).unwrap(),
Expand All @@ -625,14 +628,14 @@ mod test {
assert_eq!(engine.get("kv", key).unwrap().unwrap(), val);
}
}
std::fs::remove_dir_all(dir).unwrap();
dir.close().unwrap();
}

#[tokio::test]
#[abort_on_panic]
async fn txn_revert_should_success() {
let dir = PathBuf::from("/tmp/txn_revert_should_success");
let rocks_engine_path = dir.join("rocks_engine");
let dir = TempDir::with_prefix("/tmp/txn_revert_should_success").unwrap();
let rocks_engine_path = dir.path().join("rocks_engine");
let engines = vec![
Engine::new(EngineType::Memory, &TESTTABLES).unwrap(),
Engine::new(EngineType::Rocks(rocks_engine_path), &TESTTABLES).unwrap(),
Expand All @@ -659,6 +662,6 @@ mod test {
assert!(engine.get("kv", key).unwrap().is_none());
}
}
std::fs::remove_dir_all(dir).unwrap();
dir.close().unwrap();
}
}
18 changes: 9 additions & 9 deletions crates/engine/src/rocksdb_engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,8 +725,7 @@ impl SnapshotApi for RocksSnapshot {

#[cfg(test)]
mod test {
use std::env::temp_dir;

use tempfile::TempDir;
use test_macros::abort_on_panic;

use super::*;
Expand All @@ -735,9 +734,9 @@ mod test {
#[tokio::test]
#[abort_on_panic]
async fn test_rocks_errors() {
let dir = PathBuf::from("/tmp/test_rocks_errors");
let engine_path = dir.join("engine");
let snapshot_path = dir.join("snapshot");
let dir = TempDir::with_prefix("/tmp/test_rocks_errors").unwrap();
let engine_path = dir.path().join("engine");
let snapshot_path = dir.path().join("snapshot");
let engine = RocksEngine::new(engine_path, &TEST_TABLES).unwrap();
let res = engine.get("not_exist", "key");
assert!(res.is_err());
Expand All @@ -756,13 +755,14 @@ mod test {
};
let res = engine.apply_snapshot(fake_snapshot, &["not_exist"]).await;
assert!(res.is_err());
fs::remove_dir_all(dir).unwrap();
dir.close().unwrap();
}

#[test]
fn test_engine_size() {
let path = temp_dir().join("test_engine_size");
let engine = RocksEngine::new(path.clone(), &TEST_TABLES).unwrap();
let dir = TempDir::with_prefix("/tmp/test_engine_size").unwrap();
let engine_path = dir.path().join("engine");
let engine = RocksEngine::new(engine_path, &TEST_TABLES).unwrap();
let size1 = engine.file_size().unwrap();
engine
.write_multi(
Expand All @@ -776,6 +776,6 @@ mod test {
.unwrap();
let size2 = engine.file_size().unwrap();
assert!(size2 > size1);
fs::remove_dir_all(path).unwrap();
dir.close().unwrap();
}
}
1 change: 1 addition & 0 deletions crates/xline/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ mockall = "0.12.1"
rand = "0.8.5"
strum = "0.26"
strum_macros = "0.26.4"
tempfile = "3"
test-macros = { path = "../test-macros" }
xline-client = { path = "../xline-client" }
xline-test-utils = { path = "../xline-test-utils" }
11 changes: 6 additions & 5 deletions crates/xline/src/server/maintenance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,9 @@ fn snapshot_stream(

#[cfg(test)]
mod test {
use std::{error::Error, path::PathBuf};
use std::error::Error;

use tempfile::TempDir;
use test_macros::abort_on_panic;
use tokio_stream::StreamExt;
use utils::config::EngineConfig;
Expand All @@ -285,9 +286,9 @@ mod test {
#[tokio::test]
#[abort_on_panic]
async fn test_snapshot_stream() -> Result<(), Box<dyn Error>> {
let dir = PathBuf::from("/tmp/test_snapshot_rpc");
let db_path = dir.join("db");
let snapshot_path = dir.join("snapshot");
let dir = TempDir::with_prefix("/tmp/test_snapshot_rpc").unwrap();
let db_path = dir.path().join("db");
let snapshot_path = dir.path().join("snapshot");

let db = DB::open(&EngineConfig::RocksDB(db_path.clone()))?;
let header_gen = HeaderGenerator::new(0, 0);
Expand All @@ -310,7 +311,7 @@ mod test {
assert_eq!(snap1_data, snap2_data);

snap2.clean().await.unwrap();
std::fs::remove_dir_all(dir).unwrap();
dir.close().unwrap();
Ok(())
}
}
35 changes: 18 additions & 17 deletions crates/xline/src/storage/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ pub enum WriteOp<'a> {

#[cfg(test)]
mod test {
use std::path::PathBuf;
use tempfile::TempDir;

use engine::SnapshotApi;
use test_macros::abort_on_panic;
Expand All @@ -361,8 +361,9 @@ mod test {
#[tokio::test]
#[abort_on_panic]
async fn test_reset() -> Result<(), ExecuteError> {
let data_dir = PathBuf::from("/tmp/test_reset");
let db = DB::open(&EngineConfig::RocksDB(data_dir.clone()))?;
let dir = TempDir::with_prefix("/tmp/test_reset").unwrap();
let engine_path = dir.path().join("engine");
let db = DB::open(&EngineConfig::RocksDB(engine_path))?;

let revision = Revision::new(1, 1);
let key = revision.encode_to_vec();
Expand All @@ -382,17 +383,17 @@ mod test {
let res = db.get_all(KV_TABLE)?;
assert!(res.is_empty());

std::fs::remove_dir_all(data_dir).unwrap();
dir.close().unwrap();
Ok(())
}

#[tokio::test]
#[abort_on_panic]
async fn test_db_snapshot() -> Result<(), ExecuteError> {
let dir = PathBuf::from("/tmp/test_db_snapshot");
let origin_db_path = dir.join("origin_db");
let new_db_path = dir.join("new_db");
let snapshot_path = dir.join("snapshot");
let dir = TempDir::with_prefix("/tmp/test_db_snapshot").unwrap();
let origin_db_path = dir.path().join("origin_db");
let new_db_path = dir.path().join("new_db");
let snapshot_path = dir.path().join("snapshot");
let origin_db = DB::open(&EngineConfig::RocksDB(origin_db_path))?;

let revision = Revision::new(1, 1);
Expand All @@ -412,39 +413,39 @@ mod test {
let res = new_db.get_values(KV_TABLE, &[&key])?;
assert_eq!(res, vec![Some(kv.encode_to_vec())]);

std::fs::remove_dir_all(dir).unwrap();
dir.close().unwrap();
Ok(())
}

#[tokio::test]
#[abort_on_panic]
async fn test_db_snapshot_wrong_type() -> Result<(), ExecuteError> {
let dir = PathBuf::from("/tmp/test_db_snapshot_wrong_type");
let db_path = dir.join("db");
let snapshot_path = dir.join("snapshot");
let dir = TempDir::with_prefix("/tmp/test_db_snapshot_wrong_type").unwrap();
let db_path = dir.path().join("db");
let snapshot_path = dir.path().join("snapshot");
let rocks_db = DB::open(&EngineConfig::RocksDB(db_path))?;
let mem_db = DB::open(&EngineConfig::Memory)?;

let rocks_snap = rocks_db.get_snapshot(snapshot_path)?;
let res = mem_db.reset(Some(rocks_snap)).await;
assert!(res.is_err());

std::fs::remove_dir_all(dir).unwrap();
dir.close().unwrap();
Ok(())
}

#[tokio::test]
#[abort_on_panic]
async fn test_get_snapshot() -> Result<(), ExecuteError> {
let dir = PathBuf::from("/tmp/test_get_snapshot");
let data_path = dir.join("data");
let snapshot_path = dir.join("snapshot");
let dir = TempDir::with_prefix("/tmp/test_get_snapshot").unwrap();
let data_path = dir.path().join("data");
let snapshot_path = dir.path().join("snapshot");
let db = DB::open(&EngineConfig::RocksDB(data_path))?;
let mut res = db.get_snapshot(snapshot_path)?;
assert_ne!(res.size(), 0);
res.clean().await.unwrap();

std::fs::remove_dir_all(dir).unwrap();
dir.close().unwrap();
Ok(())
}

Expand Down
Loading