Skip to content

Commit

Permalink
test: add test for config
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Nov 6, 2024
1 parent cd66f0d commit 9c13cbb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/config/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,7 @@ impl ConfigStorage for FileStorage {
#[cfg(test)]
mod tests {
use super::FileStorage;
use crate::config::{
ConfigStorage, PingapConf, CATEGORY_BASIC, CATEGORY_LOCATION,
CATEGORY_PLUGIN, CATEGORY_SERVER, CATEGORY_STORAGE, CATEGORY_UPSTREAM,
};
use crate::config::{list_category, ConfigStorage, PingapConf};
use nanoid::nanoid;
use pretty_assertions::assert_eq;

Expand All @@ -138,23 +135,26 @@ mod tests {
);

let path = format!("/tmp/{}", nanoid!(16));
tokio::fs::create_dir(&path).await.unwrap();
let file = format!("/tmp/{}.toml", nanoid!(16));
tokio::fs::write(&file, b"").await.unwrap();
let storage = FileStorage::new(&path).unwrap();
let file_storage = FileStorage::new(&file).unwrap();
let result = storage.load_config(false, false).await;
assert_eq!(true, result.is_ok());

let toml_data = include_bytes!("../../conf/pingap.toml");
let conf =
PingapConf::new(toml_data.to_vec().as_slice(), false).unwrap();

storage.save_config(&conf, CATEGORY_BASIC).await.unwrap();
storage.save_config(&conf, CATEGORY_UPSTREAM).await.unwrap();
storage.save_config(&conf, CATEGORY_LOCATION).await.unwrap();
storage.save_config(&conf, CATEGORY_PLUGIN).await.unwrap();
storage.save_config(&conf, CATEGORY_SERVER).await.unwrap();
storage.save_config(&conf, CATEGORY_STORAGE).await.unwrap();
for category in list_category().iter() {
storage.save_config(&conf, category).await.unwrap();
file_storage.save_config(&conf, category).await.unwrap();
}

let current_conf = storage.load_config(false, false).await.unwrap();
assert_eq!(current_conf.hash().unwrap(), conf.hash().unwrap());

let current_conf =
file_storage.load_config(false, false).await.unwrap();
assert_eq!(current_conf.hash().unwrap(), conf.hash().unwrap());
}
}
25 changes: 25 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,28 @@ pub async fn sync_config(path: &str) -> Result<()> {
pub use common::*;
pub use etcd::{EtcdStorage, ETCD_PROTOCOL};
pub use file::FileStorage;

#[cfg(test)]
mod tests {
use super::{
get_config_storage, load_config, support_observer, sync_config,
try_init_config_storage,
};
use tempfile::TempDir;

#[tokio::test]
async fn test_load_config() {
assert_eq!(true, get_config_storage().is_none());
try_init_config_storage("./conf/pingap.toml").unwrap();
let conf = load_config(true, false).await.unwrap();
assert_eq!("CAAD3074", conf.hash().unwrap());
assert_eq!(false, support_observer());
assert_eq!(true, get_config_storage().is_some());
let dir = TempDir::new().unwrap();
let file = dir.into_path().join("pingap.toml");
tokio::fs::write(&file, b"").await.unwrap();
sync_config(&file.to_string_lossy().to_string())
.await
.unwrap();
}
}

0 comments on commit 9c13cbb

Please sign in to comment.