From 969a70f798b9bb5aaa7a81223f6d2264d586a553 Mon Sep 17 00:00:00 2001 From: Steve Francia Date: Wed, 13 Jan 2016 12:41:35 -0500 Subject: [PATCH] test CopyOnWriteFs with OsFs --- composite_test.go | 107 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) diff --git a/composite_test.go b/composite_test.go index e8d7be6..20ebc05 100644 --- a/composite_test.go +++ b/composite_test.go @@ -5,8 +5,11 @@ import ( "os" "testing" "time" + "fmt" ) +var tempDirs []string + func TestUnionCreateExisting(t *testing.T) { base := &MemMapFs{} roBase := &ReadOnlyFs{source: base} @@ -82,6 +85,110 @@ func TestUnionMergeReaddir(t *testing.T) { } } +func TestExistingDirectoryCollisionReaddir(t *testing.T) { + base := &MemMapFs{} + roBase := &ReadOnlyFs{source: base} + overlay := &MemMapFs{} + + ufs := &CopyOnWriteFs{base: roBase, layer: overlay} + + base.MkdirAll("/home/test", 0777) + fh, _ := base.Create("/home/test/file.txt") + fh.WriteString("This is a test") + fh.Close() + + overlay.MkdirAll("home/test", 0777) + fh, _ = overlay.Create("/home/test/file2.txt") + fh.WriteString("This is a test") + fh.Close() + + fh, _ = ufs.Create("/home/test/file3.txt") + fh.WriteString("This is a test") + fh.Close() + + fh, _ = ufs.Open("/home/test") + files, err := fh.Readdirnames(-1) + if err != nil { + t.Errorf("Readdirnames failed") + } + if len(files) != 3 { + t.Errorf("Got wrong number of files in union: %v", files) + } + + fh, _ = overlay.Open("/home/test") + files, err = fh.Readdirnames(-1) + if err != nil { + t.Errorf("Readdirnames failed") + } + if len(files) != 2 { + t.Errorf("Got wrong number of files in overlay: %v", files) + } +} + +func NewTempOsBaseFs(t *testing.T) Fs { + name, err := TempDir(NewOsFs(), "", "") + if err != nil { + t.Error("error creating tempDir", err) + } + + fmt.Println("created tempdir", name) + tempDirs = append(tempDirs, name) + + return NewBasePathFs(NewOsFs(), name) +} + +func CleanupTempDirs() { + osfs := NewOsFs() + for _, x := range tempDirs { + err := osfs.RemoveAll(x) + if err != nil { + fmt.Println("error removing tempDir", x, err) + } + } + tempDirs = []string{} +} + +func TestCopyOnWriteFsWithOsFs(t *testing.T) { + defer CleanupTempDirs() + base := NewTempOsBaseFs(t) + roBase := &ReadOnlyFs{source: base} + overlay := NewTempOsBaseFs(t) + + ufs := &CopyOnWriteFs{base: roBase, layer: overlay} + + base.MkdirAll("/home/test", 0777) + fh, _ := base.Create("/home/test/file.txt") + fh.WriteString("This is a test") + fh.Close() + + overlay.MkdirAll("home/test", 0777) + fh, _ = overlay.Create("/home/test/file2.txt") + fh.WriteString("This is a test") + fh.Close() + + fh, _ = ufs.Create("/home/test/file3.txt") + fh.WriteString("This is a test") + fh.Close() + + fh, _ = ufs.Open("/home/test") + files, err := fh.Readdirnames(-1) + if err != nil { + t.Errorf("Readdirnames failed") + } + if len(files) != 3 { + t.Errorf("Got wrong number of files in union: %v", files) + } + + fh, _ = overlay.Open("/home/test") + files, err = fh.Readdirnames(-1) + if err != nil { + t.Errorf("Readdirnames failed") + } + if len(files) != 2 { + t.Errorf("Got wrong number of files in overlay: %v", files) + } +} + func TestUnionCacheWrite(t *testing.T) { base := &MemMapFs{} layer := &MemMapFs{}