github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/pkg/directory/directory_test.go (about)

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