2018-02-05 16:05:59 -05:00
|
|
|
package directory // import "github.com/docker/docker/pkg/directory"
|
2015-02-27 13:50:55 -05:00
|
|
|
|
|
|
|
import (
|
2018-03-29 11:34:58 -04:00
|
|
|
"context"
|
2015-02-27 13:50:55 -05:00
|
|
|
"os"
|
2015-10-08 11:51:41 -04:00
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
|
|
|
"sort"
|
2015-02-27 13:50:55 -05:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Size of an empty directory should be 0
|
|
|
|
func TestSizeEmpty(t *testing.T) {
|
2015-03-12 10:16:16 -04:00
|
|
|
var dir string
|
2015-02-27 13:50:55 -05:00
|
|
|
var err error
|
2021-08-24 06:10:50 -04:00
|
|
|
if dir, err = os.MkdirTemp(os.TempDir(), "testSizeEmptyDirectory"); err != nil {
|
2015-02-27 13:50:55 -05:00
|
|
|
t.Fatalf("failed to create directory: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var size int64
|
2018-03-29 11:34:58 -04:00
|
|
|
if size, _ = Size(context.Background(), dir); size != 0 {
|
2015-02-27 13:50:55 -05:00
|
|
|
t.Fatalf("empty directory has size: %d", size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size of a directory with one empty file should be 0
|
|
|
|
func TestSizeEmptyFile(t *testing.T) {
|
2015-03-12 10:16:16 -04:00
|
|
|
var dir string
|
2015-02-27 13:50:55 -05:00
|
|
|
var err error
|
2021-08-24 06:10:50 -04:00
|
|
|
if dir, err = os.MkdirTemp(os.TempDir(), "testSizeEmptyFile"); err != nil {
|
2015-02-27 13:50:55 -05:00
|
|
|
t.Fatalf("failed to create directory: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-03-12 10:16:16 -04:00
|
|
|
var file *os.File
|
2021-08-24 06:10:50 -04:00
|
|
|
if file, err = os.CreateTemp(dir, "file"); err != nil {
|
2015-02-27 13:50:55 -05:00
|
|
|
t.Fatalf("failed to create file: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var size int64
|
2018-03-29 11:34:58 -04:00
|
|
|
if size, _ = Size(context.Background(), file.Name()); size != 0 {
|
2015-02-27 13:50:55 -05:00
|
|
|
t.Fatalf("directory with one file has size: %d", size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size of a directory with one 5-byte file should be 5
|
|
|
|
func TestSizeNonemptyFile(t *testing.T) {
|
2015-03-12 10:16:16 -04:00
|
|
|
var dir string
|
2015-02-27 13:50:55 -05:00
|
|
|
var err error
|
2021-08-24 06:10:50 -04:00
|
|
|
if dir, err = os.MkdirTemp(os.TempDir(), "testSizeNonemptyFile"); err != nil {
|
2015-02-27 13:50:55 -05:00
|
|
|
t.Fatalf("failed to create directory: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var file *os.File
|
2021-08-24 06:10:50 -04:00
|
|
|
if file, err = os.CreateTemp(dir, "file"); err != nil {
|
2015-02-27 13:50:55 -05:00
|
|
|
t.Fatalf("failed to create file: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d := []byte{97, 98, 99, 100, 101}
|
|
|
|
file.Write(d)
|
|
|
|
|
|
|
|
var size int64
|
2018-03-29 11:34:58 -04:00
|
|
|
if size, _ = Size(context.Background(), file.Name()); size != 5 {
|
2015-02-27 13:50:55 -05:00
|
|
|
t.Fatalf("directory with one 5-byte file has size: %d", size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size of a directory with one empty directory should be 0
|
|
|
|
func TestSizeNestedDirectoryEmpty(t *testing.T) {
|
2015-03-12 10:16:16 -04:00
|
|
|
var dir string
|
2015-02-27 13:50:55 -05:00
|
|
|
var err error
|
2021-08-24 06:10:50 -04:00
|
|
|
if dir, err = os.MkdirTemp(os.TempDir(), "testSizeNestedDirectoryEmpty"); err != nil {
|
2015-02-27 13:50:55 -05:00
|
|
|
t.Fatalf("failed to create directory: %s", err)
|
|
|
|
}
|
2021-08-24 06:10:50 -04:00
|
|
|
if dir, err = os.MkdirTemp(dir, "nested"); err != nil {
|
2015-03-12 10:16:16 -04:00
|
|
|
t.Fatalf("failed to create nested directory: %s", err)
|
|
|
|
}
|
2015-02-27 13:50:55 -05:00
|
|
|
|
|
|
|
var size int64
|
2018-03-29 11:34:58 -04:00
|
|
|
if size, _ = Size(context.Background(), dir); size != 0 {
|
2015-02-27 13:50:55 -05:00
|
|
|
t.Fatalf("directory with one empty directory has size: %d", size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test directory with 1 file and 1 empty directory
|
|
|
|
func TestSizeFileAndNestedDirectoryEmpty(t *testing.T) {
|
2015-03-12 10:16:16 -04:00
|
|
|
var dir string
|
2015-02-27 13:50:55 -05:00
|
|
|
var err error
|
2021-08-24 06:10:50 -04:00
|
|
|
if dir, err = os.MkdirTemp(os.TempDir(), "testSizeFileAndNestedDirectoryEmpty"); err != nil {
|
2015-02-27 13:50:55 -05:00
|
|
|
t.Fatalf("failed to create directory: %s", err)
|
|
|
|
}
|
2021-08-24 06:10:50 -04:00
|
|
|
if dir, err = os.MkdirTemp(dir, "nested"); err != nil {
|
2015-03-12 10:16:16 -04:00
|
|
|
t.Fatalf("failed to create nested directory: %s", err)
|
|
|
|
}
|
2015-02-27 13:50:55 -05:00
|
|
|
|
|
|
|
var file *os.File
|
2021-08-24 06:10:50 -04:00
|
|
|
if file, err = os.CreateTemp(dir, "file"); err != nil {
|
2015-02-27 13:50:55 -05:00
|
|
|
t.Fatalf("failed to create file: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d := []byte{100, 111, 99, 107, 101, 114}
|
|
|
|
file.Write(d)
|
|
|
|
|
|
|
|
var size int64
|
2018-03-29 11:34:58 -04:00
|
|
|
if size, _ = Size(context.Background(), dir); size != 6 {
|
2015-02-27 13:50:55 -05:00
|
|
|
t.Fatalf("directory with 6-byte file and empty directory has size: %d", size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test directory with 1 file and 1 non-empty directory
|
|
|
|
func TestSizeFileAndNestedDirectoryNonempty(t *testing.T) {
|
2015-03-12 10:16:16 -04:00
|
|
|
var dir, dirNested string
|
2015-02-27 13:50:55 -05:00
|
|
|
var err error
|
2021-08-24 06:10:50 -04:00
|
|
|
if dir, err = os.MkdirTemp(os.TempDir(), "TestSizeFileAndNestedDirectoryNonempty"); err != nil {
|
2015-02-27 13:50:55 -05:00
|
|
|
t.Fatalf("failed to create directory: %s", err)
|
|
|
|
}
|
2021-08-24 06:10:50 -04:00
|
|
|
if dirNested, err = os.MkdirTemp(dir, "nested"); err != nil {
|
2015-03-12 10:16:16 -04:00
|
|
|
t.Fatalf("failed to create nested directory: %s", err)
|
|
|
|
}
|
2015-02-27 13:50:55 -05:00
|
|
|
|
|
|
|
var file *os.File
|
2021-08-24 06:10:50 -04:00
|
|
|
if file, err = os.CreateTemp(dir, "file"); err != nil {
|
2015-02-27 13:50:55 -05:00
|
|
|
t.Fatalf("failed to create file: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
data := []byte{100, 111, 99, 107, 101, 114}
|
|
|
|
file.Write(data)
|
|
|
|
|
|
|
|
var nestedFile *os.File
|
2021-08-24 06:10:50 -04:00
|
|
|
if nestedFile, err = os.CreateTemp(dirNested, "file"); err != nil {
|
2015-03-12 10:16:16 -04:00
|
|
|
t.Fatalf("failed to create file in nested directory: %s", err)
|
2015-02-27 13:50:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
nestedData := []byte{100, 111, 99, 107, 101, 114}
|
|
|
|
nestedFile.Write(nestedData)
|
|
|
|
|
|
|
|
var size int64
|
2018-03-29 11:34:58 -04:00
|
|
|
if size, _ = Size(context.Background(), dir); size != 12 {
|
2015-03-12 10:16:16 -04:00
|
|
|
t.Fatalf("directory with 6-byte file and nested directory with 6-byte file has size: %d", size)
|
2015-02-27 13:50:55 -05:00
|
|
|
}
|
|
|
|
}
|
2015-10-08 11:51:41 -04:00
|
|
|
|
|
|
|
// Test migration of directory to a subdir underneath itself
|
|
|
|
func TestMoveToSubdir(t *testing.T) {
|
|
|
|
var outerDir, subDir string
|
|
|
|
var err error
|
|
|
|
|
2021-08-24 06:10:50 -04:00
|
|
|
if outerDir, err = os.MkdirTemp(os.TempDir(), "TestMoveToSubdir"); err != nil {
|
2015-10-08 11:51:41 -04:00
|
|
|
t.Fatalf("failed to create directory: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-08-24 06:10:50 -04:00
|
|
|
if subDir, err = os.MkdirTemp(outerDir, "testSub"); err != nil {
|
2015-10-08 11:51:41 -04:00
|
|
|
t.Fatalf("failed to create subdirectory: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// write 4 temp files in the outer dir to get moved
|
|
|
|
filesList := []string{"a", "b", "c", "d"}
|
|
|
|
for _, fName := range filesList {
|
|
|
|
if file, err := os.Create(filepath.Join(outerDir, fName)); err != nil {
|
|
|
|
t.Fatalf("couldn't create temp file %q: %v", fName, err)
|
|
|
|
} else {
|
|
|
|
file.WriteString(fName)
|
|
|
|
file.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = MoveToSubdir(outerDir, filepath.Base(subDir)); err != nil {
|
|
|
|
t.Fatalf("Error during migration of content to subdirectory: %v", err)
|
|
|
|
}
|
|
|
|
// validate that the files were moved to the subdirectory
|
2021-08-24 06:10:50 -04:00
|
|
|
infos, err := os.ReadDir(subDir)
|
2016-03-16 22:43:26 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-10-08 11:51:41 -04:00
|
|
|
if len(infos) != 4 {
|
|
|
|
t.Fatalf("Should be four files in the subdir after the migration: actual length: %d", len(infos))
|
|
|
|
}
|
|
|
|
var results []string
|
|
|
|
for _, info := range infos {
|
|
|
|
results = append(results, info.Name())
|
|
|
|
}
|
2019-08-05 09:43:54 -04:00
|
|
|
sort.Strings(results)
|
2015-10-08 11:51:41 -04:00
|
|
|
if !reflect.DeepEqual(filesList, results) {
|
|
|
|
t.Fatalf("Results after migration do not equal list of files: expected: %v, got: %v", filesList, results)
|
|
|
|
}
|
|
|
|
}
|
2016-05-26 22:47:32 -04:00
|
|
|
|
|
|
|
// Test a non-existing directory
|
|
|
|
func TestSizeNonExistingDirectory(t *testing.T) {
|
2018-03-29 11:34:58 -04:00
|
|
|
if _, err := Size(context.Background(), "/thisdirectoryshouldnotexist/TestSizeNonExistingDirectory"); err == nil {
|
2016-05-26 22:47:32 -04:00
|
|
|
t.Fatalf("error is expected")
|
|
|
|
}
|
|
|
|
}
|