Skip to content

Commit 3a35911

Browse files
authored
Merge pull request #4079 from apostasie/tigron-3-json-comparator
[Tigron]: expect.JSON comparator and updated volume test
2 parents d3cb96e + 9255358 commit 3a35911

File tree

5 files changed

+280
-44
lines changed

5 files changed

+280
-44
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
},

mod/tigron/expect/comparators.go

+17
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919
package expect
2020

2121
import (
22+
"encoding/json"
2223
"regexp"
2324
"testing"
2425

2526
"github.com/containerd/nerdctl/mod/tigron/internal/assertive"
2627
"github.com/containerd/nerdctl/mod/tigron/test"
28+
"github.com/containerd/nerdctl/mod/tigron/tig"
2729
)
2830

2931
// All can be used as a parameter for expected.Output to group a set of comparators.
@@ -69,3 +71,18 @@ func Match(reg *regexp.Regexp) test.Comparator {
6971
assertive.Match(assertive.WithFailLater(t), stdout, reg, info)
7072
}
7173
}
74+
75+
// JSON allows to verify that the output can be marshalled into T, and optionally can be further verified by a provided
76+
// method.
77+
func JSON[T any](obj T, verifier func(T, string, tig.T)) test.Comparator {
78+
return func(stdout, info string, t *testing.T) {
79+
t.Helper()
80+
81+
err := json.Unmarshal([]byte(stdout), &obj)
82+
assertive.ErrorIsNil(assertive.WithFailLater(t), err, "failed to unmarshal JSON from stdout")
83+
84+
if verifier != nil && err == nil {
85+
verifier(obj, info, t)
86+
}
87+
}
88+
}

mod/tigron/expect/comparators_test.go

+26-5
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,45 @@ package expect_test
2020
// TODO: add a lot more tests including failure conditions with mimicry
2121

2222
import (
23+
"encoding/json"
2324
"regexp"
2425
"testing"
2526

2627
"github.com/containerd/nerdctl/mod/tigron/expect"
28+
"github.com/containerd/nerdctl/mod/tigron/internal/assertive"
29+
"github.com/containerd/nerdctl/mod/tigron/tig"
2730
)
2831

2932
func TestExpect(t *testing.T) {
3033
t.Parallel()
3134

32-
expect.Contains("b")("a b c", "info", t)
33-
expect.DoesNotContain("d")("a b c", "info", t)
34-
expect.Equals("a b c")("a b c", "info", t)
35-
expect.Match(regexp.MustCompile("[a-z ]+"))("a b c", "info", t)
35+
expect.Contains("b")("a b c", "contains works", t)
36+
expect.DoesNotContain("d")("a b c", "does not contain works", t)
37+
expect.Equals("a b c")("a b c", "equals work", t)
38+
expect.Match(regexp.MustCompile("[a-z ]+"))("a b c", "match works", t)
3639

3740
expect.All(
3841
expect.Contains("b"),
3942
expect.DoesNotContain("d"),
4043
expect.Equals("a b c"),
4144
expect.Match(regexp.MustCompile("[a-z ]+")),
42-
)("a b c", "info", t)
45+
)("a b c", "all", t)
46+
47+
type foo struct {
48+
Foo map[string]string `json:"foo"`
49+
}
50+
51+
data, err := json.Marshal(&foo{
52+
Foo: map[string]string{
53+
"foo": "bar",
54+
},
55+
})
56+
57+
assertive.ErrorIsNil(t, err)
58+
59+
expect.JSON(&foo{}, nil)(string(data), "json, no verifier", t)
60+
61+
expect.JSON(&foo{}, func(obj *foo, info string, t tig.T) {
62+
assertive.IsEqual(t, obj.Foo["foo"], "bar", info)
63+
})(string(data), "json, with verifier", t)
4364
}

0 commit comments

Comments
 (0)