|
4 | 4 | "fmt"
|
5 | 5 | "net"
|
6 | 6 | "os"
|
| 7 | + "path" |
7 | 8 | "path/filepath"
|
8 | 9 | "strings"
|
9 | 10 | "testing"
|
@@ -181,3 +182,162 @@ func TestWaitMgoFreePort(t *testing.T) {
|
181 | 182 | }
|
182 | 183 | })
|
183 | 184 | }
|
| 185 | + |
| 186 | +func TestRemoveAll(t *testing.T) { |
| 187 | + t.Run("removes all files in dir", func(t *testing.T) { |
| 188 | + tmpDir := setupTestFiles(t) |
| 189 | + |
| 190 | + err := removeAll(tmpDir, log.DiscardEvent) |
| 191 | + if err != nil { |
| 192 | + t.Fatalf("got error when removing all files, err=%v", err) |
| 193 | + } |
| 194 | + |
| 195 | + files := readDir(t, tmpDir) |
| 196 | + if len(files) != 0 { |
| 197 | + t.Fatalf("dir should be empty, got=%d files", len(files)) |
| 198 | + } |
| 199 | + }) |
| 200 | + |
| 201 | + t.Run("skipping internal mongod log files", func(t *testing.T) { |
| 202 | + tmpDir := setupTestFiles(t) |
| 203 | + |
| 204 | + err := removeAll(tmpDir, log.DiscardEvent, getInternalLogFileSkipRule()) |
| 205 | + if err != nil { |
| 206 | + t.Fatalf("got error when removing all files, err=%v", err) |
| 207 | + } |
| 208 | + |
| 209 | + files := readDir(t, tmpDir) |
| 210 | + if len(files) != 3 { |
| 211 | + t.Fatalf("expected to have 3 log files, got=%d files, names=%s", len(files), files) |
| 212 | + } |
| 213 | + }) |
| 214 | + |
| 215 | + t.Run("skipping fallback dir", func(t *testing.T) { |
| 216 | + tmpDir := setupTestFiles(t) |
| 217 | + |
| 218 | + err := removeAll(tmpDir, log.DiscardEvent, getFallbackSyncFileSkipRule()) |
| 219 | + if err != nil { |
| 220 | + t.Fatalf("got error when removing all files, err=%v", err) |
| 221 | + } |
| 222 | + |
| 223 | + files := readDir(t, tmpDir) |
| 224 | + if len(files) != 1 || files[0] != fallbackDir { |
| 225 | + t.Fatalf("expected to have fallback dir, got=%d files/dirs, names=%s", len(files), files) |
| 226 | + } |
| 227 | + |
| 228 | + files = readDir(t, path.Join(tmpDir, fallbackDir)) |
| 229 | + if len(files) != 1 || files[0] != "file.fallback" { |
| 230 | + t.Fatalf("expected to have 1 file within fallback dir, got=%d files/dirs, names=%s", len(files), files) |
| 231 | + } |
| 232 | + }) |
| 233 | + |
| 234 | + t.Run("skipping all pbm related", func(t *testing.T) { |
| 235 | + tmpDir := setupTestFiles(t) |
| 236 | + |
| 237 | + err := removeAll(tmpDir, log.DiscardEvent, getFallbackSyncFileSkipRule(), getInternalLogFileSkipRule()) |
| 238 | + if err != nil { |
| 239 | + t.Fatalf("got error when removing all files, err=%v", err) |
| 240 | + } |
| 241 | + |
| 242 | + files := readDir(t, tmpDir) |
| 243 | + if len(files) != 4 { |
| 244 | + t.Fatalf("expected to have fallback dir and mongod log file, got=%d files/dirs, names=%s", len(files), files) |
| 245 | + } |
| 246 | + }) |
| 247 | +} |
| 248 | + |
| 249 | +func TestRemoveInternalMongoLogs(t *testing.T) { |
| 250 | + tmpDir := setupTestFiles(t) |
| 251 | + |
| 252 | + err := removeInternalMongoLogs(tmpDir, log.DiscardEvent) |
| 253 | + if err != nil { |
| 254 | + t.Fatalf("got error when removing internal mongod logs, err=%v", err) |
| 255 | + } |
| 256 | + |
| 257 | + files := readDir(t, tmpDir) |
| 258 | + if len(files) != 6 { |
| 259 | + t.Fatalf("only mongod log files should be removed, expected 6 files, got=%d files", len(files)) |
| 260 | + } |
| 261 | + for _, f := range files { |
| 262 | + if strings.HasPrefix(f, internalMongodLog) { |
| 263 | + t.Fatalf("internal mongod log file is not deleted: %s", f) |
| 264 | + } |
| 265 | + } |
| 266 | +} |
| 267 | + |
| 268 | +func readDir(t *testing.T, dir string) []string { |
| 269 | + t.Helper() |
| 270 | + |
| 271 | + d, err := os.Open(dir) |
| 272 | + if err != nil { |
| 273 | + t.Fatalf("failing opening dir, err=%v", err) |
| 274 | + } |
| 275 | + defer d.Close() |
| 276 | + |
| 277 | + files, err := d.Readdirnames(-1) |
| 278 | + if err != nil { |
| 279 | + t.Fatalf("failing reading dir, err=%v", err) |
| 280 | + } |
| 281 | + |
| 282 | + return files |
| 283 | +} |
| 284 | + |
| 285 | +func setupTestFiles(t *testing.T) string { |
| 286 | + tmpDir, err := os.MkdirTemp("", "phys-test-*") |
| 287 | + if err != nil { |
| 288 | + t.Fatalf("error while creating setup files: %v", err) |
| 289 | + } |
| 290 | + |
| 291 | + t.Cleanup(func() { |
| 292 | + os.RemoveAll(tmpDir) |
| 293 | + }) |
| 294 | + |
| 295 | + // tmpDir/ |
| 296 | + // - file1.txt |
| 297 | + // - file2.log |
| 298 | + // - file3.txt.tmp |
| 299 | + // - subdir/ |
| 300 | + // - file4.txt |
| 301 | + // - file5.log |
| 302 | + // - file6.txt.tmp |
| 303 | + // - empty/ |
| 304 | + // - pbm.restore.log |
| 305 | + // - pbm.restore.log.2025-05-15T14-33-34 |
| 306 | + // - pbm.restore.log.2025-05-15T14-37-34 |
| 307 | + // - .fallbacksync/ |
| 308 | + // - file.fs.txt |
| 309 | + createTestFile(t, filepath.Join(tmpDir, "file1.txt"), "content1") |
| 310 | + createTestFile(t, filepath.Join(tmpDir, "file2.log"), "content2") |
| 311 | + createTestFile(t, filepath.Join(tmpDir, "file3.txt.tmp"), "content3") |
| 312 | + |
| 313 | + createTestDir(t, filepath.Join(tmpDir, "subdir")) |
| 314 | + createTestFile(t, filepath.Join(tmpDir, "subdir", "file4.txt"), "content4") |
| 315 | + createTestFile(t, filepath.Join(tmpDir, "subdir", "file5.log"), "content5") |
| 316 | + createTestFile(t, filepath.Join(tmpDir, "subdir", "file6.txt.tmp"), "content6") |
| 317 | + |
| 318 | + createTestDir(t, filepath.Join(tmpDir, "empty")) |
| 319 | + |
| 320 | + // files & dirs with additional semantic: |
| 321 | + createTestFile(t, filepath.Join(tmpDir, "pbm.restore.log"), "log-content-1") |
| 322 | + createTestFile(t, filepath.Join(tmpDir, "pbm.restore.log.2025-05-15T14-33-34"), "log-content-2") |
| 323 | + createTestFile(t, filepath.Join(tmpDir, "pbm.restore.log.2025-05-15T14-37-34"), "log-content-3") |
| 324 | + |
| 325 | + createTestDir(t, filepath.Join(tmpDir, fallbackDir)) |
| 326 | + createTestFile(t, filepath.Join(tmpDir, fallbackDir, "file.fallback"), "content") |
| 327 | + |
| 328 | + return tmpDir |
| 329 | +} |
| 330 | + |
| 331 | +func createTestFile(t *testing.T, path, content string) { |
| 332 | + t.Helper() |
| 333 | + if err := os.WriteFile(path, []byte(content), 0o644); err != nil { |
| 334 | + t.Fatalf("error while creating file %s: %v", path, err) |
| 335 | + } |
| 336 | +} |
| 337 | + |
| 338 | +func createTestDir(t *testing.T, path string) { |
| 339 | + t.Helper() |
| 340 | + if err := os.Mkdir(path, 0o755); err != nil { |
| 341 | + t.Fatalf("error while creating dir %s: %v", path, err) |
| 342 | + } |
| 343 | +} |
0 commit comments