[Libguestfs] [PATCH 2/2] rust: add some (but not yet all) tests

Tage Johansson tage.j.lists at posteo.net
Tue Jun 20 09:31:37 UTC 2023


From: Tage Johansson <frans.tage at gmail.com>

---
 rust/Cargo.toml                         |  3 +
 rust/tests/test_100_handle.rs           | 25 ++++++++
 rust/tests/test_110_defaults.rs         | 33 ++++++++++
 rust/tests/test_120_set_non_defaults.rs | 56 +++++++++++++++++
 rust/tests/test_130_private_data.rs     | 28 +++++++++
 rust/tests/test_140_explicit_close.rs   | 31 +++++++++
 rust/tests/test_log/mod.rs              | 84 +++++++++++++++++++++++++
 7 files changed, 260 insertions(+)

diff --git a/rust/Cargo.toml b/rust/Cargo.toml
index f4fe8fb..d51c6cb 100644
--- a/rust/Cargo.toml
+++ b/rust/Cargo.toml
@@ -46,3 +46,6 @@ log = { version = "0.4.19", optional = true }
 
 [features]
 default = ["log"]
+
+[dev-dependencies]
+once_cell = "1.18.0"
diff --git a/rust/tests/test_100_handle.rs b/rust/tests/test_100_handle.rs
new file mode 100644
index 0000000..d850466
--- /dev/null
+++ b/rust/tests/test_100_handle.rs
@@ -0,0 +1,25 @@
+// libnbd Rust test case
+// 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
+
+//! Just check that we can link with libnbd and create a handle.
+
+#![deny(warnings)]
+
+#[test]
+fn test_nbd_handle_new() {
+    let _ = libnbd::NbdHandle::new().unwrap();
+}
diff --git a/rust/tests/test_110_defaults.rs b/rust/tests/test_110_defaults.rs
new file mode 100644
index 0000000..bcfab92
--- /dev/null
+++ b/rust/tests/test_110_defaults.rs
@@ -0,0 +1,33 @@
+// libnbd Rust test case
+// 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)]
+
+#[test]
+fn test_defaults() {
+    let mut nbd = libnbd::NbdHandle::new().unwrap();
+
+    assert!(nbd.get_export_name().unwrap().to_str().unwrap().is_empty());
+    assert!(!nbd.get_full_info().unwrap());
+    assert_eq!(nbd.get_tls(), libnbd::Tls::Disable);
+    assert!(nbd.get_request_structured_replies());
+    assert!(nbd.get_request_meta_context().unwrap());
+    assert!(nbd.get_request_block_size().unwrap());
+    assert!(nbd.get_pread_initialize());
+    assert!(nbd.get_handshake_flags().is_all());
+    assert!(!nbd.get_opt_mode());
+}
diff --git a/rust/tests/test_120_set_non_defaults.rs b/rust/tests/test_120_set_non_defaults.rs
new file mode 100644
index 0000000..e46d088
--- /dev/null
+++ b/rust/tests/test_120_set_non_defaults.rs
@@ -0,0 +1,56 @@
+// libnbd Rust test case
+// 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 std::ffi::CString;
+
+#[test]
+fn test_set_non_defaults() {
+    let mut nbd = libnbd::NbdHandle::new().unwrap();
+
+    let name = CString::new("name").unwrap();
+    nbd.set_export_name(&name).unwrap();
+    assert_eq!(nbd.get_export_name().unwrap(), name);
+
+    nbd.set_full_info(true).unwrap();
+    assert!(nbd.get_full_info().unwrap());
+
+    if nbd.supports_tls() {
+        nbd.set_tls(libnbd::Tls::Allow).unwrap();
+        assert_eq!(nbd.get_tls(), libnbd::Tls::Allow);
+    }
+
+    nbd.set_request_structured_replies(false).unwrap();
+    assert!(!nbd.get_request_structured_replies());
+
+    nbd.set_request_meta_context(false).unwrap();
+    assert!(!nbd.get_request_meta_context().unwrap());
+
+    nbd.set_request_block_size(false).unwrap();
+    assert!(!nbd.get_request_block_size().unwrap());
+
+    nbd.set_pread_initialize(false).unwrap();
+    assert!(!nbd.get_pread_initialize());
+
+    nbd.set_handshake_flags(libnbd::HandshakeFlag::empty())
+        .unwrap();
+    assert!(nbd.get_handshake_flags().is_empty());
+
+    nbd.set_opt_mode(true).unwrap();
+    assert!(nbd.get_opt_mode());
+}
diff --git a/rust/tests/test_130_private_data.rs b/rust/tests/test_130_private_data.rs
new file mode 100644
index 0000000..719bdcc
--- /dev/null
+++ b/rust/tests/test_130_private_data.rs
@@ -0,0 +1,28 @@
+// libnbd Rust test case
+// 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)]
+
+#[test]
+fn test_private_data() {
+    let mut nbd = libnbd::NbdHandle::new().unwrap();
+
+    assert_eq!(nbd.get_private_data(), 0);
+    assert_eq!(nbd.set_private_data(42), 0);
+    assert_eq!(nbd.set_private_data(314), 42);
+    assert_eq!(nbd.get_private_data(), 314);
+}
diff --git a/rust/tests/test_140_explicit_close.rs b/rust/tests/test_140_explicit_close.rs
new file mode 100644
index 0000000..cf5002a
--- /dev/null
+++ b/rust/tests/test_140_explicit_close.rs
@@ -0,0 +1,31 @@
+// libnbd Rust test case
+// 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)]
+
+mod test_log;
+
+use test_log::DEBUG_LOGGER;
+
+#[test]
+fn test_private_data() {
+    DEBUG_LOGGER.init();
+
+    let nbd = libnbd::NbdHandle::new().unwrap();
+    drop(nbd);
+    assert!(DEBUG_LOGGER.contains("closing handle"));
+}
diff --git a/rust/tests/test_log/mod.rs b/rust/tests/test_log/mod.rs
new file mode 100644
index 0000000..d34a149
--- /dev/null
+++ b/rust/tests/test_log/mod.rs
@@ -0,0 +1,84 @@
+// libnbd Rust test case
+// 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
+
+//! This module provides facilities for capturing log output and asserting that it does or does not
+//! contain certain messages. The primary use of this module is to assert that certain libnbd
+//! operations are or are not performed.
+
+#![allow(unused)]
+
+use once_cell::sync::Lazy;
+use std::collections::HashSet;
+use std::sync::Mutex;
+
+/// Logger that stores all debug messages in a list.
+pub struct DebugLogger {
+    /// All messages logged. Wrapped in a mutex so that it can be updated with an imutable
+    /// reference to self.
+    messages: Mutex<HashSet<String>>,
+    is_initialized: Mutex<bool>,
+}
+
+impl DebugLogger {
+    fn new() -> Self {
+        Self {
+            messages: Mutex::new(HashSet::new()),
+            is_initialized: Mutex::new(false),
+        }
+    }
+
+    /// Set this logger as the global logger.
+    pub fn init(&'static self) {
+        let mut is_initialized = self.is_initialized.lock().unwrap();
+        if !*is_initialized {
+            log::set_logger(self).unwrap();
+            log::set_max_level(log::LevelFilter::Debug);
+            *is_initialized = true;
+        }
+    }
+
+    /// Check wether a specific message has been logged.
+    pub fn contains(&self, msg: &str) -> bool {
+        self.messages.lock().unwrap().contains(msg)
+    }
+
+    /// Print all logged messages, in no particular order.
+    ///
+    /// Only for debug purposes. Remember to run cargo test with the `-- --nocapture` arguments.
+    /// That is, from the rust directory run: `./../run cargo test -- --nocapture`
+    pub fn print_messages(&self) {
+        dbg!(&*self.messages.lock().unwrap());
+    }
+}
+
+/// A static global `DebugLogger`. Just call `.init()` on this to set it as the global logger.
+pub static DEBUG_LOGGER: Lazy<DebugLogger> = Lazy::new(DebugLogger::new);
+
+impl log::Log for DebugLogger {
+    fn enabled(&self, metadata: &log::Metadata<'_>) -> bool {
+        metadata.level() == log::Level::Debug
+    }
+
+    fn log(&self, record: &log::Record<'_>) {
+        self.messages
+            .lock()
+            .unwrap()
+            .insert(record.args().to_string());
+    }
+
+    fn flush(&self) {}
+}
-- 
2.41.0



More information about the Libguestfs mailing list