Revert "ci: skip "lib/test-fork-safe-execvpe.sh" on Alpine Linux"
[libnbd.git] / golang / libnbd_245_opt_list_meta_queries_test.go
blobfc1ab60cf4f826045889d33a2a54ae8a51ea2502
1 /* libnbd golang tests
2 * Copyright Red Hat
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 package libnbd
21 import "testing"
23 var listq_count uint
24 var listq_seen bool
26 func listmetaqf(user_data int, name string) int {
27 if user_data != 42 {
28 panic("expected user_data == 42")
30 listq_count++
31 if (name == context_base_allocation) {
32 listq_seen = true
34 return 0
37 func Test245OptListMetaQueries(t *testing.T) {
38 /* Get into negotiating state. */
39 h, err := Create()
40 if err != nil {
41 t.Fatalf("could not create handle: %s", err)
43 defer h.Close()
45 err = h.SetOptMode(true)
46 if err != nil {
47 t.Fatalf("could not set opt mode: %s", err)
50 err = h.ConnectCommand([]string{
51 "nbdkit", "-s", "--exit-with-parent", "-v",
52 "memory", "size=1M",
54 if err != nil {
55 t.Fatalf("could not connect: %s", err)
58 /* First pass: empty query should give at least "base:allocation".
59 * The explicit query overrides a non-empty nbd_add_meta_context.
61 listq_count = 0
62 listq_seen = false
63 err = h.AddMetaContext("x-nosuch:")
64 if err != nil {
65 t.Fatalf("could not request add_meta_context: %s", err)
67 r, err := h.OptListMetaContextQueries([]string{ },
68 func(name string) int {
69 return listmetaqf(42, name)
71 if err != nil {
72 t.Fatalf("could not request opt_list_meta_context_queries: %s", err)
74 if r != listq_count || r < 1 || !listq_seen {
75 t.Fatalf("unexpected count after opt_list_meta_context_queries")
78 /* Second pass: bogus query has no response. */
79 listq_count = 0
80 listq_seen = false
81 err = h.ClearMetaContexts()
82 if err != nil {
83 t.Fatalf("could not request add_meta_context: %s", err)
85 r, err = h.OptListMetaContextQueries([]string{ "x-nosuch:" },
86 func(name string) int {
87 return listmetaqf(42, name)
89 if err != nil {
90 t.Fatalf("could not request opt_list_meta_context_queries: %s", err)
92 if r != 0 || r != listq_count || listq_seen {
93 t.Fatalf("unexpected count after opt_list_meta_context_queries")
96 /* Third pass: specific query should have one match. */
97 listq_count = 0
98 listq_seen = false
99 r, err = h.OptListMetaContextQueries([]string{
100 "x-nosuch:", context_base_allocation },
101 func(name string) int {
102 return listmetaqf(42, name)
104 if err != nil {
105 t.Fatalf("could not request opt_list_meta_context_queries: %s", err)
107 if r != 1 || r != listq_count || !listq_seen {
108 t.Fatalf("unexpected count after opt_list_meta_context_queries")
111 err = h.OptAbort()
112 if err != nil {
113 t.Fatalf("could not request opt_abort: %s", err)