Skip to content

Commit

Permalink
Cron and Interval check scheduling are now mutually exclusive (#941)
Browse files Browse the repository at this point in the history
* Cron and Interval check scheduling are now mutually exclusive

Signed-off-by: Greg Poirier <greg.istehbest@gmail.com>
  • Loading branch information
grepory committed Jan 29, 2018
1 parent 779b7a1 commit 44cc345
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Versioning](http://semver.org/spec/v2.0.0.html).

### Changed
- Govaluate logic is now wrapped in the `util/eval` package.
- Cron and Interval scheduling are now mutually exclusive.

### Fixed
- Fixed a bug where retrieving check hooks were only from the check's
Expand Down
1 change: 1 addition & 0 deletions testing/e2e/check_scheduling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func TestCheckScheduling(t *testing.T) {
assert.NotEqual(t, count2, count3)

// Change the check schedule to cron
check.Interval = 0
check.Cron = "* * * * *"
err = sensuClient.UpdateCheck(check)
assert.NoError(t, err)
Expand Down
10 changes: 9 additions & 1 deletion types/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,23 @@ func (c *CheckConfig) Validate() error {
}

if c.Cron != "" {
if c.Interval != 0 {
return errors.New("must only specify either an interval or a cron schedule")
}

if _, err := cron.ParseStandard(c.Cron); err != nil {
return errors.New("check cron string is invalid")
}
}

if c.Interval == 0 {
if c.Interval == 0 && c.Cron == "" {
return errors.New("check interval must be greater than 0")
}

if c.Interval > 0 && c.Cron != "" {
return errors.New("must only specify either an interval or a cron schedule")
}

if c.Environment == "" {
return errors.New("environment cannot be empty")
}
Expand Down
21 changes: 17 additions & 4 deletions types/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ func TestCheckConfig(t *testing.T) {
assert.Error(t, c.Validate())
c.Interval = 60

// Invalid cron
assert.Error(t, c.Validate())
c.Cron = "* * * * *"

// Invalid command
assert.Error(t, c.Validate())
c.Command = "echo 'foo'"
Expand All @@ -74,6 +70,23 @@ func TestCheckConfig(t *testing.T) {
assert.NoError(t, c.Validate())
}

func TestScheduleValidation(t *testing.T) {
c := FixtureCheck("check")
config := c.Config

// Fixture comes with valid interval-based schedule
assert.NoError(t, config.Validate())

config.Cron = "* * * * *"
assert.Error(t, config.Validate())

config.Interval = 0
assert.NoError(t, config.Validate())

config.Cron = "this is an invalid cron"
assert.Error(t, config.Validate())
}

func TestFixtureCheckIsValid(t *testing.T) {
c := FixtureCheck("check")
config := c.Config
Expand Down

0 comments on commit 44cc345

Please sign in to comment.