github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/ios/fsutils_test.go (about) 1 // Package ios is a collection of interfaces to the local storage subsystem; 2 // the package includes OS-dependent implementations for those interfaces. 3 /* 4 * Copyright (c) 2018-2022, NVIDIA CORPORATION. All rights reserved. 5 */ 6 package ios_test 7 8 import ( 9 "os" 10 "path" 11 "testing" 12 13 "github.com/NVIDIA/aistore/cmn/cos" 14 "github.com/NVIDIA/aistore/cmn/mono" 15 "github.com/NVIDIA/aistore/ios" 16 ) 17 18 func TestGetFSUsedPercentage(t *testing.T) { 19 percentage, ok := ios.GetFSUsedPercentage("/") 20 if !ok { 21 t.Error("Unable to retrieve FS used percentage!") 22 } 23 if percentage > 100 { 24 t.Errorf("Invalid FS used percentage [%d].", percentage) 25 } 26 } 27 28 func TestDirSize(t *testing.T) { 29 name, err := os.MkdirTemp("/tmp", t.Name()) 30 if err != nil { 31 t.Error(err) 32 return 33 } 34 defer os.RemoveAll(name) 35 36 size := mkFile(t, name, "file.txt") 37 38 totalSize, err := ios.DirSizeOnDisk(name, false /*withNonDirPrefix*/) 39 if err != nil { 40 t.Error(err) 41 } 42 if totalSize < uint64(size) { 43 t.Fatalf("Dir size %d < %d file", totalSize, size) 44 } 45 } 46 47 func TestDirFiles(t *testing.T) { 48 name, err := os.MkdirTemp("/tmp", t.Name()) 49 if err != nil { 50 t.Error(err) 51 return 52 } 53 defer os.RemoveAll(name) 54 55 checkFileCount(t, name, 0) 56 57 subdir, err := os.MkdirTemp(name, "") 58 if err != nil { 59 t.Error(err) 60 return 61 } 62 63 checkFileCount(t, name, 0) 64 65 size := mkFile(t, name, "f1.txt") 66 checkFileCount(t, name, 1) 67 68 size += mkFile(t, subdir, "f2.txt") 69 size += mkFile(t, subdir, "f3.txt") 70 checkFileCount(t, name, 3) 71 checkFileSizes(t, name, size) 72 } 73 74 func mkFile(t *testing.T, dir, fname string) (written int) { 75 k := mono.NanoTime() & 0xff 76 f, err := os.Create(path.Join(dir, fname)) 77 if err != nil { 78 t.Error(err) 79 return 80 } 81 size := cos.KiB * int(k) 82 written, err = f.Write(make([]byte, size)) 83 f.Close() 84 if err != nil { 85 t.Error(err) 86 } 87 if written != size { 88 t.Fatalf("written %d != %d", size, written) 89 } 90 return 91 } 92 93 func checkFileCount(t *testing.T, dir string, expcnt int) { 94 fileCount, err := ios.DirFileCount(dir) 95 if err != nil { 96 t.Error(err) 97 } 98 if fileCount != expcnt { 99 t.Fatalf("Expected %d files, got %d", expcnt, fileCount) 100 } 101 } 102 103 func checkFileSizes(t *testing.T, dir string, expsize int) { 104 size, err := ios.DirSumFileSizes(dir) 105 if err != nil { 106 t.Error(err) 107 } 108 if size != uint64(expsize) { 109 t.Fatalf("Expected %d size, got %d", expsize, size) 110 } 111 }