github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/utils/os/fs/filesystem_test.go (about) 1 // Copyright © 2021 Alibaba Group Holding Ltd. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package fs 16 17 import ( 18 "os" 19 "path/filepath" 20 "strings" 21 "testing" 22 23 "github.com/stretchr/testify/assert" 24 ) 25 26 func TestMkdir(t *testing.T) { 27 type args struct { 28 dirName string 29 } 30 tests := []struct { 31 name string 32 args args 33 wantErr bool 34 }{ 35 { 36 "test crete dri", 37 args{dirName: "./test/deep/dir/test"}, 38 false, 39 }, 40 } 41 for _, tt := range tests { 42 t.Run(tt.name, func(t *testing.T) { 43 if err := NewFilesystem().MkdirAll(tt.args.dirName); (err != nil) != tt.wantErr { 44 t.Errorf("Mkdir() error = %v, wantErr %v", err, tt.wantErr) 45 } 46 }) 47 } 48 } 49 50 func TestRenameDir(t *testing.T) { 51 oldPath := "/tmp/mytest" 52 newPath := "/tmp/abc/mytest" 53 defer os.RemoveAll(newPath) 54 55 err := FS.MkdirAll(oldPath) 56 if err != nil { 57 t.Fatalf("TempDir %s: %v", t.Name(), err) 58 } 59 60 filename := "tmp-file" 61 data := []byte("i am a tmp file\n") 62 if err := os.WriteFile(filepath.Join(oldPath, filename), data, 0644); err != nil { 63 t.Fatalf("WriteFile %s: %v", filename, err) 64 } 65 66 type args struct { 67 filename string 68 fileContent []byte 69 newPath string 70 oldPath string 71 } 72 tests := []struct { 73 name string 74 args args 75 wantErr bool 76 }{ 77 { 78 "test rename dri", 79 args{ 80 filename: filename, 81 fileContent: data, 82 oldPath: oldPath, 83 newPath: newPath}, 84 false, 85 }, 86 } 87 88 for _, tt := range tests { 89 t.Run(tt.name, func(t *testing.T) { 90 if err := FS.Rename(tt.args.oldPath, tt.args.newPath); (err != nil) != tt.wantErr { 91 t.Errorf("Rename() error = %v, wantErr %v", err, tt.wantErr) 92 } 93 94 content, err := os.ReadFile(filepath.Join(tt.args.newPath, filename)) 95 assert.NoErrorf(t, err, "failed to load file content form new path") 96 assert.Equal(t, tt.args.fileContent, content) 97 }) 98 } 99 } 100 101 /*func TestCopyDir(t *testing.T) { 102 type args struct { 103 src, dst string 104 } 105 tests := []struct { 106 name string 107 args args 108 wantErr bool 109 }{ 110 { 111 "test copyDir when src is dir", 112 args{src: "/root/test", dst: "/tmp"}, 113 false, 114 }, 115 { 116 "test copyDir when src is not exist", 117 args{src: "/root/Notexist", dst: "/tmp"}, 118 true, 119 }, 120 { 121 "test copyDir when dst is not exist", 122 args{src: "/root/test", dst: "/tmp"}, 123 false, 124 }, 125 { 126 "test copyDir when dst is not dir", 127 args{src: "/root/test", dst: "/tmp"}, 128 false, 129 }, 130 } 131 for _, tt := range tests { 132 t.Run(tt.name, func(t *testing.T) { 133 if err := NewFilesystem().CopyDir(tt.args.src, tt.args.dst); (err != nil) != tt.wantErr { 134 t.Errorf("CopyDir() error = %v, wantErr %v", err, tt.wantErr) 135 } 136 }) 137 } 138 } 139 */ 140 141 /*func TestCopySingleFile(t *testing.T) { 142 type args struct { 143 src, dst string 144 } 145 tests := []struct { 146 name string 147 args args 148 wantErr bool 149 }{ 150 { 151 "test copy single file when src is dir", 152 args{src: "/root", dst: "/tmp"}, 153 true, 154 }, 155 { 156 "test copy single file when src is not regular file, seems like link file", 157 args{src: "/root/link", dst: "/tmp"}, 158 true, 159 }, 160 { 161 "test copy single file when src is not exist", 162 args{src: "/root/Notexist", dst: "/tmp"}, 163 true, 164 }, 165 { 166 "test copy single file dst exist", 167 args{src: "/root/test", dst: "/tmp"}, 168 false, 169 }, 170 { 171 "test copy single file dst file path not exist", 172 args{src: "/root/test", dst: "/tmp/abc"}, 173 false, 174 }, 175 { 176 "test copy single file when dst file is exist", 177 args{src: "/root/test", dst: "/tmp/test"}, 178 false, 179 }, 180 } 181 for _, tt := range tests { 182 t.Run(tt.name, func(t *testing.T) { 183 if _, err := NewFilesystem().CopyFile(tt.args.src, tt.args.dst); (err != nil) != tt.wantErr { 184 t.Errorf("CopySingleFile() error = %v, wantErr %v", err, tt.wantErr) 185 } 186 }) 187 } 188 }*/ 189 190 type FakeDir struct { 191 path string 192 dirs []FakeDir 193 files []FakeFile 194 } 195 196 type FakeFile struct { 197 name string 198 content string 199 } 200 201 func makeFakeSourceDir(dir FakeDir) error { 202 var ( 203 err error 204 fi *os.File 205 curRoot = dir.path 206 ) 207 208 err = os.MkdirAll(curRoot, 0755) 209 if err != nil { 210 return err 211 } 212 for _, f := range dir.files { 213 fi, err = os.Create(filepath.Join(dir.path, f.name)) 214 if err != nil { 215 return err 216 } 217 _, err = fi.Write([]byte(f.content)) 218 if err != nil { 219 fi.Close() 220 return err 221 } 222 fi.Close() 223 } 224 225 for _, d := range dir.dirs { 226 if !strings.HasPrefix(d.path, curRoot) { 227 d.path = filepath.Join(curRoot, d.path) 228 } 229 err = makeFakeSourceDir(d) 230 if err != nil { 231 return err 232 } 233 } 234 return nil 235 } 236 237 func TestRecursionHardLink(t *testing.T) { 238 var ( 239 err error 240 dstPath = "/tmp/link-test-dst" 241 ) 242 243 testDir := FakeDir{ 244 path: "/tmp/link-test", 245 dirs: []FakeDir{ 246 { 247 path: "subtest", 248 files: []FakeFile{ 249 { 250 name: "a", 251 content: "a", 252 }, 253 { 254 name: "b", 255 content: "b", 256 }, 257 }, 258 dirs: []FakeDir{ 259 { 260 path: "deepSubtest", 261 files: []FakeFile{ 262 { 263 name: "e", 264 content: "e", 265 }, 266 }, 267 }, 268 }, 269 }, 270 }, 271 files: []FakeFile{ 272 { 273 name: "c", 274 content: "c", 275 }, 276 { 277 name: "d", 278 content: "d", 279 }, 280 }, 281 } 282 283 err = makeFakeSourceDir(testDir) 284 defer func() { 285 err = os.RemoveAll(testDir.path) 286 if err != nil { 287 t.Logf("failed to remove all source files, %v", err) 288 } 289 err = os.RemoveAll(dstPath) 290 if err != nil { 291 t.Logf("failed to remove all dst files, %v", err) 292 } 293 }() 294 if err != nil { 295 t.Fatalf("failed to make fake dir, err: %v", err) 296 } 297 }