access_log_off_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package nginx_log
  2. import (
  3. "testing"
  4. )
  5. // TestAccessLogOffDirective tests that "access_log off;" directives are properly ignored
  6. func TestAccessLogOffDirective(t *testing.T) {
  7. // Clear cache before test
  8. ClearLogCache()
  9. configPath := "/etc/nginx/sites-available/test.conf"
  10. // Test 1: Normal logs without "off"
  11. t.Run("Normal logs", func(t *testing.T) {
  12. ClearLogCache()
  13. content := []byte(`
  14. server {
  15. access_log /var/log/nginx/access.log;
  16. error_log /var/log/nginx/error.log;
  17. }`)
  18. err := scanForLogDirectives(configPath, content)
  19. if err != nil {
  20. t.Fatalf("Scan failed: %v", err)
  21. }
  22. logs := GetAllLogPaths()
  23. if len(logs) != 2 {
  24. t.Errorf("Expected 2 logs, got %d", len(logs))
  25. }
  26. })
  27. // Test 2: Logs with "off" directive
  28. t.Run("With off directive", func(t *testing.T) {
  29. ClearLogCache()
  30. content := []byte(`
  31. server {
  32. access_log /var/log/nginx/access.log;
  33. access_log off;
  34. error_log off;
  35. error_log /var/log/nginx/error.log;
  36. }`)
  37. err := scanForLogDirectives(configPath, content)
  38. if err != nil {
  39. t.Fatalf("Scan failed: %v", err)
  40. }
  41. logs := GetAllLogPaths()
  42. if len(logs) != 2 {
  43. t.Errorf("Expected 2 logs (ignoring 'off'), got %d", len(logs))
  44. for _, log := range logs {
  45. t.Logf("Found: %s (%s)", log.Path, log.Type)
  46. }
  47. }
  48. // Verify "off" is not treated as a path
  49. for _, log := range logs {
  50. if log.Path == "off" {
  51. t.Errorf("'off' should not be treated as a log path")
  52. }
  53. }
  54. })
  55. // Test 3: Only "off" directives
  56. t.Run("Only off directives", func(t *testing.T) {
  57. ClearLogCache()
  58. content := []byte(`
  59. server {
  60. access_log off;
  61. error_log off;
  62. }`)
  63. err := scanForLogDirectives(configPath, content)
  64. if err != nil {
  65. t.Fatalf("Scan failed: %v", err)
  66. }
  67. logs := GetAllLogPaths()
  68. if len(logs) != 0 {
  69. t.Errorf("Expected 0 logs (all 'off'), got %d", len(logs))
  70. for _, log := range logs {
  71. t.Logf("Found: %s (%s)", log.Path, log.Type)
  72. }
  73. }
  74. })
  75. // Test 4: Mixed with format parameters
  76. t.Run("With format parameters", func(t *testing.T) {
  77. ClearLogCache()
  78. content := []byte(`
  79. server {
  80. access_log /var/log/nginx/access.log combined buffer=32k;
  81. access_log off;
  82. error_log /var/log/nginx/error.log warn;
  83. }`)
  84. err := scanForLogDirectives(configPath, content)
  85. if err != nil {
  86. t.Fatalf("Scan failed: %v", err)
  87. }
  88. logs := GetAllLogPaths()
  89. if len(logs) != 2 {
  90. t.Errorf("Expected 2 logs, got %d", len(logs))
  91. for _, log := range logs {
  92. t.Logf("Found: %s (%s)", log.Path, log.Type)
  93. }
  94. }
  95. })
  96. }