1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
package integrationtests
import (
"context"
"fmt"
"os"
"strings"
"sync"
"testing"
"time"
"github.com/mimecast/dtail/internal/config"
)
// TestDGrepLiteralModeInfo verifies that the server logs info message when using literal mode
func TestDGrepLiteralModeInfo(t *testing.T) {
if !config.Env("DTAIL_INTEGRATION_TEST_RUN_MODE") {
t.Log("Skipping")
return
}
cleanupTmpFiles(t)
testLogger := NewTestLogger("TestDGrepLiteralModeInfo")
defer testLogger.WriteLogFile()
// Create test data
testData := `ERROR test line 1
WARNING test line 2
ERROR test line 3
INFO test line 4
ERROR test line 5
`
testFile := "literal_info_test.log.tmp"
if err := os.WriteFile(testFile, []byte(testData), 0644); err != nil {
t.Fatal("Failed to create test file:", err)
}
defer os.Remove(testFile)
// Test patterns - both literal and regex
tests := []struct {
name string
pattern string
expectLiteral bool
expectedCount int
}{
{
name: "SimpleLiteral",
pattern: "ERROR",
expectLiteral: true,
expectedCount: 3,
},
{
name: "LiteralWithSpace",
pattern: "ERROR test",
expectLiteral: true,
expectedCount: 3,
},
{
name: "RegexPattern",
pattern: "ERROR.*line [0-9]",
expectLiteral: false,
expectedCount: 3,
},
{
name: "RegexAlternation",
pattern: "(ERROR|WARNING)",
expectLiteral: false,
expectedCount: 4,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
port := getUniquePortNumber()
bindAddress := "localhost"
ctx, cancel := context.WithCancel(context.Background())
ctx = WithTestLogger(ctx, testLogger)
defer cancel()
// Start dserver with info log level to capture our message
stdoutCh, stderrCh, _, err := startCommand(ctx, t,
"", "../dserver",
"--cfg", "none",
"--logger", "stdout",
"--logLevel", "info", // Changed from error to info
"--bindAddress", bindAddress,
"--port", fmt.Sprintf("%d", port),
)
if err != nil {
t.Error(err)
return
}
// Capture server output
var serverOutput strings.Builder
var outputMutex sync.Mutex
outputDone := make(chan struct{})
go func() {
defer close(outputDone)
for {
select {
case line := <-stdoutCh:
outputMutex.Lock()
serverOutput.WriteString(line)
serverOutput.WriteString("\n")
outputMutex.Unlock()
case line := <-stderrCh:
outputMutex.Lock()
serverOutput.WriteString(line)
serverOutput.WriteString("\n")
outputMutex.Unlock()
case <-ctx.Done():
return
}
}
}()
if err := waitForServerReady(ctx, bindAddress, port); err != nil {
t.Error(err)
return
}
// Run dgrep
outFile := fmt.Sprintf("dgrep_info_%s.stdout.tmp", test.name)
defer os.Remove(outFile)
err = runCommandUntilValid(ctx, t, 5, 200*time.Millisecond, outFile, "../dgrep", func() error {
content, readErr := os.ReadFile(outFile)
if readErr != nil {
return fmt.Errorf("failed to read output file: %w", readErr)
}
lines := strings.Split(strings.TrimSpace(string(content)), "\n")
actualCount := 0
for _, line := range lines {
if line != "" {
actualCount++
}
}
if actualCount != test.expectedCount {
return fmt.Errorf("pattern %q: expected %d matches, got %d", test.pattern, test.expectedCount, actualCount)
}
return nil
},
"--plain",
"--cfg", "none",
"--grep", test.pattern,
"--servers", fmt.Sprintf("%s:%d", bindAddress, port),
"--trustAllHosts",
"--noColor",
"--files", testFile)
if err != nil {
t.Errorf("Failed to run dgrep with pattern '%s': %v", test.pattern, err)
return
}
// Give time for server output to be captured
time.Sleep(500 * time.Millisecond)
// Stop server
cancel()
// Wait for output capture goroutine to finish
select {
case <-outputDone:
case <-time.After(2 * time.Second):
t.Log("Warning: output capture goroutine did not finish in time")
}
// Check server output for literal mode message
outputMutex.Lock()
serverLog := serverOutput.String()
outputMutex.Unlock()
// The server uses structured logging, so the message appears as "pattern:|<pattern>"
literalMsg := fmt.Sprintf("Using optimized literal string matching for pattern:|%s", test.pattern)
containsLiteralMsg := strings.Contains(serverLog, literalMsg)
if test.expectLiteral && !containsLiteralMsg {
t.Errorf("Expected literal mode info message for pattern '%s' but didn't find it", test.pattern)
t.Logf("Server output:\n%s", serverLog)
} else if !test.expectLiteral && containsLiteralMsg {
t.Errorf("Did not expect literal mode info message for pattern '%s' but found it", test.pattern)
t.Logf("Server output:\n%s", serverLog)
}
})
}
}
|