Skip to content

Commit 50fd6c8

Browse files
committed
Moved some logic of integration/helpers.go to pkg/testing/match.go
Signed-off-by: pawarpranav83 <[email protected]>
1 parent f6779d8 commit 50fd6c8

24 files changed

+153
-86
lines changed

integration/helpers.go

+4-58
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"encoding/json"
2121
"fmt"
2222
"os/exec"
23-
"reflect"
2423
"strings"
2524
"testing"
2625

@@ -30,6 +29,7 @@ import (
3029

3130
containercollection "github.com/inspektor-gadget/inspektor-gadget/pkg/container-collection"
3231
"github.com/inspektor-gadget/inspektor-gadget/pkg/container-utils/testutils"
32+
"github.com/inspektor-gadget/inspektor-gadget/pkg/testing/match"
3333
eventtypes "github.com/inspektor-gadget/inspektor-gadget/pkg/types"
3434
)
3535

@@ -38,28 +38,6 @@ var cmpIgnoreUnexported = cmpopts.IgnoreUnexported(
3838
containercollection.K8sMetadata{},
3939
)
4040

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-
6341
func parseJSONArrayOutput[T any](t *testing.T, output string, normalize func(*T)) []*T {
6442
entries := []*T{}
6543

@@ -104,7 +82,7 @@ func expectAllToMatch[T any](t *testing.T, entries []*T, expectedEntry *T) {
10482
// ExpectAllToMatch verifies that the expectedEntry is matched by all the
10583
// entries in the output (Lines of independent JSON objects).
10684
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)
10886
expectAllToMatch(t, entries, expectedEntry)
10987
}
11088

@@ -122,50 +100,18 @@ func ExpectAllInMultipleArrayToMatch[T any](t *testing.T, output string, normali
122100
expectAllToMatch(t, entries, expectedEntry)
123101
}
124102

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-
157103
// ExpectEntriesInArrayToMatch verifies that all the entries in expectedEntries are
158104
// matched by at least one entry in the output (JSON array of JSON objects).
159105
func ExpectEntriesInArrayToMatch[T any](t *testing.T, output string, normalize func(*T), expectedEntries ...*T) {
160106
entries := parseJSONArrayOutput(t, output, normalize)
161-
expectEntriesToMatch(t, entries, expectedEntries...)
107+
match.ExpectNormalizedEntriesToMatch(t, entries, expectedEntries...)
162108
}
163109

164110
// ExpectEntriesInMultipleArrayToMatch verifies that all the entries in expectedEntries are
165111
// matched by at least one entry in the output (multiple JSON array of JSON objects separated by newlines).
166112
func ExpectEntriesInMultipleArrayToMatch[T any](t *testing.T, output string, normalize func(*T), expectedEntries ...*T) {
167113
entries := parseMultipleJSONArrayOutput(t, output, normalize)
168-
expectEntriesToMatch(t, entries, expectedEntries...)
114+
match.ExpectNormalizedEntriesToMatch(t, entries, expectedEntries...)
169115
}
170116

171117
type CommonDataOption func(commonData *eventtypes.CommonData)

integration/ig/non-k8s/list_containers_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
. "github.com/inspektor-gadget/inspektor-gadget/integration"
2222
containercollection "github.com/inspektor-gadget/inspektor-gadget/pkg/container-collection"
2323
"github.com/inspektor-gadget/inspektor-gadget/pkg/testing/containers"
24+
"github.com/inspektor-gadget/inspektor-gadget/pkg/testing/match"
2425
"github.com/inspektor-gadget/inspektor-gadget/pkg/types"
2526
)
2627

@@ -127,7 +128,7 @@ func TestWatchContainers(t *testing.T) {
127128
e.Container.Runtime.ContainerImageDigest = ""
128129
}
129130

130-
ExpectEntriesToMatch(t, output, normalize, expectedEvents...)
131+
match.ExpectEntriesToMatch(t, output, normalize, expectedEvents...)
131132
},
132133
}
133134

integration/ig/non-k8s/trace_dns_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
. "github.com/inspektor-gadget/inspektor-gadget/integration"
2323
dnsTypes "github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/trace/dns/types"
2424
"github.com/inspektor-gadget/inspektor-gadget/pkg/testing/containers"
25+
"github.com/inspektor-gadget/inspektor-gadget/pkg/testing/match"
2526
eventtypes "github.com/inspektor-gadget/inspektor-gadget/pkg/types"
2627
)
2728

@@ -145,7 +146,7 @@ func newTraceDnsSteps(cn string, dnsServerArgs string) []TestStep {
145146
}
146147
}
147148

148-
ExpectEntriesToMatch(t, output, normalize, expectedEntries...)
149+
match.ExpectEntriesToMatch(t, output, normalize, expectedEntries...)
149150
},
150151
}
151152

integration/ig/non-k8s/trace_network_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
. "github.com/inspektor-gadget/inspektor-gadget/integration"
2222
networkTypes "github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/trace/network/types"
2323
"github.com/inspektor-gadget/inspektor-gadget/pkg/testing/containers"
24+
"github.com/inspektor-gadget/inspektor-gadget/pkg/testing/match"
2425
eventtypes "github.com/inspektor-gadget/inspektor-gadget/pkg/types"
2526
)
2627

@@ -93,7 +94,7 @@ func TestTraceNetwork(t *testing.T) {
9394
e.Runtime.ContainerImageDigest = ""
9495
}
9596

96-
ExpectEntriesToMatch(t, output, normalize, expectedEntries...)
97+
match.ExpectEntriesToMatch(t, output, normalize, expectedEntries...)
9798
},
9899
}
99100

integration/k8s/advise_networkpolicy_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"testing"
2020

2121
networkTypes "github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/trace/network/types"
22+
"github.com/inspektor-gadget/inspektor-gadget/pkg/testing/match"
2223
eventtypes "github.com/inspektor-gadget/inspektor-gadget/pkg/types"
2324

2425
. "github.com/inspektor-gadget/inspektor-gadget/integration"
@@ -97,7 +98,7 @@ func TestAdviseNetworkpolicy(t *testing.T) {
9798
e.Runtime.ContainerImageDigest = ""
9899
}
99100

100-
ExpectEntriesToMatch(t, output, normalize, expectedEntry)
101+
match.ExpectEntriesToMatch(t, output, normalize, expectedEntry)
101102
},
102103
},
103104
&Command{
@@ -151,7 +152,7 @@ func TestAdviseNetworkpolicy(t *testing.T) {
151152
e.Runtime.ContainerImageDigest = ""
152153
}
153154

154-
ExpectEntriesToMatch(t, output, normalize, expectedEntry)
155+
match.ExpectEntriesToMatch(t, output, normalize, expectedEntry)
155156
},
156157
},
157158
&Command{

integration/k8s/audit_seccomp_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"testing"
2020

2121
seccompauditTypes "github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/audit/seccomp/types"
22+
"github.com/inspektor-gadget/inspektor-gadget/pkg/testing/match"
2223

2324
. "github.com/inspektor-gadget/inspektor-gadget/integration"
2425
)
@@ -203,7 +204,7 @@ EOF
203204
e.Runtime.ContainerImageDigest = ""
204205
}
205206

206-
ExpectEntriesToMatch(t, output, normalize, expectedEntry)
207+
match.ExpectEntriesToMatch(t, output, normalize, expectedEntry)
207208
},
208209
},
209210
&Command{

integration/k8s/enrichment_pod_label_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
. "github.com/inspektor-gadget/inspektor-gadget/integration"
2222
containercollection "github.com/inspektor-gadget/inspektor-gadget/pkg/container-collection"
23+
"github.com/inspektor-gadget/inspektor-gadget/pkg/testing/match"
2324
"github.com/inspektor-gadget/inspektor-gadget/pkg/types"
2425
)
2526

@@ -196,7 +197,7 @@ func TestEnrichmentPodLabelNewPod(t *testing.T) {
196197
// Watching containers is a command that needs to be started before
197198
// the container is created, so we can't filter by container name
198199
// neither use ExpectAllInArrayToMatch here.
199-
ExpectEntriesToMatch(t, output, normalize, expectedEvent)
200+
match.ExpectEntriesToMatch(t, output, normalize, expectedEvent)
200201
},
201202
}
202203

integration/k8s/list_containers_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
. "github.com/inspektor-gadget/inspektor-gadget/integration"
2222
containercollection "github.com/inspektor-gadget/inspektor-gadget/pkg/container-collection"
23+
"github.com/inspektor-gadget/inspektor-gadget/pkg/testing/match"
2324
"github.com/inspektor-gadget/inspektor-gadget/pkg/types"
2425
)
2526

@@ -234,7 +235,7 @@ func TestWatchCreatedContainers(t *testing.T) {
234235
// Watching containers is a command that needs to be started before
235236
// the container is created, so we can't filter by container name
236237
// neither use ExpectAllInArrayToMatch here.
237-
ExpectEntriesToMatch(t, output, normalize, expectedEvent)
238+
match.ExpectEntriesToMatch(t, output, normalize, expectedEvent)
238239
},
239240
}
240241

@@ -329,7 +330,7 @@ func TestWatchDeletedContainers(t *testing.T) {
329330
// Watching containers is a command that needs to be started before
330331
// the container is created, so we can't filter by container name
331332
// neither use ExpectAllInArrayToMatch here.
332-
ExpectEntriesToMatch(t, output, normalize, expectedEvent)
333+
match.ExpectEntriesToMatch(t, output, normalize, expectedEvent)
333334
},
334335
}
335336

@@ -427,7 +428,7 @@ func TestPodWithSecurityContext(t *testing.T) {
427428
// Watching containers is a command that needs to be started before
428429
// the container is created, so we can't filter by container name
429430
// neither use ExpectAllInArrayToMatch here.
430-
ExpectEntriesToMatch(t, output, normalize, expectedEvent)
431+
match.ExpectEntriesToMatch(t, output, normalize, expectedEvent)
431432
},
432433
}
433434

integration/k8s/profile_blockio_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
. "github.com/inspektor-gadget/inspektor-gadget/integration"
2222
bioprofileTypes "github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/profile/block-io/types"
2323
"github.com/inspektor-gadget/inspektor-gadget/pkg/histogram"
24+
"github.com/inspektor-gadget/inspektor-gadget/pkg/testing/match"
2425
)
2526

2627
func TestProfileBlockIO(t *testing.T) {
@@ -41,7 +42,7 @@ func TestProfileBlockIO(t *testing.T) {
4142
e.Intervals = nil
4243
}
4344

44-
ExpectEntriesToMatch(t, output, normalize, expectedEntry)
45+
match.ExpectEntriesToMatch(t, output, normalize, expectedEntry)
4546
},
4647
}
4748

integration/k8s/profile_cpu_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
. "github.com/inspektor-gadget/inspektor-gadget/integration"
2222
cpuprofileTypes "github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/profile/cpu/types"
23+
"github.com/inspektor-gadget/inspektor-gadget/pkg/testing/match"
2324
)
2425

2526
func TestProfileCpu(t *testing.T) {
@@ -55,7 +56,7 @@ func TestProfileCpu(t *testing.T) {
5556
normalizeCommonData(&e.CommonData, ns)
5657
}
5758

58-
ExpectEntriesToMatch(t, output, normalize, expectedEntry)
59+
match.ExpectEntriesToMatch(t, output, normalize, expectedEntry)
5960
},
6061
}
6162

integration/k8s/profile_tcprtt_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
. "github.com/inspektor-gadget/inspektor-gadget/integration"
2222
tcprttProfileTypes "github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/profile/tcprtt/types"
2323
"github.com/inspektor-gadget/inspektor-gadget/pkg/histogram"
24+
"github.com/inspektor-gadget/inspektor-gadget/pkg/testing/match"
2425
)
2526

2627
func TestProfileTCPRTT(t *testing.T) {
@@ -237,7 +238,7 @@ func newProfileTCPRTTCmd(flags string, useTimeout bool, node string, unit histog
237238
}
238239
}
239240

240-
ExpectEntriesToMatch(t, output, normalize, expectedEntry)
241+
match.ExpectEntriesToMatch(t, output, normalize, expectedEntry)
241242
},
242243
}
243244
}

integration/k8s/trace_bind_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
. "github.com/inspektor-gadget/inspektor-gadget/integration"
2222
tracebindTypes "github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/trace/bind/types"
23+
"github.com/inspektor-gadget/inspektor-gadget/pkg/testing/match"
2324
)
2425

2526
func TestTraceBind(t *testing.T) {
@@ -66,7 +67,7 @@ func TestTraceBind(t *testing.T) {
6667

6768
// Since we aren't doing any filtering in traceBindCmd we avoid using ExpectAllToMatch
6869
// Issue: https://github.com/inspektor-gadget/inspektor-gadget/issues/644
69-
ExpectEntriesToMatch(t, output, normalize, expectedEntry)
70+
match.ExpectEntriesToMatch(t, output, normalize, expectedEntry)
7071
},
7172
}
7273

integration/k8s/trace_capabilities_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
. "github.com/inspektor-gadget/inspektor-gadget/integration"
2222
capabilitiesTypes "github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/trace/capabilities/types"
23+
"github.com/inspektor-gadget/inspektor-gadget/pkg/testing/match"
2324
)
2425

2526
func TestTraceCapabilities(t *testing.T) {
@@ -85,7 +86,7 @@ func TestTraceCapabilities(t *testing.T) {
8586
normalizeCommonData(&e.CommonData, ns)
8687
}
8788

88-
ExpectEntriesToMatch(t, output, normalize, expectedEntry)
89+
match.ExpectEntriesToMatch(t, output, normalize, expectedEntry)
8990
},
9091
}
9192

integration/k8s/trace_dns_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
. "github.com/inspektor-gadget/inspektor-gadget/integration"
2323
dnsTypes "github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/trace/dns/types"
24+
"github.com/inspektor-gadget/inspektor-gadget/pkg/testing/match"
2425
eventtypes "github.com/inspektor-gadget/inspektor-gadget/pkg/types"
2526
)
2627

@@ -203,7 +204,7 @@ func newTraceDnsCmd(t *testing.T, ns string, dnsServerArgs string) *Command {
203204
normalizeCommonData(&e.CommonData, ns)
204205
}
205206

206-
ExpectEntriesToMatch(t, output, normalize, expectedEntries...)
207+
match.ExpectEntriesToMatch(t, output, normalize, expectedEntries...)
207208
},
208209
}
209210

@@ -317,7 +318,7 @@ func TestTraceDnsHost(t *testing.T) {
317318
e.Runtime.ContainerImageDigest = ""
318319
}
319320

320-
ExpectEntriesToMatch(t, output, normalize, expectedEntries...)
321+
match.ExpectEntriesToMatch(t, output, normalize, expectedEntries...)
321322
},
322323
}
323324

integration/k8s/trace_exec_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
. "github.com/inspektor-gadget/inspektor-gadget/integration"
2323
traceexecTypes "github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/trace/exec/types"
24+
"github.com/inspektor-gadget/inspektor-gadget/pkg/testing/match"
2425
eventtypes "github.com/inspektor-gadget/inspektor-gadget/pkg/types"
2526
)
2627

@@ -103,7 +104,7 @@ func TestTraceExec(t *testing.T) {
103104
normalizeCommonData(&e.CommonData, ns)
104105
}
105106

106-
ExpectEntriesToMatch(t, output, normalize, expectedEntries...)
107+
match.ExpectEntriesToMatch(t, output, normalize, expectedEntries...)
107108
},
108109
}
109110

@@ -164,7 +165,7 @@ func TestTraceExecHost(t *testing.T) {
164165
e.MountNsID = 0
165166
}
166167

167-
ExpectEntriesToMatch(t, output, normalize, expectedEntries...)
168+
match.ExpectEntriesToMatch(t, output, normalize, expectedEntries...)
168169
},
169170
}
170171

0 commit comments

Comments
 (0)