forked from SumoLogic/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker_log_test.go
188 lines (173 loc) · 5.06 KB
/
docker_log_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
package docker_log
import (
"bytes"
"context"
"crypto/tls"
"io"
"testing"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/pkg/stdcopy"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/require"
)
type MockClient struct {
ContainerListF func(ctx context.Context, options types.ContainerListOptions) ([]types.Container, error)
ContainerInspectF func(ctx context.Context, containerID string) (types.ContainerJSON, error)
ContainerLogsF func(ctx context.Context, containerID string, options types.ContainerLogsOptions) (io.ReadCloser, error)
}
func (c *MockClient) ContainerList(
ctx context.Context,
options types.ContainerListOptions,
) ([]types.Container, error) {
return c.ContainerListF(ctx, options)
}
func (c *MockClient) ContainerInspect(
ctx context.Context,
containerID string,
) (types.ContainerJSON, error) {
return c.ContainerInspectF(ctx, containerID)
}
func (c *MockClient) ContainerLogs(
ctx context.Context,
containerID string,
options types.ContainerLogsOptions,
) (io.ReadCloser, error) {
return c.ContainerLogsF(ctx, containerID, options)
}
type Response struct {
io.Reader
}
func (r *Response) Close() error {
return nil
}
func MustParse(layout, value string) time.Time {
tm, err := time.Parse(layout, value)
if err != nil {
panic(err)
}
return tm
}
func Test(t *testing.T) {
tests := []struct {
name string
client *MockClient
expected []telegraf.Metric
}{
{
name: "no containers",
client: &MockClient{
ContainerListF: func(ctx context.Context, options types.ContainerListOptions) ([]types.Container, error) {
return nil, nil
},
},
},
{
name: "one container tty",
client: &MockClient{
ContainerListF: func(ctx context.Context, options types.ContainerListOptions) ([]types.Container, error) {
return []types.Container{
{
ID: "deadbeef",
Names: []string{"/telegraf"},
Image: "influxdata/telegraf:1.11.0",
},
}, nil
},
ContainerInspectF: func(ctx context.Context, containerID string) (types.ContainerJSON, error) {
return types.ContainerJSON{
Config: &container.Config{
Tty: true,
},
}, nil
},
ContainerLogsF: func(ctx context.Context, containerID string, options types.ContainerLogsOptions) (io.ReadCloser, error) {
return &Response{Reader: bytes.NewBuffer([]byte("2020-04-28T18:43:16.432691200Z hello\n"))}, nil
},
},
expected: []telegraf.Metric{
testutil.MustMetric(
"docker_log",
map[string]string{
"container_name": "telegraf",
"container_image": "influxdata/telegraf",
"container_version": "1.11.0",
"stream": "tty",
"source": "deadbeef",
},
map[string]interface{}{
"container_id": "deadbeef",
"message": "hello",
},
MustParse(time.RFC3339Nano, "2020-04-28T18:43:16.432691200Z"),
),
},
},
{
name: "one container multiplex",
client: &MockClient{
ContainerListF: func(ctx context.Context, options types.ContainerListOptions) ([]types.Container, error) {
return []types.Container{
{
ID: "deadbeef",
Names: []string{"/telegraf"},
Image: "influxdata/telegraf:1.11.0",
},
}, nil
},
ContainerInspectF: func(ctx context.Context, containerID string) (types.ContainerJSON, error) {
return types.ContainerJSON{
Config: &container.Config{
Tty: false,
},
}, nil
},
ContainerLogsF: func(ctx context.Context, containerID string, options types.ContainerLogsOptions) (io.ReadCloser, error) {
var buf bytes.Buffer
w := stdcopy.NewStdWriter(&buf, stdcopy.Stdout)
w.Write([]byte("2020-04-28T18:42:16.432691200Z hello from stdout"))
return &Response{Reader: &buf}, nil
},
},
expected: []telegraf.Metric{
testutil.MustMetric(
"docker_log",
map[string]string{
"container_name": "telegraf",
"container_image": "influxdata/telegraf",
"container_version": "1.11.0",
"stream": "stdout",
"source": "deadbeef",
},
map[string]interface{}{
"container_id": "deadbeef",
"message": "hello from stdout",
},
MustParse(time.RFC3339Nano, "2020-04-28T18:42:16.432691200Z"),
),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var acc testutil.Accumulator
plugin := &DockerLogs{
Timeout: internal.Duration{Duration: time.Second * 5},
newClient: func(string, *tls.Config) (Client, error) { return tt.client, nil },
containerList: make(map[string]context.CancelFunc),
IncludeSourceTag: true,
}
err := plugin.Init()
require.NoError(t, err)
err = plugin.Gather(&acc)
require.NoError(t, err)
acc.Wait(len(tt.expected))
plugin.Stop()
require.Nil(t, acc.Errors) // no errors during gathering
testutil.RequireMetricsEqual(t, tt.expected, acc.GetTelegrafMetrics())
})
}
}