| 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 testutil |
| 18 |
|
| 19 |
import ( |
| 20 |
"compress/gzip" |
| 21 |
"fmt" |
| 22 |
"io" |
| 23 |
"math/rand" |
| 24 |
"os" |
| 25 |
"path" |
| 26 |
"path/filepath" |
| 27 |
"reflect" |
| 28 |
"strings" |
| 29 |
"testing" |
| 30 |
"time" |
| 31 |
|
| 32 |
"github.com/containerd/stargz-snapshotter/estargz" |
| 33 |
"github.com/containerd/stargz-snapshotter/metadata" |
| 34 |
tutil "github.com/containerd/stargz-snapshotter/util/testutil" |
| 35 |
"github.com/hashicorp/go-multierror" |
| 36 |
"github.com/klauspost/compress/zstd" |
| 37 |
digest "github.com/opencontainers/go-digest" |
| 38 |
) |
| 39 |
|
| 40 |
func init() { |
| 41 |
rand.Seed(time.Now().UnixNano()) |
| 42 |
} |
| 43 |
|
| 44 |
var allowedPrefix = [4]string{"", "./", "/", "../"} |
| 45 |
|
| 46 |
var srcCompressions = map[string]tutil.CompressionFactory{ |
| 47 |
"zstd-fastest": tutil.ZstdCompressionWithLevel(zstd.SpeedFastest), |
| 48 |
"zstd-default": tutil.ZstdCompressionWithLevel(zstd.SpeedDefault), |
| 49 |
"zstd-bettercompression": tutil.ZstdCompressionWithLevel(zstd.SpeedBetterCompression), |
| 50 |
"gzip-no-compression": tutil.GzipCompressionWithLevel(gzip.NoCompression), |
| 51 |
"gzip-bestspeed": tutil.GzipCompressionWithLevel(gzip.BestSpeed), |
| 52 |
"gzip-bestcompression": tutil.GzipCompressionWithLevel(gzip.BestCompression), |
| 53 |
"gzip-defaultcompression": tutil.GzipCompressionWithLevel(gzip.DefaultCompression), |
| 54 |
"gzip-huffmanonly": tutil.GzipCompressionWithLevel(gzip.HuffmanOnly), |
| 55 |
"externaltoc-gzip-bestspeed": tutil.ExternalTOCGzipCompressionWithLevel(gzip.BestSpeed), |
| 56 |
"externaltoc-gzip-bestcompression": tutil.ExternalTOCGzipCompressionWithLevel(gzip.BestCompression), |
| 57 |
"externaltoc-gzip-defaultcompression": tutil.ExternalTOCGzipCompressionWithLevel(gzip.DefaultCompression), |
| 58 |
"externaltoc-gzip-huffmanonly": tutil.ExternalTOCGzipCompressionWithLevel(gzip.HuffmanOnly), |
| 59 |
} |
| 60 |
|
| 61 |
type ReaderFactory func(sr *io.SectionReader, opts ...metadata.Option) (r TestableReader, err error) |
| 62 |
|
| 63 |
type TestableReader interface { |
| 64 |
metadata.Reader |
| 65 |
NumOfNodes() (i int, _ error) |
| 66 |
} |
| 67 |
|
| 68 |
// TestReader tests Reader returns correct file metadata. |
| 69 |
func TestReader(t *testing.T, factory ReaderFactory) { |
| 70 |
sampleTime := time.Now().Truncate(time.Second) |
| 71 |
sampleText := "qwer" + "tyui" + "opas" + "dfgh" + "jk" |
| 72 |
data64KB := string(tutil.RandomBytes(t, 64000)) |
| 73 |
tests := []struct { |
| 74 |
name string |
| 75 |
chunkSize int |
| 76 |
minChunkSize int |
| 77 |
in []tutil.TarEntry |
| 78 |
want []check |
| 79 |
}{ |
| 80 |
{ |
| 81 |
name: "empty", |
| 82 |
in: []tutil.TarEntry{}, |
| 83 |
want: []check{ |
| 84 |
numOfNodes(2), // root dir + prefetch landmark |
| 85 |
}, |
| 86 |
}, |
| 87 |
{ |
| 88 |
name: "files", |
| 89 |
in: []tutil.TarEntry{ |
| 90 |
tutil.File("foo", "foofoo", tutil.WithFileMode(0644|os.ModeSetuid)), |
| 91 |
tutil.Dir("bar/"), |
| 92 |
tutil.File("bar/baz.txt", "bazbazbaz", tutil.WithFileOwner(1000, 1000)), |
| 93 |
tutil.File("xxx.txt", "xxxxx", tutil.WithFileModTime(sampleTime)), |
| 94 |
tutil.File("y.txt", "", tutil.WithFileXattrs(map[string]string{"testkey": "testval"})), |
| 95 |
}, |
| 96 |
want: []check{ |
| 97 |
numOfNodes(7), // root dir + prefetch landmark + 1 dir + 4 files |
| 98 |
hasFile("foo", "foofoo", 6), |
| 99 |
hasMode("foo", 0644|os.ModeSetuid), |
| 100 |
hasFile("bar/baz.txt", "bazbazbaz", 9), |
| 101 |
hasOwner("bar/baz.txt", 1000, 1000), |
| 102 |
hasFile("xxx.txt", "xxxxx", 5), |
| 103 |
hasModTime("xxx.txt", sampleTime), |
| 104 |
hasFile("y.txt", "", 0), |
| 105 |
hasXattrs("y.txt", map[string]string{"testkey": "testval"}), |
| 106 |
}, |
| 107 |
}, |
| 108 |
{ |
| 109 |
name: "dirs", |
| 110 |
in: []tutil.TarEntry{ |
| 111 |
tutil.Dir("foo/", tutil.WithDirMode(os.ModeDir|0600|os.ModeSticky)), |
| 112 |
tutil.Dir("foo/bar/", tutil.WithDirOwner(1000, 1000)), |
| 113 |
tutil.File("foo/bar/baz.txt", "testtest"), |
| 114 |
tutil.File("foo/bar/xxxx", "x"), |
| 115 |
tutil.File("foo/bar/yyy", "yyy"), |
| 116 |
tutil.Dir("foo/a/", tutil.WithDirModTime(sampleTime)), |
| 117 |
tutil.Dir("foo/a/1/", tutil.WithDirXattrs(map[string]string{"testkey": "testval"})), |
| 118 |
tutil.File("foo/a/1/2", "1111111111"), |
| 119 |
}, |
| 120 |
want: []check{ |
| 121 |
numOfNodes(10), // root dir + prefetch landmark + 4 dirs + 4 files |
| 122 |
hasDirChildren("foo", "bar", "a"), |
| 123 |
hasDirChildren("foo/bar", "baz.txt", "xxxx", "yyy"), |
| 124 |
hasDirChildren("foo/a", "1"), |
| 125 |
hasDirChildren("foo/a/1", "2"), |
| 126 |
hasMode("foo", os.ModeDir|0600|os.ModeSticky), |
| 127 |
hasOwner("foo/bar", 1000, 1000), |
| 128 |
hasModTime("foo/a", sampleTime), |
| 129 |
hasXattrs("foo/a/1", map[string]string{"testkey": "testval"}), |
| 130 |
hasFile("foo/bar/baz.txt", "testtest", 8), |
| 131 |
hasFile("foo/bar/xxxx", "x", 1), |
| 132 |
hasFile("foo/bar/yyy", "yyy", 3), |
| 133 |
hasFile("foo/a/1/2", "1111111111", 10), |
| 134 |
}, |
| 135 |
}, |
| 136 |
{ |
| 137 |
name: "hardlinks", |
| 138 |
in: []tutil.TarEntry{ |
| 139 |
tutil.File("foo", "foofoo", tutil.WithFileOwner(1000, 1000)), |
| 140 |
tutil.Dir("bar/"), |
| 141 |
tutil.Link("bar/foolink", "foo"), |
| 142 |
tutil.Link("bar/foolink2", "bar/foolink"), |
| 143 |
tutil.Dir("bar/1/"), |
| 144 |
tutil.File("bar/1/baz.txt", "testtest"), |
| 145 |
tutil.Link("barlink", "bar/1/baz.txt"), |
| 146 |
tutil.Symlink("foosym", "bar/foolink2"), |
| 147 |
}, |
| 148 |
want: []check{ |
| 149 |
numOfNodes(7), // root dir + prefetch landmark + 2 dirs + 1 flie(linked) + 1 file(linked) + 1 symlink |
| 150 |
hasFile("foo", "foofoo", 6), |
| 151 |
hasOwner("foo", 1000, 1000), |
| 152 |
hasFile("bar/foolink", "foofoo", 6), |
| 153 |
hasOwner("bar/foolink", 1000, 1000), |
| 154 |
hasFile("bar/foolink2", "foofoo", 6), |
| 155 |
hasOwner("bar/foolink2", 1000, 1000), |
| 156 |
hasFile("bar/1/baz.txt", "testtest", 8), |
| 157 |
hasFile("barlink", "testtest", 8), |
| 158 |
hasDirChildren("bar", "foolink", "foolink2", "1"), |
| 159 |
hasDirChildren("bar/1", "baz.txt"), |
| 160 |
sameNodes("foo", "bar/foolink", "bar/foolink2"), |
| 161 |
sameNodes("bar/1/baz.txt", "barlink"), |
| 162 |
linkName("foosym", "bar/foolink2"), |
| 163 |
hasNumLink("foo", 3), // parent dir + 2 links |
| 164 |
hasNumLink("barlink", 2), // parent dir + 1 link |
| 165 |
hasNumLink("bar", 3), // parent + "." + child's ".." |
| 166 |
}, |
| 167 |
}, |
| 168 |
{ |
| 169 |
name: "various files", |
| 170 |
in: []tutil.TarEntry{ |
| 171 |
tutil.Dir("bar/"), |
| 172 |
tutil.File("bar/../bar///////////////////foo", ""), |
| 173 |
tutil.Chardev("bar/cdev", 10, 11), |
| 174 |
tutil.Blockdev("bar/bdev", 100, 101), |
| 175 |
tutil.Fifo("bar/fifo"), |
| 176 |
}, |
| 177 |
want: []check{ |
| 178 |
numOfNodes(7), // root dir + prefetch landmark + 1 file + 1 dir + 1 cdev + 1 bdev + 1 fifo |
| 179 |
hasFile("bar/foo", "", 0), |
| 180 |
hasChardev("bar/cdev", 10, 11), |
| 181 |
hasBlockdev("bar/bdev", 100, 101), |
| 182 |
hasFifo("bar/fifo"), |
| 183 |
}, |
| 184 |
}, |
| 185 |
{ |
| 186 |
name: "chunks", |
| 187 |
chunkSize: 4, |
| 188 |
in: []tutil.TarEntry{ |
| 189 |
tutil.Dir("foo/"), |
| 190 |
tutil.File("foo/small", sampleText[:2]), |
| 191 |
tutil.File("foo/large", sampleText), |
| 192 |
}, |
| 193 |
want: []check{ |
| 194 |
numOfNodes(5), // root dir + prefetch landmark + 1 dir + 2 files |
| 195 |
numOfChunks("foo/large", 1+(len(sampleText)/4)), |
| 196 |
hasFileContentsOffset("foo/small", 0, sampleText[:2]), |
| 197 |
hasFileContentsOffset("foo/large", 0, sampleText[0:]), |
| 198 |
hasFileContentsOffset("foo/large", 1, sampleText[1:]), |
| 199 |
hasFileContentsOffset("foo/large", 2, sampleText[2:]), |
| 200 |
hasFileContentsOffset("foo/large", 3, sampleText[3:]), |
| 201 |
hasFileContentsOffset("foo/large", 4, sampleText[4:]), |
| 202 |
hasFileContentsOffset("foo/large", 5, sampleText[5:]), |
| 203 |
hasFileContentsOffset("foo/large", 6, sampleText[6:]), |
| 204 |
hasFileContentsOffset("foo/large", 7, sampleText[7:]), |
| 205 |
hasFileContentsOffset("foo/large", 8, sampleText[8:]), |
| 206 |
hasFileContentsOffset("foo/large", 9, sampleText[9:]), |
| 207 |
hasFileContentsOffset("foo/large", 10, sampleText[10:]), |
| 208 |
hasFileContentsOffset("foo/large", 11, sampleText[11:]), |
| 209 |
hasFileContentsOffset("foo/large", 12, sampleText[12:]), |
| 210 |
hasFileContentsOffset("foo/large", int64(len(sampleText)-1), ""), |
| 211 |
}, |
| 212 |
}, |
| 213 |
{ |
| 214 |
name: "several_files_in_chunk", |
| 215 |
minChunkSize: 8000, |
| 216 |
in: []tutil.TarEntry{ |
| 217 |
tutil.Dir("foo/"), |
| 218 |
tutil.File("foo/foo1", data64KB), |
| 219 |
tutil.File("foo2", "bb"), |
| 220 |
tutil.File("foo22", "ccc"), |
| 221 |
tutil.Dir("bar/"), |
| 222 |
tutil.File("bar/bar.txt", "aaa"), |
| 223 |
tutil.File("foo3", data64KB), |
| 224 |
}, |
| 225 |
// NOTE: we assume that the compressed "data64KB" is still larger than 8KB |
| 226 |
// landmark+dir+foo1, foo2+foo22+dir+bar.txt+foo3, TOC, footer |
| 227 |
want: []check{ |
| 228 |
numOfNodes(9), // root dir, prefetch landmark, dir, foo1, foo2, foo22, dir, bar.txt, foo3 |
| 229 |
hasDirChildren("foo/", "foo1"), |
| 230 |
hasDirChildren("bar/", "bar.txt"), |
| 231 |
hasFile("foo/foo1", data64KB, int64(len(data64KB))), |
| 232 |
hasFile("foo2", "bb", 2), |
| 233 |
hasFile("foo22", "ccc", 3), |
| 234 |
hasFile("bar/bar.txt", "aaa", 3), |
| 235 |
hasFile("foo3", data64KB, int64(len(data64KB))), |
| 236 |
hasFileContentsWithPreRead("foo22", 0, "ccc", chunkInfo{"foo2", "bb", 0, 2, digest.FromString("bb").String()}, |
| 237 |
chunkInfo{"bar/bar.txt", "aaa", 0, 3, digest.FromString("aaa").String()}, chunkInfo{"foo3", data64KB, 0, 64000, digest.FromString(data64KB).String()}), |
| 238 |
hasFileContentsOffset("foo/foo1", 0, data64KB), |
| 239 |
hasFileContentsOffset("foo2", 0, "bb"), |
| 240 |
hasFileContentsOffset("foo2", 1, "b"), |
| 241 |
hasFileContentsOffset("foo22", 0, "ccc"), |
| 242 |
hasFileContentsOffset("foo22", 1, "cc"), |
| 243 |
hasFileContentsOffset("foo22", 2, "c"), |
| 244 |
hasFileContentsOffset("bar/bar.txt", 0, "aaa"), |
| 245 |
hasFileContentsOffset("bar/bar.txt", 1, "aa"), |
| 246 |
hasFileContentsOffset("bar/bar.txt", 2, "a"), |
| 247 |
hasFileContentsOffset("foo3", 0, data64KB), |
| 248 |
hasFileContentsOffset("foo3", 1, data64KB[1:]), |
| 249 |
hasFileContentsOffset("foo3", 2, data64KB[2:]), |
| 250 |
hasFileContentsOffset("foo3", int64(len(data64KB)/2), data64KB[len(data64KB)/2:]), |
| 251 |
hasFileContentsOffset("foo3", int64(len(data64KB)-1), data64KB[len(data64KB)-1:]), |
| 252 |
}, |
| 253 |
}, |
| 254 |
{ |
| 255 |
name: "several_files_in_chunk_chunked", |
| 256 |
minChunkSize: 8000, |
| 257 |
chunkSize: 32000, |
| 258 |
in: []tutil.TarEntry{ |
| 259 |
tutil.Dir("foo/"), |
| 260 |
tutil.File("foo/foo1", data64KB), |
| 261 |
tutil.File("foo2", "bb"), |
| 262 |
tutil.Dir("bar/"), |
| 263 |
tutil.File("foo3", data64KB), |
| 264 |
}, |
| 265 |
// NOTE: we assume that the compressed chunk of "data64KB" is still larger than 8KB |
| 266 |
// landmark+dir+foo1(1), foo1(2), foo2+dir+foo3(1), foo3(2), TOC, footer |
| 267 |
want: []check{ |
| 268 |
numOfNodes(7), // root dir, prefetch landmark, dir, foo1, foo2, dir, foo3 |
| 269 |
hasDirChildren("foo", "foo1"), |
| 270 |
hasFile("foo/foo1", data64KB, int64(len(data64KB))), |
| 271 |
hasFile("foo2", "bb", 2), |
| 272 |
hasFile("foo3", data64KB, int64(len(data64KB))), |
| 273 |
hasFileContentsWithPreRead("foo2", 0, "bb", chunkInfo{"foo3", data64KB[:32000], 0, 32000, digest.FromString(data64KB[:32000]).String()}), |
| 274 |
hasFileContentsOffset("foo/foo1", 0, data64KB), |
| 275 |
hasFileContentsOffset("foo/foo1", 1, data64KB[1:]), |
| 276 |
hasFileContentsOffset("foo/foo1", 2, data64KB[2:]), |
| 277 |
hasFileContentsOffset("foo/foo1", int64(len(data64KB)/2), data64KB[len(data64KB)/2:]), |
| 278 |
hasFileContentsOffset("foo/foo1", int64(len(data64KB)-1), data64KB[len(data64KB)-1:]), |
| 279 |
hasFileContentsOffset("foo2", 0, "bb"), |
| 280 |
hasFileContentsOffset("foo2", 1, "b"), |
| 281 |
hasFileContentsOffset("foo3", 0, data64KB), |
| 282 |
hasFileContentsOffset("foo3", 1, data64KB[1:]), |
| 283 |
hasFileContentsOffset("foo3", 2, data64KB[2:]), |
| 284 |
hasFileContentsOffset("foo3", int64(len(data64KB)/2), data64KB[len(data64KB)/2:]), |
| 285 |
hasFileContentsOffset("foo3", int64(len(data64KB)-1), data64KB[len(data64KB)-1:]), |
| 286 |
}, |
| 287 |
}, |
| 288 |
} |
| 289 |
for _, tt := range tests { |
| 290 |
for _, prefix := range allowedPrefix { |
| 291 |
prefix := prefix |
| 292 |
for srcCompresionName, srcCompression := range srcCompressions { |
| 293 |
srcCompression := srcCompression() |
| 294 |
t.Run(tt.name+"-"+srcCompresionName, func(t *testing.T) { |
| 295 |
opts := []tutil.BuildEStargzOption{ |
| 296 |
tutil.WithBuildTarOptions(tutil.WithPrefix(prefix)), |
| 297 |
tutil.WithEStargzOptions(estargz.WithCompression(srcCompression)), |
| 298 |
} |
| 299 |
if tt.chunkSize > 0 { |
| 300 |
opts = append(opts, tutil.WithEStargzOptions(estargz.WithChunkSize(tt.chunkSize))) |
| 301 |
} |
| 302 |
if tt.minChunkSize > 0 { |
| 303 |
t.Logf("minChunkSize = %d", tt.minChunkSize) |
| 304 |
opts = append(opts, tutil.WithEStargzOptions(estargz.WithMinChunkSize(tt.minChunkSize))) |
| 305 |
} |
| 306 |
esgz, _, err := tutil.BuildEStargz(tt.in, opts...) |
| 307 |
if err != nil { |
| 308 |
t.Fatalf("failed to build sample eStargz: %v", err) |
| 309 |
} |
| 310 |
|
| 311 |
telemetry, checkCalled := newCalledTelemetry() |
| 312 |
r, err := factory(esgz, |
| 313 |
metadata.WithDecompressors(srcCompression), metadata.WithTelemetry(telemetry)) |
| 314 |
if err != nil { |
| 315 |
t.Fatalf("failed to create new reader: %v", err) |
| 316 |
} |
| 317 |
defer r.Close() |
| 318 |
t.Logf("vvvvv Node tree vvvvv") |
| 319 |
t.Logf("[%d] ROOT", r.RootID()) |
| 320 |
dumpNodes(t, r, r.RootID(), 1) |
| 321 |
t.Logf("^^^^^^^^^^^^^^^^^^^^^") |
| 322 |
for _, want := range tt.want { |
| 323 |
want(t, r) |
| 324 |
} |
| 325 |
if err := checkCalled(); err != nil { |
| 326 |
t.Errorf("telemetry failure: %v", err) |
| 327 |
} |
| 328 |
|
| 329 |
// Test the cloned reader works correctly as well |
| 330 |
esgz2, _, err := tutil.BuildEStargz(tt.in, opts...) |
| 331 |
if err != nil { |
| 332 |
t.Fatalf("failed to build sample eStargz: %v", err) |
| 333 |
} |
| 334 |
clonedR, err := r.Clone(esgz2) |
| 335 |
if err != nil { |
| 336 |
t.Fatalf("failed to clone reader: %v", err) |
| 337 |
} |
| 338 |
defer clonedR.Close() |
| 339 |
t.Logf("vvvvv Node tree (cloned) vvvvv") |
| 340 |
t.Logf("[%d] ROOT", clonedR.RootID()) |
| 341 |
dumpNodes(t, clonedR.(TestableReader), clonedR.RootID(), 1) |
| 342 |
t.Logf("^^^^^^^^^^^^^^^^^^^^^") |
| 343 |
for _, want := range tt.want { |
| 344 |
want(t, clonedR.(TestableReader)) |
| 345 |
} |
| 346 |
}) |
| 347 |
} |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
t.Run("clone-id-stability", func(t *testing.T) { |
| 352 |
var mapEntries func(r TestableReader, id uint32, m map[string]uint32) (map[string]uint32, error) |
| 353 |
mapEntries = func(r TestableReader, id uint32, m map[string]uint32) (map[string]uint32, error) { |
| 354 |
if m == nil { |
| 355 |
m = make(map[string]uint32) |
| 356 |
} |
| 357 |
return m, r.ForeachChild(id, func(name string, id uint32, mode os.FileMode) bool { |
| 358 |
m[name] = id |
| 359 |
if _, err := mapEntries(r, id, m); err != nil { |
| 360 |
t.Fatalf("could not map files: %s", err) |
| 361 |
return false |
| 362 |
} |
| 363 |
return true |
| 364 |
}) |
| 365 |
} |
| 366 |
|
| 367 |
in := []tutil.TarEntry{ |
| 368 |
tutil.File("foo", "foofoo"), |
| 369 |
tutil.Dir("bar/"), |
| 370 |
tutil.File("bar/zzz.txt", "bazbazbaz"), |
| 371 |
tutil.File("bar/aaa.txt", "bazbazbaz"), |
| 372 |
tutil.File("bar/fff.txt", "bazbazbaz"), |
| 373 |
tutil.File("xxx.txt", "xxxxx"), |
| 374 |
tutil.File("y.txt", ""), |
| 375 |
} |
| 376 |
|
| 377 |
esgz, _, err := tutil.BuildEStargz(in) |
| 378 |
if err != nil { |
| 379 |
t.Fatalf("failed to build sample eStargz: %v", err) |
| 380 |
} |
| 381 |
|
| 382 |
r, err := factory(esgz) |
| 383 |
if err != nil { |
| 384 |
t.Fatalf("failed to create new reader: %v", err) |
| 385 |
} |
| 386 |
|
| 387 |
fileMap, err := mapEntries(r, r.RootID(), nil) |
| 388 |
if err != nil { |
| 389 |
t.Fatalf("could not map files: %s", err) |
| 390 |
} |
| 391 |
cr, err := r.Clone(esgz) |
| 392 |
if err != nil { |
| 393 |
t.Fatalf("could not clone reader: %s", err) |
| 394 |
} |
| 395 |
cloneFileMap, err := mapEntries(cr.(TestableReader), cr.RootID(), nil) |
| 396 |
if err != nil { |
| 397 |
t.Fatalf("could not map files in cloned reader: %s", err) |
| 398 |
} |
| 399 |
if !reflect.DeepEqual(fileMap, cloneFileMap) { |
| 400 |
for f, id := range fileMap { |
| 401 |
t.Logf("original mapping %s -> %d", f, id) |
| 402 |
} |
| 403 |
for f, id := range cloneFileMap { |
| 404 |
t.Logf("clone mapping %s -> %d", f, id) |
| 405 |
} |
| 406 |
t.Fatal("file -> ID mappings did not match between original and cloned reader") |
| 407 |
} |
| 408 |
}) |
| 409 |
} |
| 410 |
|
| 411 |
func newCalledTelemetry() (telemetry *metadata.Telemetry, check func() error) { |
| 412 |
var getFooterLatencyCalled bool |
| 413 |
var getTocLatencyCalled bool |
| 414 |
var deserializeTocLatencyCalled bool |
| 415 |
return &metadata.Telemetry{ |
| 416 |
GetFooterLatency: func(time.Time) { getFooterLatencyCalled = true }, |
| 417 |
GetTocLatency: func(time.Time) { getTocLatencyCalled = true }, |
| 418 |
DeserializeTocLatency: func(time.Time) { deserializeTocLatencyCalled = true }, |
| 419 |
}, func() error { |
| 420 |
var allErr error |
| 421 |
if !getFooterLatencyCalled { |
| 422 |
allErr = multierror.Append(allErr, fmt.Errorf("metrics GetFooterLatency isn't called")) |
| 423 |
} |
| 424 |
if !getTocLatencyCalled { |
| 425 |
allErr = multierror.Append(allErr, fmt.Errorf("metrics GetTocLatency isn't called")) |
| 426 |
} |
| 427 |
if !deserializeTocLatencyCalled { |
| 428 |
allErr = multierror.Append(allErr, fmt.Errorf("metrics DeserializeTocLatency isn't called")) |
| 429 |
} |
| 430 |
return allErr |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
func dumpNodes(t *testing.T, r TestableReader, id uint32, level int) { |
| 435 |
if err := r.ForeachChild(id, func(name string, id uint32, mode os.FileMode) bool { |
| 436 |
ind := "" |
| 437 |
for i := 0; i < level; i++ { |
| 438 |
ind += " " |
| 439 |
} |
| 440 |
t.Logf("%v+- [%d] %q : %v", ind, id, name, mode) |
| 441 |
dumpNodes(t, r, id, level+1) |
| 442 |
return true |
| 443 |
}); err != nil { |
| 444 |
t.Errorf("failed to dump nodes %v", err) |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
type check func(*testing.T, TestableReader) |
| 449 |
|
| 450 |
func numOfNodes(want int) check { |
| 451 |
return func(t *testing.T, r TestableReader) { |
| 452 |
i, err := r.NumOfNodes() |
| 453 |
if err != nil { |
| 454 |
t.Errorf("num of nodes: %v", err) |
| 455 |
} |
| 456 |
if want != i { |
| 457 |
t.Errorf("unexpected num of nodes %d; want %d", i, want) |
| 458 |
} |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
func numOfChunks(name string, num int) check { |
| 463 |
return func(t *testing.T, r TestableReader) { |
| 464 |
nr, ok := r.(interface { |
| 465 |
NumOfChunks(id uint32) (i int, _ error) |
| 466 |
}) |
| 467 |
if !ok { |
| 468 |
return // skip |
| 469 |
} |
| 470 |
id, err := lookup(r, name) |
| 471 |
if err != nil { |
| 472 |
t.Errorf("failed to lookup %q: %v", name, err) |
| 473 |
return |
| 474 |
} |
| 475 |
i, err := nr.NumOfChunks(id) |
| 476 |
if err != nil { |
| 477 |
t.Errorf("failed to get num of chunks of %q: %v", name, err) |
| 478 |
return |
| 479 |
} |
| 480 |
if i != num { |
| 481 |
t.Errorf("unexpected num of chunk of %q : %d want %d", name, i, num) |
| 482 |
} |
| 483 |
} |
| 484 |
} |
| 485 |
|
| 486 |
func sameNodes(n string, nodes ...string) check { |
| 487 |
return func(t *testing.T, r TestableReader) { |
| 488 |
id, err := lookup(r, n) |
| 489 |
if err != nil { |
| 490 |
t.Errorf("failed to lookup %q: %v", n, err) |
| 491 |
return |
| 492 |
} |
| 493 |
for _, en := range nodes { |
| 494 |
eid, err := lookup(r, en) |
| 495 |
if err != nil { |
| 496 |
t.Errorf("failed to lookup %q: %v", en, err) |
| 497 |
return |
| 498 |
} |
| 499 |
if eid != id { |
| 500 |
t.Errorf("unexpected ID of %q: %d want %d", en, eid, id) |
| 501 |
} |
| 502 |
} |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
func linkName(name string, linkName string) check { |
| 507 |
return func(t *testing.T, r TestableReader) { |
| 508 |
id, err := lookup(r, name) |
| 509 |
if err != nil { |
| 510 |
t.Errorf("failed to lookup %q: %v", name, err) |
| 511 |
return |
| 512 |
} |
| 513 |
attr, err := r.GetAttr(id) |
| 514 |
if err != nil { |
| 515 |
t.Errorf("failed to get attr of %q: %v", name, err) |
| 516 |
return |
| 517 |
} |
| 518 |
if attr.Mode&os.ModeSymlink == 0 { |
| 519 |
t.Errorf("%q is not a symlink: %v", name, attr.Mode) |
| 520 |
return |
| 521 |
} |
| 522 |
if attr.LinkName != linkName { |
| 523 |
t.Errorf("unexpected link name of %q : %q want %q", name, attr.LinkName, linkName) |
| 524 |
return |
| 525 |
} |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
func hasNumLink(name string, numLink int) check { |
| 530 |
return func(t *testing.T, r TestableReader) { |
| 531 |
id, err := lookup(r, name) |
| 532 |
if err != nil { |
| 533 |
t.Errorf("failed to lookup %q: %v", name, err) |
| 534 |
return |
| 535 |
} |
| 536 |
attr, err := r.GetAttr(id) |
| 537 |
if err != nil { |
| 538 |
t.Errorf("failed to get attr of %q: %v", name, err) |
| 539 |
return |
| 540 |
} |
| 541 |
if attr.NumLink != numLink { |
| 542 |
t.Errorf("unexpected numLink of %q: %d want %d", name, attr.NumLink, numLink) |
| 543 |
return |
| 544 |
} |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
func hasDirChildren(name string, children ...string) check { |
| 549 |
return func(t *testing.T, r TestableReader) { |
| 550 |
id, err := lookup(r, name) |
| 551 |
if err != nil { |
| 552 |
t.Errorf("failed to lookup %q: %v", name, err) |
| 553 |
return |
| 554 |
} |
| 555 |
attr, err := r.GetAttr(id) |
| 556 |
if err != nil { |
| 557 |
t.Errorf("failed to get attr of %q: %v", name, err) |
| 558 |
return |
| 559 |
} |
| 560 |
if !attr.Mode.IsDir() { |
| 561 |
t.Errorf("%q is not directory: %v", name, attr.Mode) |
| 562 |
return |
| 563 |
} |
| 564 |
found := map[string]struct{}{} |
| 565 |
if err := r.ForeachChild(id, func(name string, id uint32, mode os.FileMode) bool { |
| 566 |
found[name] = struct{}{} |
| 567 |
return true |
| 568 |
}); err != nil { |
| 569 |
t.Errorf("failed to see children %v", err) |
| 570 |
return |
| 571 |
} |
| 572 |
if len(found) != len(children) { |
| 573 |
t.Errorf("unexpected number of children of %q : %d want %d", name, len(found), len(children)) |
| 574 |
} |
| 575 |
for _, want := range children { |
| 576 |
if _, ok := found[want]; !ok { |
| 577 |
t.Errorf("expected child %q not found in %q", want, name) |
| 578 |
} |
| 579 |
} |
| 580 |
} |
| 581 |
} |
| 582 |
|
| 583 |
func hasChardev(name string, maj, min int) check { |
| 584 |
return func(t *testing.T, r TestableReader) { |
| 585 |
id, err := lookup(r, name) |
| 586 |
if err != nil { |
| 587 |
t.Errorf("cannot find chardev %q: %v", name, err) |
| 588 |
return |
| 589 |
} |
| 590 |
attr, err := r.GetAttr(id) |
| 591 |
if err != nil { |
| 592 |
t.Errorf("cannot get attr of chardev %q: %v", name, err) |
| 593 |
return |
| 594 |
} |
| 595 |
if attr.Mode&os.ModeDevice == 0 || attr.Mode&os.ModeCharDevice == 0 { |
| 596 |
t.Errorf("file %q is not a chardev: %v", name, attr.Mode) |
| 597 |
return |
| 598 |
} |
| 599 |
if attr.DevMajor != maj || attr.DevMinor != min { |
| 600 |
t.Errorf("unexpected major/minor of chardev %q: %d/%d want %d/%d", name, attr.DevMajor, attr.DevMinor, maj, min) |
| 601 |
return |
| 602 |
} |
| 603 |
} |
| 604 |
} |
| 605 |
|
| 606 |
func hasBlockdev(name string, maj, min int) check { |
| 607 |
return func(t *testing.T, r TestableReader) { |
| 608 |
id, err := lookup(r, name) |
| 609 |
if err != nil { |
| 610 |
t.Errorf("cannot find blockdev %q: %v", name, err) |
| 611 |
return |
| 612 |
} |
| 613 |
attr, err := r.GetAttr(id) |
| 614 |
if err != nil { |
| 615 |
t.Errorf("cannot get attr of blockdev %q: %v", name, err) |
| 616 |
return |
| 617 |
} |
| 618 |
if attr.Mode&os.ModeDevice == 0 || attr.Mode&os.ModeCharDevice != 0 { |
| 619 |
t.Errorf("file %q is not a blockdev: %v", name, attr.Mode) |
| 620 |
return |
| 621 |
} |
| 622 |
if attr.DevMajor != maj || attr.DevMinor != min { |
| 623 |
t.Errorf("unexpected major/minor of blockdev %q: %d/%d want %d/%d", name, attr.DevMajor, attr.DevMinor, maj, min) |
| 624 |
return |
| 625 |
} |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
func hasFifo(name string) check { |
| 630 |
return func(t *testing.T, r TestableReader) { |
| 631 |
id, err := lookup(r, name) |
| 632 |
if err != nil { |
| 633 |
t.Errorf("cannot find blockdev %q: %v", name, err) |
| 634 |
return |
| 635 |
} |
| 636 |
attr, err := r.GetAttr(id) |
| 637 |
if err != nil { |
| 638 |
t.Errorf("cannot get attr of blockdev %q: %v", name, err) |
| 639 |
return |
| 640 |
} |
| 641 |
if attr.Mode&os.ModeNamedPipe == 0 { |
| 642 |
t.Errorf("file %q is not a fifo: %v", name, attr.Mode) |
| 643 |
return |
| 644 |
} |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
func hasFile(name, content string, size int64) check { |
| 649 |
return func(t *testing.T, r TestableReader) { |
| 650 |
id, err := lookup(r, name) |
| 651 |
if err != nil { |
| 652 |
t.Errorf("cannot find file %q: %v", name, err) |
| 653 |
return |
| 654 |
} |
| 655 |
attr, err := r.GetAttr(id) |
| 656 |
if err != nil { |
| 657 |
t.Errorf("cannot get attr of file %q: %v", name, err) |
| 658 |
return |
| 659 |
} |
| 660 |
if !attr.Mode.IsRegular() { |
| 661 |
t.Errorf("file %q is not a regular file: %v", name, attr.Mode) |
| 662 |
return |
| 663 |
} |
| 664 |
sr, err := r.OpenFile(id) |
| 665 |
if err != nil { |
| 666 |
t.Errorf("cannot open file %q: %v", name, err) |
| 667 |
return |
| 668 |
} |
| 669 |
data, err := io.ReadAll(io.NewSectionReader(sr, 0, attr.Size)) |
| 670 |
if err != nil { |
| 671 |
t.Errorf("cannot read file %q: %v", name, err) |
| 672 |
return |
| 673 |
} |
| 674 |
if attr.Size != size { |
| 675 |
t.Errorf("unexpected size of file %q : %d (%q) want %d (%q)", name, attr.Size, longBytesView(data), size, longBytesView([]byte(content))) |
| 676 |
return |
| 677 |
} |
| 678 |
if string(data) != content { |
| 679 |
t.Errorf("unexpected content of %q: %q want %q", name, longBytesView(data), longBytesView([]byte(content))) |
| 680 |
return |
| 681 |
} |
| 682 |
} |
| 683 |
} |
| 684 |
|
| 685 |
type chunkInfo struct { |
| 686 |
name string |
| 687 |
data string |
| 688 |
chunkOffset int64 |
| 689 |
chunkSize int64 |
| 690 |
chunkDigest string |
| 691 |
} |
| 692 |
|
| 693 |
func hasFileContentsWithPreRead(name string, off int64, contents string, extra ...chunkInfo) check { |
| 694 |
return func(t *testing.T, r TestableReader) { |
| 695 |
extraMap := make(map[uint32]chunkInfo) |
| 696 |
for _, e := range extra { |
| 697 |
id, err := lookup(r, e.name) |
| 698 |
if err != nil { |
| 699 |
t.Errorf("failed to lookup extra %q: %v", e.name, err) |
| 700 |
return |
| 701 |
} |
| 702 |
extraMap[id] = e |
| 703 |
} |
| 704 |
var extraNames []string |
| 705 |
for _, e := range extraMap { |
| 706 |
extraNames = append(extraNames, e.name) |
| 707 |
} |
| 708 |
id, err := lookup(r, name) |
| 709 |
if err != nil { |
| 710 |
t.Errorf("failed to lookup %q: %v", name, err) |
| 711 |
return |
| 712 |
} |
| 713 |
fr, err := r.OpenFileWithPreReader(id, func(id uint32, chunkOffset, chunkSize int64, chunkDigest string, cr io.Reader) error { |
| 714 |
t.Logf("On %q: got preread of %d", name, id) |
| 715 |
ex, ok := extraMap[id] |
| 716 |
if !ok { |
| 717 |
t.Fatalf("fail on %q: unexpected entry %d: %+v", name, id, extraNames) |
| 718 |
} |
| 719 |
if chunkOffset != ex.chunkOffset || chunkSize != ex.chunkSize || chunkDigest != ex.chunkDigest { |
| 720 |
t.Fatalf("fail on %q: unexpected node %d: %+v", name, id, ex) |
| 721 |
} |
| 722 |
got, err := io.ReadAll(cr) |
| 723 |
if err != nil { |
| 724 |
t.Fatalf("fail on %q: failed to read %d: %v", name, id, err) |
| 725 |
} |
| 726 |
if ex.data != string(got) { |
| 727 |
t.Fatalf("fail on %q: unexpected contents of %d: len=%d; want=%d", name, id, len(got), len(ex.data)) |
| 728 |
} |
| 729 |
delete(extraMap, id) |
| 730 |
return nil |
| 731 |
}) |
| 732 |
if err != nil { |
| 733 |
t.Errorf("failed to open file %q: %v", name, err) |
| 734 |
return |
| 735 |
} |
| 736 |
buf := make([]byte, len(contents)) |
| 737 |
n, err := fr.ReadAt(buf, off) |
| 738 |
if err != nil && err != io.EOF { |
| 739 |
t.Errorf("failed to read file %q (off:%d, want:%q): %v", name, off, contents, err) |
| 740 |
return |
| 741 |
} |
| 742 |
if n != len(contents) { |
| 743 |
t.Errorf("failed to read contents %q (off:%d, want:%q) got %q", name, off, longBytesView([]byte(contents)), longBytesView(buf)) |
| 744 |
return |
| 745 |
} |
| 746 |
if string(buf) != contents { |
| 747 |
t.Errorf("unexpected content of %q: %q want %q", name, longBytesView(buf), longBytesView([]byte(contents))) |
| 748 |
return |
| 749 |
} |
| 750 |
if len(extraMap) != 0 { |
| 751 |
var exNames []string |
| 752 |
for _, ex := range extraMap { |
| 753 |
exNames = append(exNames, ex.name) |
| 754 |
} |
| 755 |
t.Fatalf("fail on %q: some entries aren't read: %+v", name, exNames) |
| 756 |
} |
| 757 |
} |
| 758 |
} |
| 759 |
|
| 760 |
func hasFileContentsOffset(name string, off int64, contents string) check { |
| 761 |
return func(t *testing.T, r TestableReader) { |
| 762 |
id, err := lookup(r, name) |
| 763 |
if err != nil { |
| 764 |
t.Errorf("failed to lookup %q: %v", name, err) |
| 765 |
return |
| 766 |
} |
| 767 |
fr, err := r.OpenFile(id) |
| 768 |
if err != nil { |
| 769 |
t.Errorf("failed to open file %q: %v", name, err) |
| 770 |
return |
| 771 |
} |
| 772 |
buf := make([]byte, len(contents)) |
| 773 |
n, err := fr.ReadAt(buf, off) |
| 774 |
if err != nil && err != io.EOF { |
| 775 |
t.Errorf("failed to read file %q (off:%d, want:%q): %v", name, off, contents, err) |
| 776 |
return |
| 777 |
} |
| 778 |
if n != len(contents) { |
| 779 |
t.Errorf("failed to read contents %q (off:%d, want:%q) got %q", name, off, longBytesView([]byte(contents)), longBytesView(buf)) |
| 780 |
return |
| 781 |
} |
| 782 |
if string(buf) != contents { |
| 783 |
t.Errorf("unexpected content of %q: %q want %q", name, longBytesView(buf), longBytesView([]byte(contents))) |
| 784 |
return |
| 785 |
} |
| 786 |
} |
| 787 |
} |
| 788 |
|
| 789 |
func hasMode(name string, mode os.FileMode) check { |
| 790 |
return func(t *testing.T, r TestableReader) { |
| 791 |
id, err := lookup(r, name) |
| 792 |
if err != nil { |
| 793 |
t.Errorf("cannot find file %q: %v", name, err) |
| 794 |
return |
| 795 |
} |
| 796 |
attr, err := r.GetAttr(id) |
| 797 |
if err != nil { |
| 798 |
t.Errorf("cannot get attr of file %q: %v", name, err) |
| 799 |
return |
| 800 |
} |
| 801 |
if attr.Mode != mode { |
| 802 |
t.Errorf("unexpected mode of %q: %v want %v", name, attr.Mode, mode) |
| 803 |
return |
| 804 |
} |
| 805 |
} |
| 806 |
} |
| 807 |
|
| 808 |
func hasOwner(name string, uid, gid int) check { |
| 809 |
return func(t *testing.T, r TestableReader) { |
| 810 |
id, err := lookup(r, name) |
| 811 |
if err != nil { |
| 812 |
t.Errorf("cannot find file %q: %v", name, err) |
| 813 |
return |
| 814 |
} |
| 815 |
attr, err := r.GetAttr(id) |
| 816 |
if err != nil { |
| 817 |
t.Errorf("cannot get attr of file %q: %v", name, err) |
| 818 |
return |
| 819 |
} |
| 820 |
if attr.UID != uid || attr.GID != gid { |
| 821 |
t.Errorf("unexpected owner of %q: (%d:%d) want (%d:%d)", name, attr.UID, attr.GID, uid, gid) |
| 822 |
return |
| 823 |
} |
| 824 |
} |
| 825 |
} |
| 826 |
|
| 827 |
func hasModTime(name string, modTime time.Time) check { |
| 828 |
return func(t *testing.T, r TestableReader) { |
| 829 |
id, err := lookup(r, name) |
| 830 |
if err != nil { |
| 831 |
t.Errorf("cannot find file %q: %v", name, err) |
| 832 |
return |
| 833 |
} |
| 834 |
attr, err := r.GetAttr(id) |
| 835 |
if err != nil { |
| 836 |
t.Errorf("cannot get attr of file %q: %v", name, err) |
| 837 |
return |
| 838 |
} |
| 839 |
attrModTime := attr.ModTime |
| 840 |
if attrModTime.Before(modTime) || attrModTime.After(modTime) { |
| 841 |
t.Errorf("unexpected time of %q: %v; want %v", name, attrModTime, modTime) |
| 842 |
return |
| 843 |
} |
| 844 |
} |
| 845 |
} |
| 846 |
|
| 847 |
func hasXattrs(name string, xattrs map[string]string) check { |
| 848 |
return func(t *testing.T, r TestableReader) { |
| 849 |
id, err := lookup(r, name) |
| 850 |
if err != nil { |
| 851 |
t.Errorf("cannot find file %q: %v", name, err) |
| 852 |
return |
| 853 |
} |
| 854 |
attr, err := r.GetAttr(id) |
| 855 |
if err != nil { |
| 856 |
t.Errorf("cannot get attr of file %q: %v", name, err) |
| 857 |
return |
| 858 |
} |
| 859 |
if len(attr.Xattrs) != len(xattrs) { |
| 860 |
t.Errorf("unexpected size of xattr of %q: %d want %d", name, len(attr.Xattrs), len(xattrs)) |
| 861 |
return |
| 862 |
} |
| 863 |
for k, v := range attr.Xattrs { |
| 864 |
if xattrs[k] != string(v) { |
| 865 |
t.Errorf("unexpected xattr of %q: %q=%q want %q=%q", name, k, string(v), k, xattrs[k]) |
| 866 |
} |
| 867 |
} |
| 868 |
} |
| 869 |
} |
| 870 |
|
| 871 |
func lookup(r TestableReader, name string) (uint32, error) { |
| 872 |
name = strings.TrimPrefix(path.Clean("/"+name), "/") |
| 873 |
if name == "" { |
| 874 |
return r.RootID(), nil |
| 875 |
} |
| 876 |
dir, base := filepath.Split(name) |
| 877 |
pid, err := lookup(r, dir) |
| 878 |
if err != nil { |
| 879 |
return 0, err |
| 880 |
} |
| 881 |
id, _, err := r.GetChild(pid, base) |
| 882 |
return id, err |
| 883 |
} |
| 884 |
|
| 885 |
// longBytesView is an alias of []byte suitable for printing a long data as an omitted string to avoid long data being printed. |
| 886 |
type longBytesView []byte |
| 887 |
|
| 888 |
func (b longBytesView) String() string { |
| 889 |
if len(b) < 100 { |
| 890 |
return string(b) |
| 891 |
} |
| 892 |
return string(b[:50]) + "...(omit)..." + string(b[len(b)-50:]) |
| 893 |
} |