2016-11-29 02:58:25 +00:00
|
|
|
package snapshot
|
2016-09-27 04:35:34 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2016-12-08 03:59:03 +00:00
|
|
|
|
|
|
|
"github.com/docker/containerd"
|
2017-01-13 23:31:21 +00:00
|
|
|
"github.com/docker/containerd/snapshot/testutil"
|
2016-09-27 04:35:34 +00:00
|
|
|
)
|
|
|
|
|
2016-11-29 02:58:25 +00:00
|
|
|
// TestSnapshotManagerBasic implements something similar to the conceptual
|
2016-09-27 04:35:34 +00:00
|
|
|
// examples we've discussed thus far. It does perform mounts, so you must run
|
|
|
|
// as root.
|
2016-11-29 02:58:25 +00:00
|
|
|
func TestSnapshotManagerBasic(t *testing.T) {
|
2017-01-17 08:42:27 +00:00
|
|
|
tmpDir, err := ioutil.TempDir("", "test-sm-")
|
2016-09-27 04:35:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-01-12 01:08:00 +00:00
|
|
|
defer func() {
|
|
|
|
t.Log("Removing", tmpDir)
|
|
|
|
err := os.RemoveAll(tmpDir)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
2016-09-27 04:35:34 +00:00
|
|
|
|
|
|
|
root := filepath.Join(tmpDir, "root")
|
|
|
|
|
2017-01-17 08:42:27 +00:00
|
|
|
sm, err := NewManager(root)
|
2016-09-27 04:35:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
preparing := filepath.Join(tmpDir, "preparing")
|
|
|
|
if err := os.MkdirAll(preparing, 0777); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-01-17 08:42:27 +00:00
|
|
|
mounts, err := sm.Prepare(preparing, "")
|
2016-09-27 04:35:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(mounts) < 1 {
|
|
|
|
t.Fatal("expected mounts to have entries")
|
|
|
|
}
|
|
|
|
|
2017-01-17 21:26:56 +00:00
|
|
|
if err := containerd.MountAll(mounts, preparing); err != nil {
|
2016-09-27 04:35:34 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-01-17 21:26:56 +00:00
|
|
|
defer testutil.Unmount(t, preparing)
|
2016-09-27 04:35:34 +00:00
|
|
|
|
|
|
|
if err := ioutil.WriteFile(filepath.Join(preparing, "foo"), []byte("foo\n"), 0777); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
os.MkdirAll(preparing+"/a/b/c", 0755)
|
|
|
|
|
2017-01-17 08:42:27 +00:00
|
|
|
committed := filepath.Join(sm.root, "committed")
|
2016-09-27 04:35:34 +00:00
|
|
|
|
2017-01-17 08:42:27 +00:00
|
|
|
if err := sm.Commit(committed, preparing); err != nil {
|
2016-09-27 04:35:34 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-01-17 08:42:27 +00:00
|
|
|
if sm.Parent(preparing) != "" {
|
|
|
|
t.Fatalf("parent of new layer should be empty, got sm.Parent(%q) == %q", preparing, sm.Parent(preparing))
|
2016-09-27 04:35:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
next := filepath.Join(tmpDir, "nextlayer")
|
|
|
|
if err := os.MkdirAll(next, 0777); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-01-17 08:42:27 +00:00
|
|
|
mounts, err = sm.Prepare(next, committed)
|
2016-09-27 04:35:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-01-17 21:26:56 +00:00
|
|
|
if err := containerd.MountAll(mounts, next); err != nil {
|
2016-09-27 04:35:34 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-01-17 21:26:56 +00:00
|
|
|
defer testutil.Unmount(t, next)
|
2016-09-27 04:35:34 +00:00
|
|
|
|
|
|
|
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")
|
2017-01-17 08:42:27 +00:00
|
|
|
nextCommitted := filepath.Join(sm.root, "committed-next")
|
|
|
|
if err := sm.Commit(nextCommitted, next); err != nil {
|
2016-09-27 04:35:34 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-01-17 08:42:27 +00:00
|
|
|
if sm.Parent(nextCommitted) != committed {
|
|
|
|
t.Fatalf("parent of new layer should be %q, got sm.Parent(%q) == %q (%#v)", committed, next, sm.Parent(next), sm.parents)
|
2016-09-27 04:35:34 +00:00
|
|
|
}
|
|
|
|
}
|