Re: [Libguestfs] [libnbd PATCH v5 02/12] rust: Add a couple of integration tests
On 8/16/2023 11:35 PM, Eric Blake wrote: On Thu, Aug 03, 2023 at 03:36:06PM +, Tage Johansson wrote: A couple of integration tests are added in rust/tests. They are mostly ported from the OCaml tests. --- Overall, this looked like a nice counterpart to the OCaml unit tests, and I was able to easily figure out how to amend my own tests in for the unit tests I added in my 64-bit extension work. Keeping similar unit test numbers across language bindings has been a big boon :-) Style question: +++ b/rust/tests/test_250_opt_set_meta.rs + +/// A struct with information about set meta contexts. +#[derive(Debug, Clone, PartialEq, Eq)] +struct CtxInfo { +/// Whether the meta context "base:allocation" is set. +has_alloc: bool, +/// The number of set meta contexts. +count: u32, +} Here, you use a trailing comma, + +fn set_meta_ctxs(nbd: ::Handle) -> libnbd::Result { +let info = Arc::new(Mutex::new(CtxInfo { +has_alloc: false, +count: 0, +})); +let info_clone = info.clone(); +let replies = nbd.opt_set_meta_context(move |ctx| { +let mut info = info_clone.lock().unwrap(); +info.count += 1; +if ctx == CONTEXT_BASE_ALLOCATION { +info.has_alloc = true; +} +0 +})?; +let info = Arc::into_inner(info).unwrap().into_inner().unwrap(); +assert_eq!(info.count, replies); +Ok(info) +} + ... + +// nbdkit does not match wildcard for SET, even though it does for LIST +nbd.clear_meta_contexts().unwrap(); +nbd.add_meta_context("base:").unwrap(); +assert_eq!( +set_meta_ctxs().unwrap(), +CtxInfo { +count: 0, +has_alloc: false +} whereas here, you did not. Does it make a difference? 'make check' still passes, and rustfmt doesn't complain, if I temporarily add a trailing comma here. No, it doesn't make a difference. I nearly always runs rustfmt on my code but it doesn't seem that rustfmt cares about that comma. I also note that 'rustfmt --check rust/tests/*.rs' flags a few style suggestions. I had started work on automating gofmt for all checked-in *.go files; maybe I should revive that patch and extend it to also automate rustfmt on all checked-in *.rs files. Here's what rustfmt suggested to me (rustfmt 1.5.2-stable ( ) on Fedora 38): Diff in /home/eblake/libnbd/rust/tests/test_200_connect_command.rs at line 17: #![deny(warnings)] - #[test] fn test_connect_command() { let nbd = libnbd::Handle::new().unwrap(); Diff in /home/eblake/libnbd/rust/tests/test_200_connect_command.rs at line 24: -nbd.connect_command(&[ -"nbdkit", -"-s", -"--exit-with-parent", -"-v", -"null", -]) -.unwrap(); +nbd.connect_command(&["nbdkit", "-s", "--exit-with-parent", "-v", "null"]) +.unwrap(); } Diff in /home/eblake/libnbd/rust/tests/test_300_get_size.rs at line 17: #![deny(warnings)] - #[test] fn test_get_size() { let nbd = libnbd::Handle::new().unwrap(); Diff in /home/eblake/libnbd/rust/tests/test_620_stats.rs at line 17: #![deny(warnings)] - #[test] fn test_stats() { let nbd = libnbd::Handle::new().unwrap(); Diff in /home/eblake/libnbd/rust/tests/test_620_stats.rs at line 31: // Connection performs handshaking, which increments stats. // The number of bytes/chunks here may grow over time as more features get // automatically negotiated, so merely check that they are non-zero. -nbd.connect_command(&[ -"nbdkit", -"-s", -"--exit-with-parent", -"-v", -"null", -]) -.unwrap(); +nbd.connect_command(&["nbdkit", "-s", "--exit-with-parent", "-v", "null"]) +.unwrap(); let bs1 = nbd.stats_bytes_sent(); let cs1 = nbd.stats_chunks_sent(); Automating rustfmt on all checked-in rust files sounds like a good idea. It is strange that those file were not proprly formatted since I just mentioned that I nearly always runs rustfmt on every edit. For the files which are not upstream yet, (test_async_*), I will fix the formatting in the next patch series. For the other files I will create an extra patch to correct the formatting. -- Best regards, Tage ___ Libguestfs mailing list Libguestfs@redhat.com https://listman.redhat.com/mailman/listinfo/libguestfs
Re: [Libguestfs] [libnbd PATCH v5 02/12] rust: Add a couple of integration tests
On Thu, Aug 03, 2023 at 03:36:06PM +, Tage Johansson wrote: > A couple of integration tests are added in rust/tests. They are mostly > ported from the OCaml tests. > --- Overall, this looked like a nice counterpart to the OCaml unit tests, and I was able to easily figure out how to amend my own tests in for the unit tests I added in my 64-bit extension work. Keeping similar unit test numbers across language bindings has been a big boon :-) Style question: > +++ b/rust/tests/test_250_opt_set_meta.rs > + > +/// A struct with information about set meta contexts. > +#[derive(Debug, Clone, PartialEq, Eq)] > +struct CtxInfo { > +/// Whether the meta context "base:allocation" is set. > +has_alloc: bool, > +/// The number of set meta contexts. > +count: u32, > +} Here, you use a trailing comma, > + > +fn set_meta_ctxs(nbd: ::Handle) -> libnbd::Result { > +let info = Arc::new(Mutex::new(CtxInfo { > +has_alloc: false, > +count: 0, > +})); > +let info_clone = info.clone(); > +let replies = nbd.opt_set_meta_context(move |ctx| { > +let mut info = info_clone.lock().unwrap(); > +info.count += 1; > +if ctx == CONTEXT_BASE_ALLOCATION { > +info.has_alloc = true; > +} > +0 > +})?; > +let info = Arc::into_inner(info).unwrap().into_inner().unwrap(); > +assert_eq!(info.count, replies); > +Ok(info) > +} > + ... > + > +// nbdkit does not match wildcard for SET, even though it does for LIST > +nbd.clear_meta_contexts().unwrap(); > +nbd.add_meta_context("base:").unwrap(); > +assert_eq!( > +set_meta_ctxs().unwrap(), > +CtxInfo { > +count: 0, > +has_alloc: false > +} whereas here, you did not. Does it make a difference? 'make check' still passes, and rustfmt doesn't complain, if I temporarily add a trailing comma here. I also note that 'rustfmt --check rust/tests/*.rs' flags a few style suggestions. I had started work on automating gofmt for all checked-in *.go files; maybe I should revive that patch and extend it to also automate rustfmt on all checked-in *.rs files. Here's what rustfmt suggested to me (rustfmt 1.5.2-stable ( ) on Fedora 38): Diff in /home/eblake/libnbd/rust/tests/test_200_connect_command.rs at line 17: #![deny(warnings)] - #[test] fn test_connect_command() { let nbd = libnbd::Handle::new().unwrap(); Diff in /home/eblake/libnbd/rust/tests/test_200_connect_command.rs at line 24: -nbd.connect_command(&[ -"nbdkit", -"-s", -"--exit-with-parent", -"-v", -"null", -]) -.unwrap(); +nbd.connect_command(&["nbdkit", "-s", "--exit-with-parent", "-v", "null"]) +.unwrap(); } Diff in /home/eblake/libnbd/rust/tests/test_300_get_size.rs at line 17: #![deny(warnings)] - #[test] fn test_get_size() { let nbd = libnbd::Handle::new().unwrap(); Diff in /home/eblake/libnbd/rust/tests/test_620_stats.rs at line 17: #![deny(warnings)] - #[test] fn test_stats() { let nbd = libnbd::Handle::new().unwrap(); Diff in /home/eblake/libnbd/rust/tests/test_620_stats.rs at line 31: // Connection performs handshaking, which increments stats. // The number of bytes/chunks here may grow over time as more features get // automatically negotiated, so merely check that they are non-zero. -nbd.connect_command(&[ -"nbdkit", -"-s", -"--exit-with-parent", -"-v", -"null", -]) -.unwrap(); +nbd.connect_command(&["nbdkit", "-s", "--exit-with-parent", "-v", "null"]) +.unwrap(); let bs1 = nbd.stats_bytes_sent(); let cs1 = nbd.stats_chunks_sent(); -- Eric Blake, Principal Software Engineer Red Hat, Inc. Virtualization: qemu.org | libguestfs.org ___ Libguestfs mailing list Libguestfs@redhat.com https://listman.redhat.com/mailman/listinfo/libguestfs
[Libguestfs] [libnbd PATCH v5 02/12] rust: Add a couple of integration tests
A couple of integration tests are added in rust/tests. They are mostly ported from the OCaml tests. --- rust/Cargo.toml | 4 + rust/Makefile.am | 22 +++ rust/run-tests.sh.in | 4 +- rust/tests/nbdkit_pattern/mod.rs | 28 rust/tests/test_100_handle.rs| 25 rust/tests/test_110_defaults.rs | 33 + rust/tests/test_120_set_non_defaults.rs | 53 +++ rust/tests/test_130_private_data.rs | 28 rust/tests/test_140_explicit_close.rs| 31 rust/tests/test_200_connect_command.rs | 32 rust/tests/test_210_opt_abort.rs | 31 rust/tests/test_220_opt_list.rs | 86 +++ rust/tests/test_230_opt_info.rs | 120 +++ rust/tests/test_240_opt_list_meta.rs | 147 +++ rust/tests/test_245_opt_list_meta_queries.rs | 93 rust/tests/test_250_opt_set_meta.rs | 123 rust/tests/test_255_opt_set_meta_queries.rs | 109 ++ rust/tests/test_300_get_size.rs | 35 + rust/tests/test_400_pread.rs | 39 + rust/tests/test_405_pread_structured.rs | 79 ++ rust/tests/test_410_pwrite.rs| 58 rust/tests/test_460_block_status.rs | 92 rust/tests/test_620_stats.rs | 75 ++ rust/tests/test_log/mod.rs | 86 +++ 24 files changed, 1432 insertions(+), 1 deletion(-) create mode 100644 rust/tests/nbdkit_pattern/mod.rs create mode 100644 rust/tests/test_100_handle.rs create mode 100644 rust/tests/test_110_defaults.rs create mode 100644 rust/tests/test_120_set_non_defaults.rs create mode 100644 rust/tests/test_130_private_data.rs create mode 100644 rust/tests/test_140_explicit_close.rs create mode 100644 rust/tests/test_200_connect_command.rs create mode 100644 rust/tests/test_210_opt_abort.rs create mode 100644 rust/tests/test_220_opt_list.rs create mode 100644 rust/tests/test_230_opt_info.rs create mode 100644 rust/tests/test_240_opt_list_meta.rs create mode 100644 rust/tests/test_245_opt_list_meta_queries.rs create mode 100644 rust/tests/test_250_opt_set_meta.rs create mode 100644 rust/tests/test_255_opt_set_meta_queries.rs create mode 100644 rust/tests/test_300_get_size.rs create mode 100644 rust/tests/test_400_pread.rs create mode 100644 rust/tests/test_405_pread_structured.rs create mode 100644 rust/tests/test_410_pwrite.rs create mode 100644 rust/tests/test_460_block_status.rs create mode 100644 rust/tests/test_620_stats.rs create mode 100644 rust/tests/test_log/mod.rs diff --git a/rust/Cargo.toml b/rust/Cargo.toml index c745972..b498930 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -47,3 +47,7 @@ libc = "0.2.147" [features] default = ["log"] + +[dev-dependencies] +once_cell = "1.18.0" +tempfile = "3.6.0" diff --git a/rust/Makefile.am b/rust/Makefile.am index 8615794..db8fc66 100644 --- a/rust/Makefile.am +++ b/rust/Makefile.am @@ -36,6 +36,27 @@ source_files = \ cargo_test/Cargo.toml \ cargo_test/src/lib.rs \ cargo_test/README.md \ + tests/nbdkit_pattern/mod.rs \ + tests/test_100_handle.rs \ + tests/test_110_defaults.rs \ + tests/test_120_set_non_defaults.rs \ + tests/test_130_private_data.rs \ + tests/test_140_explicit_close.rs \ + tests/test_200_connect_command.rs \ + tests/test_210_opt_abort.rs \ + tests/test_220_opt_list.rs \ + tests/test_230_opt_info.rs \ + tests/test_240_opt_list_meta.rs \ + tests/test_245_opt_list_meta_queries.rs \ + tests/test_250_opt_set_meta.rs \ + tests/test_255_opt_set_meta_queries.rs \ + tests/test_300_get_size.rs \ + tests/test_400_pread.rs \ + tests/test_405_pread_structured.rs \ + tests/test_410_pwrite.rs \ + tests/test_460_block_status.rs \ + tests/test_620_stats.rs \ + tests/test_log/mod.rs \ run-tests.sh.in \ $(NULL) @@ -63,6 +84,7 @@ TESTS_ENVIRONMENT = \ LIBNBD_DEBUG=1 \ $(MALLOC_CHECKS) \ abs_top_srcdir=$(abs_top_srcdir) \ + CARGO=$(CARGO) \ $(NULL) LOG_COMPILER = $(top_builddir)/run TESTS = run-tests.sh diff --git a/rust/run-tests.sh.in b/rust/run-tests.sh.in index f4d220c..d45b1bf 100755 --- a/rust/run-tests.sh.in +++ b/rust/run-tests.sh.in @@ -1,6 +1,6 @@ #!/bin/bash - # nbd client library in userspace -# Copyright Red Hat +# 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 @@ -21,4 +21,6 @@ set -e set -x +requires nbdkit --version + @CARGO@ test -- --nocapture diff --git a/rust/tests/nbdkit_pattern/mod.rs b/rust/tests/nbdkit_pattern/mod.rs new file mode 100644 index 000..5f4069e