-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathforwarder_test.go
230 lines (188 loc) · 5.74 KB
/
forwarder_test.go
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
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"os"
"testing"
)
const (
testDir = "/tmp"
testMsg1 = "FooBar"
testMsg2 = "FooBaz"
)
var (
isNoop = true
testLogPattern = "tailr_test_*"
testOutputChan = make(chan string, 1)
)
func fakeOutputHandler(noopOutputChan <-chan string) {
msg := <-noopOutputChan
testOutputChan <- msg
}
func genTempDir(srcDir string) (string, error) {
tempDir, err := ioutil.TempDir(srcDir, "tailr_testdir_")
if err != nil {
return "", err
}
return tempDir, nil
}
func genTempFile(srcDir string) (string, error) {
tempFile, err := ioutil.TempFile(srcDir, "tailr_test_")
if err != nil {
return "", err
}
return tempFile.Name(), nil
}
func TestUpdateOffsetFile(t *testing.T) {
tailrConfig := NewTailrConfig()
// create a temp file for offset
tempOffsetFile, err := genTempFile(testDir)
if err != nil {
t.Errorf("Error creating temporary file %v", err)
}
defer os.Remove(tempOffsetFile)
tailrConfig.offsetFilePath = &tempOffsetFile
tailrConfig.fileOffsetMap[tempOffsetFile] = 100
tailrConfig.updateOffsetFile()
newOffsetMap, err := ioutil.ReadFile(*tailrConfig.offsetFilePath)
if err != nil {
t.Errorf("Error reading from the temporary offset file %v", err)
}
json, err := json.Marshal(tailrConfig.fileOffsetMap)
if err != nil {
t.Errorf("Error marshalling offsetMap %v", err)
}
// check if the offsets in memory and file are same
if !bytes.Equal(newOffsetMap, json) {
t.Errorf("Error updating offset file. Expected %v, got %v", string(newOffsetMap), string(json))
}
}
func TestCheckOffsetFile(t *testing.T) {
tailrConfig := NewTailrConfig()
// create a temp file for offset
tempOffsetFile, err := genTempFile(testDir)
if err != nil {
t.Errorf("Error creating temporary file %v", err)
}
tailrConfig.offsetFilePath = &tempOffsetFile
// for empty offset file, checkOffsetFile() should raise error
err = tailrConfig.checkOffsetFile()
if err == nil {
t.Error("Expected checkOffsetFile() to throw error for empty file")
}
os.Remove(tempOffsetFile)
// if the offset file is defined but doesnt exists, checkOffsetFile() should raise error
err = tailrConfig.checkOffsetFile()
if err == nil {
t.Error("Expected checkOffsetFile() to throw error if no offset file exists")
}
// create a temp file for offset
tempOffsetFile, err = genTempFile(testDir)
if err != nil {
t.Errorf("Error creating temporary file %v", err)
}
defer os.Remove(tempOffsetFile)
tailrConfig.offsetFilePath = &tempOffsetFile
// update offset file
tailrConfig.fileOffsetMap[tempOffsetFile] = 100
tailrConfig.updateOffsetFile()
// For a valid offset file, checkOffsetFile() shouldn't raise any error
err = tailrConfig.checkOffsetFile()
if err != nil {
t.Errorf("Error checking offset file %v", err)
}
}
func TestStartFileForwarder(t *testing.T) {
tailrConfig := NewTailrConfig()
// create a temp file for offset
tempOffsetFile, err := genTempFile(testDir)
if err != nil {
t.Errorf("Error creating temporary offset file %v", err)
}
tailrConfig.offsetFilePath = &tempOffsetFile
tailrConfig.noop = &isNoop
// create a temp file for offset
tempLogFile, err := genTempFile(testDir)
if err != nil {
t.Errorf("Error creating temporary log file %v", err)
}
defer func() {
os.Remove(tempLogFile)
// wait for the go routine to stop
tailrConfig.watcherWaitgroup.Wait()
os.Remove(tempOffsetFile)
}()
// spin up the fake output handler
go fakeOutputHandler(tailrConfig.noopOutputChan)
// spin up the file forwarder
tailrConfig.watcherWaitgroup.Add(1)
go tailrConfig.startFileForwarder(tempLogFile)
// lets write something to the temporary log file
f, err := os.OpenFile(tempLogFile, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
t.Errorf("Error opening temp log file %v", err)
}
_, err = f.Write([]byte(testMsg1 + "\n"))
if err != nil {
t.Errorf("Error writing to temp log file %v", err)
}
if err := f.Close(); err != nil {
t.Errorf("Error closing temp log file %v", err)
}
// get the message from the fake output handler and compare
output := <-testOutputChan
if output != testMsg1 {
t.Errorf("Message mismatch. Expected %v and got %v", testMsg1, output)
}
}
func TestDirWatcher(t *testing.T) {
tailrConfig := NewTailrConfig()
// create a temp file for offset
tempOffsetFile, err := genTempFile(testDir)
if err != nil {
t.Errorf("Error creating temporary offset file %v", err)
}
tailrConfig.offsetFilePath = &tempOffsetFile
tailrConfig.noop = &isNoop
tailrConfig.logPattern = &testLogPattern
// create a temp directory to watch
tempLogDir, err := genTempDir(testDir)
if err != nil {
t.Errorf("Error creating temporary log directory %v", err)
}
defer func() {
tailrConfig.Kill(nil)
// wait for the go routine to stop
tailrConfig.watcherWaitgroup.Wait()
os.RemoveAll(tempLogDir)
os.Remove(tempOffsetFile)
}()
// spin up the fake output handler
go fakeOutputHandler(tailrConfig.noopOutputChan)
// spin up the dirwatcher for the temp log directory
tailrConfig.watcherWaitgroup.Add(1)
go tailrConfig.dirWatcher(tempLogDir)
// create a temp log file inside our temp log directory
tempLogFile, err := genTempFile(tempLogDir)
if err != nil {
t.Errorf("Error creating temporary log file %v", err)
}
// lets write something to the temporary log file
f, err := os.OpenFile(tempLogFile, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
t.Errorf("Error opening temp log file %v", err)
}
_, err = f.Write([]byte(testMsg1 + "\n"))
if err != nil {
t.Errorf("Error writing to temp log file %v", err)
}
if err := f.Close(); err != nil {
t.Errorf("Error closing temp log file %v", err)
}
// get the message from the fake output handler and compare
output := <-testOutputChan
if output != testMsg1 {
t.Errorf("Message mismatch. Expected %v and got %v", testMsg1, output)
}
}