From 79c472ded3b11a7c5cf6b10e5d5ffd934f3b8175 Mon Sep 17 00:00:00 2001 From: Luca Iachini <77624486+luca-iachini@users.noreply.github.com> Date: Thu, 14 Oct 2021 18:02:03 +0200 Subject: [PATCH] fix: sensor controller validate sensor (#1378) Signed-off-by: luca-iachini --- controllers/sensor/validate.go | 6 +++--- controllers/sensor/validate_test.go | 11 +++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/controllers/sensor/validate.go b/controllers/sensor/validate.go index 742f3b4ea7..c5ceb40785 100644 --- a/controllers/sensor/validate.go +++ b/controllers/sensor/validate.go @@ -54,13 +54,13 @@ func validateTriggers(triggers []v1alpha1.Trigger) error { trigNames := make(map[string]bool) for _, trigger := range triggers { + if err := validateTriggerTemplate(trigger.Template); err != nil { + return err + } if _, ok := trigNames[trigger.Template.Name]; ok { return fmt.Errorf("duplicate trigger name: %s", trigger.Template.Name) } trigNames[trigger.Template.Name] = true - if err := validateTriggerTemplate(trigger.Template); err != nil { - return err - } if err := validateTriggerPolicy(&trigger); err != nil { return err } diff --git a/controllers/sensor/validate_test.go b/controllers/sensor/validate_test.go index 3e5c4d1318..6df59ce318 100644 --- a/controllers/sensor/validate_test.go +++ b/controllers/sensor/validate_test.go @@ -115,4 +115,15 @@ func TestValidTriggers(t *testing.T) { assert.NotNil(t, err) assert.Equal(t, true, strings.Contains(err.Error(), "duplicate trigger name:")) }) + + t.Run("empty trigger template", func(t *testing.T) { + triggers := []v1alpha1.Trigger{ + { + Template: nil, + }, + } + err := validateTriggers(triggers) + assert.NotNil(t, err) + assert.Equal(t, true, strings.Contains(err.Error(), "trigger template can't be nil")) + }) }