From 339e708be0d072212e9cb8772624904ca309d693 Mon Sep 17 00:00:00 2001 From: ruflin Date: Tue, 8 Mar 2016 16:11:23 +0100 Subject: [PATCH 1/2] Refactor and test creation of moduler instances --- metricbeat/helper/metricset_test.go | 2 +- metricbeat/helper/module.go | 4 +-- metricbeat/helper/module_test.go | 47 +++++++++++++++++++++++++++++ metricbeat/helper/register.go | 6 ++-- metricbeat/module/apache/apache.go | 7 ++++- metricbeat/module/mysql/mysql.go | 7 ++++- metricbeat/module/redis/redis.go | 7 ++++- 7 files changed, 71 insertions(+), 9 deletions(-) diff --git a/metricbeat/helper/metricset_test.go b/metricbeat/helper/metricset_test.go index 0700f12bb4c..578c723b50d 100644 --- a/metricbeat/helper/metricset_test.go +++ b/metricbeat/helper/metricset_test.go @@ -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, diff --git a/metricbeat/helper/module.go b/metricbeat/helper/module.go index 386c1f3b422..e99546fba21 100644 --- a/metricbeat/helper/module.go +++ b/metricbeat/helper/module.go @@ -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{ @@ -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{}), diff --git a/metricbeat/helper/module_test.go b/metricbeat/helper/module_test.go index 2200b7a90f4..2cb5098410c 100644 --- a/metricbeat/helper/module_test.go +++ b/metricbeat/helper/module_test.go @@ -4,6 +4,8 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/urso/ucfg" + "reflect" ) func TestGetMetricSetsList(t *testing.T) { @@ -19,3 +21,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 +} diff --git a/metricbeat/helper/register.go b/metricbeat/helper/register.go index 7066b33490b..754e2b2bd01 100644 --- a/metricbeat/helper/register.go +++ b/metricbeat/helper/register.go @@ -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) diff --git a/metricbeat/module/apache/apache.go b/metricbeat/module/apache/apache.go index 75f6c271a27..a0720371071 100644 --- a/metricbeat/module/apache/apache.go +++ b/metricbeat/module/apache/apache.go @@ -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{} diff --git a/metricbeat/module/mysql/mysql.go b/metricbeat/module/mysql/mysql.go index e031b947ece..8f8ad7a7dee 100644 --- a/metricbeat/module/mysql/mysql.go +++ b/metricbeat/module/mysql/mysql.go @@ -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{} diff --git a/metricbeat/module/redis/redis.go b/metricbeat/module/redis/redis.go index 42363054c2b..a73b918664e 100644 --- a/metricbeat/module/redis/redis.go +++ b/metricbeat/module/redis/redis.go @@ -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{} From b9fcef23b18a1203a28c73f30400aebf72ec49ef Mon Sep 17 00:00:00 2001 From: ruflin Date: Tue, 8 Mar 2016 16:32:20 +0100 Subject: [PATCH 2/2] Add metricbeat to codecov coverage report --- .travis.yml | 1 + metricbeat/helper/module_test.go | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 2c0c9933975..510755edca4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/metricbeat/helper/module_test.go b/metricbeat/helper/module_test.go index 2cb5098410c..5e39c33b2bf 100644 --- a/metricbeat/helper/module_test.go +++ b/metricbeat/helper/module_test.go @@ -1,11 +1,12 @@ package helper import ( + "reflect" "testing" "github.com/stretchr/testify/assert" + "github.com/urso/ucfg" - "reflect" ) func TestGetMetricSetsList(t *testing.T) {