forked from elastic/e2e-testing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetricbeat_test.go
622 lines (511 loc) · 18.4 KB
/
metricbeat_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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
// 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.
package main
import (
"context"
"fmt"
"os"
"path"
"strings"
"testing"
"time"
"github.com/cucumber/godog"
"github.com/cucumber/godog/colors"
messages "github.com/cucumber/messages-go/v10"
"github.com/elastic/e2e-testing/cli/config"
"github.com/elastic/e2e-testing/e2e/steps"
"github.com/elastic/e2e-testing/internal/common"
"github.com/elastic/e2e-testing/internal/deploy"
"github.com/elastic/e2e-testing/internal/elasticsearch"
"github.com/elastic/e2e-testing/internal/shell"
"github.com/elastic/e2e-testing/internal/utils"
log "github.com/sirupsen/logrus"
flag "github.com/spf13/pflag"
"go.elastic.co/apm"
)
var serviceManager deploy.ServiceManager
var testSuite MetricbeatTestSuite
var tx *apm.Transaction
var stepSpan *apm.Span
func setupSuite() {
config.Init()
common.InitVersions()
serviceManager = deploy.NewServiceManager()
testSuite = MetricbeatTestSuite{
Query: elasticsearch.Query{},
}
}
// MetricbeatTestSuite represents a test suite, holding references to both metricbeat ant
// the service to be monitored
//nolint:unused
type MetricbeatTestSuite struct {
cleanUpTmpFiles bool // if it's needed to clean up temporary files
configurationFile string // the name of the configuration file to be used in this test suite
ServiceName string // the service to be monitored by metricbeat
ServiceType string // the type of the service to be monitored by metricbeat
ServiceVariant string // the variant of the service to be monitored by metricbeat
ServiceVersion string // the version of the service to be monitored by metricbeat
Query elasticsearch.Query // the specs for the ES query
Version string // the metricbeat version for the test
// instrumentation
currentContext context.Context
}
// getIndexName returns the index to be used when querying Elasticsearch
func (mts *MetricbeatTestSuite) getIndexName() string {
return mts.Query.IndexName
}
func (mts *MetricbeatTestSuite) setEventModule(eventModule string) {
mts.Query.EventModule = eventModule
}
// As we are using an index per scenario outline, with an index name formed by metricbeat-version1-module-version2,
// or metricbeat-version1-module-variant-version2,
// and because of the ILM is configured on metricbeat side, then we can use an asterisk for the index name:
// each scenario outline will be namespaced, so no collitions between different test cases should appear
func (mts *MetricbeatTestSuite) setIndexName() {
mVersion := strings.ReplaceAll(mts.Version, "-SNAPSHOT", "")
var index string
if mts.ServiceName != "" {
if mts.ServiceVariant == "" {
index = fmt.Sprintf("metricbeat-%s-%s-%s", mVersion, mts.ServiceName, mts.ServiceVersion)
} else {
index = fmt.Sprintf("metricbeat-%s-%s-%s-%s", mVersion, mts.ServiceName, mts.ServiceVariant, mts.ServiceVersion)
}
} else {
index = fmt.Sprintf("metricbeat-%s", mVersion)
}
index += "-" + utils.RandomString(8)
mts.Query.IndexName = strings.ToLower(index)
}
func (mts *MetricbeatTestSuite) setServiceVersion(version string) {
mts.Query.ServiceVersion = version
}
// CleanUp cleans up services in the test suite
func (mts *MetricbeatTestSuite) CleanUp() error {
span := tx.StartSpan("Clean up", "test.scenario.clean", nil)
testSuite.currentContext = apm.ContextWithSpan(context.Background(), span)
defer span.End()
serviceManager := deploy.NewServiceManager()
fn := func(ctx context.Context) {
err := elasticsearch.DeleteIndex(ctx, mts.getIndexName())
if err != nil {
log.WithFields(log.Fields{
"profile": "metricbeat",
"index": mts.getIndexName(),
}).Warn("The index was not deleted, but we are not failing the test case")
}
}
defer fn(context.Background())
env := map[string]string{
"stackVersion": common.StackVersion,
}
services := []deploy.ServiceRequest{deploy.NewServiceRequest("metricbeat")}
if mts.ServiceName != "" {
services = append(services, deploy.NewServiceRequest(mts.ServiceName))
}
err := serviceManager.RemoveServicesFromCompose(mts.currentContext, deploy.NewServiceRequest("metricbeat"), services, env)
if mts.cleanUpTmpFiles {
if _, err := os.Stat(mts.configurationFile); err == nil {
beatsLocalPath := shell.GetEnv("BEATS_LOCAL_PATH", "")
if beatsLocalPath == "" {
os.Remove(mts.configurationFile)
log.WithFields(log.Fields{
"path": mts.configurationFile,
}).Trace("Metricbeat configuration file removed.")
} else {
log.WithFields(log.Fields{
"path": mts.configurationFile,
}).Trace("Metricbeat configuration file not removed because it's part of a repository.")
}
}
}
return err
}
func InitializeMetricbeatScenarios(ctx *godog.ScenarioContext) {
ctx.BeforeScenario(func(p *messages.Pickle) {
log.Trace("Before Metricbeat scenario...")
tx = apm.DefaultTracer.StartTransaction(p.GetName(), "test.scenario")
tx.Context.SetLabel("suite", "metricbeat")
})
ctx.AfterScenario(func(p *messages.Pickle, err error) {
if err != nil {
e := apm.DefaultTracer.NewError(err)
e.Context.SetLabel("scenario", p.GetName())
e.Context.SetLabel("gherkin_type", "scenario")
e.Send()
}
f := func() {
tx.End()
apm.DefaultTracer.Flush(nil)
}
defer f()
log.Trace("After Metricbeat scenario...")
cleanUpErr := testSuite.CleanUp()
if cleanUpErr != nil {
log.Errorf("CleanUp failed: %v", cleanUpErr)
e := apm.DefaultTracer.NewError(cleanUpErr)
e.Context.SetLabel("step", p.GetName())
e.Context.SetLabel("gherkin_type", "step")
e.Send()
}
})
ctx.BeforeStep(func(step *godog.Step) {
stepSpan = tx.StartSpan(step.GetText(), "test.scenario.step", nil)
testSuite.currentContext = apm.ContextWithSpan(context.Background(), stepSpan)
})
ctx.AfterStep(func(st *godog.Step, err error) {
if err != nil {
e := apm.DefaultTracer.NewError(err)
e.Context.SetLabel("step", st.GetText())
e.Context.SetLabel("gherkin_type", "step")
e.Send()
}
if stepSpan != nil {
stepSpan.End()
}
})
ctx.Step(`^"([^"]*)" "([^"]*)" is running for metricbeat$`, testSuite.serviceIsRunningForMetricbeat)
ctx.Step(`^"([^"]*)" v([^"]*), variant of "([^"]*)", is running for metricbeat$`, testSuite.serviceVariantIsRunningForMetricbeat)
ctx.Step(`^metricbeat is installed and configured for "([^"]*)" module$`, testSuite.installedAndConfiguredForModule)
ctx.Step(`^metricbeat is installed and configured for "([^"]*)", variant of the "([^"]*)" module$`, testSuite.installedAndConfiguredForVariantModule)
ctx.Step(`^there are no errors in the index$`, testSuite.thereAreNoErrorsInTheIndex)
ctx.Step(`^there are "([^"]*)" events in the index$`, testSuite.thereAreEventsInTheIndex)
ctx.Step(`^metricbeat is installed using "([^"]*)" configuration$`, testSuite.installedUsingConfiguration)
}
// InitializeMetricbeatTestSuite adds steps to the Godog test suite
//nolint:deadcode,unused
func InitializeMetricbeatTestSuite(ctx *godog.TestSuiteContext) {
ctx.BeforeSuite(func() {
setupSuite()
log.Trace("Before Metricbeat Suite...")
var suiteTx *apm.Transaction
var suiteParentSpan *apm.Span
var suiteContext = context.Background()
// instrumentation
defer apm.DefaultTracer.Flush(nil)
suiteTx = apm.DefaultTracer.StartTransaction("Initialise Metricbeat", "test.suite")
defer suiteTx.End()
suiteParentSpan = suiteTx.StartSpan("Before Metricbeat test suite", "test.suite.before", nil)
suiteContext = apm.ContextWithSpan(suiteContext, suiteParentSpan)
defer suiteParentSpan.End()
env := map[string]string{
"stackPlatform": "linux/" + utils.GetArchitecture(),
"stackVersion": common.StackVersion,
}
deployer := deploy.New(common.Provider)
err := deployer.Bootstrap(suiteContext, deploy.NewServiceRequest("metricbeat"), env, func() error {
minutesToBeHealthy := time.Duration(utils.TimeoutFactor) * time.Minute
healthy, err := elasticsearch.WaitForElasticsearch(suiteContext, minutesToBeHealthy)
if !healthy {
return fmt.Errorf("the Elasticsearch cluster could not get the healthy status")
}
return err
})
if err != nil {
log.WithFields(log.Fields{
"error": err,
"profile": "metricbeat",
}).Fatal("Could not run the profile")
}
})
ctx.AfterSuite(func() {
f := func() {
apm.DefaultTracer.Flush(nil)
}
defer f()
// instrumentation
var suiteTx *apm.Transaction
var suiteParentSpan *apm.Span
var suiteContext = context.Background()
defer apm.DefaultTracer.Flush(nil)
suiteTx = apm.DefaultTracer.StartTransaction("Tear Down Metricbeat", "test.suite")
defer suiteTx.End()
suiteParentSpan = suiteTx.StartSpan("After Metricbeat test suite", "test.suite.after", nil)
suiteContext = apm.ContextWithSpan(suiteContext, suiteParentSpan)
defer suiteParentSpan.End()
if !common.DeveloperMode {
deployer := deploy.New(common.Provider)
err := deployer.Destroy(suiteContext, deploy.NewServiceRequest("metricbeat"))
if err != nil {
log.WithFields(log.Fields{
"profile": "metricbeat",
}).Error("Could not stop the profile.")
}
}
})
}
func (mts *MetricbeatTestSuite) installedAndConfiguredForModule(serviceType string) error {
serviceType = strings.ToLower(serviceType)
// at this point we have everything to define the index name
mts.Version = common.BeatVersion
mts.setIndexName()
mts.ServiceType = serviceType
// look up configurations under workspace's configurations directory. If does not exist, look up tool's workspace
dir, _ := os.Getwd()
configFile := path.Join(dir, "configurations", mts.ServiceName+".yml")
found, err := config.FileExists(configFile)
if !found || err != nil {
log.WithFields(log.Fields{
"error": err,
"path": configFile,
"service": mts.ServiceName,
}).Debug("Could not retrieve configuration file under test workspace. Looking up tool's workspace")
configFile = path.Join(config.OpDir(), "compose", "services", mts.ServiceName, "_meta", "config.yml")
ok, err := config.FileExists(configFile)
if !ok {
return fmt.Errorf("The configuration file for %s does not exist", mts.ServiceName)
}
if err != nil {
return err
}
}
mts.configurationFile = configFile
_ = os.Chmod(mts.configurationFile, 0666)
mts.setEventModule(mts.ServiceType)
mts.setServiceVersion(mts.Version)
err = mts.runMetricbeatService()
if err != nil {
return err
}
return nil
}
func (mts *MetricbeatTestSuite) installedAndConfiguredForVariantModule(serviceVariant string, serviceType string) error {
mts.ServiceVariant = serviceVariant
err := mts.installedAndConfiguredForModule(serviceType)
if err != nil {
return err
}
return nil
}
func (mts *MetricbeatTestSuite) installedUsingConfiguration(configuration string) error {
// at this point we have everything to define the index name
mts.Version = common.BeatVersion
mts.setIndexName()
configurationFilePath, err := steps.FetchBeatConfiguration(mts.currentContext, false, "metricbeat", configuration+".yml")
if err != nil {
return err
}
mts.configurationFile = configurationFilePath
mts.cleanUpTmpFiles = true
mts.setEventModule("system")
mts.setServiceVersion(mts.Version)
err = mts.runMetricbeatService()
if err != nil {
return err
}
return nil
}
// runMetricbeatService runs a metricbeat service entity for a service to monitor it
func (mts *MetricbeatTestSuite) runMetricbeatService() error {
useCISnapshots := shell.GetEnvBool("BEATS_USE_CI_SNAPSHOTS")
beatsLocalPath := shell.GetEnv("BEATS_LOCAL_PATH", "")
if useCISnapshots || beatsLocalPath != "" {
arch := utils.GetArchitecture()
_, imagePath, err := utils.FetchElasticArtifact(mts.currentContext, "metricbeat", mts.Version, "linux", arch, "tar.gz", true, true)
if err != nil {
return err
}
err = deploy.LoadImage(imagePath)
if err != nil {
return err
}
mts.Version = mts.Version + "-" + arch
err = deploy.TagImage(
"docker.elastic.co/beats/metricbeat:"+common.BeatVersionBase,
"docker.elastic.co/observability-ci/metricbeat:"+mts.Version,
)
if err != nil {
return err
}
}
// this is needed because, in general, the target service (apache, mysql, redis) does not have a healthcheck
waitForService := time.Duration(utils.TimeoutFactor) * 10 * time.Second
if mts.ServiceName == "ceph" {
// see https://github.com/elastic/beats/blob/ef6274d0d1e36308a333cbed69846a1bd63528ae/metricbeat/module/ceph/mgr_osd_tree/mgr_osd_tree_integration_test.go#L35
// Ceph service needs more time to start up
waitForService = waitForService * 4
}
utils.Sleep(waitForService)
serviceManager := deploy.NewServiceManager()
logLevel := log.GetLevel().String()
if log.GetLevel() == log.TraceLevel {
logLevel = log.DebugLevel.String()
}
arch := utils.GetArchitecture()
env := map[string]string{
"BEAT_STRICT_PERMS": "false",
"indexName": mts.getIndexName(),
"logLevel": logLevel,
"metricbeatConfigFile": mts.configurationFile,
"metricbeatTag": mts.Version,
"stackVersion": common.StackVersion,
mts.ServiceName + "Tag": mts.ServiceVersion,
"serviceName": mts.ServiceName,
}
env["metricbeatDockerNamespace"] = utils.GetDockerNamespaceEnvVar("beats")
env["metricbeatPlatform"] = "linux/" + arch
err := serviceManager.AddServicesToCompose(
testSuite.currentContext,
deploy.NewServiceRequest("metricbeat"),
[]deploy.ServiceRequest{deploy.NewServiceRequest("metricbeat")},
env)
if err != nil {
log.WithFields(log.Fields{
"error": err,
"metricbeatVersion": mts.Version,
"service": mts.ServiceName,
"serviceVersion": mts.ServiceVersion,
}).Error("Could not run Metricbeat for the service")
return err
}
if mts.ServiceName != "" && mts.ServiceVersion != "" {
fields := log.Fields{
"metricbeatVersion": mts.Version,
"service": mts.ServiceName,
"serviceVersion": mts.ServiceVersion,
}
if mts.ServiceVariant != "" {
fields["variant"] = mts.ServiceVariant
}
log.WithFields(fields).Info("Metricbeat is running configured for the service")
} else {
log.WithFields(log.Fields{
"metricbeatVersion": mts.Version,
}).Info("Metricbeat is running")
}
if log.IsLevelEnabled(log.DebugLevel) {
services := []deploy.ServiceRequest{
deploy.NewServiceRequest("metricbeat"), // metricbeat service
}
if common.DeveloperMode {
err = serviceManager.RunCommand(mts.currentContext, deploy.NewServiceRequest("metricbeat"), services, []string{"logs", "metricbeat"}, env)
if err != nil {
log.WithFields(log.Fields{
"error": err,
"metricbeatVersion": mts.Version,
"service": mts.ServiceName,
"serviceVersion": mts.ServiceVersion,
}).Warn("Could not retrieve Metricbeat logs")
}
}
}
return nil
}
func (mts *MetricbeatTestSuite) serviceIsRunningForMetricbeat(serviceType string, serviceVersion string) error {
serviceType = strings.ToLower(serviceType)
env := map[string]string{
"stackVersion": common.StackVersion,
}
env = config.PutServiceEnvironment(env, serviceType, serviceVersion)
err := serviceManager.AddServicesToCompose(
testSuite.currentContext, deploy.NewServiceRequest("metricbeat"),
[]deploy.ServiceRequest{deploy.NewServiceRequest(serviceType)},
env)
if err != nil {
log.WithFields(log.Fields{
"service": serviceType,
"version": serviceVersion,
}).Error("Could not run the service.")
}
mts.ServiceName = serviceType
mts.ServiceVersion = serviceVersion
return err
}
func (mts *MetricbeatTestSuite) serviceVariantIsRunningForMetricbeat(
serviceVariant string, serviceVersion string, serviceType string) error {
serviceVariant = strings.ToLower(serviceVariant)
serviceType = strings.ToLower(serviceType)
env := map[string]string{
"stackVersion": common.StackVersion,
}
env = config.PutServiceVariantEnvironment(env, serviceType, serviceVariant, serviceVersion)
err := serviceManager.AddServicesToCompose(
testSuite.currentContext, deploy.NewServiceRequest("metricbeat"),
[]deploy.ServiceRequest{deploy.NewServiceRequest(serviceType)},
env)
if err != nil {
log.WithFields(log.Fields{
"service": serviceType,
"variant": serviceVariant,
"version": serviceVersion,
}).Error("Could not run the service.")
}
mts.ServiceName = serviceType
mts.ServiceVersion = serviceVersion
return err
}
func (mts *MetricbeatTestSuite) thereAreEventsInTheIndex() error {
esQuery := map[string]interface{}{
"query": map[string]interface{}{
"bool": map[string]interface{}{
"must": []map[string]interface{}{
{
"match": map[string]interface{}{
"event.module": mts.Query.EventModule,
},
},
},
},
},
}
minimumHitsCount := 5
maxTimeout := time.Duration(utils.TimeoutFactor) * time.Minute
result, err := elasticsearch.WaitForNumberOfHits(mts.currentContext, mts.getIndexName(), esQuery, minimumHitsCount, maxTimeout)
if err != nil {
return err
}
err = elasticsearch.AssertHitsArePresent(result)
if err != nil {
log.WithFields(log.Fields{
"eventModule": mts.Query.EventModule,
"index": mts.Query.IndexName,
"serviceVersion": mts.Query.ServiceVersion,
}).Error(err.Error())
return err
}
return nil
}
func (mts *MetricbeatTestSuite) thereAreNoErrorsInTheIndex() error {
esQuery := map[string]interface{}{
"query": map[string]interface{}{
"bool": map[string]interface{}{
"must": []map[string]interface{}{
{
"match": map[string]interface{}{
"event.module": mts.Query.EventModule,
},
},
},
},
},
}
minimumHitsCount := 5
maxTimeout := time.Duration(utils.TimeoutFactor) * time.Minute
result, err := elasticsearch.WaitForNumberOfHits(mts.currentContext, mts.getIndexName(), esQuery, minimumHitsCount, maxTimeout)
if err != nil {
return err
}
return elasticsearch.AssertHitsDoNotContainErrors(result, mts.Query)
}
var opts = godog.Options{
Output: colors.Colored(os.Stdout),
Format: "progress", // can define default values
}
func init() {
godog.BindCommandLineFlags("godog.", &opts) // godog v0.11.0 (latest)
}
func TestMain(m *testing.M) {
flag.Parse()
opts.Paths = flag.Args()
status := godog.TestSuite{
Name: "godogs",
TestSuiteInitializer: InitializeMetricbeatTestSuite,
ScenarioInitializer: InitializeMetricbeatScenarios,
Options: &opts,
}.Run()
// Optional: Run `testing` package's logic besides godog.
if st := m.Run(); st > status {
status = st
}
os.Exit(status)
}