Skip to content

Commit

Permalink
Return empty registry if module path doesn't exist
Browse files Browse the repository at this point in the history
This is a follow up for elastic#3405, to avoid having a nil moduleRegistry.
Also adds a unit test for that case.
  • Loading branch information
Tudor Golubenco committed Jan 25, 2017
1 parent 5878188 commit a95f2c1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
10 changes: 3 additions & 7 deletions filebeat/beater/filebeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,9 @@ func New(b *beat.Beat, rawConfig *common.Config) (beat.Beater, error) {
return nil, err
}

var moduleProspectors []*common.Config
if moduleRegistry != nil {
var err error
moduleProspectors, err = moduleRegistry.GetProspectorConfigs()
if err != nil {
return nil, err
}
moduleProspectors, err := moduleRegistry.GetProspectorConfigs()
if err != nil {
return nil, err
}

if err := config.FetchConfigs(); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions filebeat/fileset/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ func NewModuleRegistry(moduleConfigs []*common.Config) (*ModuleRegistry, error)

stat, err := os.Stat(modulesPath)
if err != nil || !stat.IsDir() {
logp.Info("Not loading modules. Module directory not found: %s")
return nil, nil
logp.Err("Not loading modules. Module directory not found: %s", modulesPath)
return &ModuleRegistry{}, nil // empty registry, no error
}

modulesCLIList, modulesOverrides, err := getModulesCLIConfig()
Expand Down
20 changes: 20 additions & 0 deletions filebeat/fileset/modules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/paths"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -322,3 +323,22 @@ func TestMcfgFromConfig(t *testing.T) {
}
}
}

func TestMissingModuleFolder(t *testing.T) {
home := paths.Paths.Home
paths.Paths.Home = "/no/such/path"
defer func() { paths.Paths.Home = home }()

configs := []*common.Config{
load(t, map[string]interface{}{"module": "nginx"}),
}

reg, err := NewModuleRegistry(configs)
assert.NoError(t, err)
assert.NotNil(t, reg)

// this should return an empty list, but no error
prospectors, err := reg.GetProspectorConfigs()
assert.NoError(t, err)
assert.Equal(t, 0, len(prospectors))
}

0 comments on commit a95f2c1

Please sign in to comment.