1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
// libnbd Rust test case
// Copyright Tage Johansson
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#![deny(warnings)]
use std::env;
use std::path::Path;
use std::sync::Arc;
use std::sync::Mutex;
fn block_status_get_entries(
nbd: &libnbd::Handle,
count: u64,
offset: u64,
flags: Option<libnbd::CmdFlag>,
) -> Vec<u32> {
let entries = Arc::new(Mutex::new(None));
let entries_clone = entries.clone();
nbd.block_status(
count,
offset,
move |metacontext, _, entries, err| {
assert_eq!(*err, 0);
if metacontext == libnbd::CONTEXT_BASE_ALLOCATION {
*entries_clone.lock().unwrap() = Some(entries.to_vec());
}
0
},
flags,
)
.unwrap();
Arc::try_unwrap(entries)
.unwrap()
.into_inner()
.unwrap()
.unwrap()
}
#[test]
fn test_block_status() {
let srcdir = env::var("srcdir").unwrap();
let srcdir = Path::new(&srcdir);
let script_path = srcdir.join("../tests/meta-base-allocation.sh");
let script_path = script_path.to_str().unwrap();
let nbd = libnbd::Handle::new().unwrap();
nbd.add_meta_context(libnbd::CONTEXT_BASE_ALLOCATION)
.unwrap();
nbd.connect_command(&[
"nbdkit",
"-s",
"--exit-with-parent",
"-v",
"sh",
script_path,
])
.unwrap();
assert_eq!(
block_status_get_entries(&nbd, 65536, 0, None).as_slice(),
&[8192, 0, 8192, 1, 16384, 3, 16384, 2, 16384, 0,]
);
assert_eq!(
block_status_get_entries(&nbd, 1024, 32256, None).as_slice(),
&[512, 3, 16384, 2]
);
assert_eq!(
block_status_get_entries(
&nbd,
1024,
32256,
Some(libnbd::CmdFlag::REQ_ONE)
)
.as_slice(),
&[512, 3]
);
}
|