-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathlog_relay_test.go
More file actions
189 lines (149 loc) · 5.82 KB
/
log_relay_test.go
File metadata and controls
189 lines (149 loc) · 5.82 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
package main
import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/Nitro/sidecar-executor/container"
docker "github.com/fsouza/go-dockerclient"
log "github.com/sirupsen/logrus"
. "github.com/smartystreets/goconvey/convey"
)
func Test_relayLogs(t *testing.T) {
// This test can't run on Travis due to some issue with sending UDP
if os.Getenv("TRAVIS") != "true" {
log.SetOutput(ioutil.Discard)
Convey("relayLogs()", t, func() {
// Stub the docker client
dockerClient := &container.MockDockerClient{
LogOutputString: "this is some stdout text\n",
LogErrorString: "this is some stderr text\n",
}
config, err := initConfig()
So(err, ShouldBeNil)
log.SetOutput(ioutil.Discard)
exec := newSidecarExecutor(dockerClient, &docker.AuthConfiguration{}, config)
exec.config.SendDockerLabels = []string{"Environment", "ServiceName"}
// Stub out the fetcher
fetcher := &mockFetcher{}
exec.fetcher = fetcher
quitChan := make(chan struct{})
tmpdir, _ := ioutil.TempDir("", "testing")
tmpfn := filepath.Join(tmpdir, "log-relay")
Reset(func() { os.RemoveAll(tmpdir) })
Convey("handles both stderr and stdout", func() {
// Capture logging output
result, _ := os.OpenFile(tmpfn, os.O_RDWR|os.O_CREATE, 0644)
// Janky that we have to sleep here, but not a good way to
// sync on this.
go func() { time.Sleep(20 * time.Millisecond); close(quitChan) }()
exec.relayLogs(quitChan, "deadbeef123123123", map[string]string{}, result)
resultBytes, _ := ioutil.ReadFile(tmpfn)
So(string(resultBytes), ShouldContainSubstring, "some stdout text")
So(string(resultBytes), ShouldContainSubstring, "some stderr text")
result.Close()
})
Convey("includes the requested Docker labels", func() {
result, _ := os.OpenFile(tmpfn, os.O_RDWR|os.O_CREATE, 0644)
// Janky that we have to sleep here, but not a good way to
// sync on this.
go func() { time.Sleep(1 * time.Millisecond); close(quitChan) }()
labels := map[string]string{
"Environment": "prod",
"ServiceName": "beowulf",
}
exec.relayLogs(quitChan, "deadbeef123123123", labels, result)
exec.config.ContainerLogsStdout = true
resultBytes, _ := ioutil.ReadFile(tmpfn)
So(string(resultBytes), ShouldContainSubstring, `"Environment":"prod"`)
So(string(resultBytes), ShouldContainSubstring, `"ServiceName":"beowulf"`)
result.Close()
})
Convey("sends the hostname", func() {
result, _ := os.OpenFile(tmpfn, os.O_RDWR|os.O_CREATE, 0644)
// Janky that we have to sleep here, but not a good way to
// sync on this.
go func() { time.Sleep(1 * time.Millisecond); close(quitChan) }()
labels := map[string]string{
"Environment": "prod",
"ServiceName": "beowulf",
}
exec.relayLogs(quitChan, "deadbeef123123123", labels, result)
exec.config.ContainerLogsStdout = true
resultBytes, _ := ioutil.ReadFile(tmpfn)
So(exec.config.LogHostname, ShouldNotBeEmpty)
So(string(resultBytes), ShouldContainSubstring, `"Hostname":"`+exec.config.LogHostname)
})
Convey("shuts down after RelaySyslogStartupTime when configured", func() {
result, _ := os.OpenFile(tmpfn, os.O_RDWR|os.O_CREATE, 0644)
exec.config.RelaySyslogStartupOnly = true
exec.config.RelaySyslogStartupTime = 1 * time.Millisecond
// Attempt time-limited read from the channel. If we get a read,
// the channel closed.
var channelClosedSuccess bool
go func() {
select {
case <-quitChan:
channelClosedSuccess = true
case <-time.After(10 * time.Millisecond):
}
}()
exec.relayLogs(quitChan, "deadbeef123123123", map[string]string{}, result)
exec.config.ContainerLogsStdout = true
So(channelClosedSuccess, ShouldBeTrue)
resultBytes, _ := ioutil.ReadFile(tmpfn)
So(resultBytes, ShouldNotBeEmpty)
})
})
}
}
func Test_handleOneStream(t *testing.T) {
Convey("handleOneStream()", t, func() {
fetcher := &mockFetcher{}
client := &container.MockDockerClient{}
config, err := initConfig()
So(err, ShouldBeNil)
log.SetOutput(ioutil.Discard)
exec := newSidecarExecutor(client, &docker.AuthConfiguration{}, config)
exec.fetcher = fetcher
quitChan := make(chan struct{})
data := []byte("testing testing testing\n123\n456")
reader := bytes.NewReader(data)
var result bytes.Buffer
logger := log.New()
logger.SetOutput(&result)
relay := logger.WithFields(log.Fields{"SomeTag": "test"})
Reset(func() {
result.Reset()
})
Convey("splits line appropriately", func() {
// This test exist on EOF from the buffer
var captured bytes.Buffer // System log, NOT logger
log.SetOutput(&captured)
exec.handleOneStream(quitChan, "stdout", relay, reader)
So(result.String(), ShouldContainSubstring,
`level=info msg="testing testing testing" SomeTag=test`)
So(len(strings.Split(result.String(), "\n")), ShouldEqual, 4)
So(captured.String(), ShouldNotContainSubstring, "error reading Docker")
})
Convey("errors out when the name is not stderr or stdout", func() {
var captured bytes.Buffer // System log, NOT logger
log.SetOutput(&captured)
exec.handleOneStream(quitChan, "junk", relay, reader)
So(captured.String(), ShouldContainSubstring, "Unknown stream type")
})
Convey("sends strings containing 'error' to stderr, everything else to stdout", func() {
dataWithError := []byte("ERROR: testing testing testing\n123\n456")
readerWithError := bytes.NewReader(dataWithError)
var captured bytes.Buffer // System log, NOT logger
log.SetOutput(&captured)
exec.handleOneStream(quitChan, "stderr", relay, readerWithError)
So(result.String(), ShouldContainSubstring, `level=error msg="ERROR:`)
So(result.String(), ShouldContainSubstring, `level=info msg=123`)
So(result.String(), ShouldContainSubstring, `level=info msg=456`)
})
})
}