/
fixture_image_simple_test.go
299 lines (260 loc) · 8 KB
/
fixture_image_simple_test.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
//go:build !windows
// +build !windows
package integration
import (
"context"
"fmt"
"io"
"io/ioutil"
"strings"
"testing"
"github.com/anchore/stereoscope"
"github.com/anchore/stereoscope/pkg/file"
"github.com/anchore/stereoscope/pkg/filetree"
"github.com/anchore/stereoscope/pkg/image"
"github.com/anchore/stereoscope/pkg/imagetest"
v1Types "github.com/google/go-containerregistry/pkg/v1/types"
"github.com/scylladb/go-set"
"github.com/stretchr/testify/require"
)
var simpleImageTestCases = []testCase{
{
name: "FromTarball",
source: "docker-archive",
imageMediaType: v1Types.DockerManifestSchema2,
layerMediaType: v1Types.DockerLayer,
tagCount: 1,
},
{
name: "FromDocker",
source: "docker",
imageMediaType: v1Types.DockerManifestSchema2,
layerMediaType: v1Types.DockerLayer,
// name:hash
// name:latest
tagCount: 2,
},
{
name: "FromPodman",
source: "podman",
imageMediaType: v1Types.DockerManifestSchema2,
layerMediaType: v1Types.DockerLayer,
tagCount: 2,
},
{
name: "FromOciTarball",
source: "oci-archive",
imageMediaType: v1Types.OCIManifestSchema1,
layerMediaType: v1Types.OCILayer,
tagCount: 0,
},
{
name: "FromOciDirectory",
source: "oci-dir",
imageMediaType: v1Types.OCIManifestSchema1,
layerMediaType: v1Types.OCILayer,
tagCount: 0,
},
}
type testCase struct {
name string
source string
imageMediaType v1Types.MediaType
layerMediaType v1Types.MediaType
tagCount int
}
func TestSimpleImage(t *testing.T) {
expectedSet := set.NewIntSet()
for _, src := range image.AllSources {
expectedSet.Add(int(src))
}
expectedSet.Remove(int(image.OciRegistrySource))
for _, c := range simpleImageTestCases {
t.Run(c.name, func(t *testing.T) {
i := imagetest.GetFixtureImage(t, c.source, "image-simple")
assertImageSimpleMetadata(t, i, c)
assertImageSimpleTrees(t, i)
assertImageSimpleSquashedTrees(t, i)
assertImageSimpleContents(t, i)
})
}
if len(simpleImageTestCases) < expectedSet.Size() {
t.Fatalf("probably missed a source during testing, double check that all image.sources are covered")
}
}
func BenchmarkSimpleImage_GetImage(b *testing.B) {
var err error
for _, c := range simpleImageTestCases {
if c.source == "docker" {
// skip benchmark testing against the docker daemon
continue
}
request := imagetest.PrepareFixtureImage(b, c.source, "image-simple")
b.Run(c.name, func(b *testing.B) {
var bi *image.Image
for i := 0; i < b.N; i++ {
bi, err = stereoscope.GetImage(context.TODO(), request)
b.Cleanup(func() {
require.NoError(b, bi.Cleanup())
})
if err != nil {
b.Fatal("could not get fixture image:", err)
}
}
})
}
}
func BenchmarkSimpleImage_FetchSquashedContents(b *testing.B) {
for _, c := range simpleImageTestCases {
if c.source == "docker" {
// skip benchmark testing against the docker daemon
continue
}
img := imagetest.GetFixtureImage(b, c.source, "image-simple")
paths := img.SquashedTree().AllFiles()
if len(paths) == 0 {
b.Fatalf("expected paths but found none")
}
b.Run(c.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, ref := range paths {
f, err := img.FileCatalog.Get(ref)
if err != nil {
b.Fatalf("unable to read: %+v", err)
}
_, err = ioutil.ReadAll(f.Contents())
}
}
})
}
}
func assertImageSimpleMetadata(t *testing.T, i *image.Image, expectedValues testCase) {
t.Helper()
t.Log("Asserting metadata...")
if i.Metadata.Size != 65 {
t.Errorf("unexpected image size: %d", i.Metadata.Size)
}
if i.Metadata.MediaType != expectedValues.imageMediaType {
t.Errorf("unexpected image media type: %+v", i.Metadata.MediaType)
}
if len(i.Metadata.Tags) != expectedValues.tagCount {
t.Errorf("unexpected number of tags: %d != %d : %+v", len(i.Metadata.Tags), expectedValues.tagCount, i.Metadata.Tags)
} else if expectedValues.tagCount > 0 {
if !strings.Contains(i.Metadata.Tags[0].String(), fmt.Sprintf("%s-image-simple:", imagetest.ImagePrefix)) {
t.Errorf("unexpected image tag: %+v", i.Metadata.Tags)
}
}
expected := []image.LayerMetadata{
{
Index: 0,
Size: 22,
MediaType: expectedValues.layerMediaType,
},
{
Index: 1,
Size: 16,
MediaType: expectedValues.layerMediaType,
},
{
Index: 2,
Size: 27,
MediaType: expectedValues.layerMediaType,
},
}
if len(expected) != len(i.Layers) {
t.Fatal("unexpected number of layers:", len(i.Layers))
}
for idx, l := range i.Layers {
if expected[idx].Size != l.Metadata.Size {
t.Errorf("mismatched layer 'Size' (layer %d): %+v", idx, l.Metadata.Size)
}
if expected[idx].MediaType != l.Metadata.MediaType {
t.Errorf("mismatched layer 'MediaType' (layer %d): %+v", idx, l.Metadata.MediaType)
}
if expected[idx].Index != l.Metadata.Index {
t.Errorf("mismatched layer 'Index' (layer %d): %+v", idx, l.Metadata.Index)
}
}
}
func assertImageSimpleSquashedTrees(t *testing.T, i *image.Image) {
t.Log("Asserting squashed trees...")
one := filetree.NewFileTree()
one.AddFile("/somefile-1.txt")
two := filetree.NewFileTree()
two.AddFile("/somefile-1.txt")
two.AddFile("/somefile-2.txt")
three := filetree.NewFileTree()
three.AddFile("/somefile-1.txt")
three.AddFile("/somefile-2.txt")
three.AddFile("/really/.wh..wh..opq")
three.AddFile("/really/nested/file-3.txt")
expectedTrees := map[uint]*filetree.FileTree{
0: one,
1: two,
2: three,
}
// there is a difference in behavior between docker 18 and 19 regarding opaque whiteout
// creation during docker build (which could lead to test inconsistencies depending where
// this test is run. However, this opaque whiteout is not important to theses tests, only
// the correctness of the layer representation and squashing ability.
ignorePaths := []file.Path{"/really/.wh..wh..opq"}
compareLayerSquashTrees(t, expectedTrees, i, ignorePaths)
squashed := filetree.NewFileTree()
squashed.AddFile("/somefile-1.txt")
squashed.AddFile("/somefile-2.txt")
squashed.AddFile("/really/nested/file-3.txt")
compareSquashTree(t, squashed, i)
}
func assertImageSimpleTrees(t *testing.T, i *image.Image) {
t.Log("Asserting trees...")
one := filetree.NewFileTree()
one.AddFile("/somefile-1.txt")
two := filetree.NewFileTree()
two.AddFile("/somefile-2.txt")
three := filetree.NewFileTree()
three.AddFile("/really/.wh..wh..opq")
three.AddFile("/really/nested/file-3.txt")
expectedTrees := map[uint]*filetree.FileTree{
0: one,
1: two,
2: three,
}
// there is a difference in behavior between docker 18 and 19 regarding opaque whiteout
// creation during docker build (which could lead to test inconsistencies depending where
// this test is run. However, this opaque whiteout is not important to theses tests, only
// the correctness of the layer representation and squashing ability.
ignorePaths := []file.Path{"/really/.wh..wh..opq"}
compareLayerTrees(t, expectedTrees, i, ignorePaths)
}
func assertImageSimpleContents(t *testing.T, i *image.Image) {
t.Log("Asserting contents...")
expectedContents := map[string]string{
"/somefile-1.txt": "this file has contents",
"/somefile-2.txt": "file-2 contents!",
"/really/nested/file-3.txt": "another file!\nwith lines...",
}
actualContents := make(map[string]io.Reader)
for path := range expectedContents {
reader, err := i.FileContentsFromSquash(file.Path(path))
if err != nil {
t.Fatal("unable to fetch multiple contents", err)
}
actualContents[path] = reader
}
if len(expectedContents) != len(actualContents) {
t.Fatalf("mismatched number of contents: %d!=%d", len(expectedContents), len(actualContents))
}
for path, actual := range actualContents {
expected, ok := expectedContents[path]
if !ok {
t.Errorf("extra path found: %+v", path)
}
b, err := ioutil.ReadAll(actual)
if err != nil {
t.Errorf("failed to read %+v : %+v", path, err)
}
if expected != string(b) {
t.Errorf("mismatched contents (%s)", path)
}
}
}