[Libguestfs] [libnbd PATCH v4 11.5/25] api: Add tests for [aio_]nbd_block_status_64

Eric Blake eblake at redhat.com
Fri Aug 11 18:27:58 UTC 2023


Add unit test coverage to prove that the new API works in each of C,
Python, OCaml, Go, and Rust bindings.

Signed-off-by: Eric Blake <eblake at redhat.com>
---

The bulk of this patch is unchanged from
https://listman.redhat.com/archives/libguestfs/2023-August/032238.html
so I've elided those files from the listing below.

However, the Rust portion is new, because I had to rebase on top of
Tage's work that landed in the meantime.  'make check' passed for me
with Rust in the build, so I assume I got it right, but I would
appreciate the double-check.

The patches can also be checked out from
https://repo.or.cz/libnbd/ericb.git/ (warning, I may do
non-fast-forward pushes there as I continue rebase work), if that
makes it easier to check out my work.

---
 python/t/465-block-status-64.py           |  56 ++++++++++
 ocaml/tests/Makefile.am                   |   1 +
 ocaml/tests/test_465_block_status_64.ml   |  58 ++++++++++
 rust/Makefile.am                          |   1 +
 rust/tests/test_465_block_status_64.rs    | 127 ++++++++++++++++++++++
 tests/meta-base-allocation.c              | 104 +++++++++++++++---
 golang/Makefile.am                        |   1 +
 golang/libnbd_465_block_status_64_test.go | 119 ++++++++++++++++++++

...
diff --git a/rust/Makefile.am b/rust/Makefile.am
index 7098c9ad..8ec54700 100644
--- a/rust/Makefile.am
+++ b/rust/Makefile.am
@@ -58,6 +58,7 @@ source_files = \
 	tests/test_405_pread_structured.rs \
 	tests/test_410_pwrite.rs \
 	tests/test_460_block_status.rs \
+	tests/test_465_block_status_64.rs \
 	tests/test_620_stats.rs \
 	tests/test_log/mod.rs \
 	run-tests.sh.in \
diff --git a/rust/tests/test_465_block_status_64.rs b/rust/tests/test_465_block_status_64.rs
new file mode 100644
index 00000000..347ee9b5
--- /dev/null
+++ b/rust/tests/test_465_block_status_64.rs
@@ -0,3 +1,130 @@
+// libnbd Rust test case
+// Copyright Tage Johansson
+// Copyright Red Hat
+//
+// 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 libnbd::types::NbdExtent;
+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<NbdExtent> {
+    let entries = Arc::new(Mutex::new(None));
+    let entries_clone = entries.clone();
+    nbd.block_status_64(
+        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::into_inner(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(),
+        &[
+            NbdExtent {
+                length: 8192,
+                flags: 0
+            },
+            NbdExtent {
+                length: 8192,
+                flags: 1
+            },
+            NbdExtent {
+                length: 16384,
+                flags: 3
+            },
+            NbdExtent {
+                length: 16384,
+                flags: 2
+            },
+            NbdExtent {
+                length: 16384,
+                flags: 0
+            },
+        ]
+    );
+
+    assert_eq!(
+        block_status_get_entries(&nbd, 1024, 32256, None).as_slice(),
+        &[
+            NbdExtent {
+                length: 512,
+                flags: 3
+            },
+            NbdExtent {
+                length: 16384,
+                flags: 2
+            }
+        ]
+    );
+
+    assert_eq!(
+        block_status_get_entries(
+            &nbd,
+            1024,
+            32256,
+            Some(libnbd::CmdFlag::REQ_ONE)
+        )
+        .as_slice(),
+        &[NbdExtent {
+            length: 512,
+            flags: 3
+        }]
+    );
+}

...

-- 
2.41.0



More information about the Libguestfs mailing list