Skip to content

Commit

Permalink
Merge pull request #1127 from ruflin/metricbeat-moduler-tests
Browse files Browse the repository at this point in the history
Metricbeat moduler tests
  • Loading branch information
andrewkroh committed Mar 8, 2016
2 parents 7de9604 + b9fcef2 commit 3a13618
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 9 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,4 @@ after_success:
- test -f topbeat/build/coverage/full.cov && bash <(curl -s https://codecov.io/bash) -f topbeat/build/coverage/full.cov
- test -f libbeat/build/coverage/full.cov && bash <(curl -s https://codecov.io/bash) -f libbeat/build/coverage/full.cov
- test -f winlogbeat/build/coverage/full.cov && bash <(curl -s https://codecov.io/bash) -f winlogbeat/build/coverage/full.cov
- test -f metricbeat/build/coverage/full.cov && bash <(curl -s https://codecov.io/bash) -f metricbeat/build/coverage/full.cov
2 changes: 1 addition & 1 deletion metricbeat/helper/metricset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type MockMetricSeter struct {
}

func (m *MockMetricSeter) Fetch(ms *MetricSet) (events []common.MapStr, err error) {
m.counter = m.counter + 1
m.counter += 1

event := common.MapStr{
"counter": m.counter,
Expand Down
4 changes: 2 additions & 2 deletions metricbeat/helper/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type Module struct {
}

// NewModule creates a new module
func NewModule(cfg *ucfg.Config, moduler Moduler) (*Module, error) {
func NewModule(cfg *ucfg.Config, moduler func() Moduler) (*Module, error) {

// Module config defaults
config := ModuleConfig{
Expand All @@ -52,7 +52,7 @@ func NewModule(cfg *ucfg.Config, moduler Moduler) (*Module, error) {
name: config.Module,
Config: config,
cfg: cfg,
moduler: moduler,
moduler: moduler(),
metricSets: map[string]*MetricSet{},
wg: sync.WaitGroup{},
done: make(chan struct{}),
Expand Down
48 changes: 48 additions & 0 deletions metricbeat/helper/module_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package helper

import (
"reflect"
"testing"

"github.com/stretchr/testify/assert"

"github.com/urso/ucfg"
)

func TestGetMetricSetsList(t *testing.T) {
Expand All @@ -19,3 +22,48 @@ func TestGetMetricSetsList(t *testing.T) {
assert.Equal(t, "test1, test2", module.getMetricSetsList())

}

func TestNewModule(t *testing.T) {

config, _ := ucfg.NewFrom(ModuleConfig{
Module: "test",
})

module, err := NewModule(config, NewMockModuler)
assert.NoError(t, err)
assert.NotNil(t, module)

err = module.moduler.Setup(config)
assert.NoError(t, err)
}

// Check that the moduler inside each module is a different instance
func TestNewModulerDifferentInstance(t *testing.T) {

config, _ := ucfg.NewFrom(ModuleConfig{
Module: "test",
})

module1, err := NewModule(config, NewMockModuler)
assert.NoError(t, err)
module2, err := NewModule(config, NewMockModuler)
assert.NoError(t, err)

module1.moduler.Setup(config)
assert.False(t, reflect.DeepEqual(module1.moduler, module2.moduler))
assert.True(t, reflect.DeepEqual(module1.moduler, module1.moduler))
}

// New creates new instance of Moduler
func NewMockModuler() Moduler {
return &MockModuler{}
}

type MockModuler struct {
counter int
}

func (m *MockModuler) Setup(cfg *ucfg.Config) error {
m.counter += 1
return nil
}
6 changes: 3 additions & 3 deletions metricbeat/helper/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ import (
var Registry = Register{}

type Register struct {
Modulers map[string]Moduler
Modulers map[string]func() Moduler
MetricSeters map[string]map[string]func() MetricSeter
}

// AddModule registers the given module with the registry
func (r *Register) AddModuler(name string, m Moduler) {
func (r *Register) AddModuler(name string, m func() Moduler) {

if r.Modulers == nil {
r.Modulers = map[string]Moduler{}
r.Modulers = map[string]func() Moduler{}
}

logp.Info("Register module: %s", name)
Expand Down
7 changes: 6 additions & 1 deletion metricbeat/module/apache/apache.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import (
)

func init() {
helper.Registry.AddModuler("apache", Moduler{})
helper.Registry.AddModuler("apache", New)
}

// New creates new instance of Moduler
func New() helper.Moduler {
return &Moduler{}
}

type Moduler struct{}
Expand Down
7 changes: 6 additions & 1 deletion metricbeat/module/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ import (
)

func init() {
helper.Registry.AddModuler("mysql", Moduler{})
helper.Registry.AddModuler("mysql", New)
}

// New creates new instance of Moduler
func New() helper.Moduler {
return &Moduler{}
}

type Moduler struct{}
Expand Down
7 changes: 6 additions & 1 deletion metricbeat/module/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ import (
)

func init() {
helper.Registry.AddModuler("redis", Moduler{})
helper.Registry.AddModuler("redis", New)
}

// New creates new instance of Moduler
func New() helper.Moduler {
return &Moduler{}
}

type Moduler struct{}
Expand Down

0 comments on commit 3a13618

Please sign in to comment.