|
| 1 | +package fs |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/percona/percona-backup-mongodb/pbm/storage" |
| 10 | +) |
| 11 | + |
| 12 | +func TestList(t *testing.T) { |
| 13 | + t.Run("basic usage", func(t *testing.T) { |
| 14 | + tmpDir := setupTestFiles(t) |
| 15 | + fs := &FS{root: tmpDir} |
| 16 | + |
| 17 | + testCases := []struct { |
| 18 | + desc string |
| 19 | + prefix string |
| 20 | + suffix string |
| 21 | + wantFiles []storage.FileInfo |
| 22 | + }{ |
| 23 | + { |
| 24 | + desc: "list all non-tmp files", |
| 25 | + prefix: "", |
| 26 | + suffix: "", |
| 27 | + wantFiles: []storage.FileInfo{ |
| 28 | + {Name: "file1.txt", Size: 8}, |
| 29 | + {Name: "file2.log", Size: 8}, |
| 30 | + {Name: "subdir/file4.txt", Size: 8}, |
| 31 | + {Name: "subdir/file5.log", Size: 8}, |
| 32 | + }, |
| 33 | + }, |
| 34 | + { |
| 35 | + desc: "list txt files only", |
| 36 | + prefix: "", |
| 37 | + suffix: ".txt", |
| 38 | + wantFiles: []storage.FileInfo{{Name: "file1.txt", Size: 8}, {Name: "subdir/file4.txt", Size: 8}}, |
| 39 | + }, |
| 40 | + { |
| 41 | + desc: "list tmp files explicitly", |
| 42 | + prefix: "", |
| 43 | + suffix: ".tmp", |
| 44 | + wantFiles: []storage.FileInfo{{Name: "file3.txt.tmp", Size: 8}, {Name: "subdir/file6.txt.tmp", Size: 8}}, |
| 45 | + }, |
| 46 | + { |
| 47 | + desc: "List files with prefix only", |
| 48 | + prefix: "subdir", |
| 49 | + suffix: "", |
| 50 | + wantFiles: []storage.FileInfo{{Name: "file4.txt", Size: 8}, {Name: "file5.log", Size: 8}}, |
| 51 | + }, |
| 52 | + { |
| 53 | + desc: "list files with prefix & suffix", |
| 54 | + prefix: "subdir", |
| 55 | + suffix: ".log", |
| 56 | + wantFiles: []storage.FileInfo{{Name: "file5.log", Size: 8}}, |
| 57 | + }, |
| 58 | + { |
| 59 | + desc: "non existing prefix", |
| 60 | + prefix: "nonexistent", |
| 61 | + suffix: "", |
| 62 | + wantFiles: []storage.FileInfo{}, |
| 63 | + }, |
| 64 | + { |
| 65 | + desc: "empty dir", |
| 66 | + prefix: "empty", |
| 67 | + suffix: "", |
| 68 | + wantFiles: []storage.FileInfo{}, |
| 69 | + }, |
| 70 | + } |
| 71 | + |
| 72 | + for _, tC := range testCases { |
| 73 | + t.Run(tC.desc, func(t *testing.T) { |
| 74 | + files, err := fs.List(tC.prefix, tC.suffix) |
| 75 | + if err != nil { |
| 76 | + t.Errorf("got error while executing list: %v", err) |
| 77 | + |
| 78 | + } |
| 79 | + |
| 80 | + if len(files) != len(tC.wantFiles) { |
| 81 | + t.Errorf("wrong number of returned files: want:%d, got=%d", len(tC.wantFiles), len(files)) |
| 82 | + } |
| 83 | + |
| 84 | + gotFiles := map[string]int64{} |
| 85 | + for _, f := range files { |
| 86 | + gotFiles[f.Name] = f.Size |
| 87 | + } |
| 88 | + |
| 89 | + for _, f := range tC.wantFiles { |
| 90 | + size, exists := gotFiles[f.Name] |
| 91 | + if !exists { |
| 92 | + t.Errorf("missing file: %s", f.Name) |
| 93 | + } |
| 94 | + if f.Size != size { |
| 95 | + t.Errorf("wrong file size: want=%d, got=%d", f.Size, size) |
| 96 | + } |
| 97 | + } |
| 98 | + }) |
| 99 | + } |
| 100 | + }) |
| 101 | +} |
| 102 | + |
| 103 | +func setupTestFiles(t *testing.T) string { |
| 104 | + tmpDir, err := os.MkdirTemp("", "fs-test-*") |
| 105 | + if err != nil { |
| 106 | + t.Fatalf("error while creating setup files: %v", err) |
| 107 | + } |
| 108 | + |
| 109 | + t.Cleanup(func() { |
| 110 | + os.RemoveAll(tmpDir) |
| 111 | + }) |
| 112 | + |
| 113 | + // tmpDir/ |
| 114 | + // - file1.txt |
| 115 | + // - file2.log |
| 116 | + // - file3.txt.tmp |
| 117 | + // - subdir/ |
| 118 | + // - file4.txt |
| 119 | + // - file5.log |
| 120 | + // - file6.txt.tmp |
| 121 | + // - empty/ |
| 122 | + createTestFile(t, filepath.Join(tmpDir, "file1.txt"), "content1") |
| 123 | + createTestFile(t, filepath.Join(tmpDir, "file2.log"), "content2") |
| 124 | + createTestFile(t, filepath.Join(tmpDir, "file3.txt.tmp"), "content3") |
| 125 | + |
| 126 | + createTestDir(t, filepath.Join(tmpDir, "subdir")) |
| 127 | + createTestFile(t, filepath.Join(tmpDir, "subdir", "file4.txt"), "content4") |
| 128 | + createTestFile(t, filepath.Join(tmpDir, "subdir", "file5.log"), "content5") |
| 129 | + createTestFile(t, filepath.Join(tmpDir, "subdir", "file6.txt.tmp"), "content6") |
| 130 | + |
| 131 | + createTestDir(t, filepath.Join(tmpDir, "empty")) |
| 132 | + |
| 133 | + return tmpDir |
| 134 | +} |
| 135 | + |
| 136 | +func createTestFile(t *testing.T, path, content string) { |
| 137 | + t.Helper() |
| 138 | + if err := os.WriteFile(path, []byte(content), 0644); err != nil { |
| 139 | + t.Fatalf("error while creating file %s: %v", path, err) |
| 140 | + } |
| 141 | +} |
| 142 | +func createTestDir(t *testing.T, path string) { |
| 143 | + t.Helper() |
| 144 | + if err := os.Mkdir(path, 0755); err != nil { |
| 145 | + t.Fatalf("error while creating dir %s: %v", path, err) |
| 146 | + } |
| 147 | +} |
0 commit comments