2021-10-13 19:20:06 +03:00
use std ::path ::Path ;
use std ::process ::Command ;
use anyhow ::{ bail , Error } ;
2024-01-31 18:26:01 +03:00
use proxmox_rrd ::rrd ::Database ;
2021-11-23 19:57:00 +03:00
use proxmox_sys ::fs ::CreateOptions ;
2021-10-13 19:20:06 +03:00
fn compare_file ( fn1 : & str , fn2 : & str ) -> Result < ( ) , Error > {
let status = Command ::new ( " /usr/bin/cmp " )
. arg ( fn1 )
. arg ( fn2 )
. status ( )
. expect ( " failed to execute process " ) ;
if ! status . success ( ) {
bail! ( " file compare failed " ) ;
}
Ok ( ( ) )
}
const RRD_V2_FN : & str = " ./tests/testdata/cpu.rrd_v2 " ;
// make sure we can load and convert RRD v1
#[ test ]
2024-01-31 16:51:54 +03:00
#[ cfg(feature = " rrd_v1 " ) ]
2021-10-13 19:20:06 +03:00
fn upgrade_from_rrd_v1 ( ) -> Result < ( ) , Error > {
2024-01-31 16:51:54 +03:00
const RRD_V1_FN : & str = " ./tests/testdata/cpu.rrd_v1 " ;
2024-01-31 18:26:01 +03:00
let rrd = Database ::load ( Path ::new ( RRD_V1_FN ) , true ) ? ;
2021-10-13 19:20:06 +03:00
const RRD_V2_NEW_FN : & str = " ./tests/testdata/cpu.rrd_v2.upgraded " ;
let new_path = Path ::new ( RRD_V2_NEW_FN ) ;
2021-10-19 19:41:03 +03:00
rrd . save ( new_path , CreateOptions ::new ( ) , true ) ? ;
2021-10-13 19:20:06 +03:00
let result = compare_file ( RRD_V2_FN , RRD_V2_NEW_FN ) ;
let _ = std ::fs ::remove_file ( RRD_V2_NEW_FN ) ;
result ? ;
Ok ( ( ) )
}
// make sure we can load and save RRD v2
#[ test ]
fn load_and_save_rrd_v2 ( ) -> Result < ( ) , Error > {
2024-01-31 18:26:01 +03:00
let rrd = Database ::load ( Path ::new ( RRD_V2_FN ) , true ) ? ;
2021-10-13 19:20:06 +03:00
const RRD_V2_NEW_FN : & str = " ./tests/testdata/cpu.rrd_v2.saved " ;
let new_path = Path ::new ( RRD_V2_NEW_FN ) ;
2021-10-19 19:41:03 +03:00
rrd . save ( new_path , CreateOptions ::new ( ) , true ) ? ;
2021-10-13 19:20:06 +03:00
let result = compare_file ( RRD_V2_FN , RRD_V2_NEW_FN ) ;
let _ = std ::fs ::remove_file ( RRD_V2_NEW_FN ) ;
result ? ;
Ok ( ( ) )
}