-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_generic_parser.go
329 lines (268 loc) · 9.3 KB
/
test_generic_parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package pkgtest
import (
"fmt"
"io"
"os"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/nextlinux/sbom/sbom/artifact"
"github.com/nextlinux/sbom/sbom/linux"
"github.com/nextlinux/sbom/sbom/pkg"
"github.com/nextlinux/sbom/sbom/pkg/cataloger/generic"
"github.com/nextlinux/sbom/sbom/source"
"github.com/nextlinux/stereoscope/pkg/imagetest"
)
type locationComparer func(x, y source.Location) bool
type CatalogTester struct {
expectedPkgs []pkg.Package
expectedRelationships []artifact.Relationship
assertResultExpectations bool
expectedPathResponses []string // this is a minimum set, the resolver may return more that just this list
expectedContentQueries []string // this is a full set, any other queries are unexpected (and will fail the test)
ignoreUnfulfilledPathResponses map[string][]string
ignoreAnyUnfulfilledPaths []string
env *generic.Environment
reader source.LocationReadCloser
resolver source.FileResolver
wantErr require.ErrorAssertionFunc
compareOptions []cmp.Option
locationComparer locationComparer
}
func NewCatalogTester() *CatalogTester {
return &CatalogTester{
wantErr: require.NoError,
locationComparer: DefaultLocationComparer,
ignoreUnfulfilledPathResponses: map[string][]string{
"FilesByPath": {
// most catalogers search for a linux release, which will not be fulfilled in testing
"/etc/os-release",
"/usr/lib/os-release",
"/etc/system-release-cpe",
"/etc/redhat-release",
"/bin/busybox",
},
},
}
}
func DefaultLocationComparer(x, y source.Location) bool {
return cmp.Equal(x.Coordinates, y.Coordinates) && cmp.Equal(x.VirtualPath, y.VirtualPath)
}
func (p *CatalogTester) FromDirectory(t *testing.T, path string) *CatalogTester {
t.Helper()
s, err := source.NewFromDirectory(path)
require.NoError(t, err)
resolver, err := s.FileResolver(source.AllLayersScope)
require.NoError(t, err)
p.resolver = resolver
return p
}
func (p *CatalogTester) FromFile(t *testing.T, path string) *CatalogTester {
t.Helper()
fixture, err := os.Open(path)
require.NoError(t, err)
p.reader = source.LocationReadCloser{
Location: source.NewLocation(fixture.Name()),
ReadCloser: fixture,
}
return p
}
func (p *CatalogTester) FromString(location, data string) *CatalogTester {
p.reader = source.LocationReadCloser{
Location: source.NewLocation(location),
ReadCloser: io.NopCloser(strings.NewReader(data)),
}
return p
}
func (p *CatalogTester) WithLinuxRelease(r linux.Release) *CatalogTester {
if p.env == nil {
p.env = &generic.Environment{}
}
p.env.LinuxRelease = &r
return p
}
func (p *CatalogTester) WithEnv(env *generic.Environment) *CatalogTester {
p.env = env
return p
}
func (p *CatalogTester) WithError() *CatalogTester {
p.assertResultExpectations = true
p.wantErr = require.Error
return p
}
func (p *CatalogTester) WithErrorAssertion(a require.ErrorAssertionFunc) *CatalogTester {
p.wantErr = a
return p
}
func (p *CatalogTester) WithResolver(r source.FileResolver) *CatalogTester {
p.resolver = r
return p
}
func (p *CatalogTester) WithImageResolver(t *testing.T, fixtureName string) *CatalogTester {
t.Helper()
img := imagetest.GetFixtureImage(t, "docker-archive", fixtureName)
s, err := source.NewFromImage(img, fixtureName)
require.NoError(t, err)
r, err := s.FileResolver(source.SquashedScope)
require.NoError(t, err)
p.resolver = r
return p
}
func (p *CatalogTester) IgnoreLocationLayer() *CatalogTester {
p.locationComparer = func(x, y source.Location) bool {
return cmp.Equal(x.Coordinates.RealPath, y.Coordinates.RealPath) && cmp.Equal(x.VirtualPath, y.VirtualPath)
}
return p
}
func (p *CatalogTester) IgnorePackageFields(fields ...string) *CatalogTester {
p.compareOptions = append(p.compareOptions, cmpopts.IgnoreFields(pkg.Package{}, fields...))
return p
}
func (p *CatalogTester) WithCompareOptions(opts ...cmp.Option) *CatalogTester {
p.compareOptions = append(p.compareOptions, opts...)
return p
}
func (p *CatalogTester) Expects(pkgs []pkg.Package, relationships []artifact.Relationship) *CatalogTester {
p.assertResultExpectations = true
p.expectedPkgs = pkgs
p.expectedRelationships = relationships
return p
}
func (p *CatalogTester) ExpectsResolverPathResponses(locations []string) *CatalogTester {
p.expectedPathResponses = locations
return p
}
func (p *CatalogTester) ExpectsResolverContentQueries(locations []string) *CatalogTester {
p.expectedContentQueries = locations
return p
}
func (p *CatalogTester) IgnoreUnfulfilledPathResponses(paths ...string) *CatalogTester {
p.ignoreAnyUnfulfilledPaths = append(p.ignoreAnyUnfulfilledPaths, paths...)
return p
}
func (p *CatalogTester) TestParser(t *testing.T, parser generic.Parser) {
t.Helper()
pkgs, relationships, err := parser(p.resolver, p.env, p.reader)
p.wantErr(t, err)
p.assertPkgs(t, pkgs, relationships)
}
func (p *CatalogTester) TestCataloger(t *testing.T, cataloger pkg.Cataloger) {
t.Helper()
resolver := NewObservingResolver(p.resolver)
pkgs, relationships, err := cataloger.Catalog(resolver)
// this is a minimum set, the resolver may return more that just this list
for _, path := range p.expectedPathResponses {
assert.Truef(t, resolver.ObservedPathResponses(path), "expected path query for %q was not observed", path)
}
// this is a full set, any other queries are unexpected (and will fail the test)
if len(p.expectedContentQueries) > 0 {
assert.ElementsMatchf(t, p.expectedContentQueries, resolver.AllContentQueries(), "unexpected content queries observed: diff %s", cmp.Diff(p.expectedContentQueries, resolver.AllContentQueries()))
}
if p.assertResultExpectations {
p.wantErr(t, err)
p.assertPkgs(t, pkgs, relationships)
} else {
resolver.PruneUnfulfilledPathResponses(p.ignoreUnfulfilledPathResponses, p.ignoreAnyUnfulfilledPaths...)
// if we aren't testing the results, we should focus on what was searched for (for glob-centric tests)
assert.Falsef(t, resolver.HasUnfulfilledPathRequests(), "unfulfilled path requests: \n%v", resolver.PrettyUnfulfilledPathRequests())
}
}
func (p *CatalogTester) assertPkgs(t *testing.T, pkgs []pkg.Package, relationships []artifact.Relationship) {
t.Helper()
p.compareOptions = append(p.compareOptions,
cmpopts.IgnoreFields(pkg.Package{}, "id"), // note: ID is not deterministic for test purposes
cmpopts.SortSlices(pkg.Less),
cmp.Comparer(
func(x, y source.LocationSet) bool {
xs := x.ToSlice()
ys := y.ToSlice()
if len(xs) != len(ys) {
return false
}
for i, xe := range xs {
ye := ys[i]
if !p.locationComparer(xe, ye) {
return false
}
}
return true
},
),
)
{
var r diffReporter
var opts []cmp.Option
opts = append(opts, p.compareOptions...)
opts = append(opts, cmp.Reporter(&r))
if diff := cmp.Diff(p.expectedPkgs, pkgs, opts...); diff != "" {
t.Log("Specific Differences:\n" + r.String())
t.Errorf("unexpected packages from parsing (-expected +actual)\n%s", diff)
}
}
{
var r diffReporter
var opts []cmp.Option
opts = append(opts, p.compareOptions...)
opts = append(opts, cmp.Reporter(&r))
if diff := cmp.Diff(p.expectedRelationships, relationships, opts...); diff != "" {
t.Log("Specific Differences:\n" + r.String())
t.Errorf("unexpected relationships from parsing (-expected +actual)\n%s", diff)
}
}
}
func TestFileParser(t *testing.T, fixturePath string, parser generic.Parser, expectedPkgs []pkg.Package, expectedRelationships []artifact.Relationship) {
t.Helper()
NewCatalogTester().FromFile(t, fixturePath).Expects(expectedPkgs, expectedRelationships).TestParser(t, parser)
}
func TestFileParserWithEnv(t *testing.T, fixturePath string, parser generic.Parser, env *generic.Environment, expectedPkgs []pkg.Package, expectedRelationships []artifact.Relationship) {
t.Helper()
NewCatalogTester().FromFile(t, fixturePath).WithEnv(env).Expects(expectedPkgs, expectedRelationships).TestParser(t, parser)
}
func AssertPackagesEqual(t *testing.T, a, b pkg.Package) {
t.Helper()
opts := []cmp.Option{
cmpopts.IgnoreFields(pkg.Package{}, "id"), // note: ID is not deterministic for test purposes
cmp.Comparer(
func(x, y source.LocationSet) bool {
xs := x.ToSlice()
ys := y.ToSlice()
if len(xs) != len(ys) {
return false
}
for i, xe := range xs {
ye := ys[i]
if !DefaultLocationComparer(xe, ye) {
return false
}
}
return true
},
),
}
if diff := cmp.Diff(a, b, opts...); diff != "" {
t.Errorf("unexpected packages from parsing (-expected +actual)\n%s", diff)
}
}
// diffReporter is a simple custom reporter that only records differences detected during comparison.
type diffReporter struct {
path cmp.Path
diffs []string
}
func (r *diffReporter) PushStep(ps cmp.PathStep) {
r.path = append(r.path, ps)
}
func (r *diffReporter) Report(rs cmp.Result) {
if !rs.Equal() {
vx, vy := r.path.Last().Values()
r.diffs = append(r.diffs, fmt.Sprintf("%#v:\n\t-: %+v\n\t+: %+v\n", r.path, vx, vy))
}
}
func (r *diffReporter) PopStep() {
r.path = r.path[:len(r.path)-1]
}
func (r *diffReporter) String() string {
return strings.Join(r.diffs, "\n")
}