[Libguestfs] [libnbd PATCH v2 10/12] tests: Add coverage for nbd_set_export_name fix

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

Add test coverage for the sequences fixed in the previous patch, where
changing the export name should clean up anything remembered about a
former export name.  The tests in other language bindings are
logically equivalent.
---
 python/t/230-opt-info.py           | 11 ++++++++---
 ocaml/tests/test_230_opt_info.ml   | 11 ++++++++---
 tests/opt-info.c                   | 24 ++++++++++++++++++------
 golang/libnbd_230_opt_info_test.go | 23 +++++++++++++++++------
 4 files changed, 51 insertions(+), 18 deletions(-)

diff --git a/python/t/230-opt-info.py b/python/t/230-opt-info.py
index 8aa47ae..fc22a97 100644
--- a/python/t/230-opt-info.py
+++ b/python/t/230-opt-info.py
@@ -45,17 +45,22 @@ assert h.get_size() == 0
 assert h.is_read_only() is True
 assert h.can_meta_context(nbd.CONTEXT_BASE_ALLOCATION) is True

-# info on something not present fails, wipes out prior info
-h.set_export_name("a")
-must_fail(h.opt_info)
+# changing export wipes out prior info
+h.set_export_name("b")
 must_fail(h.get_size)
 must_fail(h.is_read_only)
 must_fail(h.can_meta_context, nbd.CONTEXT_BASE_ALLOCATION)

+# info on something not present fails
+h.set_export_name("a")
+must_fail(h.opt_info)
+
 # info for a different export, with automatic meta_context disabled
 h.set_export_name("b")
 h.set_request_meta_context(False)
 h.opt_info()
+# idempotent name change is no-op
+h.set_export_name("b")
 assert h.get_size() == 1
 assert h.is_read_only() is False
 must_fail(h.can_meta_context, nbd.CONTEXT_BASE_ALLOCATION)
diff --git a/ocaml/tests/test_230_opt_info.ml b/ocaml/tests/test_230_opt_info.ml
index ec735ff..0403d14 100644
--- a/ocaml/tests/test_230_opt_info.ml
+++ b/ocaml/tests/test_230_opt_info.ml
@@ -62,17 +62,22 @@ let
   let meta = NBD.can_meta_context nbd NBD.context_base_allocation in
   assert meta;

-  (* info on something not present fails, wipes out prior info *)
-  NBD.set_export_name nbd "a";
-  fail_unary NBD.opt_info nbd;
+  (* changing export wipes out prior info *)
+  NBD.set_export_name nbd "b";
   fail_unary NBD.get_size nbd;
   fail_unary NBD.is_read_only nbd;
   fail_binary NBD.can_meta_context nbd NBD.context_base_allocation;

+  (* info on something not present fails *)
+  NBD.set_export_name nbd "a";
+  fail_unary NBD.opt_info nbd;
+
   (* 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;
+  (* idempotent name change is no-op *)
+  NBD.set_export_name nbd "b";
   let size = NBD.get_size nbd in
   assert (size = 1L);
   let ro = NBD.is_read_only nbd in
diff --git a/tests/opt-info.c b/tests/opt-info.c
index 26de0ee..70028ff 100644
--- a/tests/opt-info.c
+++ b/tests/opt-info.c
@@ -17,6 +17,7 @@
  */

 /* Test behavior of nbd_opt_info. */
+/* See also unit test 230 in the various language ports. */

 #include <config.h>

@@ -80,15 +81,11 @@ main (int argc, char *argv[])
     exit (EXIT_FAILURE);
   }

-  /* info on something not present fails, wipes out prior info */
-  if (nbd_set_export_name (nbd, "a") == -1) {
+  /* changing export wipes out prior info */
+  if (nbd_set_export_name (nbd, "b") == -1) {
     fprintf (stderr, "%s\n", nbd_get_error ());
     exit (EXIT_FAILURE);
   }
-  if (nbd_opt_info (nbd) != -1) {
-    fprintf (stderr, "expecting error for opt_info\n");
-    exit (EXIT_FAILURE);
-  }
   if (nbd_get_size (nbd) != -1) {
     fprintf (stderr, "expecting error for get_size\n");
     exit (EXIT_FAILURE);
@@ -102,6 +99,16 @@ main (int argc, char *argv[])
     exit (EXIT_FAILURE);
   }

+  /* info on something not present fails */
+  if (nbd_set_export_name (nbd, "a") == -1) {
+    fprintf (stderr, "%s\n", nbd_get_error ());
+    exit (EXIT_FAILURE);
+  }
+  if (nbd_opt_info (nbd) != -1) {
+    fprintf (stderr, "expecting error for opt_info\n");
+    exit (EXIT_FAILURE);
+  }
+
   /* info for a different export, with automatic meta_context disabled */
   if (nbd_set_export_name (nbd, "b") == -1) {
     fprintf (stderr, "%s\n", nbd_get_error ());
@@ -115,6 +122,11 @@ main (int argc, char *argv[])
     fprintf (stderr, "%s\n", nbd_get_error ());
     exit (EXIT_FAILURE);
   }
+  /* idempotent name change is no-op */
+  if (nbd_set_export_name (nbd, "b") == -1) {
+    fprintf (stderr, "%s\n", nbd_get_error ());
+    exit (EXIT_FAILURE);
+  }
   if ((r = nbd_get_size (nbd)) != 1) {
     fprintf (stderr, "expecting size of 1, got %" PRId64
"\n", r);
     exit (EXIT_FAILURE);
diff --git a/golang/libnbd_230_opt_info_test.go
b/golang/libnbd_230_opt_info_test.go
index bc4cadf..e16f661 100644
--- a/golang/libnbd_230_opt_info_test.go
+++ b/golang/libnbd_230_opt_info_test.go
@@ -91,15 +91,11 @@ func Test230OptInfo(t *testing.T) {
 		t.Fatalf("unexpected meta context")
 	}

-	/* info on something not present fails, wipes out prior info */
-	err = h.SetExportName("a")
+	/* changing export wipes out prior info */
+	err = h.SetExportName("b")
 	if err != nil {
 		t.Fatalf("set export name failed unexpectedly: %s", err)
 	}
-	err = h.OptInfo()
-	if err == nil {
-		t.Fatalf("expected error")
-	}
 	_, err = h.GetSize()
 	if err == nil {
 		t.Fatalf("expected error")
@@ -113,6 +109,16 @@ func Test230OptInfo(t *testing.T) {
 		t.Fatalf("expected error")
 	}

+	/* info on something not present fails */
+	err = h.SetExportName("a")
+	if err != nil {
+		t.Fatalf("set export name failed unexpectedly: %s", err)
+	}
+	err = h.OptInfo()
+	if err == nil {
+		t.Fatalf("expected error")
+	}
+
 	/* info for a different export, with automatic meta_context disabled */
 	err = h.SetExportName("b")
 	if err != nil {
@@ -126,6 +132,11 @@ func Test230OptInfo(t *testing.T) {
 	if err != nil {
 		t.Fatalf("opt_info failed unexpectedly: %s", err)
 	}
+	/* idempotent name change is no-op */
+	err = h.SetExportName("b")
+	if err != nil {
+		t.Fatalf("set export name failed unexpectedly: %s", err)
+	}
 	size, err = h.GetSize()
 	if err != nil {
 		t.Fatalf("size failed unexpectedly: %s", err)
--
2.37.2