Skip to content

Commit 2f2f8e6

Browse files
committed
Migrate Volume test to use expect.JSON
Signed-off-by: apostasie <[email protected]>
1 parent a30b0f9 commit 2f2f8e6

File tree

1 file changed

+11
-31
lines changed

1 file changed

+11
-31
lines changed

cmd/nerdctl/volume/volume_inspect_test.go

+11-31
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package volume
1818

1919
import (
2020
"crypto/rand"
21-
"encoding/json"
2221
"errors"
2322
"fmt"
2423
"os"
@@ -31,6 +30,7 @@ import (
3130
"github.com/containerd/nerdctl/mod/tigron/expect"
3231
"github.com/containerd/nerdctl/mod/tigron/require"
3332
"github.com/containerd/nerdctl/mod/tigron/test"
33+
"github.com/containerd/nerdctl/mod/tigron/tig"
3434

3535
"github.com/containerd/nerdctl/v2/pkg/inspecttypes/native"
3636
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
@@ -99,15 +99,11 @@ func TestVolumeInspect(t *testing.T) {
9999
return &test.Expected{
100100
Output: expect.All(
101101
expect.Contains(data.Get("vol1")),
102-
func(stdout string, info string, t *testing.T) {
103-
var dc []native.Volume
104-
if err := json.Unmarshal([]byte(stdout), &dc); err != nil {
105-
t.Fatal(err)
106-
}
102+
expect.JSON([]native.Volume{}, func(dc []native.Volume, info string, t tig.T) {
107103
assert.Assert(t, len(dc) == 1, fmt.Sprintf("one result, not %d", len(dc))+info)
108104
assert.Assert(t, dc[0].Name == data.Get("vol1"), fmt.Sprintf("expected name to be %q (was %q)", data.Get("vol1"), dc[0].Name)+info)
109105
assert.Assert(t, dc[0].Labels == nil, fmt.Sprintf("expected labels to be nil and were %v", dc[0].Labels)+info)
110-
},
106+
}),
111107
),
112108
}
113109
},
@@ -121,16 +117,12 @@ func TestVolumeInspect(t *testing.T) {
121117
return &test.Expected{
122118
Output: expect.All(
123119
expect.Contains(data.Get("vol2")),
124-
func(stdout string, info string, t *testing.T) {
125-
var dc []native.Volume
126-
if err := json.Unmarshal([]byte(stdout), &dc); err != nil {
127-
t.Fatal(err)
128-
}
120+
expect.JSON([]native.Volume{}, func(dc []native.Volume, info string, t tig.T) {
129121
labels := *dc[0].Labels
130122
assert.Assert(t, len(labels) == 2, fmt.Sprintf("two results, not %d", len(labels)))
131123
assert.Assert(t, labels["foo"] == "fooval", fmt.Sprintf("label foo should be fooval, not %s", labels["foo"]))
132124
assert.Assert(t, labels["bar"] == "barval", fmt.Sprintf("label bar should be barval, not %s", labels["bar"]))
133-
},
125+
}),
134126
),
135127
}
136128
},
@@ -145,13 +137,9 @@ func TestVolumeInspect(t *testing.T) {
145137
return &test.Expected{
146138
Output: expect.All(
147139
expect.Contains(data.Get("vol1")),
148-
func(stdout string, info string, t *testing.T) {
149-
var dc []native.Volume
150-
if err := json.Unmarshal([]byte(stdout), &dc); err != nil {
151-
t.Fatal(err)
152-
}
140+
expect.JSON([]native.Volume{}, func(dc []native.Volume, info string, t tig.T) {
153141
assert.Assert(t, dc[0].Size == size, fmt.Sprintf("expected size to be %d (was %d)", size, dc[0].Size))
154-
},
142+
}),
155143
),
156144
}
157145
},
@@ -166,15 +154,11 @@ func TestVolumeInspect(t *testing.T) {
166154
Output: expect.All(
167155
expect.Contains(data.Get("vol1")),
168156
expect.Contains(data.Get("vol2")),
169-
func(stdout string, info string, t *testing.T) {
170-
var dc []native.Volume
171-
if err := json.Unmarshal([]byte(stdout), &dc); err != nil {
172-
t.Fatal(err)
173-
}
157+
expect.JSON([]native.Volume{}, func(dc []native.Volume, info string, t tig.T) {
174158
assert.Assert(t, len(dc) == 2, fmt.Sprintf("two results, not %d", len(dc)))
175159
assert.Assert(t, dc[0].Name == data.Get("vol1"), fmt.Sprintf("expected name to be %q (was %q)", data.Get("vol1"), dc[0].Name))
176160
assert.Assert(t, dc[1].Name == data.Get("vol2"), fmt.Sprintf("expected name to be %q (was %q)", data.Get("vol2"), dc[1].Name))
177-
},
161+
}),
178162
),
179163
}
180164
},
@@ -190,14 +174,10 @@ func TestVolumeInspect(t *testing.T) {
190174
Errors: []error{errdefs.ErrNotFound, errdefs.ErrInvalidArgument},
191175
Output: expect.All(
192176
expect.Contains(data.Get("vol1")),
193-
func(stdout string, info string, t *testing.T) {
194-
var dc []native.Volume
195-
if err := json.Unmarshal([]byte(stdout), &dc); err != nil {
196-
t.Fatal(err)
197-
}
177+
expect.JSON([]native.Volume{}, func(dc []native.Volume, info string, t tig.T) {
198178
assert.Assert(t, len(dc) == 1, fmt.Sprintf("one result, not %d", len(dc)))
199179
assert.Assert(t, dc[0].Name == data.Get("vol1"), fmt.Sprintf("expected name to be %q (was %q)", data.Get("vol1"), dc[0].Name))
200-
},
180+
}),
201181
),
202182
}
203183
},

0 commit comments

Comments
 (0)