Revert "ci: skip "lib/test-fork-safe-execvpe.sh" on Alpine Linux"
[libnbd.git] / golang / libnbd_460_block_status_test.go
blobdca0d1b87ec3103ea3bb5ccab3f727ce11d0993a
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 (
22 "fmt"
23 "os"
24 "strings"
25 "testing"
28 var entries []uint32
30 func mcf(metacontext string, offset uint64, e []uint32, error *int) int {
31 if *error != 0 {
32 panic("expected *error == 0")
34 if metacontext == "base:allocation" {
35 entries = e
37 return 0
40 // Seriously WTF?
41 func mc_compare(a1 []uint32, a2 []uint32) bool {
42 if len(a1) != len(a2) {
43 return false
45 for i := 0; i < len(a1); i++ {
46 if a1[i] != a2[i] {
47 return false
50 return true
53 // Like, WTF, again?
54 func mc_to_string(a []uint32) string {
55 ss := make([]string, len(a))
56 for i := 0; i < len(a); i++ {
57 ss[i] = fmt.Sprint(a[i])
59 return strings.Join(ss, ", ")
62 func Test460BlockStatus(t *testing.T) {
63 srcdir := os.Getenv("abs_top_srcdir")
64 script := srcdir + "/tests/meta-base-allocation.sh"
66 h, err := Create()
67 if err != nil {
68 t.Fatalf("could not create handle: %s", err)
70 defer h.Close()
72 err = h.AddMetaContext("base:allocation")
73 if err != nil {
74 t.Fatalf("%s", err)
76 err = h.ConnectCommand([]string{
77 "nbdkit", "-s", "--exit-with-parent", "-v",
78 "sh", script,
80 if err != nil {
81 t.Fatalf("%s", err)
84 err = h.BlockStatus(65536, 0, mcf, nil)
85 if err != nil {
86 t.Fatalf("%s", err)
88 if !mc_compare(entries, []uint32{
89 8192, 0,
90 8192, 1,
91 16384, 3,
92 16384, 2,
93 16384, 0,
94 }) {
95 t.Fatalf("unexpected entries (1): %s", mc_to_string(entries))
98 err = h.BlockStatus(1024, 32256, mcf, nil)
99 if err != nil {
100 t.Fatalf("%s", err)
102 if !mc_compare(entries, []uint32{
103 512, 3,
104 16384, 2,
105 }) {
106 t.Fatalf("unexpected entries (2): %s", mc_to_string(entries))
109 var optargs BlockStatusOptargs
110 optargs.FlagsSet = true
111 optargs.Flags = CMD_FLAG_REQ_ONE
112 err = h.BlockStatus(1024, 32256, mcf, &optargs)
113 if err != nil {
114 t.Fatalf("%s", err)
116 if !mc_compare(entries, []uint32{512, 3}) {
117 t.Fatalf("unexpected entries (3): %s", mc_to_string(entries))