Eric Blake
2022-Sep-26 22:06 UTC
[Libguestfs] [libnbd PATCH v3 18/18] tests: Language port of nbd_opt_structured_reply() tests
As promised in the previous patch, this addresses the FIXME comments added in language port tests earlier in the series now that we can control structured replies when we want. --- python/t/240-opt-list-meta.py | 11 ++++++- python/t/250-opt-set-meta.py | 20 ++++++++---- ocaml/tests/test_240_opt_list_meta.ml | 11 ++++++- ocaml/tests/test_250_opt_set_meta.ml | 29 ++++++++++++++--- golang/libnbd_240_opt_list_meta_test.go | 21 ++++++++++++- golang/libnbd_250_opt_set_meta_test.go | 41 ++++++++++++++++++++++--- 6 files changed, 114 insertions(+), 19 deletions(-) diff --git a/python/t/240-opt-list-meta.py b/python/t/240-opt-list-meta.py index 8cafdd54..c9170689 100644 --- a/python/t/240-opt-list-meta.py +++ b/python/t/240-opt-list-meta.py @@ -124,6 +124,15 @@ def must_fail(f, *args, **kwds): assert h.stats_bytes_sent() > bytes print("ignoring failure from old server: %s" % ex.string) -# FIXME: Once nbd_opt_structured_reply exists, use it here and retry. +# Now enable structured replies, and a retry should pass. +r = h.opt_structured_reply() +assert r is True + +count = 0 +seen = False +r = h.opt_list_meta_context(lambda *args: f(42, *args)) +assert r == count +assert r >= 1 +assert seen is True h.opt_abort() diff --git a/python/t/250-opt-set-meta.py b/python/t/250-opt-set-meta.py index c543a5d6..0d08bc0a 100644 --- a/python/t/250-opt-set-meta.py +++ b/python/t/250-opt-set-meta.py @@ -39,21 +39,29 @@ def must_fail(f, *args, **kwds): pass -# First process, with structured replies. Get into negotiating state. +# First process, delay structured replies. Get into negotiating state. h = nbd.NBD() h.set_opt_mode(True) +h.set_request_structured_replies(False) h.connect_command(["nbdkit", "-s", "--exit-with-parent", "-v", "memory", "size=1M"]) # No contexts negotiated yet; can_meta should be error if any requested +assert h.get_structured_replies_negotiated() is False +assert h.can_meta_context(nbd.CONTEXT_BASE_ALLOCATION) is False +h.add_meta_context(nbd.CONTEXT_BASE_ALLOCATION) +must_fail(h.can_meta_context, nbd.CONTEXT_BASE_ALLOCATION) + +# SET cannot succeed until SR is negotiated. +count = 0 +seen = False +must_fail(h.opt_set_meta_context, lambda *args: f(42, *args)) +assert count == 0 +assert seen is False +assert h.opt_structured_reply() is True assert h.get_structured_replies_negotiated() is True -assert h.can_meta_context(nbd.CONTEXT_BASE_ALLOCATION) is False -h.add_meta_context(nbd.CONTEXT_BASE_ALLOCATION) must_fail(h.can_meta_context, nbd.CONTEXT_BASE_ALLOCATION) -# FIXME: Once nbd_opt_structured_reply exists, check that set before -# SR fails server-side, then enable SR for rest of process. - # nbdkit does not match wildcard for SET, even though it does for LIST count = 0 seen = False diff --git a/ocaml/tests/test_240_opt_list_meta.ml b/ocaml/tests/test_240_opt_list_meta.ml index 18582c44..f3487c27 100644 --- a/ocaml/tests/test_240_opt_list_meta.ml +++ b/ocaml/tests/test_240_opt_list_meta.ml @@ -135,7 +135,16 @@ let printf "ignoring failure from old server %s\n" errstr ); - (* FIXME: Once nbd_opt_structured_reply exists, use it here and retry. *) + (* Now enable structured replies, and a retry should pass. *) + let sr = NBD.opt_structured_reply nbd in + assert sr; + + count := 0; + seen := false; + let r = NBD.opt_list_meta_context nbd (f 42) in + assert (r = !count); + assert (r >= 1); + assert !seen; NBD.opt_abort nbd diff --git a/ocaml/tests/test_250_opt_set_meta.ml b/ocaml/tests/test_250_opt_set_meta.ml index f35012fd..0e1c17b5 100644 --- a/ocaml/tests/test_250_opt_set_meta.ml +++ b/ocaml/tests/test_250_opt_set_meta.ml @@ -27,16 +27,17 @@ let 0 let () - (* First process, with structured replies. Get into negotiating state. *) + (* First process, delay structured replies. Get into negotiating state. *) let nbd = NBD.create () in NBD.set_opt_mode nbd true; + NBD.set_request_structured_replies nbd false; NBD.connect_command nbd ["nbdkit"; "-s"; "--exit-with-parent"; "-v"; "memory"; "size=1M"]; (* No contexts negotiated yet; can_meta should be error if any requested *) let sr = NBD.get_structured_replies_negotiated nbd in - assert sr; + assert (not sr); let m = NBD.can_meta_context nbd NBD.context_base_allocation in assert (not m); NBD.add_meta_context nbd NBD.context_base_allocation; @@ -47,9 +48,27 @@ let NBD.Error (errstr, errno) -> () ); - (* FIXME: Once nbd_opt_structured_reply exists, check that set before - * SR fails server-side, then enable SR for rest of process. - *) + (* SET cannot succeed until SR is negotiated. *) + count := 0; + seen := false; + (try + let _ = NBD.can_meta_context nbd NBD.context_base_allocation in + assert false + with + NBD.Error (errstr, errno) -> () + ); + assert (!count = 0); + assert (not !seen); + let sr = NBD.opt_structured_reply nbd in + assert sr; + let sr = NBD.get_structured_replies_negotiated nbd in + assert sr; + (try + let _ = NBD.can_meta_context nbd NBD.context_base_allocation in + assert false + with + NBD.Error (errstr, errno) -> () + ); (* nbdkit does not match wildcard for SET, even though it does for LIST *) count := 0; diff --git a/golang/libnbd_240_opt_list_meta_test.go b/golang/libnbd_240_opt_list_meta_test.go index 9d6f6f28..9b0b4144 100644 --- a/golang/libnbd_240_opt_list_meta_test.go +++ b/golang/libnbd_240_opt_list_meta_test.go @@ -256,7 +256,26 @@ func Test240OptListMeta(t *testing.T) { t.Fatalf("unexpected count after opt_list_meta_context") } - /* FIXME: Once nbd_opt_structured_reply exists, use it here and retry. */ + /* Now enable structured replies, and a retry should pass. */ + sr, err := h.OptStructuredReply() + if err != nil { + t.Fatalf("could not request opt_structured_reply: %s", err) + } + if !sr { + t.Fatalf("structured replies not enabled: %s", err) + } + + 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 < 1 || r != list_count || !list_seen { + t.Fatalf("unexpected count after opt_list_meta_context") + } err = h.OptAbort() if err != nil { diff --git a/golang/libnbd_250_opt_set_meta_test.go b/golang/libnbd_250_opt_set_meta_test.go index 1740d83e..cf036f45 100644 --- a/golang/libnbd_250_opt_set_meta_test.go +++ b/golang/libnbd_250_opt_set_meta_test.go @@ -35,7 +35,7 @@ func setmetaf(user_data int, name string) int { } func Test250OptSetMeta(t *testing.T) { - /* First process, with structured replies. Get into negotiating state. */ + /* First process, delay structured replies. Get into negotiating state. */ h, err := Create() if err != nil { t.Fatalf("could not create handle: %s", err) @@ -46,6 +46,10 @@ func Test250OptSetMeta(t *testing.T) { if err != nil { t.Fatalf("could not set opt mode: %s", err) } + err = h.SetRequestStructuredReplies(false) + if err != nil { + t.Fatalf("could not set opt mode: %s", err) + } err = h.ConnectCommand([]string{ "nbdkit", "-s", "--exit-with-parent", "-v", @@ -60,7 +64,7 @@ func Test250OptSetMeta(t *testing.T) { if err != nil { t.Fatalf("could not check structured replies negotiated: %s", err) } - if !sr { + if sr { t.Fatalf("unexpected structured replies state") } meta, err := h.CanMetaContext(context_base_allocation) @@ -79,9 +83,36 @@ func Test250OptSetMeta(t *testing.T) { t.Fatalf("expected error") } - /* FIXME: Once OptStructuredReply exists, check that set before - * SR fails server-side, then enable SR for rest of process. - */ + /* SET cannot succeed until SR is negotiated. */ + set_count = 0 + set_seen = false + _, err = h.OptSetMetaContext(func(name string) int { + return setmetaf(42, name) + }) + if err == nil { + t.Fatalf("expected error") + } + if set_count != 0 || set_seen { + t.Fatalf("unexpected set_count after opt_set_meta_context") + } + sr, err = h.OptStructuredReply() + if err != nil { + t.Fatalf("could not trigger opt_structured_reply: %s", err) + } + if !sr { + t.Fatalf("unexpected structured replies state") + } + sr, err = h.GetStructuredRepliesNegotiated() + if err != nil { + t.Fatalf("could not check structured replies negotiated: %s", err) + } + if !sr { + t.Fatalf("unexpected structured replies state") + } + _, err = h.CanMetaContext(context_base_allocation) + if err == nil { + t.Fatalf("expected error") + } /* nbdkit does not match wildcard for SET, even though it does for LIST */ set_count = 0 -- 2.37.3
Richard W.M. Jones
2022-Sep-27 11:34 UTC
[Libguestfs] [libnbd PATCH v3 18/18] tests: Language port of nbd_opt_structured_reply() tests
For 14-18: Acked-by: Richard W.M. Jones <rjones at redhat.com> Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-builder quickly builds VMs from scratch http://libguestfs.org/virt-builder.1.html