Example #1
0
func TestSnapshotNaiveBasic(t *testing.T) {
	tmpDir, err := ioutil.TempDir("", "test-layman-")
	if err != nil {
		t.Fatal(err)
	}
	// defer os.RemoveAll(tmpDir)

	t.Log(tmpDir)
	root := filepath.Join(tmpDir, "root")

	lm, err := NewNaive(root)
	if err != nil {
		t.Fatal(err)
	}

	preparing := filepath.Join(tmpDir, "preparing")
	if err := os.MkdirAll(preparing, 0777); err != nil {
		t.Fatal(err)
	}

	mounts, err := lm.Prepare(preparing, "")
	if err != nil {
		t.Fatal(err)
	}

	for _, mount := range mounts {
		if !strings.HasPrefix(mount.Target, preparing) {
			t.Fatalf("expected mount target to be prefixed with tmpDir: %q does not startwith %q", mount.Target, preparing)
		}

		t.Log(containerd.MountCommand(mount))
	}

	if err := containerd.MountAll(mounts...); err != nil {
		t.Fatal(err)
	}

	if err := ioutil.WriteFile(filepath.Join(preparing, "foo"), []byte("foo\n"), 0777); err != nil {
		t.Fatal(err)
	}

	os.MkdirAll(preparing+"/a/b/c", 0755)

	// defer os.Remove(filepath.Join(tmpDir, "foo"))

	committed := filepath.Join(lm.root, "committed")

	if err := lm.Commit(committed, preparing); err != nil {
		t.Fatal(err)
	}

	if lm.Parent(preparing) != "" {
		t.Fatalf("parent of new layer should be empty, got lm.Parent(%q) == %q", preparing, lm.Parent(preparing))
	}

	next := filepath.Join(tmpDir, "nextlayer")
	if err := os.MkdirAll(next, 0777); err != nil {
		t.Fatal(err)
	}

	mounts, err = lm.Prepare(next, committed)
	if err != nil {
		t.Fatal(err)
	}
	if err := containerd.MountAll(mounts...); err != nil {
		t.Fatal(err)
	}

	for _, mount := range mounts {
		if !strings.HasPrefix(mount.Target, next) {
			t.Fatalf("expected mount target to be prefixed with tmpDir: %q does not startwith %q", mount.Target, next)
		}

		t.Log(containerd.MountCommand(mount))
	}

	if err := ioutil.WriteFile(filepath.Join(next, "bar"), []byte("bar\n"), 0777); err != nil {
		t.Fatal(err)
	}

	// also, change content of foo to bar
	if err := ioutil.WriteFile(filepath.Join(next, "foo"), []byte("bar\n"), 0777); err != nil {
		t.Fatal(err)
	}

	os.RemoveAll(next + "/a/b")
	nextCommitted := filepath.Join(lm.root, "committed-next")
	if err := lm.Commit(nextCommitted, next); err != nil {
		t.Fatal(err)
	}

	if lm.Parent(nextCommitted) != committed {
		t.Fatalf("parent of new layer should be %q, got lm.Parent(%q) == %q (%#v)", committed, next, lm.Parent(next), lm.parents)
	}
}
Example #2
0
func TestBtrfs(t *testing.T) {
	// SORRY(stevvooe): This is where I mount a btrfs loopback. We can probably
	// set this up as part of the test.
	root, err := ioutil.TempDir("/tmp/snapshots", "TestBtrfsPrepare-")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(root)
	// TODO(stevvooe): Cleanup subvolumes

	sm, err := NewBtrfs("/dev/loop0", root)
	if err != nil {
		t.Fatal(err)
	}
	mounts, err := sm.Prepare(filepath.Join(root, "test"), "")
	if err != nil {
		t.Fatal(err)
	}
	t.Log(mounts)

	for _, mount := range mounts {
		if mount.Type != "btrfs" {
			t.Fatal("wrong mount type: %v != btrfs", mount.Type)
		}

		// assumes the first, maybe incorrect in the future
		if !strings.HasPrefix(mount.Options[0], "subvolid=") {
			t.Fatal("no subvolid option in %v", mount.Options)
		}
	}

	if err := os.MkdirAll(mounts[0].Target, 0755); err != nil {
		t.Fatal(err)
	}
	if err := containerd.MountAll(mounts...); err != nil {
		t.Fatal(err)
	}

	// write in some data
	if err := ioutil.WriteFile(filepath.Join(mounts[0].Target, "foo"), []byte("content"), 0777); err != nil {
		t.Fatal(err)
	}

	// TODO(stevvooe): We don't really make this with the driver, but that
	// might prove annoying in practice.
	if err := os.MkdirAll(filepath.Join(root, "snapshots"), 0755); err != nil {
		t.Fatal(err)
	}

	if err := sm.Commit(filepath.Join(root, "snapshots/committed"), filepath.Join(root, "test")); err != nil {
		t.Fatal(err)
	}

	mounts, err = sm.Prepare(filepath.Join(root, "test2"), filepath.Join(root, "snapshots/committed"))
	if err != nil {
		t.Fatal(err)
	}

	if err := os.MkdirAll(filepath.Join(root, "test2"), 0755); err != nil {
		t.Fatal(err)
	}

	if err := containerd.MountAll(mounts...); err != nil {
		t.Fatal(err)
	}

	// TODO(stevvooe): Verify contents of "foo"
	if err := ioutil.WriteFile(filepath.Join(mounts[0].Target, "bar"), []byte("content"), 0777); err != nil {
		t.Fatal(err)
	}

	if err := sm.Commit(filepath.Join(root, "snapshots/committed2"), filepath.Join(root, "test2")); err != nil {
		t.Fatal(err)
	}
}