-
Notifications
You must be signed in to change notification settings - Fork 5k
Expand file tree
/
Copy pathsample_test.go
More file actions
172 lines (152 loc) · 4.72 KB
/
sample_test.go
File metadata and controls
172 lines (152 loc) · 4.72 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
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//go:build integration
package integration
import (
"context"
"fmt"
"path/filepath"
"testing"
"time"
"github.com/elastic/beats/v7/libbeat/testing/integration"
)
func TestFilebeat(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
EnsureCompiled(ctx, t)
messagePrefix := "sample test message"
fileCount := 5
lineCount := 128
reportOptions := integration.ReportOptions{
PrintLinesOnFail: 10,
PrintConfigOnFail: true,
}
t.Run("Filebeat starts and ingests files", func(t *testing.T) {
configPlainTemplate := `
filebeat.inputs:
- type: filestream
id: "test-filestream"
paths:
- %s
# we want to check that all messages are ingested
# without using an external service, this is an easy way
output.console:
enabled: true
`
configGZIPTemplate := `
filebeat.inputs:
- type: filestream
id: test-filestream
paths:
- %s
compression: auto
# we want to check that all messages are ingested
# without using an external service, this is an easy way
output.console:
enabled: true
`
// we can generate any amount of expectations
// they are light-weight
expectIngestedFiles := func(test Test, files []string) {
// ensuring we ingest every line from every file
for _, filename := range files {
for i := 1; i <= lineCount; i++ {
line := fmt.Sprintf("%s:%d", filepath.Base(filename), i)
test.ExpectOutput(line)
}
}
}
tcs := map[string]struct {
configTemplate string
GenerateLogFilesFn func(t *testing.T, files, lines int, generator LogGenerator) (path string, filenames []string)
}{
"plain": {
configTemplate: configPlainTemplate,
GenerateLogFilesFn: GenerateLogFiles,
},
"GZIP": {
configTemplate: configGZIPTemplate,
GenerateLogFilesFn: GenerateGZIPLogFiles,
},
}
for name, tc := range tcs {
t.Run(name, func(t *testing.T) {
t.Run("plain text logs - unstructured log files", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
generator := NewPlainTextGenerator(messagePrefix)
path, files := tc.GenerateLogFilesFn(t, fileCount, lineCount, generator)
config := fmt.Sprintf(tc.configTemplate, path)
test := NewTest(t, TestOptions{
Config: config,
})
expectIngestedFiles(test, files)
test.
// we expect to read all generated files to EOF
ExpectEOF(files...).
WithReportOptions(reportOptions).
// we should observe the start message of the Beat
ExpectStart().
// check that the first and the last line of the file get ingested
Start(ctx).
// wait until all the expectations are met
// or we hit the timeout set by the context
Wait()
})
t.Run("JSON logs - structured log files", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
generator := NewJSONGenerator(messagePrefix)
path, files := tc.GenerateLogFilesFn(t, fileCount, lineCount, generator)
config := fmt.Sprintf(tc.configTemplate, path)
test := NewTest(t, TestOptions{
Config: config,
})
expectIngestedFiles(test, files)
test.
ExpectEOF(files...).
WithReportOptions(reportOptions).
ExpectStart().
Start(ctx).
Wait()
})
})
}
})
t.Run("Filebeat crashes due to incorrect config", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
// path items are required, this config is invalid
config := `
filebeat.inputs:
- type: filestream
id: "test-filestream"
output.console:
enabled: true
`
test := NewTest(t, TestOptions{
Config: config,
})
test.
WithReportOptions(reportOptions).
ExpectStart().
ExpectOutput("Exiting: Failed to start crawler: starting input failed: error while initializing input: no path is configured").
ExpectStop(1).
Start(ctx).
Wait()
})
}