Skip to content
This repository has been archived by the owner on Mar 6, 2020. It is now read-only.

Validate check TTL #13

Merged
merged 3 commits into from
May 20, 2015
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
1 change: 1 addition & 0 deletions lib/sensu/settings/rules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def must_be_a_string_if_set(value)
def must_be_an_integer(value)
value.is_a?(Integer)
end
alias_method :is_an_integer?, :must_be_an_integer

# Check that a value is an integer, if set (not nil).
#
Expand Down
54 changes: 42 additions & 12 deletions lib/sensu/settings/validators/check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,32 @@ module Sensu
module Settings
module Validators
module Check
# Validate check name.
# Validates: name
#
# @param check [Hash] sensu check definition.
def validate_check_name(check)
must_be_a_string(check[:name]) ||
invalid(check, "check name must be a string")
must_match_regex(/^[\w\.-]+$/, check[:name]) ||
invalid(check, "check name cannot contain spaces or special characters")
end

# Validate check execution.
# Validates: command, extension, timeout
#
# @param check [Hash] sensu check definition.
def validate_check_execution(check)
must_be_a_string_if_set(check[:command]) ||
invalid(check, "check command must be a string")
must_be_a_string_if_set(check[:extension]) ||
invalid(check, "check extension must be a string")
(!check[:command].nil? ^ !check[:extension].nil?) ||
invalid(check, "either check command or extension must be set")
must_be_a_numeric_if_set(check[:timeout]) ||
invalid(check, "check timeout must be numeric")
end

# Validate check source.
# Validates: source
#
Expand Down Expand Up @@ -53,6 +79,19 @@ def validate_check_handling(check)
end
end

# Validate check ttl.
# Validates: ttl
#
# @param check [Hash] sensu check definition.
def validate_check_ttl(check)
if is_an_integer?(check[:ttl])
check[:ttl] > 0 ||
invalid(check, "check ttl must be greater than 0")
else
invalid(check, "check ttl must be an integer")
end
end

# Validate check flap detection.
# Validates: low_flap_threshold, high_flap_threshold
#
Expand All @@ -70,21 +109,12 @@ def validate_check_flap_detection(check)
#
# @param check [Hash] sensu check definition.
def validate_check(check)
must_be_a_string(check[:name]) ||
invalid(check, "check name must be a string")
must_match_regex(/^[\w\.-]+$/, check[:name]) ||
invalid(check, "check name cannot contain spaces or special characters")
must_be_a_string_if_set(check[:command]) ||
invalid(check, "check command must be a string")
must_be_a_string_if_set(check[:extension]) ||
invalid(check, "check extension must be a string")
(!check[:command].nil? ^ !check[:extension].nil?) ||
invalid(check, "either check command or extension must be set")
must_be_a_numeric_if_set(check[:timeout]) ||
invalid(check, "check timeout must be numeric")
validate_check_name(check)
validate_check_execution(check)
validate_check_source(check) if check[:source]
validate_check_scheduling(check)
validate_check_handling(check)
validate_check_ttl(check) if check[:ttl]
validate_check_flap_detection(check)
validate_subdue(check) if check[:subdue]
end
Expand Down
12 changes: 12 additions & 0 deletions spec/validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,18 @@
check[:handlers] = ["cat"]
@validator.validate_check(check)
expect(@validator.reset).to eq(0)
check[:ttl] = true
@validator.validate_check(check)
expect(@validator.reset).to eq(1)
check[:ttl] = -1
@validator.validate_check(check)
expect(@validator.reset).to eq(1)
check[:ttl] = 0
@validator.validate_check(check)
expect(@validator.reset).to eq(1)
check[:ttl] = 1
@validator.validate_check(check)
expect(@validator.reset).to eq(0)
check[:low_flap_threshold] = "25"
@validator.validate_check(check)
expect(@validator.reset).to eq(2)
Expand Down