summaryrefslogtreecommitdiff
path: root/integrationtests/dcat_test.go
blob: ebaffa2f96dc85e751a0afc02c1f2bee8879d313 (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
package integrationtests

import (
	"context"
	"os"
	"testing"

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

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

	inFiles := []string{"dcat1a.txt", "dcat1b.txt", "dcat1c.txt", "dcat1d.txt"}

	// 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) {
			// Test all files in both modes, restarting server for each file in server mode
			for _, inFile := range inFiles {
				if err := testDCat1(t, inFile, mode.useServer); err != nil {
					t.Error(err)
					return
				}
			}
		})
	}
}

func testDCat1(t *testing.T, inFile string, useServer bool) error {
	outFile := "dcat1.out"

	if useServer {
		// Now that channel buffer issue is fixed, use the actual test file
		return testDCatWithServer(t, []string{"--plain", "--cfg", "none", "--quiet", inFile}, outFile, inFile)
	} else {
		_, err := runCommand(context.TODO(), t, outFile,
			"../dcat", "--plain", "--cfg", "none", inFile)
		if err != nil {
			return err
		}
		if err := compareFiles(t, outFile, inFile); err != nil {
			return err
		}

		os.Remove(outFile)
		return nil
	}
}

func TestDCat2(t *testing.T) {
	if !config.Env("DTAIL_INTEGRATION_TEST_RUN_MODE") {
		return
	}
	inFile := "dcat2.txt"
	expectedFile := "dcat2.txt.expected"
	outFile := "dcat2.out"

	args := []string{"--plain", "--logLevel", "error", "--cfg", "none"}

	// Cat file 100 times in one session.
	for i := 0; i < 100; i++ {
		args = append(args, inFile)
	}

	// 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 mode.useServer {
				// Now that channel buffer issue is fixed, enable server mode for TestDCat2
				if err := testDCatWithServer(t, args, outFile, expectedFile); err != nil {
					t.Error(err)
					return
				}
			} else {
				_, err := runCommand(context.TODO(), t, outFile, "../dcat", args...)
				if err != nil {
					t.Error(err)
					return
				}

				if err := compareFilesContents(t, outFile, expectedFile); err != nil {
					t.Error(err)
					return
				}

				os.Remove(outFile)
			}
		})
	}
}

func TestDCat3(t *testing.T) {
	if !config.Env("DTAIL_INTEGRATION_TEST_RUN_MODE") {
		return
	}
	inFile := "dcat3.txt"
	expectedFile := "dcat3.txt.expected"
	outFile := "dcat3.out"

	args := []string{"--plain", "--logLevel", "error", "--cfg", "none", inFile}

	// 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 mode.useServer {
				if err := testDCatWithServerContents(t, args, outFile, expectedFile); err != nil {
					t.Error(err)
				}
			} else {
				// Notice, with DTAIL_INTEGRATION_TEST_RUN_MODE the DTail max line length is set
				// to 1024!
				_, err := runCommand(context.TODO(), t, outFile, "../dcat", args...)
				if err != nil {
					t.Error(err)
					return
				}

				if err := compareFilesContents(t, outFile, expectedFile); err != nil {
					t.Error(err)
					return
				}

				os.Remove(outFile)
			}
		})
	}
}

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

	inFile := "dcatcolors.txt"
	outFile := "dcatcolors.out"
	expectedFile := "dcatcolors.expected"
	args := []string{"--logLevel", "error", "--cfg", "none", inFile}

	// 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 mode.useServer {
				// Now that channel buffer issue is fixed, enable server mode for TestDCatColors
				if err := testDCatWithServer(t, args, outFile, expectedFile); err != nil {
					t.Error(err)
					return
				}
			} else {
				_, err := runCommand(context.TODO(), t, outFile, "../dcat", args...)

				if err != nil {
					t.Error(err)
					return
				}

				if err := compareFiles(t, outFile, expectedFile); err != nil {
					t.Error(err)
					return
				}

				os.Remove(outFile)
			}
		})
	}
}