Skip to content

Commit

Permalink
[Heartbeat] Fix NPE on reloads with run_from (elastic#35401)
Browse files Browse the repository at this point in the history
* [Heartbeat] Fix NPE on reloads with run_from

we incorrectly always read from the beat location which may be null,
this fixes that.

* add tests
  • Loading branch information
andrewvc committed May 9, 2023
1 parent 6222e6b commit 9072ba6
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
2 changes: 1 addition & 1 deletion heartbeat/monitors/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (f *RunnerFactory) Create(p beat.Pipeline, c *conf.C) (cfgfile.Runner, erro
geoMap, _ := util.GeoConfigToMap(loc.Geo)
err = c.Merge(map[string]interface{}{
"run_from": map[string]interface{}{
"id": f.beatLocation.ID,
"id": loc.ID,
"geo": geoMap,
},
})
Expand Down
65 changes: 65 additions & 0 deletions heartbeat/monitors/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,71 @@ func TestDisabledMonitor(t *testing.T) {
}
}

func TestRunFrom(t *testing.T) {
tests := []struct {
name string
loc *hbconfig.LocationWithID
}{
{
"no location",
nil,
},
{
"with id",
&hbconfig.LocationWithID{
ID: "test",
},
},
}

for _, tt := range tests {
confMap := map[string]interface{}{
"type": "test",
"urls": []string{"http://example.net"},
"schedule": "@every 1ms",
"name": "test",
}
if tt.loc != nil {
geo, err := util.GeoConfigToMap(tt.loc.Geo)
require.NoError(t, err)
confMap["run_from"] = map[string]interface{}{
"id": tt.loc.ID,
"geo": geo,
}
}

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

reg, _, _ := mockPluginsReg()
mockPipeline := &MockPipeline{}

f, sched, fClose := makeMockFactory(reg)
defer fClose()
defer sched.Stop()

makeTestMon := func() (*Monitor, error) {
mIface, err := f.Create(mockPipeline, conf)
if mIface == nil {
return nil, err
} else {
return mIface.(*Monitor), err
}
}

// Would fail if the previous newMonitor didn't free the monitor.id
m1, m1Err := makeTestMon()
require.NoError(t, m1Err)

if tt.loc == nil {
var emptyLoc *hbconfig.LocationWithID
require.Equal(t, emptyLoc, m1.stdFields.RunFrom)
} else {
require.Equal(t, tt.loc, m1.stdFields.RunFrom)
}
}
}

func TestDuplicateMonitorIDs(t *testing.T) {
serverMonConf := mockPluginConf(t, "custom", "custom", "@every 1ms", "http://example.net")
badConf := mockBadPluginConf(t, "custom")
Expand Down

0 comments on commit 9072ba6

Please sign in to comment.