@@ -20,7 +20,6 @@ import (
20
20
"encoding/json"
21
21
"fmt"
22
22
"os/exec"
23
- "reflect"
24
23
"strings"
25
24
"testing"
26
25
@@ -30,6 +29,7 @@ import (
30
29
31
30
containercollection "github.com/inspektor-gadget/inspektor-gadget/pkg/container-collection"
32
31
"github.com/inspektor-gadget/inspektor-gadget/pkg/container-utils/testutils"
32
+ "github.com/inspektor-gadget/inspektor-gadget/pkg/testing/match"
33
33
eventtypes "github.com/inspektor-gadget/inspektor-gadget/pkg/types"
34
34
)
35
35
@@ -38,28 +38,6 @@ var cmpIgnoreUnexported = cmpopts.IgnoreUnexported(
38
38
containercollection.K8sMetadata {},
39
39
)
40
40
41
- func parseMultiJSONOutput [T any ](t * testing.T , output string , normalize func (* T )) []* T {
42
- ret := []* T {}
43
-
44
- decoder := json .NewDecoder (strings .NewReader (output ))
45
- for decoder .More () {
46
- var entry T
47
- if err := decoder .Decode (& entry ); err != nil {
48
- require .NoError (t , err , "decoding json" )
49
- }
50
- // To be able to use reflect.DeepEqual and cmp.Diff, we need to
51
- // "normalize" the output so that it only includes non-default values
52
- // for the fields we are able to verify.
53
- if normalize != nil {
54
- normalize (& entry )
55
- }
56
-
57
- ret = append (ret , & entry )
58
- }
59
-
60
- return ret
61
- }
62
-
63
41
func parseJSONArrayOutput [T any ](t * testing.T , output string , normalize func (* T )) []* T {
64
42
entries := []* T {}
65
43
@@ -104,7 +82,7 @@ func expectAllToMatch[T any](t *testing.T, entries []*T, expectedEntry *T) {
104
82
// ExpectAllToMatch verifies that the expectedEntry is matched by all the
105
83
// entries in the output (Lines of independent JSON objects).
106
84
func ExpectAllToMatch [T any ](t * testing.T , output string , normalize func (* T ), expectedEntry * T ) {
107
- entries := parseMultiJSONOutput (t , output , normalize )
85
+ entries := match . ParseMultiJSONOutput (t , output , normalize )
108
86
expectAllToMatch (t , entries , expectedEntry )
109
87
}
110
88
@@ -122,50 +100,18 @@ func ExpectAllInMultipleArrayToMatch[T any](t *testing.T, output string, normali
122
100
expectAllToMatch (t , entries , expectedEntry )
123
101
}
124
102
125
- func expectEntriesToMatch [T any ](t * testing.T , entries []* T , expectedEntries ... * T ) {
126
- out:
127
- for _ , expectedEntry := range expectedEntries {
128
- for _ , entry := range entries {
129
- if reflect .DeepEqual (expectedEntry , entry ) {
130
- continue out
131
- }
132
- }
133
-
134
- var str strings.Builder
135
-
136
- str .WriteString ("output doesn't contain the expected entry\n " )
137
- str .WriteString ("captured:\n " )
138
- for _ , entry := range entries {
139
- entryJson , _ := json .Marshal (entry )
140
- str .WriteString (string (entryJson ))
141
- str .WriteString ("\n " )
142
- }
143
- expectedEntryJson , _ := json .Marshal (expectedEntry )
144
- str .WriteString ("expected:\n " )
145
- str .WriteString (string (expectedEntryJson ))
146
- t .Fatal (str .String ())
147
- }
148
- }
149
-
150
- // ExpectEntriesToMatch verifies that all the entries in expectedEntries are
151
- // matched by at least one entry in the output (Lines of independent JSON objects).
152
- func ExpectEntriesToMatch [T any ](t * testing.T , output string , normalize func (* T ), expectedEntries ... * T ) {
153
- entries := parseMultiJSONOutput (t , output , normalize )
154
- expectEntriesToMatch (t , entries , expectedEntries ... )
155
- }
156
-
157
103
// ExpectEntriesInArrayToMatch verifies that all the entries in expectedEntries are
158
104
// matched by at least one entry in the output (JSON array of JSON objects).
159
105
func ExpectEntriesInArrayToMatch [T any ](t * testing.T , output string , normalize func (* T ), expectedEntries ... * T ) {
160
106
entries := parseJSONArrayOutput (t , output , normalize )
161
- expectEntriesToMatch (t , entries , expectedEntries ... )
107
+ match . ExpectNormalizedEntriesToMatch (t , entries , expectedEntries ... )
162
108
}
163
109
164
110
// ExpectEntriesInMultipleArrayToMatch verifies that all the entries in expectedEntries are
165
111
// matched by at least one entry in the output (multiple JSON array of JSON objects separated by newlines).
166
112
func ExpectEntriesInMultipleArrayToMatch [T any ](t * testing.T , output string , normalize func (* T ), expectedEntries ... * T ) {
167
113
entries := parseMultipleJSONArrayOutput (t , output , normalize )
168
- expectEntriesToMatch (t , entries , expectedEntries ... )
114
+ match . ExpectNormalizedEntriesToMatch (t , entries , expectedEntries ... )
169
115
}
170
116
171
117
type CommonDataOption func (commonData * eventtypes.CommonData )
0 commit comments