forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modules.go
125 lines (100 loc) · 2.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
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
/*
Package testing provides utility functions for testing Module and MetricSet
implementations.
MetricSet Example
This is an example showing how to use this package to test a MetricSet. By
using these methods you ensure the MetricSet is instantiated in the same way
that Metricbeat does it and with the same validations.
package mymetricset_test
import (
mbtest "github.com/elastic/beats/metricbeat/mb/testing"
)
func TestFetch(t *testing.T) {
f := mbtest.NewEventFetcher(t, getConfig())
event, err := f.Fetch()
if err != nil {
t.Fatal(err)
}
t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(), event)
// Test event attributes...
}
func getConfig() map[string]interface{} {
return map[string]interface{}{
"module": "mymodule",
"metricsets": []string{"status"},
"hosts": []string{mymodule.GetHostFromEnv()},
}
}
*/
package testing
import (
"testing"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/metricbeat/mb"
"github.com/stretchr/testify/assert"
)
type TestModule struct {
ModName string
ModConfig mb.ModuleConfig
RawConfig *common.Config
}
func (m *TestModule) Name() string { return m.ModName }
func (m *TestModule) Config() mb.ModuleConfig { return m.ModConfig }
func (m *TestModule) UnpackConfig(to interface{}) error { return m.RawConfig.Unpack(to) }
func NewTestModule(t testing.TB, config interface{}) *TestModule {
c, err := common.NewConfigFrom(config)
if err != nil {
t.Fatal(err)
}
return &TestModule{RawConfig: c}
}
// newMetricSet instantiates a new MetricSet using the given configuration.
// The ModuleFactory and MetricSetFactory are obtained from the global
// Registry.
func newMetricSet(t testing.TB, config interface{}) mb.MetricSet {
c, err := common.NewConfigFrom(config)
if err != nil {
t.Fatal(err)
}
m, err := mb.NewModules([]*common.Config{c}, mb.Registry)
if err != nil {
t.Fatal(err)
}
if !assert.Len(t, m, 1) {
t.FailNow()
}
var metricSet mb.MetricSet
for _, v := range m {
if !assert.Len(t, v, 1) {
t.FailNow()
}
metricSet = v[0]
break
}
if !assert.NotNil(t, metricSet) {
t.FailNow()
}
return metricSet
}
// NewEventFetcher instantiates a new EventFetcher using the given
// configuration. The ModuleFactory and MetricSetFactory are obtained from the
// global Registry.
func NewEventFetcher(t testing.TB, config interface{}) mb.EventFetcher {
metricSet := newMetricSet(t, config)
fetcher, ok := metricSet.(mb.EventFetcher)
if !ok {
t.Fatal("MetricSet does not implement EventFetcher")
}
return fetcher
}
// NewEventsFetcher instantiates a new EventsFetcher using the given
// configuration. The ModuleFactory and MetricSetFactory are obtained from the
// global Registry.
func NewEventsFetcher(t testing.TB, config interface{}) mb.EventsFetcher {
metricSet := newMetricSet(t, config)
fetcher, ok := metricSet.(mb.EventsFetcher)
if !ok {
t.Fatal("MetricSet does not implement EventsFetcher")
}
return fetcher
}