github.com/rentongzhang/docker@v1.8.2-rc1/pkg/directory/directory_test.go (about) 1 package directory 2 3 import ( 4 "io/ioutil" 5 "os" 6 "testing" 7 ) 8 9 // Size of an empty directory should be 0 10 func TestSizeEmpty(t *testing.T) { 11 var dir string 12 var err error 13 if dir, err = ioutil.TempDir(os.TempDir(), "testSizeEmptyDirectory"); err != nil { 14 t.Fatalf("failed to create directory: %s", err) 15 } 16 17 var size int64 18 if size, _ = Size(dir); size != 0 { 19 t.Fatalf("empty directory has size: %d", size) 20 } 21 } 22 23 // Size of a directory with one empty file should be 0 24 func TestSizeEmptyFile(t *testing.T) { 25 var dir string 26 var err error 27 if dir, err = ioutil.TempDir(os.TempDir(), "testSizeEmptyFile"); err != nil { 28 t.Fatalf("failed to create directory: %s", err) 29 } 30 31 var file *os.File 32 if file, err = ioutil.TempFile(dir, "file"); err != nil { 33 t.Fatalf("failed to create file: %s", err) 34 } 35 36 var size int64 37 if size, _ = Size(file.Name()); size != 0 { 38 t.Fatalf("directory with one file has size: %d", size) 39 } 40 } 41 42 // Size of a directory with one 5-byte file should be 5 43 func TestSizeNonemptyFile(t *testing.T) { 44 var dir string 45 var err error 46 if dir, err = ioutil.TempDir(os.TempDir(), "testSizeNonemptyFile"); err != nil { 47 t.Fatalf("failed to create directory: %s", err) 48 } 49 50 var file *os.File 51 if file, err = ioutil.TempFile(dir, "file"); err != nil { 52 t.Fatalf("failed to create file: %s", err) 53 } 54 55 d := []byte{97, 98, 99, 100, 101} 56 file.Write(d) 57 58 var size int64 59 if size, _ = Size(file.Name()); size != 5 { 60 t.Fatalf("directory with one 5-byte file has size: %d", size) 61 } 62 } 63 64 // Size of a directory with one empty directory should be 0 65 func TestSizeNestedDirectoryEmpty(t *testing.T) { 66 var dir string 67 var err error 68 if dir, err = ioutil.TempDir(os.TempDir(), "testSizeNestedDirectoryEmpty"); err != nil { 69 t.Fatalf("failed to create directory: %s", err) 70 } 71 if dir, err = ioutil.TempDir(dir, "nested"); err != nil { 72 t.Fatalf("failed to create nested directory: %s", err) 73 } 74 75 var size int64 76 if size, _ = Size(dir); size != 0 { 77 t.Fatalf("directory with one empty directory has size: %d", size) 78 } 79 } 80 81 // Test directory with 1 file and 1 empty directory 82 func TestSizeFileAndNestedDirectoryEmpty(t *testing.T) { 83 var dir string 84 var err error 85 if dir, err = ioutil.TempDir(os.TempDir(), "testSizeFileAndNestedDirectoryEmpty"); err != nil { 86 t.Fatalf("failed to create directory: %s", err) 87 } 88 if dir, err = ioutil.TempDir(dir, "nested"); err != nil { 89 t.Fatalf("failed to create nested directory: %s", err) 90 } 91 92 var file *os.File 93 if file, err = ioutil.TempFile(dir, "file"); err != nil { 94 t.Fatalf("failed to create file: %s", err) 95 } 96 97 d := []byte{100, 111, 99, 107, 101, 114} 98 file.Write(d) 99 100 var size int64 101 if size, _ = Size(dir); size != 6 { 102 t.Fatalf("directory with 6-byte file and empty directory has size: %d", size) 103 } 104 } 105 106 // Test directory with 1 file and 1 non-empty directory 107 func TestSizeFileAndNestedDirectoryNonempty(t *testing.T) { 108 var dir, dirNested string 109 var err error 110 if dir, err = ioutil.TempDir(os.TempDir(), "TestSizeFileAndNestedDirectoryNonempty"); err != nil { 111 t.Fatalf("failed to create directory: %s", err) 112 } 113 if dirNested, err = ioutil.TempDir(dir, "nested"); err != nil { 114 t.Fatalf("failed to create nested directory: %s", err) 115 } 116 117 var file *os.File 118 if file, err = ioutil.TempFile(dir, "file"); err != nil { 119 t.Fatalf("failed to create file: %s", err) 120 } 121 122 data := []byte{100, 111, 99, 107, 101, 114} 123 file.Write(data) 124 125 var nestedFile *os.File 126 if nestedFile, err = ioutil.TempFile(dirNested, "file"); err != nil { 127 t.Fatalf("failed to create file in nested directory: %s", err) 128 } 129 130 nestedData := []byte{100, 111, 99, 107, 101, 114} 131 nestedFile.Write(nestedData) 132 133 var size int64 134 if size, _ = Size(dir); size != 12 { 135 t.Fatalf("directory with 6-byte file and nested directory with 6-byte file has size: %d", size) 136 } 137 }