-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtailer_test.go
More file actions
186 lines (156 loc) · 4.91 KB
/
tailer_test.go
File metadata and controls
186 lines (156 loc) · 4.91 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
package main
import (
"io/ioutil"
"os"
"strings"
"testing"
"time"
"github.com/Shimmur/logtailer/cache"
. "github.com/smartystreets/goconvey/convey"
)
func Test_NewTailer(t *testing.T) {
Convey("NewTailer()", t, func() {
pod := &Pod{Name: "venerable bede"}
cache := cache.NewCache(5, "/tmp/testcache")
logOutput := &mockLogOutput{}
tailer := NewTailer(pod, cache, logOutput)
So(tailer.Pod, ShouldEqual, pod)
So(tailer.LogChan, ShouldNotBeNil)
So(tailer.logger, ShouldEqual, logOutput)
So(tailer.localCache, ShouldNotBeNil)
So(tailer.looper, ShouldNotBeNil)
})
}
func Test_TailLogs(t *testing.T) {
Convey("TailLogs()", t, func() {
disco := NewDirListDiscoverer(fixturesDir, "dev")
pod := &Pod{Name: "venerable bede"}
cache := cache.NewCache(5, "/tmp/testcache")
logOutput := &mockLogOutput{}
tailer := NewTailer(pod, cache, logOutput)
pods, err := disco.Discover()
So(err, ShouldBeNil)
So(pods, ShouldNotBeEmpty)
// Make sure we have the chopper fixture, which has more than one logfile
podName := pods[1].Name
So(podName, ShouldEqual, "default_chopper-f5b66c6bf-cgslk_9df92617-0407-470e-8182-a506aa7e0499")
// Get chopper's logs
logFiles, err := disco.LogFiles(podName)
So(err, ShouldBeNil)
So(logFiles, ShouldNotBeEmpty)
Reset(func() {
// Empty the fixture files
for _, tail := range tailer.LogTails {
_ = ioutil.WriteFile(tail.Filename, []byte{}, 0644)
}
})
Convey("opens tails on all the logs and caches them", func() {
_ = LogCapture(func() {
err := tailer.TailLogs(logFiles)
So(err, ShouldBeNil)
})
// Make sure we are tracking more than one file
So(len(tailer.LogTails), ShouldEqual, 4)
So(
tailer.LogTails["fixtures/pods/default_chopper-f5b66c6bf-cgslk_9df92617-0407-470e-8182-a506aa7e0499/chopper/0.log"],
ShouldNotBeNil,
)
So(
tailer.LogTails["fixtures/pods/default_chopper-f5b66c6bf-cgslk_9df92617-0407-470e-8182-a506aa7e0499/chopper/1.log"],
ShouldNotBeNil,
)
So(
tailer.LogTails["fixtures/pods/default_chopper-f5b66c6bf-cgslk_9df92617-0407-470e-8182-a506aa7e0499/logproxy/0.log"],
ShouldNotBeNil,
)
So(
tailer.LogTails["fixtures/pods/default_chopper-f5b66c6bf-cgslk_9df92617-0407-470e-8182-a506aa7e0499/vault-init/0.log"],
ShouldNotBeNil,
)
tailer.Run()
Reset(tailer.Stop)
// Nothing should be cached yet
So(len(tailer.localCache), ShouldEqual, 0)
// Put something into the logfiles
for _, tail := range tailer.LogTails {
logF, err := os.OpenFile(tail.Filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
So(err, ShouldBeNil)
logF.WriteString("this is a test message\n")
logF.Close()
}
timeout := time.After(300 * time.Millisecond)
// We have to wait for the files to flush to the tail
for {
select {
case <-timeout:
So("we should have received something", ShouldNotBeEmpty)
default: // keep going
}
time.Sleep(1 * time.Millisecond)
if logOutput.LastLogged != nil {
break
}
}
// Now we should know about all of their offsets
tailer.lock.RLock()
So(len(tailer.localCache), ShouldEqual, 4)
tailer.lock.RUnlock()
logOutput.Lock()
defer logOutput.Unlock()
So(logOutput.CallCount, ShouldEqual, 4)
})
Convey("passes on shutdown message to the log output", func() {
tailer.Run()
tailer.Stop()
So(logOutput.StopWasCalled, ShouldBeTrue)
})
Convey("removes logs we're no longer seeing", func() {
_ = LogCapture(func() {
err := tailer.TailLogs(logFiles)
So(err, ShouldBeNil)
})
So(len(tailer.LogTails), ShouldEqual, 4)
logFiles = logFiles[1:3]
capture := LogCapture(func() {
err := tailer.TailLogs(logFiles)
So(err, ShouldBeNil)
})
numberOfDroppedLogs := strings.Count(capture, "Dropping tail")
So(numberOfDroppedLogs, ShouldEqual, 2)
So(len(tailer.LogTails), ShouldEqual, 2)
})
Convey("extracts and logs the container name", func() {
_ = LogCapture(func() {
err := tailer.TailLogs(logFiles)
So(err, ShouldBeNil)
tailer.Run()
})
Reset(tailer.Stop)
// Only send on one of the logs, so we can check the resulting container name
for _, tail := range tailer.LogTails {
if strings.Contains(tail.Filename, "vault-init") {
logF, err := os.OpenFile(tail.Filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
So(err, ShouldBeNil)
logF.WriteString("this is a test message\n")
logF.Close()
}
}
// We have to wait for the files to flush to the tail
timeout := time.After(300 * time.Millisecond)
for {
select {
case <-timeout:
So("we should have received something", ShouldNotBeEmpty)
default: // keep going
}
time.Sleep(1 * time.Millisecond)
if logOutput.LastLogged != nil {
break
}
}
So(logOutput.LastLogged, ShouldNotBeNil)
So(logOutput.LastLogged.Text, ShouldEqual, "this is a test message")
So(logOutput.LastLogged.Container, ShouldEqual, "vault-init")
})
})
}