tests: rust fmt

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
Thomas Lamprecht 2022-04-10 17:49:26 +02:00
parent 4de1c42c20
commit 6082d75966
5 changed files with 78 additions and 88 deletions

View File

@ -1,39 +1,40 @@
use std::sync::Arc;
use std::io::Cursor; use std::io::Cursor;
use std::io::{Read, Write, Seek, SeekFrom }; use std::io::{Read, Seek, SeekFrom, Write};
use std::sync::Arc;
use anyhow::{bail, Error}; use anyhow::{bail, Error};
use lazy_static::lazy_static; use lazy_static::lazy_static;
use pbs_tools::crypt_config::CryptConfig;
use pbs_datastore::{DataBlob, DataBlobReader, DataBlobWriter}; use pbs_datastore::{DataBlob, DataBlobReader, DataBlobWriter};
use pbs_tools::crypt_config::CryptConfig;
lazy_static! { lazy_static! {
static ref TEST_DATA: Vec<u8> = { static ref TEST_DATA: Vec<u8> = {
let mut data = Vec::new(); let mut data = Vec::new();
for i in 0..100_000 { for i in 0..100_000 {
data.push((i%255) as u8); data.push((i % 255) as u8);
} }
data data
}; };
static ref CRYPT_CONFIG: Arc<CryptConfig> = { static ref CRYPT_CONFIG: Arc<CryptConfig> = {
let key = [1u8; 32]; let key = [1u8; 32];
Arc::new(CryptConfig::new(key).unwrap()) Arc::new(CryptConfig::new(key).unwrap())
}; };
static ref TEST_DIGEST_PLAIN: [u8; 32] = [
static ref TEST_DIGEST_PLAIN: [u8; 32] = [83, 154, 96, 195, 167, 204, 38, 142, 204, 224, 130, 201, 24, 71, 2, 188, 130, 155, 177, 6, 162, 100, 61, 238, 38, 219, 63, 240, 191, 132, 87, 238]; 83, 154, 96, 195, 167, 204, 38, 142, 204, 224, 130, 201, 24, 71, 2, 188, 130, 155, 177, 6,
162, 100, 61, 238, 38, 219, 63, 240, 191, 132, 87, 238
static ref TEST_DIGEST_ENC: [u8; 32] = [50, 162, 191, 93, 255, 132, 9, 14, 127, 23, 92, 39, 246, 102, 245, 204, 130, 104, 4, 106, 182, 239, 218, 14, 80, 17, 150, 188, 239, 253, 198, 117]; ];
static ref TEST_DIGEST_ENC: [u8; 32] = [
50, 162, 191, 93, 255, 132, 9, 14, 127, 23, 92, 39, 246, 102, 245, 204, 130, 104, 4, 106,
182, 239, 218, 14, 80, 17, 150, 188, 239, 253, 198, 117
];
} }
fn verify_test_blob(mut cursor: Cursor<Vec<u8>>, digest: &[u8; 32]) -> Result<(), Error> { fn verify_test_blob(mut cursor: Cursor<Vec<u8>>, digest: &[u8; 32]) -> Result<(), Error> {
// run read tests with different buffer sizes // run read tests with different buffer sizes
for size in [1, 3, 64*1024].iter() { for size in [1, 3, 64 * 1024].iter() {
println!("Starting DataBlobReader test (size = {})", size); println!("Starting DataBlobReader test (size = {})", size);
cursor.seek(SeekFrom::Start(0))?; cursor.seek(SeekFrom::Start(0))?;
@ -44,7 +45,9 @@ fn verify_test_blob(mut cursor: Cursor<Vec<u8>>, digest: &[u8; 32]) -> Result<()
let mut buf = vec![0u8; *size]; let mut buf = vec![0u8; *size];
loop { loop {
let count = reader.read(&mut buf)?; let count = reader.read(&mut buf)?;
if count == 0 { break; } if count == 0 {
break;
}
buffer.extend(&buf[..count]); buffer.extend(&buf[..count]);
} }

View File

@ -5,7 +5,6 @@ use anyhow::Error;
use pbs_client::pxar::*; use pbs_client::pxar::*;
fn run_test(dir_name: &str) -> Result<(), Error> { fn run_test(dir_name: &str) -> Result<(), Error> {
println!("run pxar test {}", dir_name); println!("run pxar test {}", dir_name);
Command::new("casync") Command::new("casync")
@ -23,8 +22,10 @@ fn run_test(dir_name: &str) -> Result<(), Error> {
let writer = pxar::encoder::sync::StandardWriter::new(writer); let writer = pxar::encoder::sync::StandardWriter::new(writer);
let dir = nix::dir::Dir::open( let dir = nix::dir::Dir::open(
dir_name, nix::fcntl::OFlag::O_NOFOLLOW, dir_name,
nix::sys::stat::Mode::empty())?; nix::fcntl::OFlag::O_NOFOLLOW,
nix::sys::stat::Mode::empty(),
)?;
let options = PxarCreateOptions { let options = PxarCreateOptions {
entries_max: ENCODER_MAX_ENTRIES, entries_max: ENCODER_MAX_ENTRIES,
@ -52,7 +53,6 @@ fn run_test(dir_name: &str) -> Result<(), Error> {
} }
fn run_all_tests() -> Result<(), Error> { fn run_all_tests() -> Result<(), Error> {
run_test("tests/catar_data/test_file")?; run_test("tests/catar_data/test_file")?;
run_test("tests/catar_data/test_symlink")?; run_test("tests/catar_data/test_symlink")?;
@ -66,9 +66,9 @@ fn run_all_tests() -> Result<(), Error> {
Ok(()) Ok(())
} }
#[test] #[ignore] #[test]
#[ignore]
fn catar_simple() { fn catar_simple() {
if let Err(err) = run_all_tests() { if let Err(err) = run_all_tests() {
eprintln!("Error: {}", err); eprintln!("Error: {}", err);
std::process::exit(1); std::process::exit(1);

View File

@ -12,7 +12,6 @@ fn get_prune_list(
return_kept: bool, return_kept: bool,
options: &PruneOptions, options: &PruneOptions,
) -> Vec<PathBuf> { ) -> Vec<PathBuf> {
let mut prune_info = compute_prune_info(list, options).unwrap(); let mut prune_info = compute_prune_info(list, options).unwrap();
prune_info.reverse(); prune_info.reverse();
@ -29,11 +28,7 @@ fn get_prune_list(
.collect() .collect()
} }
fn create_info( fn create_info(snapshot: &str, partial: bool) -> BackupInfo {
snapshot: &str,
partial: bool,
) -> BackupInfo {
let backup_dir: BackupDir = snapshot.parse().unwrap(); let backup_dir: BackupDir = snapshot.parse().unwrap();
let mut files = Vec::new(); let mut files = Vec::new();
@ -42,13 +37,14 @@ fn create_info(
files.push(String::from(MANIFEST_BLOB_NAME)); files.push(String::from(MANIFEST_BLOB_NAME));
} }
BackupInfo { backup_dir, files, protected: false } BackupInfo {
backup_dir,
files,
protected: false,
}
} }
fn create_info_protected( fn create_info_protected(snapshot: &str, partial: bool) -> BackupInfo {
snapshot: &str,
partial: bool,
) -> BackupInfo {
let mut info = create_info(snapshot, partial); let mut info = create_info(snapshot, partial);
info.protected = true; info.protected = true;
info info
@ -58,34 +54,35 @@ fn create_info_protected(
fn test_prune_protected() -> Result<(), Error> { fn test_prune_protected() -> Result<(), Error> {
let mut orig_list = Vec::new(); let mut orig_list = Vec::new();
orig_list.push(create_info_protected("host/elsa/2019-11-15T09:39:15Z", false)); orig_list.push(create_info_protected(
"host/elsa/2019-11-15T09:39:15Z",
false,
));
orig_list.push(create_info("host/elsa/2019-11-15T10:39:15Z", false)); orig_list.push(create_info("host/elsa/2019-11-15T10:39:15Z", false));
orig_list.push(create_info("host/elsa/2019-11-15T10:49:15Z", false)); orig_list.push(create_info("host/elsa/2019-11-15T10:49:15Z", false));
orig_list.push(create_info_protected("host/elsa/2019-11-15T10:59:15Z", false)); orig_list.push(create_info_protected(
"host/elsa/2019-11-15T10:59:15Z",
false,
));
eprintln!("{:?}", orig_list); eprintln!("{:?}", orig_list);
let mut options = PruneOptions::default(); let mut options = PruneOptions::default();
options.keep_last = Some(1); options.keep_last = Some(1);
let remove_list = get_prune_list(orig_list.clone(), false, &options); let remove_list = get_prune_list(orig_list.clone(), false, &options);
let expect: Vec<PathBuf> = vec![ let expect: Vec<PathBuf> = vec![PathBuf::from("host/elsa/2019-11-15T10:39:15Z")];
PathBuf::from("host/elsa/2019-11-15T10:39:15Z"),
];
assert_eq!(remove_list, expect); assert_eq!(remove_list, expect);
let mut options = PruneOptions::default(); let mut options = PruneOptions::default();
options.keep_hourly = Some(1); options.keep_hourly = Some(1);
let remove_list = get_prune_list(orig_list.clone(), false, &options); let remove_list = get_prune_list(orig_list.clone(), false, &options);
let expect: Vec<PathBuf> = vec![ let expect: Vec<PathBuf> = vec![PathBuf::from("host/elsa/2019-11-15T10:39:15Z")];
PathBuf::from("host/elsa/2019-11-15T10:39:15Z"),
];
assert_eq!(remove_list, expect); assert_eq!(remove_list, expect);
Ok(()) Ok(())
} }
#[test] #[test]
fn test_prune_hourly() -> Result<(), Error> { fn test_prune_hourly() -> Result<(), Error> {
let mut orig_list = Vec::new(); let mut orig_list = Vec::new();
orig_list.push(create_info("host/elsa/2019-11-15T09:39:15Z", false)); orig_list.push(create_info("host/elsa/2019-11-15T09:39:15Z", false));
@ -121,7 +118,6 @@ fn test_prune_hourly() -> Result<(), Error> {
#[test] #[test]
fn test_prune_simple2() -> Result<(), Error> { fn test_prune_simple2() -> Result<(), Error> {
let mut orig_list = Vec::new(); let mut orig_list = Vec::new();
orig_list.push(create_info("host/elsa/2018-11-15T11:59:15Z", false)); orig_list.push(create_info("host/elsa/2018-11-15T11:59:15Z", false));
@ -138,9 +134,7 @@ fn test_prune_simple2() -> Result<(), Error> {
let mut options = PruneOptions::default(); let mut options = PruneOptions::default();
options.keep_daily = Some(1); options.keep_daily = Some(1);
let remove_list = get_prune_list(list, true, &options); let remove_list = get_prune_list(list, true, &options);
let expect: Vec<PathBuf> = vec![ let expect: Vec<PathBuf> = vec![PathBuf::from("host/elsa/2019-12-04T11:59:15Z")];
PathBuf::from("host/elsa/2019-12-04T11:59:15Z"),
];
assert_eq!(remove_list, expect); assert_eq!(remove_list, expect);
let list = orig_list.clone(); let list = orig_list.clone();
@ -194,7 +188,6 @@ fn test_prune_simple2() -> Result<(), Error> {
#[test] #[test]
fn test_prune_simple() -> Result<(), Error> { fn test_prune_simple() -> Result<(), Error> {
let mut orig_list = Vec::new(); let mut orig_list = Vec::new();
orig_list.push(create_info("host/elsa/2019-12-02T11:59:15Z", false)); orig_list.push(create_info("host/elsa/2019-12-02T11:59:15Z", false));
@ -215,9 +208,7 @@ fn test_prune_simple() -> Result<(), Error> {
let mut options = PruneOptions::default(); let mut options = PruneOptions::default();
options.keep_last = Some(3); options.keep_last = Some(3);
let remove_list = get_prune_list(list, false, &options); let remove_list = get_prune_list(list, false, &options);
let expect: Vec<PathBuf> = vec![ let expect: Vec<PathBuf> = vec![PathBuf::from("host/elsa/2019-12-02T11:59:15Z")];
PathBuf::from("host/elsa/2019-12-02T11:59:15Z"),
];
assert_eq!(remove_list, expect); assert_eq!(remove_list, expect);
let list = orig_list.clone(); let list = orig_list.clone();

View File

@ -2,8 +2,8 @@ use std::collections::HashSet;
use anyhow::{bail, format_err, Error}; use anyhow::{bail, format_err, Error};
use proxmox_router::{ApiMethod, Permission, Router, SubRoute, SubdirMap};
use proxmox_schema::*; use proxmox_schema::*;
use proxmox_router::{ApiMethod, Permission, Router, SubdirMap, SubRoute};
use proxmox_backup::api2; use proxmox_backup::api2;
@ -11,18 +11,19 @@ use proxmox_backup::api2;
// correctly sorted. // correctly sorted.
fn verify_object_schema(schema: &ObjectSchema) -> Result<(), Error> { fn verify_object_schema(schema: &ObjectSchema) -> Result<(), Error> {
let map = schema.properties; let map = schema.properties;
if !map.is_empty() { if !map.is_empty() {
for i in 1..map.len() { for i in 1..map.len() {
if map[i].0 <= map[i - 1].0 {
if map[i].0 <= map[i-1].0 {
for (name, _, _) in map.iter() { for (name, _, _) in map.iter() {
eprintln!("{}", name); eprintln!("{}", name);
} }
bail!("found unsorted property map ({} <= {})", map[i].0, map[i-1].0); bail!(
"found unsorted property map ({} <= {})",
map[i].0,
map[i - 1].0
);
} }
} }
} }
@ -79,15 +80,18 @@ fn verify_schema(schema: &Schema) -> Result<(), Error> {
} }
fn verify_access_permissions(permission: &Permission) -> Result<(), Error> { fn verify_access_permissions(permission: &Permission) -> Result<(), Error> {
match permission { match permission {
Permission::Or(list) => { Permission::Or(list) => {
for perm in list.iter() { verify_access_permissions(perm)?; } for perm in list.iter() {
verify_access_permissions(perm)?;
}
} }
Permission::And(list) => { Permission::And(list) => {
for perm in list.iter() { verify_access_permissions(perm)?; } for perm in list.iter() {
verify_access_permissions(perm)?;
}
} }
Permission::Privilege(path_comp, ..)=> { Permission::Privilege(path_comp, ..) => {
let path = format!("/{}", path_comp.join("/")); let path = format!("/{}", path_comp.join("/"));
pbs_config::acl::check_acl_path(&path)?; pbs_config::acl::check_acl_path(&path)?;
} }
@ -96,12 +100,7 @@ fn verify_access_permissions(permission: &Permission) -> Result<(), Error> {
Ok(()) Ok(())
} }
fn verify_api_method( fn verify_api_method(method: &str, path: &str, info: &ApiMethod) -> Result<(), Error> {
method: &str,
path: &str,
info: &ApiMethod
) -> Result<(), Error>
{
match &info.parameters { match &info.parameters {
ParameterSchema::Object(obj) => { ParameterSchema::Object(obj) => {
verify_object_schema(obj) verify_object_schema(obj)
@ -122,22 +121,20 @@ fn verify_api_method(
Ok(()) Ok(())
} }
fn verify_dirmap( fn verify_dirmap(path: &str, dirmap: SubdirMap) -> Result<(), Error> {
path: &str,
dirmap: SubdirMap,
) -> Result<(), Error> {
if !dirmap.is_empty() { if !dirmap.is_empty() {
for i in 1..dirmap.len() { for i in 1..dirmap.len() {
if dirmap[i].0 <= dirmap[i - 1].0 {
if dirmap[i].0 <= dirmap[i-1].0 {
for (name, _) in dirmap.iter() { for (name, _) in dirmap.iter() {
eprintln!("{}/{}", path, name); eprintln!("{}/{}", path, name);
} }
bail!("found unsorted dirmap at {:?} ({} <= {})", path, dirmap[i].0, dirmap[i-1].0); bail!(
"found unsorted dirmap at {:?} ({} <= {})",
path,
dirmap[i].0,
dirmap[i - 1].0
);
} }
} }
} }
@ -150,7 +147,6 @@ fn verify_dirmap(
} }
fn verify_router(path: &str, router: &Router) -> Result<(), Error> { fn verify_router(path: &str, router: &Router) -> Result<(), Error> {
println!("Verify {}", path); println!("Verify {}", path);
if let Some(api_method) = router.get { if let Some(api_method) = router.get {
@ -182,7 +178,6 @@ fn verify_router(path: &str, router: &Router) -> Result<(), Error> {
#[test] #[test]
fn verify_backup_api() -> Result<(), Error> { fn verify_backup_api() -> Result<(), Error> {
let api = &api2::backup::BACKUP_API_ROUTER; let api = &api2::backup::BACKUP_API_ROUTER;
verify_router("backup-api", api)?; verify_router("backup-api", api)?;
@ -191,7 +186,6 @@ fn verify_backup_api() -> Result<(), Error> {
#[test] #[test]
fn verify_reader_api() -> Result<(), Error> { fn verify_reader_api() -> Result<(), Error> {
let api = &api2::reader::READER_API_ROUTER; let api = &api2::reader::READER_API_ROUTER;
verify_router("reader-api", api)?; verify_router("reader-api", api)?;
@ -200,7 +194,6 @@ fn verify_reader_api() -> Result<(), Error> {
#[test] #[test]
fn verify_root_api() -> Result<(), Error> { fn verify_root_api() -> Result<(), Error> {
let api = &api2::ROUTER; let api = &api2::ROUTER;
verify_router("root", api)?; verify_router("root", api)?;

View File

@ -2,11 +2,11 @@ use anyhow::{bail, Error};
extern crate proxmox_backup; extern crate proxmox_backup;
extern crate tokio;
extern crate nix; extern crate nix;
extern crate tokio;
use proxmox_sys::fs::CreateOptions;
use proxmox_lang::try_block; use proxmox_lang::try_block;
use proxmox_sys::fs::CreateOptions;
use proxmox_sys::{task_log, WorkerTaskContext}; use proxmox_sys::{task_log, WorkerTaskContext};
use pbs_api_types::{Authid, UPID}; use pbs_api_types::{Authid, UPID};
@ -14,7 +14,6 @@ use pbs_api_types::{Authid, UPID};
use proxmox_rest_server::{CommandSocket, WorkerTask}; use proxmox_rest_server::{CommandSocket, WorkerTask};
fn garbage_collection(worker: &WorkerTask) -> Result<(), Error> { fn garbage_collection(worker: &WorkerTask) -> Result<(), Error> {
task_log!(worker, "start garbage collection"); task_log!(worker, "start garbage collection");
for i in 0..50 { for i in 0..50 {
@ -30,7 +29,6 @@ fn garbage_collection(worker: &WorkerTask) -> Result<(), Error> {
Ok(()) Ok(())
} }
#[test] #[test]
#[ignore] #[ignore]
fn worker_task_abort() -> Result<(), Error> { fn worker_task_abort() -> Result<(), Error> {
@ -47,9 +45,10 @@ fn worker_task_abort() -> Result<(), Error> {
let rt = tokio::runtime::Runtime::new().unwrap(); let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async move { rt.block_on(async move {
let mut commando_sock = CommandSocket::new( let mut commando_sock = CommandSocket::new(
proxmox_rest_server::our_ctrl_sock(), nix::unistd::Gid::current()); proxmox_rest_server::our_ctrl_sock(),
nix::unistd::Gid::current(),
);
let init_result: Result<(), Error> = try_block!({ let init_result: Result<(), Error> = try_block!({
proxmox_rest_server::register_task_control_commands(&mut commando_sock)?; proxmox_rest_server::register_task_control_commands(&mut commando_sock)?;
@ -61,7 +60,7 @@ fn worker_task_abort() -> Result<(), Error> {
return; return;
} }
if let Err(err) = commando_sock.spawn() { if let Err(err) = commando_sock.spawn() {
eprintln!("unable to spawn command socket - {}", err); eprintln!("unable to spawn command socket - {}", err);
return; return;
} }
@ -82,7 +81,9 @@ fn worker_task_abort() -> Result<(), Error> {
println!("got expected error: {}", err); println!("got expected error: {}", err);
} else { } else {
let mut data = errmsg.lock().unwrap(); let mut data = errmsg.lock().unwrap();
*data = Some(String::from("thread finished - seems abort did not work as expected")); *data = Some(String::from(
"thread finished - seems abort did not work as expected",
));
} }
Ok(()) Ok(())
@ -96,15 +97,17 @@ fn worker_task_abort() -> Result<(), Error> {
Ok(wid) => { Ok(wid) => {
println!("WORKER: {}", wid); println!("WORKER: {}", wid);
proxmox_rest_server::abort_worker_nowait(wid.parse::<UPID>().unwrap()); proxmox_rest_server::abort_worker_nowait(wid.parse::<UPID>().unwrap());
proxmox_rest_server::wait_for_local_worker(&wid).await.unwrap(); proxmox_rest_server::wait_for_local_worker(&wid)
} .await
.unwrap();
}
} }
}); });
let data = errmsg.lock().unwrap(); let data = errmsg.lock().unwrap();
match *data { match *data {
Some(ref err) => bail!("Error: {}", err), Some(ref err) => bail!("Error: {}", err),
None => {}, None => {}
} }
Ok(()) Ok(())