-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathscraperint.go
316 lines (274 loc) · 8.82 KB
/
scraperint.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package scraperinttest // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/scraperinttest"
import (
"context"
"errors"
"fmt"
"io"
"path/filepath"
"strings"
"sync"
"testing"
"time"
"unicode"
"github.com/docker/go-connections/nat"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/receiver/receivertest"
"go.uber.org/multierr"
"go.uber.org/zap"
"go.uber.org/zap/zaptest/observer"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest"
)
func NewIntegrationTest(f receiver.Factory, opts ...TestOption) *IntegrationTest {
it := &IntegrationTest{
factory: f,
createContainerTimeout: 5 * time.Minute,
customConfig: func(*testing.T, component.Config, *ContainerInfo) {},
expectedFile: filepath.Join("testdata", "integration", "expected.yaml"),
compareTimeout: time.Minute,
}
for _, opt := range opts {
opt(it)
}
return it
}
type IntegrationTest struct {
containerRequests []testcontainers.ContainerRequest
allowHardcodedHostPort bool
createContainerTimeout time.Duration
factory receiver.Factory
customConfig customConfigFunc
expectedFile string
compareOptions []pmetrictest.CompareMetricsOption
compareTimeout time.Duration
failOnErrorLogs bool
writeExpected bool
}
func (it *IntegrationTest) Run(t *testing.T) {
it.validate(t)
ci := it.createContainers(t)
defer ci.terminate(t)
cfg := it.factory.CreateDefaultConfig()
it.customConfig(t, cfg, ci)
sink := new(consumertest.MetricsSink)
settings := receivertest.NewNopSettings()
observedZapCore, observedLogs := observer.New(zap.WarnLevel)
settings.Logger = zap.New(observedZapCore)
rcvr, err := it.factory.CreateMetrics(context.Background(), settings, cfg, sink)
require.NoError(t, err, "failed creating metrics receiver")
require.NoError(t, rcvr.Start(context.Background(), componenttest.NewNopHost()))
defer func() {
require.NoError(t, rcvr.Shutdown(context.Background()))
}()
var expected pmetric.Metrics
if !it.writeExpected {
expected, err = golden.ReadMetrics(it.expectedFile)
require.NoError(t, err)
}
// Defined outside of Eventually so it can be printed if the test fails
var validateErr error
defer func() {
if t.Failed() && validateErr != nil {
t.Error(validateErr.Error())
logs := strings.Builder{}
for _, e := range observedLogs.All() {
logs.WriteString(e.Message + "\n")
}
t.Errorf("full log:\n%s", logs.String())
if len(sink.AllMetrics()) == 0 {
t.Error("no data emitted by scraper")
return
}
metricBytes, err := golden.MarshalMetricsYAML(sink.AllMetrics()[len(sink.AllMetrics())-1])
require.NoError(t, err)
t.Errorf("latest result:\n%s", metricBytes)
}
}()
require.Eventually(t,
func() bool {
allMetrics := sink.AllMetrics()
if len(allMetrics) == 0 {
return false
}
if it.failOnErrorLogs && len(observedLogs.All()) > 0 {
logs := strings.Builder{}
for _, e := range observedLogs.All() {
logs.WriteString(e.Message + "\n")
}
t.Errorf("full log:\n%s", logs.String())
}
if it.writeExpected {
require.NoError(t, golden.WriteMetrics(t, it.expectedFile, allMetrics[0]))
return true
}
validateErr = pmetrictest.CompareMetrics(expected, allMetrics[len(allMetrics)-1], it.compareOptions...)
return validateErr == nil
},
it.compareTimeout, it.compareTimeout/20)
}
func (it *IntegrationTest) createContainers(t *testing.T) *ContainerInfo {
var wg sync.WaitGroup
ci := &ContainerInfo{
containers: make(map[string]testcontainers.Container, len(it.containerRequests)),
}
wg.Add(len(it.containerRequests))
for _, cr := range it.containerRequests {
go func(req testcontainers.ContainerRequest) {
var errs error
assert.Eventuallyf(t, func() bool {
c, err := testcontainers.GenericContainer(
context.Background(),
testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
if err != nil {
errs = multierr.Append(errs, fmt.Errorf("execute container request: %w", err))
return false
}
ci.add(req.Name, c)
return true
}, it.createContainerTimeout, time.Second, "create container timeout: %v", errs)
wg.Done()
}(cr)
}
wg.Wait()
return ci
}
func (it *IntegrationTest) validate(t *testing.T) {
containerNames := make(map[string]bool, len(it.containerRequests))
for _, cr := range it.containerRequests {
if _, ok := containerNames[cr.Name]; ok {
require.False(t, ok, "duplicate container name: %q", cr.Name)
} else {
containerNames[cr.Name] = true
}
if !it.allowHardcodedHostPort {
for _, port := range cr.ExposedPorts {
require.False(t, strings.ContainsRune(port, ':'), "exposed port hardcoded to host port: %q", port)
}
}
require.NoError(t, cr.Validate())
}
}
type TestOption func(*IntegrationTest)
func WithContainerRequest(cr testcontainers.ContainerRequest) TestOption {
return func(it *IntegrationTest) {
it.containerRequests = append(it.containerRequests, cr)
}
}
func AllowHardcodedHostPort() TestOption {
return func(it *IntegrationTest) {
it.allowHardcodedHostPort = true
}
}
func WithCreateContainerTimeout(t time.Duration) TestOption {
return func(it *IntegrationTest) {
it.createContainerTimeout = t
}
}
func WithCustomConfig(c customConfigFunc) TestOption {
return func(it *IntegrationTest) {
it.customConfig = c
}
}
func WithExpectedFile(f string) TestOption {
return func(it *IntegrationTest) {
it.expectedFile = f
}
}
// This option is useful for debugging scrapers but should not be used permanently
// because the logs do not correlate to a single scrape interval. In other words,
// when a retryable failure occurs, this setting will likely force a failure anyways.
func FailOnErrorLogs() TestOption {
return func(it *IntegrationTest) {
it.failOnErrorLogs = true
}
}
func WriteExpected() TestOption {
return func(it *IntegrationTest) {
it.writeExpected = true
}
}
func WithCompareOptions(opts ...pmetrictest.CompareMetricsOption) TestOption {
return func(it *IntegrationTest) {
it.compareOptions = opts
}
}
func WithCompareTimeout(t time.Duration) TestOption {
return func(it *IntegrationTest) {
it.compareTimeout = t
}
}
type customConfigFunc func(*testing.T, component.Config, *ContainerInfo)
type ContainerInfo struct {
sync.Mutex
containers map[string]testcontainers.Container
}
func (ci *ContainerInfo) Host(t *testing.T) string {
return ci.HostForNamedContainer(t, "")
}
func (ci *ContainerInfo) HostForNamedContainer(t *testing.T, containerName string) string {
c := ci.container(t, containerName)
h, err := c.Host(context.Background())
require.NoErrorf(t, err, "get host for container %q: %v", containerName, err)
return h
}
func (ci *ContainerInfo) MappedPort(t *testing.T, port string) string {
return ci.MappedPortForNamedContainer(t, "", port)
}
func (ci *ContainerInfo) MappedPortForNamedContainer(t *testing.T, containerName string, port string) string {
c := ci.container(t, containerName)
p, err := c.MappedPort(context.Background(), nat.Port(port))
require.NoErrorf(t, err, "get port %q for container %q: %v", port, containerName, err)
return p.Port()
}
func (ci *ContainerInfo) container(t *testing.T, name string) testcontainers.Container {
require.NotEmpty(t, ci.containers, "no containers in use")
c, ok := ci.containers[name]
require.True(t, ok, "container with name %q not found", name)
return c
}
func (ci *ContainerInfo) add(name string, c testcontainers.Container) {
ci.Lock()
defer ci.Unlock()
ci.containers[name] = c
}
func (ci *ContainerInfo) terminate(t *testing.T) {
for name, c := range ci.containers {
require.NoError(t, c.Terminate(context.Background()), "terminate container %q", name)
}
}
func RunScript(script []string) testcontainers.ContainerHook {
return func(ctx context.Context, container testcontainers.Container) error {
code, r, err := container.Exec(ctx, script)
if err != nil {
return err
}
if code == 0 {
return nil
}
// Try to read the error message for the sake of debugging
errBytes, readerErr := io.ReadAll(r)
if readerErr != nil {
return fmt.Errorf("setup script returned non-zero exit code: %d", code)
}
// Error message may have non-printable chars, so clean it up
errStr := strings.Map(func(r rune) rune {
if unicode.IsPrint(r) {
return r
}
return -1
}, string(errBytes))
return errors.New(strings.TrimSpace(errStr))
}
}