| 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 snapshot |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
_ "crypto/sha256" |
| 22 |
"fmt" |
| 23 |
"os" |
| 24 |
"path/filepath" |
| 25 |
"syscall" |
| 26 |
"testing" |
| 27 |
|
| 28 |
"github.com/containerd/containerd/errdefs" |
| 29 |
"github.com/containerd/containerd/mount" |
| 30 |
"github.com/containerd/containerd/pkg/testutil" |
| 31 |
"github.com/containerd/containerd/snapshots" |
| 32 |
"github.com/containerd/containerd/snapshots/storage" |
| 33 |
"github.com/containerd/containerd/snapshots/testsuite" |
| 34 |
) |
| 35 |
|
| 36 |
const ( |
| 37 |
remoteSampleFile = "foo" |
| 38 |
remoteSampleFileContents = "remote layer" |
| 39 |
brokenLabel = "containerd.io/snapshot/broken" |
| 40 |
) |
| 41 |
|
| 42 |
func prepareWithTarget(t *testing.T, sn snapshots.Snapshotter, target, key, parent string, labels map[string]string) string { |
| 43 |
ctx := context.TODO() |
| 44 |
if labels == nil { |
| 45 |
labels = make(map[string]string) |
| 46 |
} |
| 47 |
labels[targetSnapshotLabel] = target |
| 48 |
if _, err := sn.Prepare(ctx, key, parent, snapshots.WithLabels(labels)); !errdefs.IsAlreadyExists(err) { |
| 49 |
t.Fatalf("failed to prepare remote snapshot: %v", err) |
| 50 |
} |
| 51 |
return target |
| 52 |
} |
| 53 |
|
| 54 |
func TestRemotePrepare(t *testing.T) { |
| 55 |
testutil.RequiresRoot(t) |
| 56 |
ctx := context.TODO() |
| 57 |
root, err := os.MkdirTemp("", "overlay") |
| 58 |
if err != nil { |
| 59 |
t.Fatal(err) |
| 60 |
} |
| 61 |
defer os.RemoveAll(root) |
| 62 |
sn, err := NewSnapshotter(context.TODO(), root, bindFileSystem(t)) |
| 63 |
if err != nil { |
| 64 |
t.Fatalf("failed to make new remote snapshotter: %q", err) |
| 65 |
} |
| 66 |
|
| 67 |
// Prepare a remote snapshot. |
| 68 |
target := prepareWithTarget(t, sn, "testTarget", "/tmp/prepareTarget", "", nil) |
| 69 |
defer sn.Remove(ctx, target) |
| 70 |
|
| 71 |
// Get internally committed remote snapshot. |
| 72 |
var tinfo *snapshots.Info |
| 73 |
if err := sn.Walk(ctx, func(ctx context.Context, i snapshots.Info) error { |
| 74 |
if tinfo == nil && i.Kind == snapshots.KindCommitted { |
| 75 |
if i.Labels[targetSnapshotLabel] != target { |
| 76 |
return nil |
| 77 |
} |
| 78 |
if i.Parent != "" { |
| 79 |
return nil |
| 80 |
} |
| 81 |
tinfo = &i |
| 82 |
} |
| 83 |
return nil |
| 84 |
|
| 85 |
}); err != nil { |
| 86 |
t.Fatalf("failed to get remote snapshot: %v", err) |
| 87 |
} |
| 88 |
if tinfo == nil { |
| 89 |
t.Fatalf("prepared remote snapshot %q not found", target) |
| 90 |
} |
| 91 |
|
| 92 |
// Stat and validate the remote snapshot. |
| 93 |
info, err := sn.Stat(ctx, tinfo.Name) |
| 94 |
if err != nil { |
| 95 |
t.Fatal("failed to stat remote snapshot") |
| 96 |
} |
| 97 |
if info.Kind != snapshots.KindCommitted { |
| 98 |
t.Errorf("snapshot Kind is %q; want %q", info.Kind, snapshots.KindCommitted) |
| 99 |
} |
| 100 |
if label, ok := info.Labels[targetSnapshotLabel]; !ok || label != target { |
| 101 |
t.Errorf("remote snapshot hasn't valid remote label: %q", label) |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
func TestRemoteOverlay(t *testing.T) { |
| 106 |
testutil.RequiresRoot(t) |
| 107 |
ctx := context.TODO() |
| 108 |
root, err := os.MkdirTemp("", "remote") |
| 109 |
if err != nil { |
| 110 |
t.Fatal(err) |
| 111 |
} |
| 112 |
defer os.RemoveAll(root) |
| 113 |
sn, err := NewSnapshotter(context.TODO(), root, bindFileSystem(t)) |
| 114 |
if err != nil { |
| 115 |
t.Fatalf("failed to make new remote snapshotter: %q", err) |
| 116 |
} |
| 117 |
|
| 118 |
// Prepare a remote snapshot. |
| 119 |
target := prepareWithTarget(t, sn, "testTarget", "/tmp/prepareTarget", "", nil) |
| 120 |
defer sn.Remove(ctx, target) |
| 121 |
|
| 122 |
// Prepare a new layer based on the remote snapshot. |
| 123 |
pKey := "/tmp/test" |
| 124 |
mounts, err := sn.Prepare(ctx, pKey, target) |
| 125 |
if err != nil { |
| 126 |
t.Fatalf("faild to prepare using lower remote layer: %v", err) |
| 127 |
} |
| 128 |
if len(mounts) != 1 { |
| 129 |
t.Errorf("should only have 1 mount but received %d", len(mounts)) |
| 130 |
} |
| 131 |
m := mounts[0] |
| 132 |
if m.Type != "overlay" { |
| 133 |
t.Errorf("mount type should be overlay but received %q", m.Type) |
| 134 |
} |
| 135 |
if m.Source != "overlay" { |
| 136 |
t.Errorf("expected source %q but received %q", "overlay", m.Source) |
| 137 |
} |
| 138 |
var ( |
| 139 |
bp = getBasePath(ctx, sn, root, pKey) |
| 140 |
work = "workdir=" + filepath.Join(bp, "work") |
| 141 |
upper = "upperdir=" + filepath.Join(bp, "fs") |
| 142 |
lower = "lowerdir=" + getParents(ctx, sn, root, pKey)[0] |
| 143 |
) |
| 144 |
for i, v := range []string{ |
| 145 |
work, |
| 146 |
upper, |
| 147 |
lower, |
| 148 |
} { |
| 149 |
if m.Options[i] != v { |
| 150 |
t.Errorf("expected %q but received %q", v, m.Options[i]) |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
// Validate the contents of the snapshot |
| 155 |
data, err := os.ReadFile(filepath.Join(getParents(ctx, sn, root, pKey)[0], remoteSampleFile)) |
| 156 |
if err != nil { |
| 157 |
t.Fatalf("failed to read a file in the remote snapshot: %v", err) |
| 158 |
} |
| 159 |
if e := string(data); e != remoteSampleFileContents { |
| 160 |
t.Fatalf("expected file contents %q but got %q", remoteSampleFileContents, e) |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
func TestRemoteCommit(t *testing.T) { |
| 165 |
testutil.RequiresRoot(t) |
| 166 |
ctx := context.TODO() |
| 167 |
root, err := os.MkdirTemp("", "remote") |
| 168 |
if err != nil { |
| 169 |
t.Fatal(err) |
| 170 |
} |
| 171 |
defer os.RemoveAll(root) |
| 172 |
sn, err := NewSnapshotter(context.TODO(), root, bindFileSystem(t)) |
| 173 |
if err != nil { |
| 174 |
t.Fatalf("failed to make new remote snapshotter: %q", err) |
| 175 |
} |
| 176 |
|
| 177 |
// Prepare a remote snapshot. |
| 178 |
target := prepareWithTarget(t, sn, "testTarget", "/tmp/prepareTarget", "", nil) |
| 179 |
defer sn.Remove(ctx, target) |
| 180 |
|
| 181 |
// Prepare a new snapshot based on the remote snapshot |
| 182 |
pKey := "/tmp/test" |
| 183 |
mounts, err := sn.Prepare(ctx, pKey, target) |
| 184 |
if err != nil { |
| 185 |
t.Fatal(err) |
| 186 |
} |
| 187 |
|
| 188 |
// Make a new active snapshot based on the remote snapshot. |
| 189 |
snapshot, err := os.MkdirTemp("", "snapshot") |
| 190 |
if err != nil { |
| 191 |
t.Fatal(err) |
| 192 |
} |
| 193 |
defer os.RemoveAll(snapshot) |
| 194 |
m := mounts[0] |
| 195 |
if err := m.Mount(snapshot); err != nil { |
| 196 |
t.Fatal(err) |
| 197 |
} |
| 198 |
defer mount.Unmount(snapshot, 0) |
| 199 |
if err := os.WriteFile(filepath.Join(snapshot, "bar"), []byte("hi"), 0660); err != nil { |
| 200 |
t.Fatal(err) |
| 201 |
} |
| 202 |
mount.Unmount(snapshot, 0) |
| 203 |
|
| 204 |
// Commit the active snapshot |
| 205 |
cKey := "/tmp/layer" |
| 206 |
if err := sn.Commit(ctx, cKey, pKey); err != nil { |
| 207 |
t.Fatal(err) |
| 208 |
} |
| 209 |
|
| 210 |
// Validate the committed snapshot |
| 211 |
check, err := os.MkdirTemp("", "check") |
| 212 |
if err != nil { |
| 213 |
t.Fatal(err) |
| 214 |
} |
| 215 |
defer os.RemoveAll(check) |
| 216 |
mounts, err = sn.Prepare(ctx, "/tmp/test2", cKey) |
| 217 |
if err != nil { |
| 218 |
t.Fatal(err) |
| 219 |
} |
| 220 |
m = mounts[0] |
| 221 |
if err := m.Mount(check); err != nil { |
| 222 |
t.Fatal(err) |
| 223 |
} |
| 224 |
defer mount.Unmount(check, 0) |
| 225 |
data, err := os.ReadFile(filepath.Join(check, "bar")) |
| 226 |
if err != nil { |
| 227 |
t.Fatal(err) |
| 228 |
} |
| 229 |
if e := string(data); e != "hi" { |
| 230 |
t.Fatalf("expected file contents %q but got %q", "hi", e) |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
func TestFailureDetection(t *testing.T) { |
| 235 |
testutil.RequiresRoot(t) |
| 236 |
tests := []struct { |
| 237 |
name string |
| 238 |
broken []bool // top element is the lowest layer |
| 239 |
overlay bool // whether appending the topmost normal overlay snapshot or not |
| 240 |
wantOK bool |
| 241 |
}{ |
| 242 |
{ |
| 243 |
name: "flat_ok", |
| 244 |
broken: []bool{false}, |
| 245 |
wantOK: true, |
| 246 |
}, |
| 247 |
{ |
| 248 |
name: "deep_ok", |
| 249 |
broken: []bool{false, false, false}, |
| 250 |
wantOK: true, |
| 251 |
}, |
| 252 |
{ |
| 253 |
name: "flat_overlay_ok", |
| 254 |
broken: []bool{false}, |
| 255 |
overlay: true, |
| 256 |
wantOK: true, |
| 257 |
}, |
| 258 |
{ |
| 259 |
name: "deep_overlay_ok", |
| 260 |
broken: []bool{false, false, false}, |
| 261 |
overlay: true, |
| 262 |
wantOK: true, |
| 263 |
}, |
| 264 |
{ |
| 265 |
name: "flat_ng", |
| 266 |
broken: []bool{true}, |
| 267 |
wantOK: false, |
| 268 |
}, |
| 269 |
{ |
| 270 |
name: "deep_ng", |
| 271 |
broken: []bool{false, true, false}, |
| 272 |
wantOK: false, |
| 273 |
}, |
| 274 |
{ |
| 275 |
name: "flat_overlay_ng", |
| 276 |
broken: []bool{true}, |
| 277 |
overlay: true, |
| 278 |
wantOK: false, |
| 279 |
}, |
| 280 |
{ |
| 281 |
name: "deep_overlay_ng", |
| 282 |
broken: []bool{false, true, false}, |
| 283 |
overlay: true, |
| 284 |
wantOK: false, |
| 285 |
}, |
| 286 |
} |
| 287 |
|
| 288 |
check := func(t *testing.T, ok bool, err error) bool { |
| 289 |
if err == nil { |
| 290 |
if !ok { |
| 291 |
t.Error("check all passed but wanted to be failed") |
| 292 |
return false |
| 293 |
} |
| 294 |
} else if errdefs.IsUnavailable(err) { |
| 295 |
if ok { |
| 296 |
t.Error("got Unavailable but wanted to be non-error") |
| 297 |
return false |
| 298 |
} |
| 299 |
} else { |
| 300 |
t.Errorf("got unexpected error %q", err) |
| 301 |
return false |
| 302 |
} |
| 303 |
return true |
| 304 |
} |
| 305 |
|
| 306 |
for _, tt := range tests { |
| 307 |
t.Run(tt.name, func(t *testing.T) { |
| 308 |
ctx := context.TODO() |
| 309 |
root, err := os.MkdirTemp("", "remote") |
| 310 |
if err != nil { |
| 311 |
t.Fatal(err) |
| 312 |
} |
| 313 |
defer os.RemoveAll(root) |
| 314 |
fi := bindFileSystem(t) |
| 315 |
sn, err := NewSnapshotter(context.TODO(), root, fi) |
| 316 |
if err != nil { |
| 317 |
t.Fatalf("failed to make new Snapshotter: %q", err) |
| 318 |
} |
| 319 |
fs, ok := fi.(*bindFs) |
| 320 |
if !ok { |
| 321 |
t.Fatalf("Invalid filesystem type(not *filesystem)") |
| 322 |
} |
| 323 |
|
| 324 |
// Prepare snapshots |
| 325 |
fs.checkFailure = false |
| 326 |
pKey := "" |
| 327 |
for i, broken := range tt.broken { |
| 328 |
var ( |
| 329 |
targetName = fmt.Sprintf("/tmp/testTarget%d", i) |
| 330 |
key = fmt.Sprintf("/tmp/testKey%d", i) |
| 331 |
labels = make(map[string]string) |
| 332 |
) |
| 333 |
if broken { |
| 334 |
labels[brokenLabel] = "true" |
| 335 |
} |
| 336 |
target := prepareWithTarget(t, sn, targetName, key, pKey, labels) |
| 337 |
defer sn.Remove(ctx, target) |
| 338 |
pKey = target |
| 339 |
} |
| 340 |
|
| 341 |
if tt.overlay { |
| 342 |
key := "/tmp/test" |
| 343 |
_, err := sn.Prepare(ctx, key, pKey) |
| 344 |
if err != nil { |
| 345 |
t.Fatal(err) |
| 346 |
} |
| 347 |
cKey := "/tmp/layer" |
| 348 |
if err := sn.Commit(ctx, cKey, key); err != nil { |
| 349 |
t.Fatal(err) |
| 350 |
} |
| 351 |
defer sn.Remove(ctx, cKey) |
| 352 |
pKey = cKey |
| 353 |
} |
| 354 |
|
| 355 |
// Tests if we can detect layer unavailablity |
| 356 |
key := "/tmp/snapshot.test" |
| 357 |
fs.checkFailure = true |
| 358 |
defer sn.Remove(ctx, key) |
| 359 |
if _, err := sn.Prepare(ctx, key, pKey); !check(t, tt.wantOK, err) { |
| 360 |
return |
| 361 |
} |
| 362 |
fs.checkFailure = false |
| 363 |
key2 := "/tmp/test2" |
| 364 |
if _, err = sn.Prepare(ctx, key2, pKey); err != nil { |
| 365 |
t.Fatal(err) |
| 366 |
} |
| 367 |
defer sn.Remove(ctx, key2) |
| 368 |
fs.checkFailure = true |
| 369 |
if _, err := sn.Mounts(ctx, key2); !check(t, tt.wantOK, err) { |
| 370 |
return |
| 371 |
} |
| 372 |
}) |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
func bindFileSystem(t *testing.T) FileSystem { |
| 377 |
root, err := os.MkdirTemp("", "remote") |
| 378 |
if err != nil { |
| 379 |
t.Fatalf("failed to prepare working-space for bind filesystem: %q", err) |
| 380 |
} |
| 381 |
if err := os.WriteFile(filepath.Join(root, remoteSampleFile), []byte(remoteSampleFileContents), 0660); err != nil { |
| 382 |
t.Fatalf("failed to write sample file of bind filesystem: %q", err) |
| 383 |
} |
| 384 |
return &bindFs{ |
| 385 |
root: root, |
| 386 |
t: t, |
| 387 |
broken: make(map[string]bool), |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
type bindFs struct { |
| 392 |
t *testing.T |
| 393 |
root string |
| 394 |
checkFailure bool |
| 395 |
broken map[string]bool |
| 396 |
} |
| 397 |
|
| 398 |
func (fs *bindFs) Mount(ctx context.Context, mountpoint string, labels map[string]string) error { |
| 399 |
if _, ok := labels[brokenLabel]; ok { |
| 400 |
fs.broken[mountpoint] = true |
| 401 |
} |
| 402 |
if err := syscall.Mount(fs.root, mountpoint, "none", syscall.MS_BIND, ""); err != nil { |
| 403 |
fs.t.Fatalf("failed to bind mount %q to %q: %v", fs.root, mountpoint, err) |
| 404 |
} |
| 405 |
return nil |
| 406 |
} |
| 407 |
|
| 408 |
func (fs *bindFs) Check(ctx context.Context, mountpoint string, labels map[string]string) error { |
| 409 |
if fs.checkFailure { |
| 410 |
if broken, ok := fs.broken[mountpoint]; ok && broken { |
| 411 |
return fmt.Errorf("broken") |
| 412 |
} |
| 413 |
} |
| 414 |
return nil |
| 415 |
} |
| 416 |
|
| 417 |
func (fs *bindFs) Unmount(ctx context.Context, mountpoint string) error { |
| 418 |
return syscall.Unmount(mountpoint, 0) |
| 419 |
} |
| 420 |
|
| 421 |
func dummyFileSystem() FileSystem { return &dummyFs{} } |
| 422 |
|
| 423 |
type dummyFs struct{} |
| 424 |
|
| 425 |
func (fs *dummyFs) Mount(ctx context.Context, mountpoint string, labels map[string]string) error { |
| 426 |
return fmt.Errorf("dummy") |
| 427 |
} |
| 428 |
|
| 429 |
func (fs *dummyFs) Check(ctx context.Context, mountpoint string, labels map[string]string) error { |
| 430 |
return fmt.Errorf("dummy") |
| 431 |
} |
| 432 |
|
| 433 |
func (fs *dummyFs) Unmount(ctx context.Context, mountpoint string) error { |
| 434 |
return fmt.Errorf("dummy") |
| 435 |
} |
| 436 |
|
| 437 |
// ============================================================================= |
| 438 |
// Tests backword-comaptibility of overlayfs snapshotter. |
| 439 |
|
| 440 |
func newSnapshotter(ctx context.Context, root string) (snapshots.Snapshotter, func() error, error) { |
| 441 |
snapshotter, err := NewSnapshotter(context.TODO(), root, dummyFileSystem()) |
| 442 |
if err != nil { |
| 443 |
return nil, nil, err |
| 444 |
} |
| 445 |
|
| 446 |
return snapshotter, func() error { return snapshotter.Close() }, nil |
| 447 |
} |
| 448 |
|
| 449 |
func TestOverlay(t *testing.T) { |
| 450 |
testutil.RequiresRoot(t) |
| 451 |
testsuite.SnapshotterSuite(t, "Overlay", newSnapshotter) |
| 452 |
} |
| 453 |
|
| 454 |
func TestOverlayMounts(t *testing.T) { |
| 455 |
ctx := context.TODO() |
| 456 |
root, err := os.MkdirTemp("", "overlay") |
| 457 |
if err != nil { |
| 458 |
t.Fatal(err) |
| 459 |
} |
| 460 |
defer os.RemoveAll(root) |
| 461 |
o, _, err := newSnapshotter(ctx, root) |
| 462 |
if err != nil { |
| 463 |
t.Fatal(err) |
| 464 |
} |
| 465 |
mounts, err := o.Prepare(ctx, "/tmp/test", "") |
| 466 |
if err != nil { |
| 467 |
t.Fatal(err) |
| 468 |
} |
| 469 |
if len(mounts) != 1 { |
| 470 |
t.Errorf("should only have 1 mount but received %d", len(mounts)) |
| 471 |
} |
| 472 |
m := mounts[0] |
| 473 |
if m.Type != "bind" { |
| 474 |
t.Errorf("mount type should be bind but received %q", m.Type) |
| 475 |
} |
| 476 |
expected := filepath.Join(root, "snapshots", "1", "fs") |
| 477 |
if m.Source != expected { |
| 478 |
t.Errorf("expected source %q but received %q", expected, m.Source) |
| 479 |
} |
| 480 |
if m.Options[0] != "rw" { |
| 481 |
t.Errorf("expected mount option rw but received %q", m.Options[0]) |
| 482 |
} |
| 483 |
if m.Options[1] != "rbind" { |
| 484 |
t.Errorf("expected mount option rbind but received %q", m.Options[1]) |
| 485 |
} |
| 486 |
} |
| 487 |
|
| 488 |
func TestOverlayCommit(t *testing.T) { |
| 489 |
ctx := context.TODO() |
| 490 |
root, err := os.MkdirTemp("", "overlay") |
| 491 |
if err != nil { |
| 492 |
t.Fatal(err) |
| 493 |
} |
| 494 |
defer os.RemoveAll(root) |
| 495 |
o, _, err := newSnapshotter(ctx, root) |
| 496 |
if err != nil { |
| 497 |
t.Fatal(err) |
| 498 |
} |
| 499 |
key := "/tmp/test" |
| 500 |
mounts, err := o.Prepare(ctx, key, "") |
| 501 |
if err != nil { |
| 502 |
t.Fatal(err) |
| 503 |
} |
| 504 |
m := mounts[0] |
| 505 |
if err := os.WriteFile(filepath.Join(m.Source, "foo"), []byte("hi"), 0660); err != nil { |
| 506 |
t.Fatal(err) |
| 507 |
} |
| 508 |
if err := o.Commit(ctx, "base", key); err != nil { |
| 509 |
t.Fatal(err) |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
func TestOverlayOverlayMount(t *testing.T) { |
| 514 |
ctx := context.TODO() |
| 515 |
root, err := os.MkdirTemp("", "overlay") |
| 516 |
if err != nil { |
| 517 |
t.Fatal(err) |
| 518 |
} |
| 519 |
defer os.RemoveAll(root) |
| 520 |
o, _, err := newSnapshotter(ctx, root) |
| 521 |
if err != nil { |
| 522 |
t.Fatal(err) |
| 523 |
} |
| 524 |
key := "/tmp/test" |
| 525 |
if _, err = o.Prepare(ctx, key, ""); err != nil { |
| 526 |
t.Fatal(err) |
| 527 |
} |
| 528 |
if err := o.Commit(ctx, "base", key); err != nil { |
| 529 |
t.Fatal(err) |
| 530 |
} |
| 531 |
var mounts []mount.Mount |
| 532 |
if mounts, err = o.Prepare(ctx, "/tmp/layer2", "base"); err != nil { |
| 533 |
t.Fatal(err) |
| 534 |
} |
| 535 |
if len(mounts) != 1 { |
| 536 |
t.Errorf("should only have 1 mount but received %d", len(mounts)) |
| 537 |
} |
| 538 |
m := mounts[0] |
| 539 |
if m.Type != "overlay" { |
| 540 |
t.Errorf("mount type should be overlay but received %q", m.Type) |
| 541 |
} |
| 542 |
if m.Source != "overlay" { |
| 543 |
t.Errorf("expected source %q but received %q", "overlay", m.Source) |
| 544 |
} |
| 545 |
var ( |
| 546 |
bp = getBasePath(ctx, o, root, "/tmp/layer2") |
| 547 |
work = "workdir=" + filepath.Join(bp, "work") |
| 548 |
upper = "upperdir=" + filepath.Join(bp, "fs") |
| 549 |
lower = "lowerdir=" + getParents(ctx, o, root, "/tmp/layer2")[0] |
| 550 |
) |
| 551 |
for i, v := range []string{ |
| 552 |
work, |
| 553 |
upper, |
| 554 |
lower, |
| 555 |
} { |
| 556 |
if m.Options[i] != v { |
| 557 |
t.Errorf("expected %q but received %q", v, m.Options[i]) |
| 558 |
} |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
func getBasePath(ctx context.Context, sn snapshots.Snapshotter, root, key string) string { |
| 563 |
o := sn.(*snapshotter) |
| 564 |
ctx, t, err := o.ms.TransactionContext(ctx, false) |
| 565 |
if err != nil { |
| 566 |
panic(err) |
| 567 |
} |
| 568 |
defer t.Rollback() |
| 569 |
|
| 570 |
s, err := storage.GetSnapshot(ctx, key) |
| 571 |
if err != nil { |
| 572 |
panic(err) |
| 573 |
} |
| 574 |
|
| 575 |
return filepath.Join(root, "snapshots", s.ID) |
| 576 |
} |
| 577 |
|
| 578 |
func getParents(ctx context.Context, sn snapshots.Snapshotter, root, key string) []string { |
| 579 |
o := sn.(*snapshotter) |
| 580 |
ctx, t, err := o.ms.TransactionContext(ctx, false) |
| 581 |
if err != nil { |
| 582 |
panic(err) |
| 583 |
} |
| 584 |
defer t.Rollback() |
| 585 |
s, err := storage.GetSnapshot(ctx, key) |
| 586 |
if err != nil { |
| 587 |
panic(err) |
| 588 |
} |
| 589 |
parents := make([]string, len(s.ParentIDs)) |
| 590 |
for i := range s.ParentIDs { |
| 591 |
parents[i] = filepath.Join(root, "snapshots", s.ParentIDs[i], "fs") |
| 592 |
} |
| 593 |
return parents |
| 594 |
} |
| 595 |
|
| 596 |
func TestOverlayOverlayRead(t *testing.T) { |
| 597 |
testutil.RequiresRoot(t) |
| 598 |
ctx := context.TODO() |
| 599 |
root, err := os.MkdirTemp("", "overlay") |
| 600 |
if err != nil { |
| 601 |
t.Fatal(err) |
| 602 |
} |
| 603 |
defer os.RemoveAll(root) |
| 604 |
o, _, err := newSnapshotter(ctx, root) |
| 605 |
if err != nil { |
| 606 |
t.Fatal(err) |
| 607 |
} |
| 608 |
key := "/tmp/test" |
| 609 |
mounts, err := o.Prepare(ctx, key, "") |
| 610 |
if err != nil { |
| 611 |
t.Fatal(err) |
| 612 |
} |
| 613 |
m := mounts[0] |
| 614 |
if err := os.WriteFile(filepath.Join(m.Source, "foo"), []byte("hi"), 0660); err != nil { |
| 615 |
t.Fatal(err) |
| 616 |
} |
| 617 |
if err := o.Commit(ctx, "base", key); err != nil { |
| 618 |
t.Fatal(err) |
| 619 |
} |
| 620 |
if mounts, err = o.Prepare(ctx, "/tmp/layer2", "base"); err != nil { |
| 621 |
t.Fatal(err) |
| 622 |
} |
| 623 |
dest := filepath.Join(root, "dest") |
| 624 |
if err := os.Mkdir(dest, 0700); err != nil { |
| 625 |
t.Fatal(err) |
| 626 |
} |
| 627 |
if err := mount.All(mounts, dest); err != nil { |
| 628 |
t.Fatal(err) |
| 629 |
} |
| 630 |
defer syscall.Unmount(dest, 0) |
| 631 |
data, err := os.ReadFile(filepath.Join(dest, "foo")) |
| 632 |
if err != nil { |
| 633 |
t.Fatal(err) |
| 634 |
} |
| 635 |
if e := string(data); e != "hi" { |
| 636 |
t.Fatalf("expected file contents hi but got %q", e) |
| 637 |
} |
| 638 |
} |
| 639 |
|
| 640 |
func TestOverlayView(t *testing.T) { |
| 641 |
ctx := context.TODO() |
| 642 |
root, err := os.MkdirTemp("", "overlay") |
| 643 |
if err != nil { |
| 644 |
t.Fatal(err) |
| 645 |
} |
| 646 |
defer os.RemoveAll(root) |
| 647 |
o, _, err := newSnapshotter(ctx, root) |
| 648 |
if err != nil { |
| 649 |
t.Fatal(err) |
| 650 |
} |
| 651 |
key := "/tmp/base" |
| 652 |
mounts, err := o.Prepare(ctx, key, "") |
| 653 |
if err != nil { |
| 654 |
t.Fatal(err) |
| 655 |
} |
| 656 |
m := mounts[0] |
| 657 |
if err := os.WriteFile(filepath.Join(m.Source, "foo"), []byte("hi"), 0660); err != nil { |
| 658 |
t.Fatal(err) |
| 659 |
} |
| 660 |
if err := o.Commit(ctx, "base", key); err != nil { |
| 661 |
t.Fatal(err) |
| 662 |
} |
| 663 |
|
| 664 |
key = "/tmp/top" |
| 665 |
_, err = o.Prepare(ctx, key, "base") |
| 666 |
if err != nil { |
| 667 |
t.Fatal(err) |
| 668 |
} |
| 669 |
if err := os.WriteFile(filepath.Join(getParents(ctx, o, root, "/tmp/top")[0], "foo"), []byte("hi, again"), 0660); err != nil { |
| 670 |
t.Fatal(err) |
| 671 |
} |
| 672 |
if err := o.Commit(ctx, "top", key); err != nil { |
| 673 |
t.Fatal(err) |
| 674 |
} |
| 675 |
|
| 676 |
mounts, err = o.View(ctx, "/tmp/view1", "base") |
| 677 |
if err != nil { |
| 678 |
t.Fatal(err) |
| 679 |
} |
| 680 |
if len(mounts) != 1 { |
| 681 |
t.Fatalf("should only have 1 mount but received %d", len(mounts)) |
| 682 |
} |
| 683 |
m = mounts[0] |
| 684 |
if m.Type != "bind" { |
| 685 |
t.Errorf("mount type should be bind but received %q", m.Type) |
| 686 |
} |
| 687 |
expected := getParents(ctx, o, root, "/tmp/view1")[0] |
| 688 |
if m.Source != expected { |
| 689 |
t.Errorf("expected source %q but received %q", expected, m.Source) |
| 690 |
} |
| 691 |
if m.Options[0] != "ro" { |
| 692 |
t.Errorf("expected mount option ro but received %q", m.Options[0]) |
| 693 |
} |
| 694 |
if m.Options[1] != "rbind" { |
| 695 |
t.Errorf("expected mount option rbind but received %q", m.Options[1]) |
| 696 |
} |
| 697 |
|
| 698 |
mounts, err = o.View(ctx, "/tmp/view2", "top") |
| 699 |
if err != nil { |
| 700 |
t.Fatal(err) |
| 701 |
} |
| 702 |
if len(mounts) != 1 { |
| 703 |
t.Fatalf("should only have 1 mount but received %d", len(mounts)) |
| 704 |
} |
| 705 |
m = mounts[0] |
| 706 |
if m.Type != "overlay" { |
| 707 |
t.Errorf("mount type should be overlay but received %q", m.Type) |
| 708 |
} |
| 709 |
if m.Source != "overlay" { |
| 710 |
t.Errorf("mount source should be overlay but received %q", m.Source) |
| 711 |
} |
| 712 |
if len(m.Options) != 1 { |
| 713 |
t.Errorf("expected 1 mount option but got %d", len(m.Options)) |
| 714 |
} |
| 715 |
lowers := getParents(ctx, o, root, "/tmp/view2") |
| 716 |
expected = fmt.Sprintf("lowerdir=%s:%s", lowers[0], lowers[1]) |
| 717 |
if m.Options[0] != expected { |
| 718 |
t.Errorf("expected option %q but received %q", expected, m.Options[0]) |
| 719 |
} |
| 720 |
} |