forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 1
/
modules.go
78 lines (68 loc) · 1.96 KB
/
modules.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
package test
import (
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/elastic/beats/libbeat/cmd/instance"
"github.com/elastic/beats/libbeat/testing"
"github.com/elastic/beats/metricbeat/beater"
"github.com/elastic/beats/metricbeat/mb/module"
)
func GenTestModulesCmd(name, beatVersion string) *cobra.Command {
return &cobra.Command{
Use: "modules [module] [metricset]",
Short: "Test modules settings",
Run: func(cmd *cobra.Command, args []string) {
var filter_module, filter_metricset string
if len(args) > 0 {
filter_module = args[0]
}
if len(args) > 1 {
filter_metricset = args[1]
}
b, err := instance.NewBeat(name, "", beatVersion)
if err != nil {
fmt.Fprintf(os.Stderr, "Error initializing beat: %s\n", err)
os.Exit(1)
}
err = b.Init()
if err != nil {
fmt.Fprintf(os.Stderr, "Error initializing beat: %s\n", err)
os.Exit(1)
}
// Use a customized instance of Metricbeat where startup delay has
// been disabled to workaround the fact that Modules() will return
// the static modules (not the dynamic ones) with a start delay.
create := beater.Creator(
beater.WithModuleOptions(
module.WithMetricSetInfo(),
module.WithMaxStartDelay(0),
),
)
mb, err := create(&b.Beat, b.Beat.BeatConfig)
if err != nil {
fmt.Fprintf(os.Stderr, "Error initializing metricbeat: %s\n", err)
os.Exit(1)
}
modules, err := mb.(*beater.Metricbeat).Modules()
if err != nil {
fmt.Fprintf(os.Stderr, "Error getting metricbeat modules: %s\n", err)
os.Exit(1)
}
driver := testing.NewConsoleDriver(os.Stdout)
for _, module := range modules {
if filter_module != "" && module.Name() != filter_module {
continue
}
driver.Run(module.Name(), func(driver testing.Driver) {
for _, set := range module.MetricSets() {
if filter_metricset != "" && set.Name() != filter_metricset {
continue
}
set.Test(driver)
}
})
}
},
}
}