-
Notifications
You must be signed in to change notification settings - Fork 5k
Expand file tree
/
Copy pathotel_test.go
More file actions
243 lines (210 loc) · 6.64 KB
/
Copy pathotel_test.go
File metadata and controls
243 lines (210 loc) · 6.64 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
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
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
//go:build integration && !agentbeat
package gcppubsub_test
import (
"bytes"
"context"
"fmt"
"testing"
"text/template"
"time"
"github.com/gofrs/uuid/v5"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/elastic/beats/v7/libbeat/tests/integration"
"github.com/elastic/beats/v7/x-pack/filebeat/input/gcppubsub/testutil"
"github.com/elastic/beats/v7/x-pack/otel/oteltest"
"github.com/elastic/beats/v7/x-pack/otel/oteltestcol"
"github.com/elastic/elastic-agent-libs/testing/estools"
)
func TestGCPInputOTelE2E(t *testing.T) {
integration.EnsureESIsRunning(t)
// Create pubsub client for setting up and communicating to emulator.
client, clientCancel := testutil.TestSetup(t)
defer func() {
clientCancel()
client.Close()
}()
testutil.CreateTopic(t, client)
testutil.CreateSubscription(t, "test-subscription-otel", client)
testutil.CreateSubscription(t, "test-subscription-fb", client)
const numMsgs = 10
testutil.PublishMessages(t, client, numMsgs)
host := integration.GetESURL(t, "http")
user := host.User.Username()
password, _ := host.User.Password()
// create a random uuid and make sure it doesn't contain dashes/
otelNamespace := fmt.Sprintf("%x", uuid.Must(uuid.NewV4()))
fbNameSpace := fmt.Sprintf("%x", uuid.Must(uuid.NewV4()))
otelIndex := "logs-integration-" + otelNamespace
fbIndex := "logs-integration-" + fbNameSpace
type options struct {
Namespace string
ESURL string
Username string
Password string
Subscription string
}
gcpFilebeatConfig := `filebeat.inputs:
- type: gcp-pubsub
project_id: test-project-id
topic: test-topic-foo
subscription.name: {{ .Subscription }}
credentials_file: "testdata/fake.json"
output:
elasticsearch:
hosts:
- {{ .ESURL }}
username: {{ .Username }}
password: {{ .Password }}
index: logs-integration-{{ .Namespace }}
queue.mem.flush.timeout: 0s
setup.template.enabled: false
processors:
- add_host_metadata: ~
- add_cloud_metadata: ~
- add_docker_metadata: ~
- add_kubernetes_metadata: ~
`
gcpOTelConfig := `exporters:
elasticsearch:
auth:
authenticator: beatsauth
compression: gzip
compression_params:
level: 1
endpoints:
- {{ .ESURL }}
logs_dynamic_pipeline:
enabled: true
logs_index: logs-integration-{{ .Namespace }}
mapping:
mode: bodymap
max_conns_per_host: 1
password: {{ .Password }}
retry:
enabled: true
initial_interval: 1s
max_interval: 1m0s
max_retries: 3
sending_queue:
batch:
flush_timeout: 10s
max_size: 1600
min_size: 0
sizer: items
block_on_overflow: true
enabled: true
num_consumers: 1
queue_size: 3200
wait_for_result: true
user: {{ .Username }}
extensions:
beatsauth:
idle_connection_timeout: 3s
proxy_disable: false
timeout: 1m30s
receivers:
filebeatreceiver:
filebeat:
inputs:
- credentials_file: "testdata/fake.json"
project_id: test-project-id
subscription:
name: {{ .Subscription }}
topic: test-topic-foo
type: gcp-pubsub
output:
otelconsumer:
processors:
- add_host_metadata: ~
- add_cloud_metadata: ~
- add_docker_metadata: ~
- add_kubernetes_metadata: ~
queue.mem.flush.timeout: 0s
setup.template.enabled: false
service:
extensions:
- beatsauth
pipelines:
logs:
exporters:
- elasticsearch
receivers:
- filebeatreceiver
telemetry:
metrics:
level: none
`
optionsValue := options{
ESURL: fmt.Sprintf("%s://%s", host.Scheme, host.Host),
Username: user,
Password: password,
}
var configBuffer bytes.Buffer
optionsValue.Namespace = otelNamespace
optionsValue.Subscription = "test-subscription-otel"
require.NoError(t, template.Must(template.New("config").Parse(gcpOTelConfig)).Execute(&configBuffer, optionsValue))
oteltestcol.New(t, configBuffer.String())
// reset buffer
configBuffer.Reset()
optionsValue.Namespace = fbNameSpace
optionsValue.Subscription = "test-subscription-fb"
require.NoError(t, template.Must(template.New("config").Parse(gcpFilebeatConfig)).Execute(&configBuffer, optionsValue))
// start filebeat
filebeat := integration.NewBeat(
t,
"filebeat",
"../../filebeat.test",
)
filebeat.WriteConfigFile(configBuffer.String())
filebeat.Start()
defer filebeat.Stop()
// prepare to query ES
es := integration.GetESClient(t, "http")
t.Cleanup(func() {
_, err := es.Indices.DeleteDataStream([]string{
otelIndex,
fbIndex,
})
require.NoError(t, err, "failed to delete indices")
})
rawQuery := map[string]any{
"query": map[string]any{
"match_phrase": map[string]any{
"input.type": "gcp-pubsub",
},
},
"sort": []map[string]any{
{"@timestamp": map[string]any{"order": "asc"}},
},
}
var filebeatDocs estools.Documents
var otelDocs estools.Documents
var err error
// wait for logs to be published
require.EventuallyWithTf(t,
func(ct *assert.CollectT) {
findCtx, findCancel := context.WithTimeout(context.Background(), 10*time.Second)
defer findCancel()
otelDocs, err = estools.PerformQueryForRawQuery(findCtx, rawQuery, ".ds-"+otelIndex+"*", es)
assert.NoError(ct, err)
assert.GreaterOrEqual(ct, otelDocs.Hits.Total.Value, 1, "expected at least 1 otel document, got %d", otelDocs.Hits.Total.Value)
filebeatDocs, err = estools.PerformQueryForRawQuery(findCtx, rawQuery, ".ds-"+fbIndex+"*", es)
assert.NoError(ct, err)
assert.GreaterOrEqual(ct, filebeatDocs.Hits.Total.Value, 1, "expected at least 1 filebeat document, got %d", filebeatDocs.Hits.Total.Value)
},
3*time.Minute, 1*time.Second, "expected at least 1 document for both filebeat and otel modes")
filebeatDoc := filebeatDocs.Hits.Hits[0].Source
otelDoc := otelDocs.Hits.Hits[0].Source
ignoredFields := []string{
// Expected to change between agentDocs and OtelDocs
"@timestamp",
"agent.ephemeral_id",
"agent.id",
"event.created",
}
oteltest.AssertMapsEqual(t, filebeatDoc, otelDoc, ignoredFields, "expected documents to be equal")
}