Debian Codegraph @

analyzer/recorder/recorder_test.go

raw
1 /*
2 Copyright The containerd Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package recorder
18
19 import (
20 "compress/gzip"
21 "context"
22 "encoding/json"
23 "fmt"
24 "io"
25 "os"
26 "path"
27 "testing"
28
29 "github.com/containerd/containerd/content"
30 "github.com/containerd/containerd/content/local"
31 "github.com/containerd/containerd/errdefs"
32 "github.com/containerd/stargz-snapshotter/recorder"
33 "github.com/containerd/stargz-snapshotter/util/testutil"
34 ocispec "github.com/opencontainers/image-spec/specs-go/v1"
35 "github.com/rs/xid"
36 )
37
38 var allowedPrefix = [4]string{"", "./", "/", "../"}
39
40 func TestNodeIndex(t *testing.T) {
41 type recordEntry struct {
42 name string
43 index int
44 }
45 tests := []struct {
46 name string
47 in [][]testutil.TarEntry
48 record []string
49 want []recordEntry
50 wantFail bool
51 }{
52 {
53 name: "single layer",
54 in: [][]testutil.TarEntry{
55 {
56 testutil.File("foo", "foo"),
57 testutil.File("bar", "bar"),
58 },
59 },
60 record: []string{"bar", "foo"},
61 want: []recordEntry{
62 {"bar", 0},
63 {"foo", 0},
64 },
65 },
66 {
67 name: "overlay",
68 in: [][]testutil.TarEntry{
69 {
70 testutil.File("foo", "foo"),
71 testutil.Dir("bar/"),
72 testutil.Dir("bar/barbar/"),
73 testutil.File("bar/barbar/bar2", "bar2"),
74 testutil.File("bar/barbar/foo2", "foo2"),
75 testutil.File("x", "x"),
76 testutil.File("y", "y"),
77 },
78 {
79 testutil.File("foo", "foo2"),
80 testutil.File("baz", "baz"),
81 testutil.Dir("bar/"),
82 testutil.Dir("bar/barbar/"),
83 testutil.File("bar/barbar/foo2", "foo2-upper"),
84 },
85 {
86 testutil.File("foo", "foo3"),
87 testutil.Dir("a/"),
88 testutil.File("a/aaaaaa", "a"),
89 testutil.File("y", "y"),
90 },
91 },
92 record: []string{
93 "baz",
94 "foo",
95 "bar/barbar/foo2",
96 "a/aaaaaa",
97 "bar/barbar/bar2",
98 "y"},
99 want: []recordEntry{
100 {"baz", 1},
101 {"foo", 2},
102 {"bar/barbar/foo2", 1},
103 {"a/aaaaaa", 2},
104 {"bar/barbar/bar2", 0},
105 {"y", 2},
106 },
107 },
108 {
109 name: "various files",
110 in: [][]testutil.TarEntry{
111 // All file path should be interpreted as the path relative to root.
112 {
113 testutil.File("foo", "foo"),
114 testutil.Dir("bar/"),
115 testutil.Symlink("bar/foosym", "foo"),
116 testutil.Dir("bar/barbar/"),
117 testutil.File("./bar/barbar/bar2", "bar2"),
118 testutil.File("../bar/barbar/foo2", "foo2"),
119 testutil.File("/x", "x"),
120 testutil.Chardev("chardev", 1, 10),
121 testutil.Fifo("bar/fifo1"),
122 },
123 {
124 testutil.File("./foo", "foo2"),
125 testutil.File("baz", "baz"),
126 testutil.Dir("bar/"),
127 testutil.Dir("./bar/barbar/"),
128 testutil.File("bar/barbar/bazlink", "baz"),
129 testutil.File("../bar/barbar/foo2", "foo2-upper"),
130 testutil.Chardev("chardev", 10, 100),
131 testutil.Chardev("./blockdev", 100, 1),
132 },
133 },
134 record: []string{
135 // All file path should be interpreted as the path relative to root.
136 "./bar/foosym",
137 "bar/barbar/bazlink",
138 "/bar/barbar/foo2",
139 "chardev",
140 "../blockdev",
141 "bar/fifo1",
142 "./bar/barbar/bar2",
143 },
144 want: []recordEntry{
145 {"bar/foosym", 0},
146 {"bar/barbar/bazlink", 1},
147 {"bar/barbar/foo2", 1},
148 {"chardev", 1},
149 {"blockdev", 1},
150 {"bar/fifo1", 0},
151 {"bar/barbar/bar2", 0},
152 },
153 },
154 {
155 name: "whiteout file",
156 in: [][]testutil.TarEntry{
157 {
158 testutil.Dir("bar/"),
159 testutil.File("bar/barfile", "bar"),
160 },
161 {
162 testutil.Dir("bar/"),
163 testutil.File(path.Join("bar", whiteoutPrefix+"barfile"), "bar"),
164 },
165 },
166 record: []string{"bar/barfile"},
167 want: []recordEntry{},
168 wantFail: true,
169 },
170 {
171 name: "whiteout dir",
172 in: [][]testutil.TarEntry{
173 {
174 testutil.Dir("bar/"),
175 testutil.File("bar/barfile", "bar"),
176 },
177 {
178 testutil.Dir("bar/"),
179 testutil.Dir(path.Join("bar", whiteoutOpaqueDir) + "/"),
180 },
181 },
182 record: []string{"bar/barfile"},
183 want: []recordEntry{},
184 wantFail: true,
185 },
186 {
187 name: "whiteout dir",
188 in: [][]testutil.TarEntry{
189 {
190 testutil.Dir("bar/"),
191 testutil.File("bar/barfile", "bar"),
192 },
193 {
194 testutil.Dir("bar/"),
195 testutil.Dir(path.Join("bar", whiteoutOpaqueDir) + "/"),
196 },
197 },
198 record: []string{"bar/barfile"},
199 want: []recordEntry{},
200 wantFail: true,
201 },
202 }
203
204 tempDir, err := os.MkdirTemp("", "test-recorder")
205 if err != nil {
206 t.Fatalf("failed to prepare content store dir: %v", err)
207 }
208 defer os.RemoveAll(tempDir)
209 cs, err := local.NewStore(tempDir)
210 if err != nil {
211 t.Fatalf("failed to prepare content store: %v", err)
212 }
213 compressWrappers := map[string]func(r io.Reader) io.Reader{
214 ocispec.MediaTypeImageLayer: func(r io.Reader) io.Reader { return r }, // nop (uncompressed)
215 ocispec.MediaTypeImageLayerGzip: gzipCompress, // gzip compression
216 }
217 ctx := context.Background()
218 for _, tt := range tests {
219 for _, prefix := range allowedPrefix {
220 prefix := prefix
221 for mediatype, cWrapper := range compressWrappers {
222 t.Run(tt.name+":"+mediatype+",prefix="+prefix, func(t *testing.T) {
223 var layers []ocispec.Descriptor
224 for _, es := range tt.in {
225 ref := fmt.Sprintf("recorder-test-%v", xid.New().String())
226 lw, err := content.OpenWriter(ctx, cs, content.WithRef(ref))
227 if err != nil {
228 t.Errorf("failed to open writer: %v", err)
229 return
230 }
231 tarR := testutil.BuildTar(es, testutil.WithPrefix(prefix))
232 if _, err := io.Copy(lw, cWrapper(tarR)); err != nil {
233 t.Errorf("failed to copy layer: %v", err)
234 return
235 }
236 if err := lw.Commit(ctx, 0, ""); err != nil && !errdefs.IsAlreadyExists(err) {
237 t.Errorf("failed to commit layer: %v", err)
238 return
239 }
240 info, err := cs.Info(ctx, lw.Digest())
241 if err != nil {
242 t.Errorf("failed to get layer info: %v", err)
243 return
244 }
245 // TODO: check compressed version as well
246 layers = append(layers, ocispec.Descriptor{
247 Digest: info.Digest,
248 Size: info.Size,
249 MediaType: mediatype,
250 })
251 }
252 ir, err := imageRecorderFromManifest(ctx, cs,
253 ocispec.Descriptor{}, ocispec.Manifest{Layers: layers})
254 if err != nil {
255 t.Errorf("failed to get recorder: %v", err)
256 return
257 }
258 defer ir.Close()
259 fail := false
260 for _, name := range tt.record {
261 if err := ir.Record(name); err != nil {
262 fail = true
263 t.Logf("failed to record: %q: %v", name, err)
264 break
265 }
266 }
267 if tt.wantFail != fail {
268 t.Errorf("unexpected record result: fail = %v; wantFail = %v", fail, tt.wantFail)
269 return
270 }
271 if tt.wantFail {
272 return // no need to check the record out
273 }
274 recordOut, err := ir.Commit(ctx)
275 if err != nil {
276 t.Errorf("failed to commit record: %v", err)
277 return
278 }
279
280 ra, err := cs.ReaderAt(ctx, ocispec.Descriptor{Digest: recordOut})
281 if err != nil {
282 t.Errorf("failed to get record out: %v", err)
283 return
284 }
285 defer ra.Close()
286 dec := json.NewDecoder(io.NewSectionReader(ra, 0, ra.Size()))
287 i := 0
288 for dec.More() {
289 var e recorder.Entry
290 if err := dec.Decode(&e); err != nil {
291 t.Errorf("failed to decord record: %v", err)
292 return
293 }
294 if len(tt.want) <= i {
295 t.Errorf("too many records: %d th but want %d", i, len(tt.want))
296 return
297 }
298 if e.Path != tt.want[i].name || *e.LayerIndex != tt.want[i].index {
299 t.Errorf("unexpected entry { name = %q, index = %d }; want { name = %q, index = %d }", e.Path, *e.LayerIndex, tt.want[i].name, tt.want[i].index)
300 return
301 }
302 i++
303 }
304 if i < len(tt.want) {
305 t.Errorf("record is too short: %d; but want %d", i, len(tt.want))
306 return
307 }
308 })
309 }
310 }
311 }
312 }
313
314 func gzipCompress(r io.Reader) io.Reader {
315 pr, pw := io.Pipe()
316 go func() {
317 gw := gzip.NewWriter(pw)
318 if _, err := io.Copy(gw, r); err != nil {
319 gw.Close()
320 pw.CloseWithError(err)
321 return
322 }
323 gw.Close()
324 pw.Close()
325 }()
326 return pr
327 }