[Libguestfs] [libnbd PATCH v2 05/12] tests: Language port of nbd_set_request_meta_context() tests

Eric Blake
Wed, 31 Aug 2022 14:39:21 -0000

As promised in the previous patch, also test the new
nbd_set_request_meta_context() API in Python, OCaml, and Golang.  No
direct C counterpart in tests/, but it's always good to check that our
language bindings are complete.
---
 python/t/110-defaults.py                   |  1 +
 python/t/120-set-non-defaults.py           |  2 +
 python/t/230-opt-info.py                   | 25 +++++-
 ocaml/tests/test_110_defaults.ml           |  2 +
 ocaml/tests/test_120_set_non_defaults.ml   |  3 +
 ocaml/tests/test_230_opt_info.ml           | 32 +++++++-
 golang/libnbd_110_defaults_test.go         |  8 ++
 golang/libnbd_120_set_non_defaults_test.go | 12 +++
 golang/libnbd_230_opt_info_test.go         | 88 ++++++++++++++++++++--
 9 files changed, 161 insertions(+), 12 deletions(-)

diff --git a/python/t/110-defaults.py b/python/t/110-defaults.py
index 749c94f..6b62c8a 100644
--- a/python/t/110-defaults.py
+++ b/python/t/110-defaults.py
@@ -22,6 +22,7 @@ assert h.get_export_name() == ""
 assert h.get_full_info() is False
 assert h.get_tls() == nbd.TLS_DISABLE
 assert h.get_request_structured_replies() is True
+assert h.get_request_meta_context() is True
 assert h.get_request_block_size() is True
 assert h.get_pread_initialize() is True
 assert h.get_handshake_flags() == nbd.HANDSHAKE_FLAG_MASK
diff --git a/python/t/120-set-non-defaults.py b/python/t/120-set-non-defaults.py
index 61a4160..8b48b4a 100644
--- a/python/t/120-set-non-defaults.py
+++ b/python/t/120-set-non-defaults.py
@@ -33,6 +33,8 @@ if h.supports_tls():
     assert h.get_tls() == nbd.TLS_ALLOW
 h.set_request_structured_replies(False)
 assert h.get_request_structured_replies() is False
+h.set_request_meta_context(False)
+assert h.get_request_meta_context() is False
 h.set_request_block_size(False)
 assert h.get_request_block_size() is False
 h.set_pread_initialize(False)
diff --git a/python/t/230-opt-info.py b/python/t/230-opt-info.py
index 37db341..8aa47ae 100644
--- a/python/t/230-opt-info.py
+++ b/python/t/230-opt-info.py
@@ -52,12 +52,14 @@ must_fail(h.get_size)
 must_fail(h.is_read_only)
 must_fail(h.can_meta_context, nbd.CONTEXT_BASE_ALLOCATION)

-# info for a different export
+# info for a different export, with automatic meta_context disabled
 h.set_export_name("b")
+h.set_request_meta_context(False)
 h.opt_info()
 assert h.get_size() == 1
 assert h.is_read_only() is False
-assert h.can_meta_context(nbd.CONTEXT_BASE_ALLOCATION) is True
+must_fail(h.can_meta_context, nbd.CONTEXT_BASE_ALLOCATION)
+h.set_request_meta_context(True)

 # go on something not present
 h.set_export_name("a")
@@ -78,5 +80,24 @@ must_fail(h.set_export_name, "a")
 assert h.get_export_name() == "good"
 must_fail(h.opt_info)
 assert h.get_size() == 4
+assert h.can_meta_context(nbd.CONTEXT_BASE_ALLOCATION) is True
+
+h.shutdown()
+
+# Another connection. This time, check that SET_META triggered by opt_info
+# persists through nbd_opt_go with set_request_meta_context disabled.
+h = nbd.NBD()
+h.set_opt_mode(True)
+h.connect_command(["nbdkit", "-s",
"--exit-with-parent", "-v", "sh", script])
+h.add_meta_context("x-unexpected:bogus")
+
+must_fail(h.can_meta_context, nbd.CONTEXT_BASE_ALLOCATION)
+h.opt_info()
+assert h.can_meta_context(nbd.CONTEXT_BASE_ALLOCATION) is False
+h.set_request_meta_context(False)
+# Adding to the request list now won't matter
+h.add_meta_context(nbd.CONTEXT_BASE_ALLOCATION)
+h.opt_go()
+assert h.can_meta_context(nbd.CONTEXT_BASE_ALLOCATION) is False

 h.shutdown()
diff --git a/ocaml/tests/test_110_defaults.ml b/ocaml/tests/test_110_defaults.ml
index ccec9c6..768ad63 100644
--- a/ocaml/tests/test_110_defaults.ml
+++ b/ocaml/tests/test_110_defaults.ml
@@ -28,6 +28,8 @@ let
       assert (tls = NBD.TLS.DISABLE);
       let sr = NBD.get_request_structured_replies nbd in
       assert sr;
+      let meta = NBD.get_request_meta_context nbd in
+      assert meta;
       let bs = NBD.get_request_block_size nbd in
       assert bs;
       let init = NBD.get_pread_initialize nbd in
diff --git a/ocaml/tests/test_120_set_non_defaults.ml
b/ocaml/tests/test_120_set_non_defaults.ml
index 092287e..76ff82f 100644
--- a/ocaml/tests/test_120_set_non_defaults.ml
+++ b/ocaml/tests/test_120_set_non_defaults.ml
@@ -42,6 +42,9 @@ let
       NBD.set_request_structured_replies nbd false;
       let sr = NBD.get_request_structured_replies nbd in
       assert (not sr);
+      NBD.set_request_meta_context nbd false;
+      let meta = NBD.get_request_meta_context nbd in
+      assert (not meta);
       NBD.set_request_block_size nbd false;
       let bs = NBD.get_request_block_size nbd in
       assert (not bs);
diff --git a/ocaml/tests/test_230_opt_info.ml b/ocaml/tests/test_230_opt_info.ml
index 693a663..ec735ff 100644
--- a/ocaml/tests/test_230_opt_info.ml
+++ b/ocaml/tests/test_230_opt_info.ml
@@ -69,15 +69,16 @@ let
   fail_unary NBD.is_read_only nbd;
   fail_binary NBD.can_meta_context nbd NBD.context_base_allocation;

-  (* info for a different export *)
+  (* info for a different export, with automatic meta_context disabled *)
   NBD.set_export_name nbd "b";
+  NBD.set_request_meta_context nbd false;
   NBD.opt_info nbd;
   let size = NBD.get_size nbd in
   assert (size = 1L);
   let ro = NBD.is_read_only nbd in
   assert (not ro);
-  let meta = NBD.can_meta_context nbd NBD.context_base_allocation in
-  assert meta;
+  fail_binary NBD.can_meta_context nbd NBD.context_base_allocation;
+  NBD.set_request_meta_context nbd true;

   (* go on something not present *)
   NBD.set_export_name nbd "a";
@@ -103,6 +104,31 @@ let
   fail_unary NBD.opt_info nbd;
   let size = NBD.get_size nbd in
   assert (size = 4L);
+  let meta = NBD.can_meta_context nbd NBD.context_base_allocation in
+  assert meta;
+
+  NBD.shutdown nbd;
+
+  (* Another connection. This time, check that SET_META triggered by opt_info
+   * persists through nbd_opt_go with set_request_meta_context disabled.
+   *)
+  let nbd = NBD.create () in
+  NBD.set_opt_mode nbd true;
+  NBD.connect_command nbd
+                      ["nbdkit"; "-s";
"--exit-with-parent"; "-v";
+                       "sh"; script];
+  NBD.add_meta_context nbd "x-unexpected:bogus";
+
+  fail_binary NBD.can_meta_context nbd NBD.context_base_allocation;
+  NBD.opt_info nbd;
+  let meta = NBD.can_meta_context nbd NBD.context_base_allocation in
+  assert (not meta);
+  NBD.set_request_meta_context nbd false;
+  (* Adding to the request list now won't matter *)
+  NBD.add_meta_context nbd NBD.context_base_allocation;
+  NBD.opt_go nbd;
+  let meta = NBD.can_meta_context nbd NBD.context_base_allocation in
+  assert (not meta);

   NBD.shutdown nbd

diff --git a/golang/libnbd_110_defaults_test.go
b/golang/libnbd_110_defaults_test.go
index f56c965..d7ad319 100644
--- a/golang/libnbd_110_defaults_test.go
+++ b/golang/libnbd_110_defaults_test.go
@@ -59,6 +59,14 @@ func Test110Defaults(t *testing.T) {
 		t.Fatalf("unexpected structured replies state")
 	}

+	meta, err := h.GetRequestMetaContext()
+	if err != nil {
+		t.Fatalf("could not get meta context state: %s", err)
+	}
+	if meta != true {
+		t.Fatalf("unexpected meta context state")
+	}
+
 	bs, err := h.GetRequestBlockSize()
 	if err != nil {
 		t.Fatalf("could not get block size state: %s", err)
diff --git a/golang/libnbd_120_set_non_defaults_test.go
b/golang/libnbd_120_set_non_defaults_test.go
index a4c411d..06bb29d 100644
--- a/golang/libnbd_120_set_non_defaults_test.go
+++ b/golang/libnbd_120_set_non_defaults_test.go
@@ -93,6 +93,18 @@ func Test120SetNonDefaults(t *testing.T) {
 		t.Fatalf("unexpected structured replies state")
 	}

+	err = h.SetRequestMetaContext(false)
+	if err != nil {
+		t.Fatalf("could not set meta context state: %s", err)
+	}
+	meta, err := h.GetRequestMetaContext()
+	if err != nil {
+		t.Fatalf("could not get meta context state: %s", err)
+	}
+	if meta != false {
+		t.Fatalf("unexpected meta context state")
+	}
+
 	err = h.SetRequestBlockSize(false)
 	if err != nil {
 		t.Fatalf("could not set block size state: %s", err)
diff --git a/golang/libnbd_230_opt_info_test.go
b/golang/libnbd_230_opt_info_test.go
index 3dd231a..bc4cadf 100644
--- a/golang/libnbd_230_opt_info_test.go
+++ b/golang/libnbd_230_opt_info_test.go
@@ -1,5 +1,5 @@
 /* libnbd golang tests
- * Copyright (C) 2013-2021 Red Hat Inc.
+ * 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
@@ -113,11 +113,15 @@ func Test230OptInfo(t *testing.T) {
 		t.Fatalf("expected error")
 	}

-	/* info for a different export */
+	/* info for a different export, with automatic meta_context disabled */
 	err = h.SetExportName("b")
 	if err != nil {
 		t.Fatalf("set export name failed unexpectedly: %s", err)
 	}
+	err = h.SetRequestMetaContext(false)
+	if err != nil {
+		t.Fatalf("set request meta context failed unexpectedly: %s", err)
+	}
 	err = h.OptInfo()
 	if err != nil {
 		t.Fatalf("opt_info failed unexpectedly: %s", err)
@@ -136,12 +140,13 @@ func Test230OptInfo(t *testing.T) {
 	if ro {
 		t.Fatalf("unexpected readonly")
 	}
-	meta, err = h.CanMetaContext(context_base_allocation)
-	if err != nil {
-		t.Fatalf("can_meta failed unexpectedly: %s", err)
+	_, err = h.CanMetaContext(context_base_allocation)
+	if err == nil {
+		t.Fatalf("expected error")
 	}
-	if !meta {
-		t.Fatalf("unexpected meta context")
+	err = h.SetRequestMetaContext(true)
+	if err != nil {
+		t.Fatalf("set request meta context failed unexpectedly: %s", err)
 	}

 	/* go on something not present */
@@ -220,6 +225,75 @@ func Test230OptInfo(t *testing.T) {
 	if size != 4 {
 		t.Fatalf("unexpected size")
 	}
+	meta, err = h.CanMetaContext(context_base_allocation)
+	if err != nil {
+		t.Fatalf("can_meta failed unexpectedly: %s", err)
+	}
+	if !meta {
+		t.Fatalf("unexpected meta context")
+	}
+
+	h.Shutdown(nil)
+
+	/* Another cnonection. This time, check that SET_META triggered by OptInfo
+	 * persists through OptGo with SetRequestMetaContext disabled.
+	 */
+	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", "sh", script,
+	})
+	if err != nil {
+		t.Fatalf("could not connect: %s", err)
+	}
+	err = h.AddMetaContext("x-unexpected:bogus")
+	if err != nil {
+		t.Fatalf("could not add meta context: %s", err)
+	}
+
+	_, err = h.CanMetaContext(context_base_allocation)
+	if err == nil {
+		t.Fatalf("expected error")
+	}
+	err = h.OptInfo()
+	if err != nil {
+		t.Fatalf("opt_info failed unexpectedly: %s", err)
+	}
+	meta, err = h.CanMetaContext(context_base_allocation)
+	if err != nil {
+		t.Fatalf("can_meta failed unexpectedly: %s", err)
+	}
+	if meta {
+		t.Fatalf("unexpected meta context")
+	}
+	err = h.SetRequestMetaContext(false)
+	if err != nil {
+		t.Fatalf("set request meta context failed unexpectedly: %s", err)
+	}
+	/* Adding to the request list now won't matter */
+	err = h.AddMetaContext(context_base_allocation)
+	if err != nil {
+		t.Fatalf("could not add meta context: %s", err)
+	}
+	err = h.OptGo()
+	if err != nil {
+		t.Fatalf("opt_go failed unexpectedly: %s", err)
+	}
+	meta, err = h.CanMetaContext(context_base_allocation)
+	if err != nil {
+		t.Fatalf("can_meta failed unexpectedly: %s", err)
+	}
+	if meta {
+		t.Fatalf("unexpected meta context")
+	}

 	h.Shutdown(nil)
 }
--
2.37.2