From a4949d25cb132274450730197ca968721e715cbe Mon Sep 17 00:00:00 2001 From: Sean Porter Date: Tue, 19 May 2015 21:32:32 -0700 Subject: [PATCH 1/3] [check-ttl] validate check ttl --- lib/sensu/settings/rules.rb | 1 + lib/sensu/settings/validators/check.rb | 14 ++++++++++++++ spec/validator_spec.rb | 12 ++++++++++++ 3 files changed, 27 insertions(+) diff --git a/lib/sensu/settings/rules.rb b/lib/sensu/settings/rules.rb index c0e81e6..ef2397b 100644 --- a/lib/sensu/settings/rules.rb +++ b/lib/sensu/settings/rules.rb @@ -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). # diff --git a/lib/sensu/settings/validators/check.rb b/lib/sensu/settings/validators/check.rb index 3419f66..be54c7d 100644 --- a/lib/sensu/settings/validators/check.rb +++ b/lib/sensu/settings/validators/check.rb @@ -53,6 +53,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 # @@ -85,6 +98,7 @@ def validate_check(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 diff --git a/spec/validator_spec.rb b/spec/validator_spec.rb index 79a1b1f..5aaefc3 100644 --- a/spec/validator_spec.rb +++ b/spec/validator_spec.rb @@ -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) From d3c50bd577f78edcc3c56e7e98f5f8ac4c03265a Mon Sep 17 00:00:00 2001 From: Sean Porter Date: Tue, 19 May 2015 21:39:21 -0700 Subject: [PATCH 2/3] [check-ttl] validate_check_execution() to clean up validate_check() --- lib/sensu/settings/validators/check.rb | 32 ++++++++++++++++---------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/lib/sensu/settings/validators/check.rb b/lib/sensu/settings/validators/check.rb index be54c7d..12e07ea 100644 --- a/lib/sensu/settings/validators/check.rb +++ b/lib/sensu/settings/validators/check.rb @@ -2,6 +2,25 @@ module Sensu module Settings module Validators module Check + # Validate check execution. + # Validates: name, command, extension, timeout + # + # @param check [Hash] sensu check definition. + def validate_check_execution(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") + end + # Validate check source. # Validates: source # @@ -83,18 +102,7 @@ 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_execution(check) validate_check_source(check) if check[:source] validate_check_scheduling(check) validate_check_handling(check) From b667f727444ab14957480ba663e7967c9a62b8c9 Mon Sep 17 00:00:00 2001 From: Sean Porter Date: Tue, 19 May 2015 21:41:44 -0700 Subject: [PATCH 3/3] [check-ttl] validate_check_name() for further cleanup --- lib/sensu/settings/validators/check.rb | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/sensu/settings/validators/check.rb b/lib/sensu/settings/validators/check.rb index 12e07ea..1d98e45 100644 --- a/lib/sensu/settings/validators/check.rb +++ b/lib/sensu/settings/validators/check.rb @@ -2,15 +2,22 @@ module Sensu module Settings module Validators module Check - # Validate check execution. - # Validates: name, command, extension, timeout + # Validate check name. + # Validates: name # # @param check [Hash] sensu check definition. - def validate_check_execution(check) + 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]) || @@ -102,6 +109,7 @@ def validate_check_flap_detection(check) # # @param check [Hash] sensu check definition. def validate_check(check) + validate_check_name(check) validate_check_execution(check) validate_check_source(check) if check[:source] validate_check_scheduling(check)