Skip to content

Commit

Permalink
fix: duplicate trigger name validation. Fixes #1262 (#1263)
Browse files Browse the repository at this point in the history
Signed-off-by: Derek Wang <whynowy@gmail.com>
  • Loading branch information
whynowy authored Jun 30, 2021
1 parent 37d5d0d commit ce67b38
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
16 changes: 11 additions & 5 deletions controllers/sensor/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,13 @@ func validateTriggers(triggers []v1alpha1.Trigger) error {
return errors.Errorf("no triggers found")
}

trigNames := make(map[string]bool)

for _, trigger := range triggers {
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
}
Expand Down Expand Up @@ -152,10 +158,10 @@ func validateTriggerTemplate(template *v1alpha1.TriggerTemplate) error {
// validateK8STrigger validates a kubernetes trigger
func validateK8STrigger(trigger *v1alpha1.StandardK8STrigger) error {
if trigger == nil {
return errors.New("k8s trigger for can't be nil")
return errors.New("k8s trigger can't be nil")
}
if trigger.Source == nil {
return errors.New("k8s trigger for does not contain an absolute action")
return errors.New("k8s trigger does not contain an absolute action")
}
if trigger.GroupVersionResource.Size() == 0 {
return errors.New("must provide group, version and resource for the resource")
Expand All @@ -179,10 +185,10 @@ func validateK8STrigger(trigger *v1alpha1.StandardK8STrigger) error {
// validateArgoWorkflowTrigger validates an Argo workflow trigger
func validateArgoWorkflowTrigger(trigger *v1alpha1.ArgoWorkflowTrigger) error {
if trigger == nil {
return errors.New("k8s trigger for can't be nil")
return errors.New("argoWorkflow trigger can't be nil")
}
if trigger.Source == nil {
return errors.New("k8s trigger for does not contain an absolute action")
return errors.New("argoWorkflow trigger does not contain an absolute action")
}
if trigger.GroupVersionResource.Size() == 0 {
return errors.New("must provide group, version and resource for the resource")
Expand All @@ -205,7 +211,7 @@ func validateArgoWorkflowTrigger(trigger *v1alpha1.ArgoWorkflowTrigger) error {
// validateHTTPTrigger validates the HTTP trigger
func validateHTTPTrigger(trigger *v1alpha1.HTTPTrigger) error {
if trigger == nil {
return errors.New("openfaas trigger for can't be nil")
return errors.New("HTTP trigger for can't be nil")
}
if trigger.URL == "" {
return errors.New("server URL is not specified")
Expand Down
39 changes: 39 additions & 0 deletions controllers/sensor/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/argoproj/argo-events/pkg/apis/sensor/v1alpha1"
"github.com/ghodss/yaml"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestValidateSensor(t *testing.T) {
Expand Down Expand Up @@ -88,3 +89,41 @@ func TestValidDepencies(t *testing.T) {
assert.Equal(t, true, strings.Contains(err.Error(), "must define a name"))
})
}

func TestValidTriggers(t *testing.T) {
t.Run("duplicate trigger names", func(t *testing.T) {
triggers := []v1alpha1.Trigger{
{
Template: &v1alpha1.TriggerTemplate{
Name: "fake-trigger",
K8s: &v1alpha1.StandardK8STrigger{
GroupVersionResource: metav1.GroupVersionResource{
Group: "k8s.io",
Version: "",
Resource: "pods",
},
Operation: "create",
Source: &v1alpha1.ArtifactLocation{},
},
},
},
{
Template: &v1alpha1.TriggerTemplate{
Name: "fake-trigger",
K8s: &v1alpha1.StandardK8STrigger{
GroupVersionResource: metav1.GroupVersionResource{
Group: "k8s.io",
Version: "",
Resource: "pods",
},
Operation: "create",
Source: &v1alpha1.ArtifactLocation{},
},
},
},
}
err := validateTriggers(triggers)
assert.NotNil(t, err)
assert.Equal(t, true, strings.Contains(err.Error(), "duplicate trigger name:"))
})
}

0 comments on commit ce67b38

Please sign in to comment.