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

fix: duplicate trigger name validation. Fixes #1262 #1263

Merged
merged 2 commits into from
Jun 30, 2021
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
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:"))
})
}