Port the C test of the previous commit over to other language
bindings, to prove that we are handling StringList translations
correctly. Golang is particularly annoying with all tests sharing the
same namespace for globals, requiring some renaming in test 240 in
relation to the code added in test 245.
---
python/t/245-opt-list-meta-queries.py | 68 +++++++++++
ocaml/tests/Makefile.am | 1 +
ocaml/tests/test_245_opt_list_meta_queries.ml | 68 +++++++++++
tests/opt-list-meta-queries.c | 1 +
golang/Makefile.am | 1 +
golang/libnbd_240_opt_list_meta_test.go | 40 +++---
.../libnbd_245_opt_list_meta_queries_test.go | 115 ++++++++++++++++++
7 files changed, 274 insertions(+), 20 deletions(-)
create mode 100644 python/t/245-opt-list-meta-queries.py
create mode 100644 ocaml/tests/test_245_opt_list_meta_queries.ml
create mode 100644 golang/libnbd_245_opt_list_meta_queries_test.go
diff --git a/python/t/245-opt-list-meta-queries.py
b/python/t/245-opt-list-meta-queries.py
new file mode 100644
index 00000000..561997ed
--- /dev/null
+++ b/python/t/245-opt-list-meta-queries.py
@@ -0,0 +1,68 @@
+# libnbd Python bindings
+# Copyright (C) 2010-2022 Red Hat Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program 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 General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+import nbd
+
+
+count = 0
+seen = False
+
+
+def f(user_data, name):
+ global count
+ global seen
+ assert user_data == 42
+ count = count + 1
+ if name == nbd.CONTEXT_BASE_ALLOCATION:
+ seen = True
+
+
+# Get into negotiating state.
+h = nbd.NBD()
+h.set_opt_mode(True)
+h.connect_command(["nbdkit", "-s", "--exit-with-parent",
"-v",
+ "memory", "size=1M"])
+
+# First pass: empty query should give at least "base:allocation".
+# The explicit query overrides a non-empty nbd_add_meta_context.
+count = 0
+seen = False
+h.add_meta_context("x-nosuch:")
+r = h.opt_list_meta_context_queries([], lambda *args: f(42, *args))
+assert r == count
+assert r >= 1
+assert seen is True
+
+# Second pass: bogus query has no response.
+count = 0
+seen = False
+h.clear_meta_contexts()
+r = h.opt_list_meta_context_queries(["x-nosuch:"], lambda *args: f(42, *args))
+assert r == 0
+assert r == count
+assert seen is False
+
+# Third pass: specific query should have one match.
+count = 0
+seen = False
+r = h.opt_list_meta_context_queries(["x-nosuch:",
nbd.CONTEXT_BASE_ALLOCATION],
+ lambda *args: f(42, *args))
+assert r == 1
+assert count == 1
+assert seen is True
+
+h.opt_abort()
diff --git a/ocaml/tests/Makefile.am b/ocaml/tests/Makefile.am
index c4751ad3..1b4d1135 100644
--- a/ocaml/tests/Makefile.am
+++ b/ocaml/tests/Makefile.am
@@ -31,6 +31,7 @@ ML_TESTS = \
test_220_opt_list.ml \
test_230_opt_info.ml \
test_240_opt_list_meta.ml \
+ test_245_opt_list_meta_queries.ml \
test_300_get_size.ml \
test_400_pread.ml \
test_405_pread_structured.ml \
diff --git a/ocaml/tests/test_245_opt_list_meta_queries.ml
b/ocaml/tests/test_245_opt_list_meta_queries.ml
new file mode 100644
index 00000000..4c5e01ef
--- /dev/null
+++ b/ocaml/tests/test_245_opt_list_meta_queries.ml
@@ -0,0 +1,68 @@
+(* hey emacs, this is OCaml code: -*- tuareg -*- *)
+(* libnbd OCaml test case
+ * Copyright (C) 2013-2022 Red Hat Inc.
+ *
+ * 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
+ *)
+
+open Printf
+
+let count = ref 0
+let seen = ref false
+let f user_data name =
+ assert (user_data = 42);
+ count := !count + 1;
+ if name = NBD.context_base_allocation then
+ seen := true;
+ 0
+
+let () =
+ (* Get into negotiating state. *)
+ let nbd = NBD.create () in
+ NBD.set_opt_mode nbd true;
+ NBD.connect_command nbd
+ ["nbdkit"; "-s";
"--exit-with-parent"; "-v";
+ "memory"; "size=1M"];
+
+ (* First pass: empty query should give at least "base:allocation". *)
+ count := 0;
+ seen := false;
+ NBD.add_meta_context nbd "x-nosuch:";
+ let r = NBD.opt_list_meta_context_queries nbd [] (f 42) in
+ assert (r = !count);
+ assert (r >= 1);
+ assert !seen;
+
+ (* Second pass: bogus query has no response. *)
+ count := 0;
+ seen := false;
+ NBD.clear_meta_contexts nbd;
+ let r = NBD.opt_list_meta_context_queries nbd ["x-nosuch:"] (f 42) in
+ assert (r = 0);
+ assert (r = !count);
+ assert (not !seen);
+
+ (* Third pass: specific query should have one match. *)
+ count := 0;
+ seen := false;
+ let r = NBD.opt_list_meta_context_queries nbd [
+ "x-nosuch:"; NBD.context_base_allocation ] (f 42) in
+ assert (r = 1);
+ assert (r = !count);
+ assert !seen;
+
+ NBD.opt_abort nbd
+
+let () = Gc.compact ()
diff --git a/tests/opt-list-meta-queries.c b/tests/opt-list-meta-queries.c
index 8570f967..3075e905 100644
--- a/tests/opt-list-meta-queries.c
+++ b/tests/opt-list-meta-queries.c
@@ -17,6 +17,7 @@
*/
/* Test behavior of nbd_opt_list_meta_context_queries. */
+/* See also unit test 245 in the various language ports. */
#include <config.h>
diff --git a/golang/Makefile.am b/golang/Makefile.am
index 4e49a9a1..6adf6ae8 100644
--- a/golang/Makefile.am
+++ b/golang/Makefile.am
@@ -35,6 +35,7 @@ source_files = \
libnbd_220_opt_list_test.go \
libnbd_230_opt_info_test.go \
libnbd_240_opt_list_meta_test.go \
+ libnbd_245_opt_list_meta_queries_test.go \
libnbd_300_get_size_test.go \
libnbd_400_pread_test.go \
libnbd_405_pread_structured_test.go \
diff --git a/golang/libnbd_240_opt_list_meta_test.go
b/golang/libnbd_240_opt_list_meta_test.go
index 2b49c895..c329aaf6 100644
--- a/golang/libnbd_240_opt_list_meta_test.go
+++ b/golang/libnbd_240_opt_list_meta_test.go
@@ -23,16 +23,16 @@
"testing"
)
-var count uint
-var seen bool
+var list_count uint
+var list_seen bool
func listmetaf(user_data int, name string) int {
if user_data != 42 {
panic("expected user_data == 42")
}
- count++
+ list_count++
if (name == context_base_allocation) {
- seen = true
+ list_seen = true
}
return 0
}
@@ -59,22 +59,22 @@ func Test240OptListMeta(t *testing.T) {
}
/* First pass: empty query should give at least "base:allocation". */
- count = 0
- seen = false
+ list_count = 0
+ list_seen = false
r, err := h.OptListMetaContext(func(name string) int {
return listmetaf(42, name)
})
if err != nil {
t.Fatalf("could not request opt_list_meta_context: %s", err)
}
- if r != count || r < 1 || !seen {
+ if r != list_count || r < 1 || !list_seen {
t.Fatalf("unexpected count after opt_list_meta_context")
}
- max := count
+ max := list_count
/* Second pass: bogus query has no response. */
- count = 0
- seen = false
+ list_count = 0
+ list_seen = false
err = h.AddMetaContext("x-nosuch:")
if err != nil {
t.Fatalf("could not request add_meta_context: %s", err)
@@ -85,13 +85,13 @@ func Test240OptListMeta(t *testing.T) {
if err != nil {
t.Fatalf("could not request opt_list_meta_context: %s", err)
}
- if r != 0 || r != count || seen {
+ if r != 0 || r != list_count || list_seen {
t.Fatalf("unexpected count after opt_list_meta_context")
}
/* Third pass: specific query should have one match. */
- count = 0
- seen = false
+ list_count = 0
+ list_seen = false
err = h.AddMetaContext(context_base_allocation)
if err != nil {
t.Fatalf("could not request add_meta_context: %s", err)
@@ -116,13 +116,13 @@ func Test240OptListMeta(t *testing.T) {
if err != nil {
t.Fatalf("could not request opt_list_meta_context: %s", err)
}
- if r != 1 || r != count || !seen {
+ if r != 1 || r != list_count || !list_seen {
t.Fatalf("unexpected count after opt_list_meta_context")
}
/* Final pass: "base:" query should get at least "base:allocation"
*/
- count = 0
- seen = false
+ list_count = 0
+ list_seen = false
err = h.ClearMetaContexts()
if err != nil {
t.Fatalf("could not request clear_meta_contexts: %s", err)
@@ -137,7 +137,7 @@ func Test240OptListMeta(t *testing.T) {
if err != nil {
t.Fatalf("could not request opt_list_meta_context: %s", err)
}
- if r < 1 || r > max || r != count || !seen {
+ if r < 1 || r > max || r != list_count || !list_seen {
t.Fatalf("unexpected count after opt_list_meta_context")
}
@@ -178,8 +178,8 @@ func Test240OptListMeta(t *testing.T) {
t.Fatalf("could not collect stats: %s", err)
}
- count = 0
- seen = false
+ list_count = 0
+ list_seen = false
r, err = h.OptListMetaContext(func(name string) int {
return listmetaf(42, name)
})
@@ -192,7 +192,7 @@ func Test240OptListMeta(t *testing.T) {
t.Fatalf("unexpected bytes sent after opt_list_meta_context")
}
fmt.Printf("ignoring failure from old server: %s", err)
- } else if r < 1 || r != count || !seen {
+ } else if r < 1 || r != list_count || !list_seen {
t.Fatalf("unexpected count after opt_list_meta_context")
}
diff --git a/golang/libnbd_245_opt_list_meta_queries_test.go
b/golang/libnbd_245_opt_list_meta_queries_test.go
new file mode 100644
index 00000000..1a1f0bfb
--- /dev/null
+++ b/golang/libnbd_245_opt_list_meta_queries_test.go
@@ -0,0 +1,115 @@
+/* libnbd golang tests
+ * Copyright (C) 2013-2022 Red Hat Inc.
+ *
+ * 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
+ */
+
+package libnbd
+
+import "testing"
+
+var listq_count uint
+var listq_seen bool
+
+func listmetaqf(user_data int, name string) int {
+ if user_data != 42 {
+ panic("expected user_data == 42")
+ }
+ listq_count++
+ if (name == context_base_allocation) {
+ listq_seen = true
+ }
+ return 0
+}
+
+func Test245OptListMetaQueries(t *testing.T) {
+ /* Get into negotiating state. */
+ h, err := Create()
+ if err != nil {
+ t.Fatalf("could not create handle: %s", err)
+ }
+ defer h.Close()
+
+ err = h.SetOptMode(true)
+ if err != nil {
+ t.Fatalf("could not set opt mode: %s", err)
+ }
+
+ err = h.ConnectCommand([]string{
+ "nbdkit", "-s", "--exit-with-parent", "-v",
+ "memory", "size=1M",
+ })
+ if err != nil {
+ t.Fatalf("could not connect: %s", err)
+ }
+
+ /* First pass: empty query should give at least "base:allocation".
+ * The explicit query overrides a non-empty nbd_add_meta_context.
+ */
+ listq_count = 0
+ listq_seen = false
+ err = h.AddMetaContext("x-nosuch:")
+ if err != nil {
+ t.Fatalf("could not request add_meta_context: %s", err)
+ }
+ r, err := h.OptListMetaContextQueries([]string{ },
+ func(name string) int {
+ return listmetaqf(42, name)
+ })
+ if err != nil {
+ t.Fatalf("could not request opt_list_meta_context_queries: %s", err)
+ }
+ if r != listq_count || r < 1 || !listq_seen {
+ t.Fatalf("unexpected count after opt_list_meta_context_queries")
+ }
+
+ /* Second pass: bogus query has no response. */
+ listq_count = 0
+ listq_seen = false
+ err = h.ClearMetaContexts()
+ if err != nil {
+ t.Fatalf("could not request add_meta_context: %s", err)
+ }
+ r, err = h.OptListMetaContextQueries([]string{ "x-nosuch:" },
+ func(name string) int {
+ return listmetaqf(42, name)
+ })
+ if err != nil {
+ t.Fatalf("could not request opt_list_meta_context_queries: %s", err)
+ }
+ if r != 0 || r != listq_count || listq_seen {
+ t.Fatalf("unexpected count after opt_list_meta_context_queries")
+ }
+
+ /* Third pass: specific query should have one match. */
+ listq_count = 0
+ listq_seen = false
+ r, err = h.OptListMetaContextQueries([]string{
+ "x-nosuch:", context_base_allocation },
+ func(name string) int {
+ return listmetaqf(42, name)
+ })
+ if err != nil {
+ t.Fatalf("could not request opt_list_meta_context_queries: %s", err)
+ }
+ if r != 1 || r != listq_count || !listq_seen {
+ t.Fatalf("unexpected count after opt_list_meta_context_queries")
+ }
+
+ err = h.OptAbort()
+ if err != nil {
+ t.Fatalf("could not request opt_abort: %s", err)
+ }
+}
--
2.37.3