Revert "ci: skip "lib/test-fork-safe-execvpe.sh" on Alpine Linux"
[libnbd.git] / golang / libnbd_250_opt_set_meta_test.go
blob6d27fa22041821dda2d16a858720948c68e0c5b8
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 set_count uint
24 var set_seen bool
26 func setmetaf(user_data int, name string) int {
27 if user_data != 42 {
28 panic("expected user_data == 42")
30 set_count++
31 if (name == context_base_allocation) {
32 set_seen = true
34 return 0
37 func Test250OptSetMeta(t *testing.T) {
38 /* Get into negotiating state without structured replies. */
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)
49 err = h.SetRequestStructuredReplies(false)
50 if err != nil {
51 t.Fatalf("could not set opt mode: %s", err)
54 err = h.ConnectCommand([]string{
55 "nbdkit", "-s", "--exit-with-parent", "-v",
56 "memory", "size=1M",
58 if err != nil {
59 t.Fatalf("could not connect: %s", err)
62 /* No contexts negotiated yet; CanMeta should be error if any requested */
63 sr, err := h.GetStructuredRepliesNegotiated()
64 if err != nil {
65 t.Fatalf("could not check structured replies negotiated: %s", err)
67 if sr {
68 t.Fatalf("unexpected structured replies state")
70 meta, err := h.CanMetaContext(context_base_allocation)
71 if err != nil {
72 t.Fatalf("could not check can meta context: %s", err)
74 if meta {
75 t.Fatalf("unexpected can meta context state")
77 err = h.AddMetaContext(context_base_allocation)
78 if err != nil {
79 t.Fatalf("could not request add_meta_context: %s", err)
81 _, err = h.CanMetaContext(context_base_allocation)
82 if err == nil {
83 t.Fatalf("expected error")
86 /* SET cannot succeed until SR is negotiated. */
87 set_count = 0
88 set_seen = false
89 _, err = h.OptSetMetaContext(func(name string) int {
90 return setmetaf(42, name)
92 if err == nil {
93 t.Fatalf("expected error")
95 if set_count != 0 || set_seen {
96 t.Fatalf("unexpected set_count after opt_set_meta_context")
98 sr, err = h.OptStructuredReply()
99 if err != nil {
100 t.Fatalf("could not trigger opt_structured_reply: %s", err)
102 if !sr {
103 t.Fatalf("unexpected structured replies state")
105 sr, err = h.GetStructuredRepliesNegotiated()
106 if err != nil {
107 t.Fatalf("could not check structured replies negotiated: %s", err)
109 if !sr {
110 t.Fatalf("unexpected structured replies state")
112 _, err = h.CanMetaContext(context_base_allocation)
113 if err == nil {
114 t.Fatalf("expected error")
117 /* nbdkit does not match wildcard for SET, even though it does for LIST */
118 set_count = 0
119 set_seen = false
120 err = h.ClearMetaContexts()
121 if err != nil {
122 t.Fatalf("could not request clear_meta_contexts: %s", err)
124 err = h.AddMetaContext("base:")
125 if err != nil {
126 t.Fatalf("could not request add_meta_context: %s", err)
128 r, err := h.OptSetMetaContext(func(name string) int {
129 return setmetaf(42, name)
131 if err != nil {
132 t.Fatalf("could not request opt_set_meta_context: %s", err)
134 if r != set_count || r != 0 || set_seen {
135 t.Fatalf("unexpected set_count after opt_set_meta_context")
138 /* Negotiating with no contexts is not an error, but selects nothing */
139 set_count = 0
140 set_seen = false
141 err = h.ClearMetaContexts()
142 if err != nil {
143 t.Fatalf("could not request clear_meta_contexts: %s", err)
145 r, err = h.OptSetMetaContext(func(name string) int {
146 return setmetaf(42, name)
148 if err != nil {
149 t.Fatalf("could not request opt_set_meta_context: %s", err)
151 if r != set_count || r != 0 || set_seen {
152 t.Fatalf("unexpected set_count after opt_set_meta_context")
154 meta, err = h.CanMetaContext(context_base_allocation)
155 if err != nil {
156 t.Fatalf("could not check can meta context: %s", err)
158 if meta {
159 t.Fatalf("unexpected can meta context state")
162 /* Request 2 with expectation of 1; with SetRequestMetaContext off */
163 set_count = 0
164 set_seen = false
165 err = h.AddMetaContext("x-nosuch:context")
166 if err != nil {
167 t.Fatalf("could not request add_meta_context: %s", err)
169 err = h.AddMetaContext(context_base_allocation)
170 if err != nil {
171 t.Fatalf("could not request add_meta_context: %s", err)
173 err = h.SetRequestMetaContext(false)
174 if err != nil {
175 t.Fatalf("could not set_request_meta_context: %s", err)
177 r, err = h.OptSetMetaContext(func(name string) int {
178 return setmetaf(42, name)
180 if err != nil {
181 t.Fatalf("could not request opt_set_meta_context: %s", err)
183 if r != 1 || r != set_count || !set_seen {
184 t.Fatalf("unexpected set_count after opt_set_meta_context")
186 meta, err = h.CanMetaContext(context_base_allocation)
187 if err != nil {
188 t.Fatalf("could not check can meta context: %s", err)
190 if !meta {
191 t.Fatalf("unexpected can meta context state")
194 /* Transition to transmission phase; our last set should remain active */
195 err = h.ClearMetaContexts()
196 if err != nil {
197 t.Fatalf("could not request clear_meta_contexts: %s", err)
199 err = h.AddMetaContext("x-nosuch:context")
200 if err != nil {
201 t.Fatalf("could not request add_meta_context: %s", err)
203 err = h.OptGo()
204 if err != nil {
205 t.Fatalf("could not request opt_go: %s", err)
207 meta, err = h.CanMetaContext(context_base_allocation)
208 if err != nil {
209 t.Fatalf("could not check can meta context: %s", err)
211 if !meta {
212 t.Fatalf("unexpected can meta context state")
215 /* Now too late to set; but should not lose earlier state */
216 set_count = 0
217 set_seen = false
218 _, err = h.OptSetMetaContext(func(name string) int {
219 return setmetaf(42, name)
221 if err == nil {
222 t.Fatalf("expected error")
224 if set_count != 0 || set_seen {
225 t.Fatalf("unexpected set_count after opt_set_meta_context")
227 meta, err = h.CanMetaContext(context_base_allocation)
228 if err != nil {
229 t.Fatalf("could not check can meta context: %s", err)
231 if !meta {
232 t.Fatalf("unexpected can meta context state")
235 err = h.Shutdown(nil)
236 if err != nil {
237 t.Fatalf("could not request shutdown: %s", err)