Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Heartbeat] Fix NPE on monitor configuration errors #11910

Merged
merged 1 commit into from
Apr 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d

*Heartbeat*

- Fix NPE on some monitor configuration errors. {pull}11910[11910]

*Journalbeat*

*Metricbeat*
Expand Down
11 changes: 11 additions & 0 deletions heartbeat/monitors/mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,14 @@ func mockPluginConf(t *testing.T, id string, schedule string, url string) *commo

return conf
}

func mockInvalidPluginConf(t *testing.T) *common.Config {
confMap := map[string]interface{}{
"hoeutnheou": "oueanthoue",
}

conf, err := common.NewConfigFrom(confMap)
require.NoError(t, err)

return conf
}
4 changes: 3 additions & 1 deletion heartbeat/monitors/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ func (m *Monitor) String() string {

func checkMonitorConfig(config *common.Config, registrar *pluginsReg, allowWatches bool) error {
m, err := newMonitor(config, registrar, nil, nil, allowWatches, nil)
m.Stop() // Stop the monitor to free up the ID from uniqueness checks
if m != nil {
m.Stop() // Stop the monitor to free up the ID from uniqueness checks
}
return err
}

Expand Down
17 changes: 17 additions & 0 deletions heartbeat/monitors/monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,20 @@ func TestDuplicateMonitorIDs(t *testing.T) {
_, m3Err := makeTestMon()
assert.NoError(t, m3Err)
}

func TestCheckInvalidConfig(t *testing.T) {
serverMonConf := mockInvalidPluginConf(t)
reg := mockPluginsReg()
pipelineConnector := &MockPipelineConnector{}

sched := scheduler.New(1)
err := sched.Start()
require.NoError(t, err)
defer sched.Stop()

m, err := newMonitor(serverMonConf, reg, pipelineConnector, sched, false, nil)
// This could change if we decide the contract for newMonitor should always return a monitor
require.Nil(t, m, "For this test to work we need a nil value for the monitor.")

require.Error(t, checkMonitorConfig(serverMonConf, reg, false))
}