Revert "ci: skip "lib/test-fork-safe-execvpe.sh" on Alpine Linux"
[libnbd.git] / golang / libnbd_255_opt_set_meta_queries_test.go
blobcca25ae7f8fa3440b4f73feccbf46150e3fa7a4d
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 setq_count uint
24 var setq_seen bool
26 func setmetaqf(user_data int, name string) int {
27 if user_data != 42 {
28 panic("expected user_data == 42")
30 setq_count++
31 if (name == context_base_allocation) {
32 setq_seen = true
34 return 0
37 func Test255OptSetMetaQueries(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 /* nbdkit does not match wildcard for SET, even though it does for LIST */
59 setq_count = 0
60 setq_seen = false
61 r, err := h.OptSetMetaContextQueries([]string{"base:"},
62 func(name string) int {
63 return setmetaqf(42, name)
65 if err != nil {
66 t.Fatalf("could not request opt_set_meta_context_queries: %s", err)
68 if r != setq_count || r != 0 || setq_seen {
69 t.Fatalf("unexpected count after opt_set_meta_context_queries")
72 /* Negotiating with no contexts is not an error, but selects nothing.
73 * An explicit empty list overrides a non-empty implicit list.
75 setq_count = 0
76 setq_seen = false
77 err = h.AddMetaContext(context_base_allocation)
78 if err != nil {
79 t.Fatalf("could not request add_meta_context: %s", err)
81 r, err = h.OptSetMetaContextQueries([]string{}, func(name string) int {
82 return setmetaqf(42, name)
84 if err != nil {
85 t.Fatalf("could not request opt_set_meta_context_queries: %s", err)
87 if r != setq_count || r != 0 || setq_seen {
88 t.Fatalf("unexpected set_count after opt_set_meta_context_queries")
90 meta, err := h.CanMetaContext(context_base_allocation)
91 if err != nil {
92 t.Fatalf("could not check can meta context: %s", err)
94 if meta {
95 t.Fatalf("unexpected can meta context state")
98 /* Request 2 with expectation of 1; with SetRequestMetaContext off */
99 setq_count = 0
100 setq_seen = false
101 r, err = h.OptSetMetaContextQueries([]string{
102 "x-nosuch:context", context_base_allocation},
103 func(name string) int {
104 return setmetaqf(42, name)
106 if err != nil {
107 t.Fatalf("could not request opt_set_meta_context_queries: %s", err)
109 if r != 1 || r != setq_count || !setq_seen {
110 t.Fatalf("unexpected set_count after opt_set_meta_context_queries")
112 meta, err = h.CanMetaContext(context_base_allocation)
113 if err != nil {
114 t.Fatalf("could not check can meta context: %s", err)
116 if !meta {
117 t.Fatalf("unexpected can meta context state")
120 /* Transition to transmission phase; our last set should remain active */
121 err = h.SetRequestMetaContext(false)
122 if err != nil {
123 t.Fatalf("could not set_request_meta_context: %s", err)
125 err = h.OptGo()
126 if err != nil {
127 t.Fatalf("could not request opt_go: %s", err)
129 meta, err = h.CanMetaContext(context_base_allocation)
130 if err != nil {
131 t.Fatalf("could not check can meta context: %s", err)
133 if !meta {
134 t.Fatalf("unexpected can meta context state")
137 err = h.Shutdown(nil)
138 if err != nil {
139 t.Fatalf("could not request shutdown: %s", err)