From c6b14833dfa9ee0887e361cde4e31468a6bad5c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20de=20Metz?= Date: Thu, 2 Jun 2016 21:38:22 +0200 Subject: [PATCH] Check for nil value on AvailableWebhooks. --- plugins/inputs/webhooks/webhooks.go | 4 +++- plugins/inputs/webhooks/webhooks_test.go | 29 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 plugins/inputs/webhooks/webhooks_test.go diff --git a/plugins/inputs/webhooks/webhooks.go b/plugins/inputs/webhooks/webhooks.go index 48dc98f50..e89da4401 100644 --- a/plugins/inputs/webhooks/webhooks.go +++ b/plugins/inputs/webhooks/webhooks.go @@ -75,7 +75,9 @@ func (wb *Webhooks) AvailableWebhooks() []Webhook { f := s.Field(i) if wbPlugin, ok := f.Interface().(Webhook); ok { - webhooks = append(webhooks, wbPlugin) + if !reflect.ValueOf(wbPlugin).IsNil() { + webhooks = append(webhooks, wbPlugin) + } } } diff --git a/plugins/inputs/webhooks/webhooks_test.go b/plugins/inputs/webhooks/webhooks_test.go new file mode 100644 index 000000000..85d359e1c --- /dev/null +++ b/plugins/inputs/webhooks/webhooks_test.go @@ -0,0 +1,29 @@ +package webhooks + +import ( + "reflect" + "testing" + + "github.com/influxdata/telegraf/plugins/inputs/webhooks/github" + "github.com/influxdata/telegraf/plugins/inputs/webhooks/rollbar" +) + +func TestAvailableWebhooks(t *testing.T) { + wb := NewWebhooks() + expected := make([]Webhook, 0) + if !reflect.DeepEqual(wb.AvailableWebhooks(), expected) { + t.Errorf("expected to %v.\nGot %v", expected, wb.AvailableWebhooks()) + } + + wb.Github = &github.GithubWebhook{Path: "/github"} + expected = append(expected, wb.Github) + if !reflect.DeepEqual(wb.AvailableWebhooks(), expected) { + t.Errorf("expected to be %v.\nGot %v", expected, wb.AvailableWebhooks()) + } + + wb.Rollbar = &rollbar.RollbarWebhook{Path: "/rollbar"} + expected = append(expected, wb.Rollbar) + if !reflect.DeepEqual(wb.AvailableWebhooks(), expected) { + t.Errorf("expected to be %v.\nGot %v", expected, wb.AvailableWebhooks()) + } +}