sensu-plugin 2.1.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5fa20b06e805a4910513f35e0d49e04190c04618
4
- data.tar.gz: 875707ed61b5a30a87aa13350f857d6836582fad
3
+ metadata.gz: 5779d44ecb71e0614dc179fb8ba0db3cd39a3111
4
+ data.tar.gz: 10795a0823625575f4b1638984a603a65e406eb6
5
5
  SHA512:
6
- metadata.gz: d7d2b872c3322ccc4e43e56329a54b3e9fba0cdae70ed9fadcadc1276ff82e12108c6bc297e668c81ca97dfb8bcf212903b5cd4192399c2047829de316c93c04
7
- data.tar.gz: bee33bfaee01fe9f133c2f92699e80e1bea059117952085afbe3ece721bb7d3072b85742e5204110c6e7966509de03561dc271dfafe094072cd3a6e1d23915a9
6
+ metadata.gz: b00f35a4eb526215f5c1f13f3732ebb35519491176be7a87495d30279389d4d72b843c1004312fface38633bb5a6d20c9dabb135f99c309a90d3e417ce68d7fc
7
+ data.tar.gz: 37d95a80143a85142c43b25d7b5651abc71a5fbc6832ee8e2c8350ef443ae8bec75dede44b76541c37082cd24e2b8a30074cfebd4c2dafa16f2387f3ebd5d759
data/lib/sensu-handler.rb CHANGED
@@ -6,7 +6,6 @@ require 'sensu-plugin/utils'
6
6
  require 'mixlib/cli'
7
7
 
8
8
  module Sensu
9
-
10
9
  class Handler
11
10
  include Sensu::Plugin::Utils
12
11
  include Mixlib::CLI
@@ -24,22 +23,19 @@ module Sensu
24
23
  puts 'ignoring event -- no handler defined'
25
24
  end
26
25
 
27
-
28
26
  # Filters exit the proccess if the event should not be handled.
29
27
  #
30
28
  # Filtering events is deprecated and will be removed in a future release.
31
29
  #
32
30
  def filter
33
- if deprecated_filtering_enabled?
34
- puts 'warning: event filtering in sensu-plugin is deprecated, see http://bit.ly/sensu-plugin'
35
- filter_disabled
36
- filter_silenced
37
- filter_dependencies
38
- if deprecated_occurrence_filtering_enabled?
39
- puts 'warning: occurrence filtering in sensu-plugin is deprecated, see http://bit.ly/sensu-plugin'
40
- filter_repeated
41
- end
42
- end
31
+ return unless deprecated_filtering_enabled?
32
+ puts 'warning: event filtering in sensu-plugin is deprecated, see http://bit.ly/sensu-plugin'
33
+ filter_disabled
34
+ filter_silenced
35
+ filter_dependencies
36
+ return unless deprecated_occurrence_filtering_enabled?
37
+ puts 'warning: occurrence filtering in sensu-plugin is deprecated, see http://bit.ly/sensu-plugin'
38
+ filter_repeated
43
39
  end
44
40
 
45
41
  # Evaluates whether the event should be processed by any of the
@@ -48,7 +44,7 @@ module Sensu
48
44
  #
49
45
  # @return [TrueClass, FalseClass]
50
46
  def deprecated_filtering_enabled?
51
- @event['check'].fetch('enable_deprecated_filtering', false).to_s == "true"
47
+ @event['check'].fetch('enable_deprecated_filtering', false).to_s == 'true'
52
48
  end
53
49
 
54
50
  # Evaluates whether the event should be processed by the
@@ -57,7 +53,7 @@ module Sensu
57
53
  #
58
54
  # @return [TrueClass, FalseClass]
59
55
  def deprecated_occurrence_filtering_enabled?
60
- @event['check'].fetch('enable_deprecated_occurrence_filtering', false).to_s == "true"
56
+ @event['check'].fetch('enable_deprecated_occurrence_filtering', false).to_s == 'true'
61
57
  end
62
58
 
63
59
  # This works just like Plugin::CLI's autorun.
@@ -65,9 +61,7 @@ module Sensu
65
61
  @@autorun = self
66
62
  class << self
67
63
  def method_added(name)
68
- if name == :handle
69
- @@autorun = self
70
- end
64
+ @@autorun = self if name == :handle
71
65
  end
72
66
  end
73
67
 
@@ -86,7 +80,7 @@ module Sensu
86
80
 
87
81
  # Helpers and filters.
88
82
 
89
- def event_summary(trim_at=100)
83
+ def event_summary(trim_at = 100)
90
84
  summary = @event['check']['notification'] || @event['check']['description']
91
85
  if summary.nil?
92
86
  source = @event['check']['source'] || @event['client']['name']
@@ -112,8 +106,7 @@ module Sensu
112
106
  # @return [Hash]
113
107
  def api_settings
114
108
  return @api_settings if @api_settings
115
- case
116
- when ENV['SENSU_API_URL']
109
+ if ENV['SENSU_API_URL']
117
110
  uri = URI(ENV['SENSU_API_URL'])
118
111
  @api_settings = {
119
112
  'host' => uri.host,
@@ -129,9 +122,9 @@ module Sensu
129
122
  @api_settings
130
123
  end
131
124
 
132
- def api_request(method, path, &blk)
125
+ def api_request(method, path, &_blk)
133
126
  if api_settings.nil?
134
- raise "api.json settings not found."
127
+ raise 'api.json settings not found.'
135
128
  end
136
129
  domain = api_settings['host'].start_with?('http') ? api_settings['host'] : 'http://' + api_settings['host']
137
130
  uri = URI("#{domain}:#{api_settings['port']}#{path}")
@@ -140,16 +133,14 @@ module Sensu
140
133
  req.basic_auth(api_settings['user'], api_settings['password'])
141
134
  end
142
135
  yield(req) if block_given?
143
- res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') do |http|
136
+ res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
144
137
  http.request(req)
145
138
  end
146
139
  res
147
140
  end
148
141
 
149
142
  def filter_disabled
150
- if @event['check']['alert'] == false
151
- bail 'alert disabled'
152
- end
143
+ bail 'alert disabled' if @event['check']['alert'] == false
153
144
  end
154
145
 
155
146
  def filter_repeated
@@ -169,12 +160,10 @@ module Sensu
169
160
  if @event['occurrences'] < occurrences
170
161
  bail 'not enough occurrences'
171
162
  end
172
- if @event['occurrences'] > occurrences && @event['action'] == 'create'
173
- number = refresh.fdiv(interval).to_i
174
- unless number == 0 || (@event['occurrences'] - occurrences) % number == 0
175
- bail 'only handling every ' + number.to_s + ' occurrences'
176
- end
177
- end
163
+ return unless @event['occurrences'] > occurrences && @event['action'] == 'create'
164
+ number = refresh.fdiv(interval).to_i
165
+ return if number.zero? || ((@event['occurrences'] - occurrences) % number).zero?
166
+ bail 'only handling every ' + number.to_s + ' occurrences'
178
167
  end
179
168
 
180
169
  def stash_exists?(path)
@@ -207,27 +196,23 @@ module Sensu
207
196
  end
208
197
 
209
198
  def filter_dependencies
210
- if @event['check'].has_key?('dependencies')
211
- if @event['check']['dependencies'].is_a?(Array)
212
- @event['check']['dependencies'].each do |dependency|
213
- begin
214
- next if dependency.to_s.empty?
215
- Timeout.timeout(2) do
216
- check, client = dependency.split('/').reverse
217
- if event_exists?(client || @event['client']['name'], check)
218
- bail 'check dependency event exists'
219
- end
220
- end
221
- rescue Errno::ECONNREFUSED
222
- puts 'connection refused attempting to query the sensu api for an event'
223
- rescue Timeout::Error
224
- puts 'timed out while attempting to query the sensu api for an event'
199
+ return unless @event['check'].key?('dependencies')
200
+ return unless @event['check']['dependencies'].is_a?(Array)
201
+ @event['check']['dependencies'].each do |dependency|
202
+ begin
203
+ next if dependency.to_s.empty?
204
+ Timeout.timeout(2) do
205
+ check, client = dependency.split('/').reverse
206
+ if event_exists?(client || @event['client']['name'], check)
207
+ bail 'check dependency event exists'
225
208
  end
226
209
  end
210
+ rescue Errno::ECONNREFUSED
211
+ puts 'connection refused attempting to query the sensu api for an event'
212
+ rescue Timeout::Error
213
+ puts 'timed out while attempting to query the sensu api for an event'
227
214
  end
228
215
  end
229
216
  end
230
-
231
217
  end
232
-
233
218
  end
data/lib/sensu-mutator.rb CHANGED
@@ -6,12 +6,12 @@
6
6
  # DESCRIPTION:
7
7
  # Base mutator class. All you need to do is extend this class and implement a
8
8
  # #mutate function. Uses the autorun feature just like sensu-handler and sensu-plugin/cli
9
- #
9
+ #
10
10
  # Example Implementation: described https://sensuapp.org/docs/latest/mutators#example-mutator-plugin
11
11
  #
12
12
  # class Helper < Sensu::Mutator
13
13
  # def mutate
14
- # @event.merge!(:mutated => true)
14
+ # @event.merge!(mutated: true)
15
15
  # end
16
16
  # end
17
17
  #
@@ -55,9 +55,7 @@ module Sensu
55
55
  @@autorun = self
56
56
  class << self
57
57
  def method_added(name)
58
- if name == :mutate
59
- @@autorun = self
60
- end
58
+ @@autorun = self if name == :mutate
61
59
  end
62
60
  end
63
61
 
@@ -66,12 +64,11 @@ module Sensu
66
64
  end
67
65
 
68
66
  at_exit do
69
- if @@autorun
70
- mutator = @@autorun.new
71
- mutator.read_event(STDIN)
72
- mutator.mutate
73
- mutator.dump
74
- end
67
+ return unless @@autorun
68
+ mutator = @@autorun.new
69
+ mutator.read_event(STDIN)
70
+ mutator.mutate
71
+ mutator.dump
75
72
  end
76
73
  end
77
74
  end
data/lib/sensu-plugin.rb CHANGED
@@ -1,11 +1,11 @@
1
1
  module Sensu
2
2
  module Plugin
3
- VERSION = "2.1.0"
3
+ VERSION = '2.2.0'.freeze
4
4
  EXIT_CODES = {
5
5
  'OK' => 0,
6
6
  'WARNING' => 1,
7
7
  'CRITICAL' => 2,
8
8
  'UNKNOWN' => 3
9
- }
9
+ }.freeze
10
10
  end
11
11
  end
@@ -4,13 +4,12 @@ module Sensu
4
4
  module Plugin
5
5
  class Check
6
6
  class CLI < Sensu::Plugin::CLI
7
-
8
7
  class << self
9
- def check_name(name=nil)
8
+ def check_name(name = nil)
10
9
  if name
11
10
  @check_name = name
12
11
  else
13
- @check_name || self.to_s
12
+ @check_name || to_s
14
13
  end
15
14
  end
16
15
  end
@@ -19,10 +18,9 @@ module Sensu
19
18
  @message = msg
20
19
  end
21
20
 
22
- def output(msg=@message)
23
- puts "#{self.class.check_name} #{@status}" + (msg ? ": #{msg}" : "")
21
+ def output(msg = @message)
22
+ puts "#{self.class.check_name} #{@status}" + (msg ? ": #{msg}" : '')
24
23
  end
25
-
26
24
  end
27
25
  end
28
26
  end
@@ -1,5 +1,6 @@
1
1
  require 'sensu-plugin'
2
2
  require 'mixlib/cli'
3
+ require 'English'
3
4
 
4
5
  module Sensu
5
6
  module Plugin
@@ -8,9 +9,9 @@ module Sensu
8
9
 
9
10
  attr_accessor :argv
10
11
 
11
- def initialize(argv=ARGV)
12
+ def initialize(argv = ARGV)
12
13
  super()
13
- self.argv = self.parse_options(argv)
14
+ self.argv = parse_options(argv)
14
15
  end
15
16
 
16
17
  # Implementing classes should override this to produce appropriate
@@ -34,7 +35,7 @@ module Sensu
34
35
  # Implementing classes must override this.
35
36
 
36
37
  def run
37
- unknown "Not implemented! You should override Sensu::Plugin::CLI#run."
38
+ unknown 'Not implemented! You should override Sensu::Plugin::CLI#run.'
38
39
  end
39
40
 
40
41
  # Behind-the-scenes stuff below. If you do something crazy like
@@ -44,14 +45,12 @@ module Sensu
44
45
  @@autorun = self
45
46
  class << self
46
47
  def method_added(name)
47
- if name == :run
48
- @@autorun = self
49
- end
48
+ @@autorun = self if name == :run
50
49
  end
51
50
  end
52
51
 
53
52
  at_exit do
54
- exit 3 if $!
53
+ exit 3 if $ERROR_INFO
55
54
  if @@autorun
56
55
  begin
57
56
  check = @@autorun.new
@@ -61,15 +60,14 @@ module Sensu
61
60
  rescue OptionParser::InvalidOption => e
62
61
  puts "Invalid check argument(s): #{e.message}, #{e.backtrace}"
63
62
  exit 3
64
- rescue Exception => e
63
+ rescue Exception => e # rubocop:disable Lint/RescueException
65
64
  # This can't call check.critical, as the check may have failed to construct
66
65
  puts "Check failed to run: #{e.message}, #{e.backtrace}"
67
66
  exit 3
68
67
  end
69
- check.warning "Check did not exit! You should call an exit code method."
68
+ check.warning 'Check did not exit! You should call an exit code method.'
70
69
  end
71
70
  end
72
-
73
71
  end
74
72
  end
75
73
  end
@@ -5,9 +5,8 @@ module Sensu
5
5
  module Plugin
6
6
  class Metric
7
7
  class CLI
8
-
9
8
  class JSON < Sensu::Plugin::CLI
10
- def output(obj=nil)
9
+ def output(obj = nil)
11
10
  if obj.is_a?(String) || obj.is_a?(Exception)
12
11
  puts obj.to_s
13
12
  elsif obj.is_a?(Hash)
@@ -19,13 +18,12 @@ module Sensu
19
18
 
20
19
  class Graphite < Sensu::Plugin::CLI
21
20
  def output(*args)
22
- unless args.empty?
23
- if args[0].is_a?(Exception) || args[1].nil?
24
- puts args[0].to_s
25
- else
26
- args[2] ||= Time.now.to_i
27
- puts args[0..2].join("\s")
28
- end
21
+ return if args.empty?
22
+ if args[0].is_a?(Exception) || args[1].nil?
23
+ puts args[0].to_s
24
+ else
25
+ args[2] ||= Time.now.to_i
26
+ puts args[0..2].join("\s")
29
27
  end
30
28
  end
31
29
  end
@@ -40,7 +38,6 @@ module Sensu
40
38
  end
41
39
  end
42
40
  end
43
-
44
41
  end
45
42
  end
46
43
  end
@@ -3,7 +3,6 @@ require 'json'
3
3
  module Sensu
4
4
  module Plugin
5
5
  module Utils
6
-
7
6
  def config_files
8
7
  if ENV['SENSU_LOADED_TEMPFILE'] && File.file?(ENV['SENSU_LOADED_TEMPFILE'])
9
8
  IO.read(ENV['SENSU_LOADED_TEMPFILE']).split(':')
@@ -15,23 +14,23 @@ module Sensu
15
14
  end
16
15
 
17
16
  def load_config(filename)
18
- JSON.parse(File.open(filename, 'r').read) rescue Hash.new
17
+ JSON.parse(File.open(filename, 'r').read)
18
+ rescue
19
+ {}
19
20
  end
20
21
 
21
22
  def settings
22
- @settings ||= config_files.map {|f| load_config(f) }.reduce {|a, b| deep_merge(a, b) }
23
+ @settings ||= config_files.map { |f| load_config(f) }.reduce { |a, b| deep_merge(a, b) }
23
24
  end
24
25
 
25
26
  def read_event(file)
26
- begin
27
- @event = ::JSON.parse(file.read)
28
- @event['occurrences'] ||= 1
29
- @event['check'] ||= Hash.new
30
- @event['client'] ||= Hash.new
31
- rescue => e
32
- puts 'error reading event: ' + e.message
33
- exit 0
34
- end
27
+ @event = ::JSON.parse(file.read)
28
+ @event['occurrences'] ||= 1
29
+ @event['check'] ||= {}
30
+ @event['client'] ||= {}
31
+ rescue => e
32
+ puts 'error reading event: ' + e.message
33
+ exit 0
35
34
  end
36
35
 
37
36
  def net_http_req_class(method)
@@ -50,14 +49,13 @@ module Sensu
50
49
  def deep_merge(hash_one, hash_two)
51
50
  merged = hash_one.dup
52
51
  hash_two.each do |key, value|
53
- merged[key] = case
54
- when hash_one[key].is_a?(Hash) && value.is_a?(Hash)
55
- deep_merge(hash_one[key], value)
56
- when hash_one[key].is_a?(Array) && value.is_a?(Array)
57
- hash_one[key].concat(value).uniq
58
- else
59
- value
60
- end
52
+ merged[key] = if hash_one[key].is_a?(Hash) && value.is_a?(Hash)
53
+ deep_merge(hash_one[key], value)
54
+ elsif hash_one[key].is_a?(Array) && value.is_a?(Array)
55
+ hash_one[key].concat(value).uniq
56
+ else
57
+ value
58
+ end
61
59
  end
62
60
  merged
63
61
  end
@@ -28,4 +28,4 @@ class TestCastBoolInt < MiniTest::Test
28
28
  def test_arbirtrary_string
29
29
  assert_equal 'foo', cast_bool_values_int('foo')
30
30
  end
31
- end
31
+ end
@@ -6,22 +6,22 @@ class TestDeepMerge < MiniTest::Test
6
6
  include Sensu::Plugin::Utils
7
7
 
8
8
  def test_hash
9
- merged = deep_merge({:a => "a"}, {:b => "b"})
10
- assert(merged == {:a => "a", :b => "b"})
9
+ merged = deep_merge({ a: 'a' }, b: 'b')
10
+ assert(merged == { a: 'a', b: 'b' })
11
11
  end
12
12
 
13
13
  def test_nested_hash
14
- merged = deep_merge({:a => {:b => "c"}}, {:a => {:d => "e"}})
15
- assert(merged == {:a => {:b => "c", :d => "e"}})
14
+ merged = deep_merge({ a: { b: 'c' } }, a: { d: 'e' })
15
+ assert(merged == { a: { b: 'c', d: 'e' } })
16
16
  end
17
17
 
18
18
  def test_nested_array
19
- merged = deep_merge({:a => ["b"]}, {:a => ["c"]})
20
- assert(merged, {:a => ["b", "c"]})
19
+ merged = deep_merge({ a: ['b'] }, a: ['c'])
20
+ assert(merged, a: %w[b c])
21
21
  end
22
22
 
23
23
  def test_conflicting_types
24
- merged = deep_merge({:a => {:b => "c"}}, {:a => ["d"]})
25
- assert(merged, {:a => {:b => "c"}})
24
+ merged = deep_merge({ a: { b: 'c' } }, a: ['d'])
25
+ assert(merged, a: { b: 'c' })
26
26
  end
27
27
  end
@@ -1,4 +1,5 @@
1
1
  require 'test_helper'
2
+ require 'English'
2
3
 
3
4
  class TestCheckExternal < MiniTest::Test
4
5
  include SensuPluginTestHelper
@@ -9,53 +10,53 @@ class TestCheckExternal < MiniTest::Test
9
10
 
10
11
  def test_ok
11
12
  run_script '-o'
12
- assert $?.exitstatus == 0
13
+ assert $CHILD_STATUS.success?
13
14
  end
14
15
 
15
16
  def test_warning
16
17
  run_script '-w'
17
- assert $?.exitstatus == 1
18
+ assert $CHILD_STATUS.exitstatus == 1
18
19
  end
19
20
 
20
21
  def test_critical
21
22
  run_script '-c'
22
- assert $?.exitstatus == 2
23
+ assert $CHILD_STATUS.exitstatus == 2
23
24
  end
24
25
 
25
26
  def test_unknown
26
27
  run_script '-u'
27
- assert $?.exitstatus == 3
28
+ assert $CHILD_STATUS.exitstatus == 3
28
29
  end
29
30
 
30
31
  def test_override
31
32
  output = run_script '-O'
32
- assert $?.exitstatus == 0 && !output.include?('argv =')
33
+ assert $CHILD_STATUS.success? && !output.include?('argv =')
33
34
  end
34
35
 
35
36
  def test_fallthrough
36
37
  run_script
37
- assert $?.exitstatus == 1
38
+ assert $CHILD_STATUS.exitstatus == 1
38
39
  end
39
40
 
40
41
  def test_exception
41
42
  output = run_script '-f'
42
- assert $?.exitstatus == 3 && output.include?('failed')
43
+ assert $CHILD_STATUS.exitstatus == 3 && output.include?('failed')
43
44
  end
44
45
 
45
46
  def test_argv
46
47
  output = run_script '-o', 'foo'
47
- assert $?.exitstatus == 0 && output.include?('argv = foo')
48
+ assert $CHILD_STATUS.success? && output.include?('argv = foo')
48
49
  end
49
50
 
50
51
  def test_bad_commandline
51
52
  output = run_script '--doesnotexist'
52
- assert $?.exitstatus == 3 && output.include?('doesnotexist') && output.include?('invalid option')
53
+ assert $CHILD_STATUS.exitstatus == 3 && output.include?('doesnotexist') && output.include?('invalid option')
53
54
  end
54
55
 
55
56
  def test_bad_require
56
- set_script 'external/bad-require' # TODO better way to switch scripts?
57
+ set_script 'external/bad-require' # TODO: better way to switch scripts?
57
58
  output = run_script '2>&1'
58
- assert_equal($?.exitstatus, 3)
59
+ assert_equal($CHILD_STATUS.exitstatus, 3)
59
60
  assert_match(/LoadError/, output)
60
61
  end
61
62
  end
@@ -1,4 +1,5 @@
1
1
  require 'rubygems'
2
+ require 'English'
2
3
  require 'minitest/autorun'
3
4
  require 'json'
4
5
 
@@ -8,18 +9,18 @@ class TestHandlerArgumentExternal < MiniTest::Test
8
9
  def test_default
9
10
  set_script 'external/handle-argument'
10
11
  output = run_script_with_input(JSON.generate({}))
11
- assert $?.exitstatus == 0 && output =~ /Value:\sfoo/
12
+ assert $CHILD_STATUS.success? && output =~ /Value:\sfoo/
12
13
  end
13
14
 
14
15
  def test_short
15
16
  set_script 'external/handle-argument -t bar'
16
17
  output = run_script_with_input(JSON.generate({}))
17
- assert $?.exitstatus == 0 && output =~ /Value:\sbar/
18
+ assert $CHILD_STATUS.success? && output =~ /Value:\sbar/
18
19
  end
19
20
 
20
21
  def test_long
21
22
  set_script 'external/handle-argument --test bar'
22
23
  output = run_script_with_input(JSON.generate({}))
23
- assert $?.exitstatus == 0 && output =~ /Value:\sbar/
24
+ assert $CHILD_STATUS.success? && output =~ /Value:\sbar/
24
25
  end
25
26
  end
@@ -1,4 +1,5 @@
1
1
  require 'test_helper'
2
+ require 'English'
2
3
  require 'json'
3
4
 
4
5
  class TestHandlerExternal < MiniTest::Test
@@ -12,16 +13,15 @@ class TestHandlerExternal < MiniTest::Test
12
13
  event = {
13
14
  'client' => { 'name' => 'test' },
14
15
  'check' => { 'name' => 'test' },
15
- 'occurrences' => 1,
16
+ 'occurrences' => 1
16
17
  }
17
18
  output = run_script_with_input(JSON.generate(event))
18
- assert $?.exitstatus == 0 && output =~ /Event:.*test/
19
+ assert $CHILD_STATUS.success? && output =~ /Event:.*test/
19
20
  end
20
21
 
21
22
  def test_missing_keys
22
23
  event = {}
23
24
  output = run_script_with_input(JSON.generate(event))
24
- assert $?.exitstatus == 0 && output =~ /Event:/
25
+ assert $CHILD_STATUS.success? && output =~ /Event:/
25
26
  end
26
-
27
27
  end
@@ -1,4 +1,5 @@
1
1
  require 'test_helper'
2
+ require 'English'
2
3
 
3
4
  class TestMetricExternal < MiniTest::Test
4
5
  include SensuPluginTestHelper
@@ -9,9 +10,8 @@ class TestMetricExternal < MiniTest::Test
9
10
 
10
11
  def test_ok
11
12
  output = JSON.parse(run_script)
12
- assert $?.exitstatus == 0 && output.key?('timestamp')
13
+ assert $CHILD_STATUS.success? && output.key?('timestamp')
13
14
  end
14
-
15
15
  end
16
16
 
17
17
  class TestGraphiteMetricExternal < MiniTest::Test
@@ -23,9 +23,8 @@ class TestGraphiteMetricExternal < MiniTest::Test
23
23
 
24
24
  def test_multi
25
25
  lines = run_script.split("\n")
26
- assert lines.size == 2 && lines.all? {|line| line.split("\s").size == 3 }
26
+ assert lines.size == 2 && lines.all? { |line| line.split("\s").size == 3 }
27
27
  end
28
-
29
28
  end
30
29
 
31
30
  class TestStatsdMetricExternal < MiniTest::Test
@@ -43,6 +42,4 @@ class TestStatsdMetricExternal < MiniTest::Test
43
42
  assert line.split('|').first.split(':').size == 2
44
43
  end
45
44
  end
46
-
47
45
  end
48
-
@@ -1,4 +1,5 @@
1
1
  require 'test_helper'
2
+ require 'English'
2
3
  require 'tempfile'
3
4
 
4
5
  class TestFilterExternal < MiniTest::Test
@@ -21,7 +22,7 @@ class TestFilterExternal < MiniTest::Test
21
22
  'action' => 'create'
22
23
  }
23
24
  output = run_script_with_input(JSON.generate(event))
24
- assert_equal(0, $?.exitstatus)
25
+ assert_equal(0, $CHILD_STATUS.exitstatus)
25
26
  assert_match(/^not enough occurrences/, output)
26
27
  end
27
28
 
@@ -38,7 +39,7 @@ class TestFilterExternal < MiniTest::Test
38
39
  'action' => 'create'
39
40
  }
40
41
  output = run_script_with_input(JSON.generate(event))
41
- assert_equal(0, $?.exitstatus)
42
+ assert_equal(0, $CHILD_STATUS.exitstatus)
42
43
  assert_match(/^Event:/, output)
43
44
  end
44
45
 
@@ -55,7 +56,7 @@ class TestFilterExternal < MiniTest::Test
55
56
  'action' => 'resolve'
56
57
  }
57
58
  output = run_script_with_input(JSON.generate(event))
58
- assert_equal(0, $?.exitstatus)
59
+ assert_equal(0, $CHILD_STATUS.exitstatus)
59
60
  assert_match(/^not enough occurrences/, output)
60
61
  end
61
62
 
@@ -71,7 +72,7 @@ class TestFilterExternal < MiniTest::Test
71
72
  'action' => 'resolve'
72
73
  }
73
74
  output = run_script_with_input(JSON.generate(event))
74
- assert_equal(0, $?.exitstatus)
75
+ assert_equal(0, $CHILD_STATUS.exitstatus)
75
76
  assert_match(/^Event:/, output)
76
77
  end
77
78
 
@@ -87,7 +88,7 @@ class TestFilterExternal < MiniTest::Test
87
88
  'action' => 'create'
88
89
  }
89
90
  output = run_script_with_input(JSON.generate(event))
90
- assert_equal(0, $?.exitstatus)
91
+ assert_equal(0, $CHILD_STATUS.exitstatus)
91
92
  assert_match(/^Event:/, output)
92
93
  end
93
94
 
@@ -103,7 +104,7 @@ class TestFilterExternal < MiniTest::Test
103
104
  'action' => 'create'
104
105
  }
105
106
  output = run_script_with_input(JSON.generate(event))
106
- assert_equal(0, $?.exitstatus)
107
+ assert_equal(0, $CHILD_STATUS.exitstatus)
107
108
  assert_match(/^only handling every/, output)
108
109
  end
109
110
 
@@ -115,7 +116,7 @@ class TestFilterExternal < MiniTest::Test
115
116
  'action' => 'create'
116
117
  }
117
118
  output = run_script_with_input(JSON.generate(event))
118
- assert_equal(0, $?.exitstatus)
119
+ assert_equal(0, $CHILD_STATUS.exitstatus)
119
120
  assert_match(/^Event:/, output)
120
121
  end
121
122
 
@@ -127,7 +128,7 @@ class TestFilterExternal < MiniTest::Test
127
128
  'action' => 'create'
128
129
  }
129
130
  output = run_script_with_input(JSON.generate(event))
130
- assert_equal(0, $?.exitstatus)
131
+ assert_equal(0, $CHILD_STATUS.exitstatus)
131
132
  assert_match(/^Event:/, output)
132
133
  end
133
134
 
@@ -142,7 +143,7 @@ class TestFilterExternal < MiniTest::Test
142
143
  'occurrences' => 1
143
144
  }
144
145
  output = run_script_with_input(JSON.generate(event))
145
- assert_equal(0, $?.exitstatus)
146
+ assert_equal(0, $CHILD_STATUS.exitstatus)
146
147
  assert_match(/dependency event exists/, output)
147
148
  end
148
149
 
@@ -158,7 +159,7 @@ class TestFilterExternal < MiniTest::Test
158
159
  'action' => 'create'
159
160
  }
160
161
  output = run_script_with_input(JSON.generate(event))
161
- assert_equal(0, $?.exitstatus)
162
+ assert_equal(0, $CHILD_STATUS.exitstatus)
162
163
  refute_match(/#{filter_deprecation_string}/, output)
163
164
  end
164
165
 
@@ -170,7 +171,7 @@ class TestFilterExternal < MiniTest::Test
170
171
  'action' => 'create'
171
172
  }
172
173
  output = run_script_with_input(JSON.generate(event))
173
- assert_equal(0, $?.exitstatus)
174
+ assert_equal(0, $CHILD_STATUS.exitstatus)
174
175
  assert_match(/#{filter_deprecation_string}/, output)
175
176
  end
176
177
 
@@ -183,10 +184,10 @@ class TestFilterExternal < MiniTest::Test
183
184
  'enable_deprecated_filtering' => false
184
185
  },
185
186
  'occurrences' => 60,
186
- 'action' => 'create',
187
+ 'action' => 'create'
187
188
  }
188
189
  output = run_script_with_input(JSON.generate(event))
189
- assert_equal(0, $?.exitstatus)
190
+ assert_equal(0, $CHILD_STATUS.exitstatus)
190
191
  refute_match(/#{filter_deprecation_string}/, output)
191
192
  end
192
193
 
@@ -198,7 +199,7 @@ class TestFilterExternal < MiniTest::Test
198
199
  'refresh' => 30
199
200
  },
200
201
  'occurrences' => 60,
201
- 'action' => 'create',
202
+ 'action' => 'create'
202
203
  }
203
204
 
204
205
  settings_file = Tempfile.new('global_filter_disable')
@@ -207,7 +208,7 @@ class TestFilterExternal < MiniTest::Test
207
208
  ENV['SENSU_CONFIG_FILES'] = settings_file.path
208
209
  output = run_script_in_env_with_input(JSON.generate(event), ENV)
209
210
  ENV['SENSU_CONFIG_FILES'] = nil
210
- assert_equal(0, $?.exitstatus)
211
+ assert_equal(0, $CHILD_STATUS.exitstatus)
211
212
  refute_match(/#{filter_deprecation_string}/, output)
212
213
  end
213
214
 
@@ -223,7 +224,7 @@ class TestFilterExternal < MiniTest::Test
223
224
  'action' => 'create'
224
225
  }
225
226
  output = run_script_with_input(JSON.generate(event))
226
- assert_equal(0, $?.exitstatus)
227
+ assert_equal(0, $CHILD_STATUS.exitstatus)
227
228
  refute_match(/#{occurrence_filter_deprecation_string}/, output)
228
229
  end
229
230
 
@@ -240,7 +241,7 @@ class TestFilterExternal < MiniTest::Test
240
241
  'action' => 'create'
241
242
  }
242
243
  output = run_script_with_input(JSON.generate(event))
243
- assert_equal(0, $?.exitstatus)
244
+ assert_equal(0, $CHILD_STATUS.exitstatus)
244
245
  assert_match(/#{occurrence_filter_deprecation_string}/, output)
245
246
  end
246
247
 
@@ -254,10 +255,10 @@ class TestFilterExternal < MiniTest::Test
254
255
  'enable_deprecated_occurrence_filtering' => false
255
256
  },
256
257
  'occurrences' => 60,
257
- 'action' => 'create',
258
+ 'action' => 'create'
258
259
  }
259
260
  output = run_script_with_input(JSON.generate(event))
260
- assert_equal(0, $?.exitstatus)
261
+ assert_equal(0, $CHILD_STATUS.exitstatus)
261
262
  refute_match(/#{occurrence_filter_deprecation_string}/, output)
262
263
  end
263
264
  end
@@ -1,4 +1,5 @@
1
1
  require 'test_helper'
2
+ require 'English'
2
3
 
3
4
  class TestHandleHelpers < MiniTest::Test
4
5
  include SensuPluginTestHelper
@@ -21,19 +22,19 @@ class TestHandleHelpers < MiniTest::Test
21
22
  'action' => 'create'
22
23
  }
23
24
  output = run_script_with_input(JSON.generate(event))
24
- assert_equal(0, $?.exitstatus)
25
+ assert_equal(0, $CHILD_STATUS.exitstatus)
25
26
  assert_match("test/test : test\n", output)
26
27
  event['check']['source'] = 'switch-x'
27
28
  output = run_script_with_input(JSON.generate(event))
28
- assert_equal(0, $?.exitstatus)
29
+ assert_equal(0, $CHILD_STATUS.exitstatus)
29
30
  assert_match("switch-x/test : test\n", output)
30
31
  event['check']['description'] = 'y is broken'
31
32
  output = run_script_with_input(JSON.generate(event))
32
- assert_equal(0, $?.exitstatus)
33
+ assert_equal(0, $CHILD_STATUS.exitstatus)
33
34
  assert_match("y is broken\n", output)
34
35
  event['check']['notification'] = 'z is broken'
35
36
  output = run_script_with_input(JSON.generate(event))
36
- assert_equal(0, $?.exitstatus)
37
+ assert_equal(0, $CHILD_STATUS.exitstatus)
37
38
  assert_match("z is broken\n", output)
38
39
  end
39
40
  end
data/test/mutator_test.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ require 'English'
2
3
 
3
4
  require 'test_helper'
4
5
 
@@ -9,7 +10,7 @@ class TestMutatorHelpers < MiniTest::Test
9
10
  set_script 'external/mutator-trivial'
10
11
  event = JSON.parse(fixture('basic_event.json').read)
11
12
  output = run_script_with_input(JSON.generate(event))
12
- assert_equal(0, $?.exitstatus)
13
+ assert_equal(0, $CHILD_STATUS.exitstatus)
13
14
  assert_equal(event, JSON.parse(output))
14
15
  end
15
16
 
@@ -17,7 +18,7 @@ class TestMutatorHelpers < MiniTest::Test
17
18
  set_script 'external/mutator-helpers'
18
19
  event = JSON.parse(fixture('basic_event.json').read)
19
20
  output = run_script_with_input(JSON.generate(event))
20
- assert_equal(0, $?.exitstatus)
21
+ assert_equal(0, $CHILD_STATUS.exitstatus)
21
22
  assert_equal(true, JSON.parse(output)['mutated'])
22
23
  end
23
24
  end
data/test/test_helper.rb CHANGED
@@ -7,7 +7,7 @@ module SensuPluginTestHelper
7
7
  end
8
8
 
9
9
  def run_script(*args)
10
- IO.popen(([@script] + args).join(' '), 'r+') do |child|
10
+ IO.popen(([@script] + args).join(' '), 'r+') do |child| # rubocop:disable Style/SymbolProc
11
11
  child.read
12
12
  end
13
13
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sensu-plugin
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Decklin Foster
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-07-07 00:00:00.000000000 Z
12
+ date: 2017-08-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
@@ -53,6 +53,20 @@ dependencies:
53
53
  - - ">="
54
54
  - !ruby/object:Gem::Version
55
55
  version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rubocop
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: 0.49.0
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: 0.49.0
56
70
  - !ruby/object:Gem::Dependency
57
71
  name: minitest
58
72
  requirement: !ruby/object:Gem::Requirement