-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegration_test.go
More file actions
309 lines (264 loc) · 7.56 KB
/
integration_test.go
File metadata and controls
309 lines (264 loc) · 7.56 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package genlog_test
import (
"bufio"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
"github.com/P1llus/genlog"
)
// TestLibraryIntegration tests using genlog as a library
func TestLibraryIntegration(t *testing.T) {
// Create a temporary directory for test outputs
tmpDir, err := os.MkdirTemp("", "genlog-test-*")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)
outputFile := filepath.Join(tmpDir, "test-output.log")
expectedLines := int64(10000)
// Create configuration
cfg := &genlog.Config{
Templates: []genlog.Template{
{
Template: "{{timestamp}} [{{level}}] {{message}}",
Weight: 10,
},
},
CustomTypes: map[string][]string{
"level": {"INFO", "WARN", "ERROR"},
"message": {"Test message 1", "Test message 2"},
},
Outputs: []genlog.OutputConfig{
{
Type: "file",
Config: map[string]interface{}{
"path": outputFile,
},
},
},
Limits: genlog.Limits{
MaxCount: expectedLines,
},
Seed: 12345, // Use seed for reproducibility
}
// Create and run generator
gen, err := genlog.New(cfg)
if err != nil {
t.Fatalf("Failed to create generator: %v", err)
}
if err := gen.Start(); err != nil {
t.Fatalf("Failed to start generator: %v", err)
}
// Wait for completion with timeout
select {
case <-gen.Done():
// Success
case <-time.After(30 * time.Second):
t.Fatal("Generation timed out after 30 seconds")
}
if err := gen.Stop(); err != nil {
t.Fatalf("Failed to stop generator: %v", err)
}
// Verify output file exists
if _, err := os.Stat(outputFile); os.IsNotExist(err) {
t.Fatalf("Output file was not created: %s", outputFile)
}
// Count lines in output file
lineCount, err := countLines(outputFile)
if err != nil {
t.Fatalf("Failed to count lines: %v", err)
}
if lineCount != expectedLines {
t.Errorf("Expected %d lines, got %d", expectedLines, lineCount)
}
// Verify stats match
stats := gen.Stats().Snapshot()
if stats.Generated != expectedLines {
t.Errorf("Stats: Expected %d generated, got %d", expectedLines, stats.Generated)
}
if stats.Written != expectedLines {
t.Errorf("Stats: Expected %d written, got %d", expectedLines, stats.Written)
}
t.Logf("Successfully generated %d logs in %v (%.0f logs/sec)",
lineCount, stats.Duration, stats.LogsPerSecond)
}
// TestCLIIntegration tests the CLI tool
func TestCLIIntegration(t *testing.T) {
// Build the CLI binary
tmpDir, err := os.MkdirTemp("", "genlog-cli-test-*")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)
binaryPath := filepath.Join(tmpDir, "genlog")
buildCmd := exec.Command("go", "build", "-o", binaryPath, "./cmd/genlog")
if output, err := buildCmd.CombinedOutput(); err != nil {
t.Fatalf("Failed to build CLI: %v\nOutput: %s", err, output)
}
// Test --version flag
t.Run("version_flag", func(t *testing.T) {
cmd := exec.Command(binaryPath, "-version")
output, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("Failed to run --version: %v\nOutput: %s", err, output)
}
if len(output) == 0 {
t.Error("Expected version output, got empty")
}
t.Logf("Version output: %s", output)
})
// Test --init flag
t.Run("init_flag", func(t *testing.T) {
configPath := filepath.Join(tmpDir, "test-config.yaml")
cmd := exec.Command(binaryPath, "-init", configPath)
output, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("Failed to generate config: %v\nOutput: %s", err, output)
}
// Verify config file was created
if _, err := os.Stat(configPath); os.IsNotExist(err) {
t.Fatalf("Config file was not created: %s", configPath)
}
t.Logf("Init output: %s", output)
// Test that init fails if file already exists
cmd = exec.Command(binaryPath, "-init", configPath)
output, err = cmd.CombinedOutput()
if err == nil {
t.Error("Expected error when file already exists, got none")
}
if !strings.Contains(string(output), "already exists") {
t.Errorf("Expected 'already exists' error message, got: %s", output)
}
t.Logf("Expected error output: %s", output)
})
// Test log generation
t.Run("generate_logs", func(t *testing.T) {
configPath := filepath.Join(tmpDir, "gen-config.yaml")
outputPath := filepath.Join(tmpDir, "cli-output.log")
expectedLines := 10000
// Create a simple config
configContent := `templates:
- template: '{{timestamp}} [{{level}}] {{message}}'
weight: 1
custom_types:
level: [INFO, WARN, ERROR]
message: ["Test message"]
outputs:
- type: file
config:
path: "` + outputPath + `"
limits:
max_count: 10000
`
if err := os.WriteFile(configPath, []byte(configContent), 0644); err != nil {
t.Fatalf("Failed to write config: %v", err)
}
// Run genlog
cmd := exec.Command(binaryPath, "-config", configPath)
output, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("Failed to generate logs: %v\nOutput: %s", err, output)
}
// Verify output file exists
if _, err := os.Stat(outputPath); os.IsNotExist(err) {
t.Fatalf("Output file was not created: %s", outputPath)
}
// Count lines
lineCount, err := countLines(outputPath)
if err != nil {
t.Fatalf("Failed to count lines: %v", err)
}
if lineCount != int64(expectedLines) {
t.Errorf("Expected %d lines, got %d", expectedLines, lineCount)
}
t.Logf("CLI generated %d logs successfully", lineCount)
})
}
// TestMultipleOutputs tests generation to multiple outputs simultaneously.
// Note: With the current architecture, logs are distributed among outputs,
// not replicated to each output. This test verifies that the total across
// all outputs matches the expected count.
func TestMultipleOutputs(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "genlog-multi-test-*")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)
output1 := filepath.Join(tmpDir, "output1.log")
output2 := filepath.Join(tmpDir, "output2.log")
expectedTotal := int64(5000)
cfg := &genlog.Config{
Templates: []genlog.Template{
{
Template: "{{timestamp}} [{{level}}] {{message}}",
Weight: 1,
},
},
CustomTypes: map[string][]string{
"level": {"INFO"},
"message": {"Test"},
},
Outputs: []genlog.OutputConfig{
{
Type: "file",
Config: map[string]interface{}{
"path": output1,
},
},
{
Type: "file",
Config: map[string]interface{}{
"path": output2,
},
},
},
Limits: genlog.Limits{
MaxCount: expectedTotal,
},
}
gen, err := genlog.New(cfg)
if err != nil {
t.Fatalf("Failed to create generator: %v", err)
}
gen.Start()
gen.Wait()
gen.Stop()
// Count lines in both outputs
count1, err := countLines(output1)
if err != nil {
t.Fatalf("Failed to count lines in output1: %v", err)
}
count2, err := countLines(output2)
if err != nil {
t.Fatalf("Failed to count lines in output2: %v", err)
}
total := count1 + count2
// Verify that the total matches expected count
// Logs are distributed between outputs based on which consumer grabs them
if total != expectedTotal {
t.Errorf("Expected total %d lines across outputs, got %d (output1: %d, output2: %d)",
expectedTotal, total, count1, count2)
}
t.Logf("Successfully distributed %d logs across 2 outputs (output1: %d, output2: %d)",
total, count1, count2)
}
// countLines counts the number of lines in a file
func countLines(filepath string) (int64, error) {
file, err := os.Open(filepath)
if err != nil {
return 0, err
}
defer file.Close()
var count int64
scanner := bufio.NewScanner(file)
for scanner.Scan() {
count++
}
if err := scanner.Err(); err != nil {
return 0, err
}
return count, nil
}