snapshotter: add more assertion

Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
This commit is contained in:
Akihiro Suda 2017-02-15 06:50:43 +00:00
parent a7ef3e5313
commit 750cdc951f
5 changed files with 92 additions and 39 deletions

View file

@ -33,7 +33,7 @@ func TestSimpleDiff(t *testing.T) {
fstest.CreateFile("/etc/profile", []byte("PATH=/usr/bin"), 0666),
fstest.CreateDir("/root", 0700),
fstest.CreateFile("/root/.bashrc", []byte("PATH=/usr/sbin:/usr/bin"), 0644),
fstest.RemoveFile("/etc/unexpected"),
fstest.Remove("/etc/unexpected"),
)
diff := []testChange{
Modify("/etc/hosts"),
@ -57,7 +57,7 @@ func TestDirectoryReplace(t *testing.T) {
)
l2 := fstest.Apply(
fstest.CreateFile("/dir1/f11", []byte("#New file here"), 0644),
fstest.RemoveFile("/dir1/f2"),
fstest.RemoveAll("/dir1/f2"),
fstest.CreateFile("/dir1/f2", []byte("Now file"), 0666),
)
diff := []testChange{
@ -77,7 +77,7 @@ func TestRemoveDirectoryTree(t *testing.T) {
fstest.CreateFile("/dir1/dir2/f2", []byte("f2"), 0644),
)
l2 := fstest.Apply(
fstest.RemoveFile("/dir1"),
fstest.RemoveAll("/dir1"),
)
diff := []testChange{
Delete("/dir1"),
@ -93,7 +93,7 @@ func TestFileReplace(t *testing.T) {
fstest.CreateFile("/dir1", []byte("a file, not a directory"), 0644),
)
l2 := fstest.Apply(
fstest.RemoveFile("/dir1"),
fstest.Remove("/dir1"),
fstest.CreateDir("/dir1/dir2", 0755),
fstest.CreateFile("/dir1/dir2/f1", []byte("also a file"), 0644),
)

View file

@ -1,6 +1,9 @@
package fstest
import (
"io/ioutil"
"os"
"github.com/pkg/errors"
"github.com/stevvooe/continuity"
)
@ -35,3 +38,16 @@ func CheckDirectoryEqual(d1, d2 string) error {
return nil
}
// CheckDirectoryEqualWithApplier compares directory against applier
func CheckDirectoryEqualWithApplier(root string, a Applier) error {
applied, err := ioutil.TempDir("", "fstest")
if err != nil {
return err
}
defer os.RemoveAll(applied)
if err := a.Apply(applied); err != nil {
return err
}
return CheckDirectoryEqual(applied, root)
}

View file

@ -37,8 +37,16 @@ func CreateFile(name string, content []byte, perm os.FileMode) Applier {
})
}
// RemoveFile returns a file applier which removes the provided file name
func RemoveFile(name string) Applier {
// Remove returns a file applier which removes the provided file name
func Remove(name string) Applier {
return applyFn(func(root string) error {
return os.Remove(filepath.Join(root, name))
})
}
// RemoveAll returns a file applier which removes the provided file name
// as in os.RemoveAll
func RemoveAll(name string) Applier {
return applyFn(func(root string) error {
return os.RemoveAll(filepath.Join(root, name))
})