github.com/ph/moby@v1.13.1/pkg/directory/directory_test.go (about)

     1  package directory
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"reflect"
     8  	"sort"
     9  	"testing"
    10  )
    11  
    12  // Size of an empty directory should be 0
    13  func TestSizeEmpty(t *testing.T) {
    14  	var dir string
    15  	var err error
    16  	if dir, err = ioutil.TempDir(os.TempDir(), "testSizeEmptyDirectory"); err != nil {
    17  		t.Fatalf("failed to create directory: %s", err)
    18  	}
    19  
    20  	var size int64
    21  	if size, _ = Size(dir); size != 0 {
    22  		t.Fatalf("empty directory has size: %d", size)
    23  	}
    24  }
    25  
    26  // Size of a directory with one empty file should be 0
    27  func TestSizeEmptyFile(t *testing.T) {
    28  	var dir string
    29  	var err error
    30  	if dir, err = ioutil.TempDir(os.TempDir(), "testSizeEmptyFile"); err != nil {
    31  		t.Fatalf("failed to create directory: %s", err)
    32  	}
    33  
    34  	var file *os.File
    35  	if file, err = ioutil.TempFile(dir, "file"); err != nil {
    36  		t.Fatalf("failed to create file: %s", err)
    37  	}
    38  
    39  	var size int64
    40  	if size, _ = Size(file.Name()); size != 0 {
    41  		t.Fatalf("directory with one file has size: %d", size)
    42  	}
    43  }
    44  
    45  // Size of a directory with one 5-byte file should be 5
    46  func TestSizeNonemptyFile(t *testing.T) {
    47  	var dir string
    48  	var err error
    49  	if dir, err = ioutil.TempDir(os.TempDir(), "testSizeNonemptyFile"); err != nil {
    50  		t.Fatalf("failed to create directory: %s", err)
    51  	}
    52  
    53  	var file *os.File
    54  	if file, err = ioutil.TempFile(dir, "file"); err != nil {
    55  		t.Fatalf("failed to create file: %s", err)
    56  	}
    57  
    58  	d := []byte{97, 98, 99, 100, 101}
    59  	file.Write(d)
    60  
    61  	var size int64
    62  	if size, _ = Size(file.Name()); size != 5 {
    63  		t.Fatalf("directory with one 5-byte file has size: %d", size)
    64  	}
    65  }
    66  
    67  // Size of a directory with one empty directory should be 0
    68  func TestSizeNestedDirectoryEmpty(t *testing.T) {
    69  	var dir string
    70  	var err error
    71  	if dir, err = ioutil.TempDir(os.TempDir(), "testSizeNestedDirectoryEmpty"); err != nil {
    72  		t.Fatalf("failed to create directory: %s", err)
    73  	}
    74  	if dir, err = ioutil.TempDir(dir, "nested"); err != nil {
    75  		t.Fatalf("failed to create nested directory: %s", err)
    76  	}
    77  
    78  	var size int64
    79  	if size, _ = Size(dir); size != 0 {
    80  		t.Fatalf("directory with one empty directory has size: %d", size)
    81  	}
    82  }
    83  
    84  // Test directory with 1 file and 1 empty directory
    85  func TestSizeFileAndNestedDirectoryEmpty(t *testing.T) {
    86  	var dir string
    87  	var err error
    88  	if dir, err = ioutil.TempDir(os.TempDir(), "testSizeFileAndNestedDirectoryEmpty"); err != nil {
    89  		t.Fatalf("failed to create directory: %s", err)
    90  	}
    91  	if dir, err = ioutil.TempDir(dir, "nested"); err != nil {
    92  		t.Fatalf("failed to create nested directory: %s", err)
    93  	}
    94  
    95  	var file *os.File
    96  	if file, err = ioutil.TempFile(dir, "file"); err != nil {
    97  		t.Fatalf("failed to create file: %s", err)
    98  	}
    99  
   100  	d := []byte{100, 111, 99, 107, 101, 114}
   101  	file.Write(d)
   102  
   103  	var size int64
   104  	if size, _ = Size(dir); size != 6 {
   105  		t.Fatalf("directory with 6-byte file and empty directory has size: %d", size)
   106  	}
   107  }
   108  
   109  // Test directory with 1 file and 1 non-empty directory
   110  func TestSizeFileAndNestedDirectoryNonempty(t *testing.T) {
   111  	var dir, dirNested string
   112  	var err error
   113  	if dir, err = ioutil.TempDir(os.TempDir(), "TestSizeFileAndNestedDirectoryNonempty"); err != nil {
   114  		t.Fatalf("failed to create directory: %s", err)
   115  	}
   116  	if dirNested, err = ioutil.TempDir(dir, "nested"); err != nil {
   117  		t.Fatalf("failed to create nested directory: %s", err)
   118  	}
   119  
   120  	var file *os.File
   121  	if file, err = ioutil.TempFile(dir, "file"); err != nil {
   122  		t.Fatalf("failed to create file: %s", err)
   123  	}
   124  
   125  	data := []byte{100, 111, 99, 107, 101, 114}
   126  	file.Write(data)
   127  
   128  	var nestedFile *os.File
   129  	if nestedFile, err = ioutil.TempFile(dirNested, "file"); err != nil {
   130  		t.Fatalf("failed to create file in nested directory: %s", err)
   131  	}
   132  
   133  	nestedData := []byte{100, 111, 99, 107, 101, 114}
   134  	nestedFile.Write(nestedData)
   135  
   136  	var size int64
   137  	if size, _ = Size(dir); size != 12 {
   138  		t.Fatalf("directory with 6-byte file and nested directory with 6-byte file has size: %d", size)
   139  	}
   140  }
   141  
   142  // Test migration of directory to a subdir underneath itself
   143  func TestMoveToSubdir(t *testing.T) {
   144  	var outerDir, subDir string
   145  	var err error
   146  
   147  	if outerDir, err = ioutil.TempDir(os.TempDir(), "TestMoveToSubdir"); err != nil {
   148  		t.Fatalf("failed to create directory: %v", err)
   149  	}
   150  
   151  	if subDir, err = ioutil.TempDir(outerDir, "testSub"); err != nil {
   152  		t.Fatalf("failed to create subdirectory: %v", err)
   153  	}
   154  
   155  	// write 4 temp files in the outer dir to get moved
   156  	filesList := []string{"a", "b", "c", "d"}
   157  	for _, fName := range filesList {
   158  		if file, err := os.Create(filepath.Join(outerDir, fName)); err != nil {
   159  			t.Fatalf("couldn't create temp file %q: %v", fName, err)
   160  		} else {
   161  			file.WriteString(fName)
   162  			file.Close()
   163  		}
   164  	}
   165  
   166  	if err = MoveToSubdir(outerDir, filepath.Base(subDir)); err != nil {
   167  		t.Fatalf("Error during migration of content to subdirectory: %v", err)
   168  	}
   169  	// validate that the files were moved to the subdirectory
   170  	infos, err := ioutil.ReadDir(subDir)
   171  	if err != nil {
   172  		t.Fatal(err)
   173  	}
   174  	if len(infos) != 4 {
   175  		t.Fatalf("Should be four files in the subdir after the migration: actual length: %d", len(infos))
   176  	}
   177  	var results []string
   178  	for _, info := range infos {
   179  		results = append(results, info.Name())
   180  	}
   181  	sort.Sort(sort.StringSlice(results))
   182  	if !reflect.DeepEqual(filesList, results) {
   183  		t.Fatalf("Results after migration do not equal list of files: expected: %v, got: %v", filesList, results)
   184  	}
   185  }
   186  
   187  // Test a non-existing directory
   188  func TestSizeNonExistingDirectory(t *testing.T) {
   189  	if _, err := Size("/thisdirectoryshouldnotexist/TestSizeNonExistingDirectory"); err == nil {
   190  		t.Fatalf("error is expected")
   191  	}
   192  }