logrotate_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package nginx_log
  2. import "testing"
  3. func TestIsLogrotateFile(t *testing.T) {
  4. tests := []struct {
  5. filename string
  6. baseLogName string
  7. expected bool
  8. description string
  9. }{
  10. // Valid logrotate patterns
  11. {"access.log", "access.log", true, "Current log file"},
  12. {"access.log.1", "access.log", true, "First rotated file"},
  13. {"access.log.2", "access.log", true, "Second rotated file"},
  14. {"access.log.10", "access.log", true, "Tenth rotated file"},
  15. {"access.log.1.gz", "access.log", true, "First compressed rotated file"},
  16. {"access.log.2.gz", "access.log", true, "Second compressed rotated file"},
  17. {"access.log.10.gz", "access.log", true, "Tenth compressed rotated file"},
  18. // Invalid patterns that should NOT match
  19. {"random.gz", "access.log", false, "Random gz file"},
  20. {"access_20230815.gz", "access.log", false, "Date-based naming"},
  21. {"access.log.gz", "access.log", false, "Direct compression without number"},
  22. {"access.log.old", "access.log", false, "Non-numeric suffix"},
  23. {"access.log.1.bz2", "access.log", false, "Different compression format"},
  24. {"error.log", "access.log", false, "Different log type"},
  25. {"access.log.1.2.gz", "access.log", false, "Multiple dots in number"},
  26. {"access.log.a.gz", "access.log", false, "Non-numeric rotation"},
  27. // Edge cases
  28. {"", "access.log", false, "Empty filename"},
  29. {"access.log", "", false, "Empty base name"},
  30. {"access", "access.log", false, "Partial match"},
  31. {"access.log.backup", "access.log", false, "Backup suffix"},
  32. }
  33. for _, tt := range tests {
  34. t.Run(tt.description, func(t *testing.T) {
  35. result := isLogrotateFile(tt.filename, tt.baseLogName)
  36. if result != tt.expected {
  37. t.Errorf("isLogrotateFile(%q, %q) = %v, want %v",
  38. tt.filename, tt.baseLogName, result, tt.expected)
  39. }
  40. })
  41. }
  42. }