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

Refactor sensu_check check_hooks property #956

Closed
wants to merge 6 commits into from
Closed
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
39 changes: 36 additions & 3 deletions lib/puppet/type/sensu_check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@
interval => 60,
}

@example Create a check that has a hook
sensu_check { 'test':
ensure => 'present',
command => 'check-cpu.sh -w 75 -c 90',
subscriptions => ['linux'],
check_hooks => [
{ 'critical' => ['ps'] },
{ 'warning' => ['ps'] },
],
interval => 60,
}

@example Create a check that is subdued
sensu_check { 'test':
ensure => 'present',
Expand Down Expand Up @@ -104,9 +116,30 @@
newvalues(/.*/, :absent)
end

newproperty(:check_hooks, :array_matching => :all, :parent => PuppetX::Sensu::ArrayProperty) do
desc "An array of Sensu hooks (names), which are commands run by the Sensu agent in response to the result of the check command execution."
newvalues(/.*/, :absent)
newproperty(:check_hooks, :array_matching => :all) do
desc "An array of Sensu hooks, which are commands run by the Sensu agent in response to the result of the check command execution."
validate do |value|
if ! value.is_a?(Hash)
raise ArgumentError, "check_hooks elements must be a Hash"
end
if value.keys.size > 1
raise ArgumentError, "check_hooks Hash must only contain one key"
end
type = value.keys[0]
hooks = value[type]
type_valid = false
if ['ok','warning','critical','unknown','non-zero'].include?(type)
type_valid = true
elsif type.to_s =~ /^\d+$/ && type.to_i.between?(1,255)
type_valid = true
end
if ! type_valid
raise ArgumentError, "check_hooks type #{type} is invalid"
end
if ! hooks.is_a?(Array)
raise ArgumentError, "check_hooks hooks must be an Array"
end
end
end

newproperty(:subdue_days, :parent => PuppetX::Sensu::HashProperty) do
Expand Down
21 changes: 16 additions & 5 deletions spec/acceptance/sensu_check_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
pp = <<-EOS
include ::sensu::backend
sensu_check { 'test':
command => 'check-http.rb',
subscriptions => ['demo'],
handlers => ['email'],
interval => 60,
command => 'check-http.rb',
subscriptions => ['demo'],
handlers => ['email'],
interval => 60,
check_hooks => [
{ 'critical' => ['httpd-restart'] },
],
extended_attributes => { 'foo' => 'baz' }
}
EOS

Expand All @@ -23,11 +27,13 @@
on node, 'sensuctl check info test --format json' do
data = JSON.parse(stdout)
expect(data['command']).to eq('check-http.rb')
expect(data['check_hooks']).to eq([{'critical' => ['httpd-restart']}])
expect(data['foo']).to eq('baz')
end
end
end

context 'extended_attributes property' do
context 'updates check' do
it 'should work without errors' do
pp = <<-EOS
include ::sensu::backend
Expand All @@ -36,6 +42,10 @@
subscriptions => ['demo'],
handlers => ['email'],
interval => 60,
check_hooks => [
{ 'critical' => ['httpd-restart'] },
{ 'warning' => ['httpd-restart'] },
],
extended_attributes => { 'foo' => 'bar' }
}
EOS
Expand All @@ -48,6 +58,7 @@
it 'should have a valid check with extended_attributes properties' do
on node, 'sensuctl check info test --format json' do
data = JSON.parse(stdout)
expect(data['check_hooks']).to eq([{'critical' => ['httpd-restart']},{'warning' => ['httpd-restart']}])
expect(data['foo']).to eq('bar')
end
end
Expand Down
43 changes: 42 additions & 1 deletion spec/unit/sensu_check_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
:subscriptions,
:handlers,
:runtime_assets,
:check_hooks,
:proxy_requests_entity_attributes,
:metric_handlers,
:output_metric_handlers,
Expand Down Expand Up @@ -180,6 +179,48 @@
end
end

describe 'check_hooks' do
[
1,
'1',
'ok',
'warning',
'critical',
'unknown',
'non-zero',
].each do |type|
it "accepts valid values for type #{type} #{type.class}" do
config[:check_hooks] = [{type => ['test']}]
expect(check[:check_hooks]).to eq([{type => ['test']}])
end
end

it 'should require Hash elements' do
config[:check_hooks] = ['foo']
expect { check }.to raise_error(Puppet::Error, /check_hooks elements must be a Hash/)
end

it 'should only allow one key' do
config[:check_hooks] = [{'critical' => ['test'],'warning' => ['test']}]
expect { check }.to raise_error(Puppet::Error, /check_hooks Hash must only contain one key/)
end

it 'should require valid type string' do
config[:check_hooks] = [{'crit' => ['test']}]
expect { check }.to raise_error(Puppet::Error, /check_hooks type crit is invalid/)
end

it 'should require valid type integer' do
config[:check_hooks] = [{'256' => ['test']}]
expect { check }.to raise_error(Puppet::Error, /check_hooks type 256 is invalid/)
end

it 'should require hooks list to be an array' do
config[:check_hooks] = [{'critical' => 'test'}]
expect { check }.to raise_error(Puppet::Error, /check_hooks hooks must be an Array/)
end
end

it 'should autorequire Package[sensu-cli]' do
package = Puppet::Type.type(:package).new(:name => 'sensu-cli')
catalog = Puppet::Resource::Catalog.new
Expand Down