2017-01-13 23:31:21 +00:00
|
|
|
package naive
|
2016-12-08 03:59:34 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2017-04-03 20:14:15 +00:00
|
|
|
"github.com/containerd/containerd"
|
|
|
|
"github.com/containerd/containerd/testutil"
|
2016-12-08 03:59:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSnapshotNaiveBasic(t *testing.T) {
|
2017-01-25 00:10:48 +00:00
|
|
|
testutil.RequiresRoot(t)
|
2017-01-17 08:42:27 +00:00
|
|
|
tmpDir, err := ioutil.TempDir("", "test-naive-")
|
2016-12-08 03:59:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-01-12 01:08:00 +00:00
|
|
|
defer os.RemoveAll(tmpDir)
|
2016-12-08 03:59:34 +00:00
|
|
|
|
|
|
|
t.Log(tmpDir)
|
|
|
|
root := filepath.Join(tmpDir, "root")
|
|
|
|
|
2017-01-17 08:42:27 +00:00
|
|
|
n, err := NewNaive(root)
|
2016-12-08 03:59: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 := n.Prepare(preparing, "")
|
2016-12-08 03:59:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-01-17 21:26:56 +00:00
|
|
|
if err := containerd.MountAll(mounts, preparing); err != nil {
|
2016-12-08 03:59:34 +00:00
|
|
|
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"))
|
|
|
|
|
2017-01-17 08:42:27 +00:00
|
|
|
committed := filepath.Join(n.root, "committed")
|
2016-12-08 03:59:34 +00:00
|
|
|
|
2017-01-17 08:42:27 +00:00
|
|
|
if err := n.Commit(committed, preparing); err != nil {
|
2016-12-08 03:59:34 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-01-17 08:42:27 +00:00
|
|
|
if n.Parent(preparing) != "" {
|
|
|
|
t.Fatalf("parent of new layer should be empty, got n.Parent(%q) == %q", preparing, n.Parent(preparing))
|
2016-12-08 03:59: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 = n.Prepare(next, committed)
|
2016-12-08 03:59: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-12-08 03:59:34 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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(n.root, "committed-next")
|
|
|
|
if err := n.Commit(nextCommitted, next); err != nil {
|
2016-12-08 03:59:34 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-01-17 08:42:27 +00:00
|
|
|
if n.Parent(nextCommitted) != committed {
|
|
|
|
t.Fatalf("parent of new layer should be %q, got n.Parent(%q) == %q (%#v)", committed, next, n.Parent(next), n.parents)
|
2016-12-08 03:59:34 +00:00
|
|
|
}
|
|
|
|
}
|