Skip to content

Commit a766bd2

Browse files
committed
fix: log index match access_log off incorrectly #1317
1 parent 691e513 commit a766bd2

File tree

4 files changed

+191
-0
lines changed

4 files changed

+191
-0
lines changed

app/components.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ declare module 'vue' {
5151
AQrcode: typeof import('ant-design-vue/es')['QRCode']
5252
ARadio: typeof import('ant-design-vue/es')['Radio']
5353
ARadioGroup: typeof import('ant-design-vue/es')['RadioGroup']
54+
ARangePicker: typeof import('ant-design-vue/es')['RangePicker']
5455
AResult: typeof import('ant-design-vue/es')['Result']
5556
ARow: typeof import('ant-design-vue/es')['Row']
5657
ASegmented: typeof import('ant-design-vue/es')['Segmented']
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package nginx_log
2+
3+
import (
4+
"testing"
5+
)
6+
7+
// TestAccessLogOffDirective tests that "access_log off;" directives are properly ignored
8+
func TestAccessLogOffDirective(t *testing.T) {
9+
// Clear cache before test
10+
ClearLogCache()
11+
12+
configPath := "/etc/nginx/sites-available/test.conf"
13+
14+
// Test 1: Normal logs without "off"
15+
t.Run("Normal logs", func(t *testing.T) {
16+
ClearLogCache()
17+
content := []byte(`
18+
server {
19+
access_log /var/log/nginx/access.log;
20+
error_log /var/log/nginx/error.log;
21+
}`)
22+
23+
err := scanForLogDirectives(configPath, content)
24+
if err != nil {
25+
t.Fatalf("Scan failed: %v", err)
26+
}
27+
28+
logs := GetAllLogPaths()
29+
if len(logs) != 2 {
30+
t.Errorf("Expected 2 logs, got %d", len(logs))
31+
}
32+
})
33+
34+
// Test 2: Logs with "off" directive
35+
t.Run("With off directive", func(t *testing.T) {
36+
ClearLogCache()
37+
content := []byte(`
38+
server {
39+
access_log /var/log/nginx/access.log;
40+
access_log off;
41+
error_log off;
42+
error_log /var/log/nginx/error.log;
43+
}`)
44+
45+
err := scanForLogDirectives(configPath, content)
46+
if err != nil {
47+
t.Fatalf("Scan failed: %v", err)
48+
}
49+
50+
logs := GetAllLogPaths()
51+
if len(logs) != 2 {
52+
t.Errorf("Expected 2 logs (ignoring 'off'), got %d", len(logs))
53+
for _, log := range logs {
54+
t.Logf("Found: %s (%s)", log.Path, log.Type)
55+
}
56+
}
57+
58+
// Verify "off" is not treated as a path
59+
for _, log := range logs {
60+
if log.Path == "off" {
61+
t.Errorf("'off' should not be treated as a log path")
62+
}
63+
}
64+
})
65+
66+
// Test 3: Only "off" directives
67+
t.Run("Only off directives", func(t *testing.T) {
68+
ClearLogCache()
69+
content := []byte(`
70+
server {
71+
access_log off;
72+
error_log off;
73+
}`)
74+
75+
err := scanForLogDirectives(configPath, content)
76+
if err != nil {
77+
t.Fatalf("Scan failed: %v", err)
78+
}
79+
80+
logs := GetAllLogPaths()
81+
if len(logs) != 0 {
82+
t.Errorf("Expected 0 logs (all 'off'), got %d", len(logs))
83+
for _, log := range logs {
84+
t.Logf("Found: %s (%s)", log.Path, log.Type)
85+
}
86+
}
87+
})
88+
89+
// Test 4: Mixed with format parameters
90+
t.Run("With format parameters", func(t *testing.T) {
91+
ClearLogCache()
92+
content := []byte(`
93+
server {
94+
access_log /var/log/nginx/access.log combined buffer=32k;
95+
access_log off;
96+
error_log /var/log/nginx/error.log warn;
97+
}`)
98+
99+
err := scanForLogDirectives(configPath, content)
100+
if err != nil {
101+
t.Fatalf("Scan failed: %v", err)
102+
}
103+
104+
logs := GetAllLogPaths()
105+
if len(logs) != 2 {
106+
t.Errorf("Expected 2 logs, got %d", len(logs))
107+
for _, log := range logs {
108+
t.Logf("Found: %s (%s)", log.Path, log.Type)
109+
}
110+
}
111+
})
112+
}

internal/nginx_log/nginx_log.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ func scanForLogDirectives(configPath string, content []byte) error {
4646
directiveType := string(match[1]) // "access_log" or "error_log"
4747
logPath := string(match[2]) // Path to log file
4848

49+
// Skip if log is disabled with "off"
50+
if logPath == "off" {
51+
continue
52+
}
53+
4954
// Handle relative paths by joining with nginx prefix
5055
if !filepath.IsAbs(logPath) {
5156
logPath = filepath.Join(prefix, logPath)

internal/nginx_log/nginx_log_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package nginx_log
22

33
import (
4+
"strings"
45
"testing"
56
)
67

@@ -299,6 +300,78 @@ func TestLogDirectiveRegex(t *testing.T) {
299300
}
300301
}
301302

303+
// TestAccessLogOff tests that "access_log off;" directives are properly ignored
304+
func TestAccessLogOff(t *testing.T) {
305+
// Clear cache before test
306+
ClearLogCache()
307+
308+
configPath := "/etc/nginx/sites-available/test.conf"
309+
310+
// Content with various access_log directives including "off"
311+
content := []byte(`
312+
server {
313+
listen 80;
314+
server_name example.com;
315+
316+
# Normal access log
317+
access_log /var/log/nginx/access.log;
318+
319+
# Disabled access log - should be ignored
320+
access_log off;
321+
322+
# Another normal access log with format
323+
access_log /var/log/nginx/custom.log combined;
324+
325+
# Error log should work normally
326+
error_log /var/log/nginx/error.log;
327+
328+
# Error log can also be turned off
329+
error_log off;
330+
}
331+
`)
332+
333+
err := scanForLogDirectives(configPath, content)
334+
if err != nil {
335+
t.Fatalf("Scan failed: %v", err)
336+
}
337+
338+
// Should only find 3 valid log paths (excluding "off" directives)
339+
logs := GetAllLogPaths()
340+
expectedCount := 3
341+
if len(logs) != expectedCount {
342+
t.Fatalf("Expected %d logs, got %d. Logs found: %+v", expectedCount, len(logs), logs)
343+
}
344+
345+
// Verify the correct paths were found
346+
expectedPaths := map[string]string{
347+
"/var/log/nginx/access.log": "access",
348+
"/var/log/nginx/custom.log": "access",
349+
"/var/log/nginx/error.log": "error",
350+
}
351+
352+
for _, log := range logs {
353+
expectedType, exists := expectedPaths[log.Path]
354+
if !exists {
355+
t.Errorf("Unexpected log path found: %s", log.Path)
356+
} else if log.Type != expectedType {
357+
t.Errorf("Expected log type %s for path %s, got %s", expectedType, log.Path, log.Type)
358+
}
359+
delete(expectedPaths, log.Path)
360+
}
361+
362+
// Check that all expected paths were found
363+
for path := range expectedPaths {
364+
t.Errorf("Expected log path not found: %s", path)
365+
}
366+
367+
// Verify "off" was not treated as a path
368+
for _, log := range logs {
369+
if log.Path == "off" || strings.Contains(log.Path, "off") {
370+
t.Errorf("'off' should not be treated as a log path: %s", log.Path)
371+
}
372+
}
373+
}
374+
302375
// TestIsCommentedMatch tests the isCommentedMatch function directly
303376
func TestIsCommentedMatch(t *testing.T) {
304377
testCases := []struct {

0 commit comments

Comments
 (0)