Skip to content

Commit

Permalink
feat: validation for prometheus trigger name (#2691)
Browse files Browse the repository at this point in the history
* feat: validation for prometheus trigger name

* refactor: use existing validation
  • Loading branch information
crgisch committed Apr 10, 2024
1 parent 1ff3c39 commit ebeccf4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
7 changes: 7 additions & 0 deletions provision/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
logTypes "github.com/tsuru/tsuru/types/log"
provTypes "github.com/tsuru/tsuru/types/provision"
volumeTypes "github.com/tsuru/tsuru/types/volume"
"github.com/tsuru/tsuru/validation"

_ "github.com/tsuru/tsuru/router/api"
)
Expand Down Expand Up @@ -546,6 +547,12 @@ func (s AutoScaleSpec) Validate(quotaLimit int, a App) error {
return err
}
}

for _, prometheus := range s.Prometheus {
if !validation.ValidateName(prometheus.Name) {
return fmt.Errorf("\"%s\" is an invalid name, it must contain only lower case letters, numbers or dashes and starts with a letter", prometheus.Name)
}
}
return nil
}

Expand Down
25 changes: 24 additions & 1 deletion provision/provision_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,33 @@ func (ProvisionSuite) TestValidate(c *check.C) {
},
"you have to configure at least one trigger between cpu, schedule and prometheus",
},
{
AutoScaleSpec{
MinUnits: 1,
MaxUnits: 2,
Prometheus: []AutoScalePrometheus{{
Name: "Invalid-Name",
}},
},
"\"Invalid-Name\" is an invalid name, it must contain only lower case letters, numbers or dashes and starts with a letter",
},
{
AutoScaleSpec{
MinUnits: 1,
MaxUnits: 2,
Prometheus: []AutoScalePrometheus{{
Name: "valid-name",
}, {
Name: "another$invalid",
}},
},
"\"another$invalid\" is an invalid name, it must contain only lower case letters, numbers or dashes and starts with a letter",
},
}

for _, test := range tests {
err := test.input.Validate(10, nil)
c.Check(err, check.ErrorMatches, test.expected)
c.Assert(err, check.NotNil)
c.Assert(err.Error(), check.Equals, test.expected)
}
}

0 comments on commit ebeccf4

Please sign in to comment.