summaryrefslogtreecommitdiff
path: root/integrationtests/dmap_test.go
blob: 25819f5e701ed90a95e5d434534977abdb7e6a9d (plain)
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package integrationtests

import (
	"context"
	"fmt"
	"os"
	"testing"

	"github.com/mimecast/dtail/internal/config"
)

func TestDMap1(t *testing.T) {
	if !config.Env("DTAIL_INTEGRATION_TEST_RUN_MODE") {
		t.Log("Skipping")
		return
	}

	// Test both serverless and server modes
	modes := []struct {
		name      string
		useServer bool
	}{
		{"Serverless", false},
		{"WithServer", true},
	}

	for _, mode := range modes {
		t.Run(mode.name, func(t *testing.T) {
			if err := testDMap1(t, mode.useServer); err != nil {
				t.Error(err)
				return
			}
		})
	}
}

func testDMap1(t *testing.T, useServer bool) error {
	testTable := map[string]string{
		"a": "from STATS select count($line),last($time)," +
			"avg($goroutines),min(concurrentConnections),max(lifetimeConnections) " +
			"group by $hostname",
		"b": "from STATS select count($line),last($time)," +
			"avg($goroutines),min(concurrentConnections),max(lifetimeConnections) " +
			"group by $hostname where lifetimeConnections >= 3",
		"c": "from STATS select count($line),last($time)," +
			"avg($goroutines),min(concurrentConnections),max(lifetimeConnections) " +
			"group by $hostname where $time eq \"1002-071949\"",
		"d": "from STATS select $mask,$md5,$foo,$bar,$baz,last($time)," +
			" set $mask = maskdigits($time), $md5 = md5sum($time), " +
			"$foo = 42, $bar = \"baz\", $baz = $time group by $hostname",
	}

	for subtestName, query := range testTable {
		t.Log("Testing dmap with input file")
		if err := testDmap1Sub(t, query, subtestName, false, useServer); err != nil {
			t.Error(err)
			return err
		}
		t.Log("Testing dmap with stdin input pipe")
		if err := testDmap1Sub(t, query, subtestName, true, useServer); err != nil {
			t.Error(err)
			return err
		}
	}
	return nil
}

func testDmap1Sub(t *testing.T, query, subtestName string, usePipe bool, useServer bool) error {
	inFile := "mapr_testdata.log"
	csvFile := fmt.Sprintf("dmap1%s.csv.tmp", subtestName)
	expectedCsvFile := fmt.Sprintf("dmap1%s.csv.expected", subtestName)
	expectedQueryFile := fmt.Sprintf("dmap1%s.csv.query.expected", subtestName)

	queryFile := fmt.Sprintf("%s.query", csvFile)
	query = fmt.Sprintf("%s outfile %s", query, csvFile)

	if useServer {
		// Server mode testing
		var args []string
		if usePipe {
			// For pipe mode with server, we need to handle this differently
			// DMap with server doesn't support stdin pipe in the same way
			// So we'll just test file mode for server
			args = []string{"--query", query, "--logger", "stdout", "--logLevel", "info", "--noColor", inFile}
		} else {
			args = []string{"--query", query, "--logger", "stdout", "--logLevel", "info", "--noColor", inFile}
		}
		return testDMapWithServer(t, args, csvFile, expectedCsvFile, queryFile, expectedQueryFile)
	} else {
		// Serverless mode testing (original code)
		ctx, cancel := context.WithCancel(context.Background())
		defer cancel()

		var stdoutCh, stderrCh <-chan string
		var cmdErrCh <-chan error
		var err error

		if usePipe {
			stdoutCh, stderrCh, cmdErrCh, err = startCommand(ctx, t,
				inFile, "../dmap",
				"--cfg", "none",
				"--query", query,
				"--logger", "stdout",
				"--logLevel", "info",
				"--noColor")
		} else {
			stdoutCh, stderrCh, cmdErrCh, err = startCommand(ctx, t,
				"", "../dmap",
				"--cfg", "none",
				"--query", query,
				"--logger", "stdout",
				"--logLevel", "info",
				"--noColor",
				inFile)
		}

		if err != nil {
			return err
		}

		waitForCommand(ctx, t, stdoutCh, stderrCh, cmdErrCh)

		if err := compareFiles(t, csvFile, expectedCsvFile); err != nil {
			return err
		}
		if err := compareFiles(t, queryFile, expectedQueryFile); err != nil {
			return err
		}

		os.Remove(csvFile)
		os.Remove(queryFile)
		return nil
	}
}

func TestDMap2(t *testing.T) {
	if !config.Env("DTAIL_INTEGRATION_TEST_RUN_MODE") {
		t.Log("Skipping")
		return
	}

	// Test both serverless and server modes
	modes := []struct {
		name      string
		useServer bool
	}{
		{"Serverless", false},
		{"WithServer", true},
	}

	for _, mode := range modes {
		t.Run(mode.name, func(t *testing.T) {
			if err := testDMap2(t, mode.useServer); err != nil {
				t.Error(err)
				return
			}
		})
	}
}

func testDMap2(t *testing.T, useServer bool) error {
	inFile := "mapr_testdata.log"
	csvFile := "dmap2.csv.tmp"
	expectedCsvFile := "dmap2.csv.expected"
	expectedQueryFile := "dmap2.csv.query.expected"
	outFile := "dmap2.stdout.tmp"

	queryFile := fmt.Sprintf("%s.query", csvFile)

	query := fmt.Sprintf("from STATS select count($time),$time,max($goroutines),"+
		"avg($goroutines),min($goroutines) group by $time order by count($time) "+
		"outfile %s", csvFile)

	if useServer {
		// Server mode testing
		args := []string{"--query", query, "--cfg", "none", inFile}
		return testDMapWithServer(t, args, csvFile, expectedCsvFile, queryFile, expectedQueryFile)
	} else {
		// Serverless mode testing (original code)
		_, err := runCommand(context.TODO(), t, outFile,
			"../dmap", "--query", query, "--cfg", "none", inFile)
		if err != nil {
			return err
		}

		if err := compareFilesContents(t, csvFile, expectedCsvFile); err != nil {
			return err
		}
		if err := compareFiles(t, queryFile, expectedQueryFile); err != nil {
			return err
		}

		os.Remove(outFile)
		os.Remove(csvFile)
		os.Remove(queryFile)
		return nil
	}
}

func TestDMap3(t *testing.T) {
	if !config.Env("DTAIL_INTEGRATION_TEST_RUN_MODE") {
		t.Log("Skipping")
		return
	}

	// Test both serverless and server modes
	modes := []struct {
		name      string
		useServer bool
	}{
		{"Serverless", false},
		{"WithServer", true},
	}

	for _, mode := range modes {
		t.Run(mode.name, func(t *testing.T) {
			if err := testDMap3(t, mode.useServer); err != nil {
				t.Error(err)
				return
			}
		})
	}
}

func testDMap3(t *testing.T, useServer bool) error {
	inFile := "mapr_testdata.log"
	csvFile := "dmap3.csv.tmp"
	expectedCsvFile := "dmap3.csv.expected"
	expectedQueryFile := "dmap3.csv.query.expected"
	outFile := "dmap3.stdout.tmp"

	queryFile := fmt.Sprintf("%s.query", csvFile)

	query := fmt.Sprintf("from STATS select count($time),$time,max($goroutines),"+
		"avg($goroutines),min($goroutines) group by $time order by count($time) "+
		"outfile %s", csvFile)

	// Create file list - use same count for both modes
	fileList := make([]string, 10)
	for i := range fileList {
		fileList[i] = inFile
	}

	if useServer {
		args := []string{"--query", query, "--cfg", "none", "--logger", "stdout", "--logLevel", "info", "--noColor"}
		args = append(args, fileList...)
		return testDMapWithServer(t, args, csvFile, expectedCsvFile, queryFile, expectedQueryFile)
	} else {
		ctx, cancel := context.WithCancel(context.Background())
		defer cancel()

		args := []string{"--query", query, "--cfg", "none", "--logger", "stdout", "--logLevel", "info", "--noColor"}
		args = append(args, fileList...)

		stdoutCh, stderrCh, cmdErrCh, err := startCommand(ctx, t, "", "../dmap", args...)

		if err != nil {
			return err
		}
		waitForCommand(ctx, t, stdoutCh, stderrCh, cmdErrCh)

		if err := compareFilesContents(t, csvFile, expectedCsvFile); err != nil {
			return err
		}
		if err := compareFiles(t, queryFile, expectedQueryFile); err != nil {
			return err
		}

		os.Remove(outFile)
		os.Remove(csvFile)
		os.Remove(queryFile)
		return nil
	}
}

func TestDMap4Append(t *testing.T) {
	if !config.Env("DTAIL_INTEGRATION_TEST_RUN_MODE") {
		t.Log("Skipping")
		return
	}

	// Test both serverless and server modes
	modes := []struct {
		name      string
		useServer bool
	}{
		{"Serverless", false},
		{"WithServer", true},
	}

	for _, mode := range modes {
		t.Run(mode.name, func(t *testing.T) {
			if err := testDMap4Append(t, mode.useServer); err != nil {
				t.Error(err)
				return
			}
		})
	}
}

func testDMap4Append(t *testing.T, useServer bool) error {
	inFile := "mapr_testdata.log"
	csvFile := "dmap4.csv.tmp"
	expectedCsvFile := "dmap4.csv.expected"
	expectedQueryFile := "dmap4.csv.query.expected"
	outFile := "dmap4.stdout.tmp"

	queryFile := fmt.Sprintf("%s.query", csvFile)

	// Delete in case it exists already. Otherwise, test will fail.
	os.Remove(csvFile)

	query := fmt.Sprintf("from STATS select count($time),$time,max($goroutines),"+
		"avg($goroutines),min($goroutines) group by $time order by count($time) "+
		"outfile append %s", csvFile)

	if useServer {
		// Server mode testing - run twice for append functionality
		args := []string{
			"--query", query,
			"--cfg", "none",
			"--logger", "stdout",
			"--logLevel", "info",
			"--noColor", inFile,
		}
		return testDMapMultipleRunsWithServer(t, args, csvFile, expectedCsvFile, queryFile, expectedQueryFile, 2)
	} else {
		// Serverless mode testing (original code)
		ctx, cancel := context.WithCancel(context.Background())
		defer cancel()

		// Run dmap command twice, it should append in the 2nd iteration the new results to the already existing
		// file as we specified "outfile append". That works transparently for any mapreduce query
		// (e.g. also for the dtail command in streaming mode). But it is easier to test with the dmap
		// command.
		for i := 0; i < 2; i++ {
			stdoutCh, stderrCh, cmdErrCh, err := startCommand(ctx, t,
				"", "../dmap",
				"--query", query,
				"--cfg", "none",
				"--logger", "stdout",
				"--logLevel", "info",
				"--noColor", inFile)

			if err != nil {
				return err
			}
			waitForCommand(ctx, t, stdoutCh, stderrCh, cmdErrCh)
		}

		if err := compareFilesContents(t, csvFile, expectedCsvFile); err != nil {
			return err
		}
		if err := compareFiles(t, queryFile, expectedQueryFile); err != nil {
			return err
		}

		os.Remove(outFile)
		os.Remove(csvFile)
		os.Remove(queryFile)
		return nil
	}
}

func TestDMap5CSV(t *testing.T) {
	if !config.Env("DTAIL_INTEGRATION_TEST_RUN_MODE") {
		t.Log("Skipping")
		return
	}

	// Test both serverless and server modes
	modes := []struct {
		name      string
		useServer bool
	}{
		{"Serverless", false},
		{"WithServer", true},
	}

	for _, mode := range modes {
		t.Run(mode.name, func(t *testing.T) {
			if err := testDMap5CSV(t, mode.useServer); err != nil {
				t.Error(err)
				return
			}
		})
	}
}

func testDMap5CSV(t *testing.T, useServer bool) error {
	inFile := "dmap5.csv.in"
	csvFile := "dmap5.csv.tmp"
	expectedCsvFile := "dmap5.csv.expected"
	expectedQueryFile := "dmap5.csv.query.expected"
	outFile := "dmap5.stdout.tmp"

	queryFile := fmt.Sprintf("%s.query", csvFile)

	// Delete in case it exists already. Otherwise, test will fail.
	os.Remove(csvFile)

	query := fmt.Sprintf("select sum($timecount),last($time),min($min_goroutines),"+
		" group by $hostname"+
		" set $timecount = `count($time)`, $time = `$time`, $min_goroutines = `min($goroutines)`"+
		" logformat csv outfile %s", csvFile)

	if useServer {
		// Server mode testing - run twice (CSV input format with append)
		args := []string{
			"--query", query,
			"--cfg", "none",
			"--logger", "stdout",
			"--logLevel", "info",
			"--noColor", inFile,
		}
		return testDMapMultipleRunsWithServer(t, args, csvFile, expectedCsvFile, queryFile, expectedQueryFile, 2)
	} else {
		// Serverless mode testing (original code)
		ctx, cancel := context.WithCancel(context.Background())
		defer cancel()

		// Run dmap command twice, it should append in the 2nd iteration the new results to the already existing
		// file as we specified "outfile append". That works transparently for any mapreduce query
		// (e.g. also for the dtail command in streaming mode). But it is easier to test with the dmap
		// command.
		for i := 0; i < 2; i++ {
			stdoutCh, stderrCh, cmdErrCh, err := startCommand(ctx, t,
				"", "../dmap",
				"--query", query,
				"--cfg", "none",
				"--logger", "stdout",
				"--logLevel", "info",
				"--noColor", inFile)

			if err != nil {
				return err
			}
			waitForCommand(ctx, t, stdoutCh, stderrCh, cmdErrCh)
		}

		if err := compareFilesContents(t, csvFile, expectedCsvFile); err != nil {
			return err
		}
		if err := compareFiles(t, queryFile, expectedQueryFile); err != nil {
			return err
		}

		os.Remove(outFile)
		os.Remove(csvFile)
		os.Remove(queryFile)
		return nil
	}
}