From 2c5f8650a98b77cd4ff65412451e10a6eeeaa60f Mon Sep 17 00:00:00 2001 From: Romain Thouvenin Date: Wed, 6 Sep 2017 08:03:26 +0200 Subject: [PATCH] Move duplicated code to libraries (#71) * Add plugin stub for tests * Add tests for queue metrics * Add tests for exchange metrics * Add tests for queue check * Add base class for metrics (#65) * Add common check class * Move common options into a module * Common rabbitmq_info getter * Fix Rubocop offenses * Split module and classes in separate files * Update Changelog * Remove require_relative * Use ruby_dig for older rubies --- CHANGELOG.md | 9 ++- bin/check-rabbitmq-queue.rb | 70 +------------------ bin/metrics-rabbitmq-exchange.rb | 92 +----------------------- bin/metrics-rabbitmq-queue.rb | 92 +----------------------- lib/sensu-plugins-rabbitmq.rb | 4 ++ lib/sensu-plugins-rabbitmq/check.rb | 17 +++++ lib/sensu-plugins-rabbitmq/metrics.rb | 34 +++++++++ lib/sensu-plugins-rabbitmq/rabbitmq.rb | 91 ++++++++++++++++++++++++ sensu-plugins-rabbitmq.gemspec | 3 +- test/check-rabbitmq-queue_spec.rb | 96 ++++++++++++++++++++++++++ test/metrics-rabbitmq-exchange_spec.rb | 90 ++++++++++++++++++++++++ test/metrics-rabbitmq-queue_spec.rb | 94 +++++++++++++++++++++++++ test/spec_helper.rb | 27 ++++++++ 13 files changed, 469 insertions(+), 250 deletions(-) create mode 100644 lib/sensu-plugins-rabbitmq/check.rb create mode 100644 lib/sensu-plugins-rabbitmq/metrics.rb create mode 100644 lib/sensu-plugins-rabbitmq/rabbitmq.rb create mode 100644 test/check-rabbitmq-queue_spec.rb create mode 100644 test/metrics-rabbitmq-exchange_spec.rb create mode 100644 test/metrics-rabbitmq-queue_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 762c844..1429b2b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,10 +14,13 @@ This CHANGELOG follows the format listed at [Keep A Changelog](http://keepachang - check-rabbitmq-consumers.rb: Added ability to select queues with regular expressions. (@jtduby) ### Added -- ruby 2.4 support (@majormoses) - + - ruby 2.4 support (@majormoses) + - RabbitMQ module with common code (@rthouvenin) + - Check and Metrics base classes (@rthouvenin) +### Changed + - metrics-rabbitmq-queue, metrics-rabbitmq-exchange, check-rabbitmq-queue: use the new base classes (@rthouvenin) ### Fixed -- PR template spelling (@majormoses) + - PR template spelling (@majormoses) ## [3.2.0] - 2017-06-20 ### Added diff --git a/bin/check-rabbitmq-queue.rb b/bin/check-rabbitmq-queue.rb index bd9f636..ff31d72 100755 --- a/bin/check-rabbitmq-queue.rb +++ b/bin/check-rabbitmq-queue.rb @@ -21,46 +21,10 @@ # Released under the same terms as Sensu (the MIT license); see LICENSE # for details. -require 'sensu-plugin/check/cli' -require 'socket' -require 'carrot-top' -require 'inifile' +require 'sensu-plugins-rabbitmq' # main plugin class -class CheckRabbitMQMessages < Sensu::Plugin::Check::CLI - option :host, - description: 'RabbitMQ management API host', - long: '--host HOST', - default: 'localhost' - - option :port, - description: 'RabbitMQ management API port', - long: '--port PORT', - proc: proc(&:to_i), - default: 15_672 - - option :vhost, - description: 'RabbitMQ vhost', - short: '-v', - long: '--vhost VHOST', - default: '' - - option :ssl, - description: 'Enable SSL for connection to the API', - long: '--ssl', - boolean: true, - default: false - - option :username, - description: 'RabbitMQ management API user', - long: '--username USER', - default: 'guest' - - option :password, - description: 'RabbitMQ management API password', - long: '--password PASSWORD', - default: 'guest' - +class CheckRabbitMQMessages < Sensu::Plugin::RabbitMQ::Check option :queue, description: 'RabbitMQ queue to monitor', long: '--queue queue_names', @@ -103,36 +67,6 @@ class CheckRabbitMQMessages < Sensu::Plugin::Check::CLI boolean: true, default: false - option :ini, - description: 'Configuration ini file', - short: '-i', - long: '--ini VALUE' - - def acquire_rabbitmq_info - begin - if config[:ini] - ini = IniFile.load(config[:ini]) - section = ini['auth'] - username = section['username'] - password = section['password'] - else - username = config[:username] - password = config[:password] - end - - rabbitmq_info = CarrotTop.new( - host: config[:host], - port: config[:port], - user: username, - password: password, - ssl: config[:ssl] - ) - rescue - warning 'could not get rabbitmq info' - end - rabbitmq_info - end - def run @crit = [] @warn = [] diff --git a/bin/metrics-rabbitmq-exchange.rb b/bin/metrics-rabbitmq-exchange.rb index e80111c..42ce658 100755 --- a/bin/metrics-rabbitmq-exchange.rb +++ b/bin/metrics-rabbitmq-exchange.rb @@ -24,44 +24,10 @@ # Released under the same terms as Sensu (the MIT license); see LICENSE # for details. -require 'sensu-plugin/metric/cli' -require 'socket' -require 'carrot-top' -require 'inifile' +require 'sensu-plugins-rabbitmq' # main plugin class -class RabbitMQExchangeMetrics < Sensu::Plugin::Metric::CLI::Graphite - option :host, - description: 'RabbitMQ management API host', - long: '--host HOST', - default: 'localhost' - - option :port, - description: 'RabbitMQ management API port', - long: '--port PORT', - proc: proc(&:to_i), - default: 15_672 - - option :vhost, - description: 'Regular expression for filtering the RabbitMQ vhost', - short: '-v', - long: '--vhost VHOST' - - option :username, - description: 'RabbitMQ management API user', - long: '--username USER', - default: 'guest' - - option :password, - description: 'RabbitMQ management API password', - long: '--password PASSWORD', - default: 'guest' - - option :scheme, - description: 'Metric naming scheme, text to prepend to $exchange_name.$metric', - long: '--scheme SCHEME', - default: "#{Socket.gethostname}.rabbitmq" - +class RabbitMQExchangeMetrics < Sensu::Plugin::RabbitMQ::Metrics option :filter, description: 'Regular expression for filtering exchanges', long: '--filter REGEX' @@ -70,61 +36,9 @@ class RabbitMQExchangeMetrics < Sensu::Plugin::Metric::CLI::Graphite description: 'Regular expression for filtering metrics in each exchange', long: '--metrics REGEX' - option :ssl, - description: 'Enable SSL for connection to the API', - long: '--ssl', - boolean: true, - default: false - - option :ini, - description: 'Configuration ini file', - short: '-i', - long: '--ini VALUE' - - def acquire_rabbitmq_exchanges - begin - if config[:ini] - ini = IniFile.load(config[:ini]) - section = ini['auth'] - username = section['username'] - password = section['password'] - else - username = config[:username] - password = config[:password] - end - - rabbitmq_info = CarrotTop.new( - host: config[:host], - port: config[:port], - user: username, - password: password, - ssl: config[:ssl] - ) - rescue - warning 'could not get rabbitmq exchange info' - end - - if config[:vhost] - return rabbitmq_info.exchanges.select { |x| x['vhost'].match(config[:vhost]) } - end - - rabbitmq_info.exchanges - end - - def dotted_keys(hash, prefix = '', keys = []) - hash.each do |k, v| - if v.is_a? Hash - keys = dotted_keys(v, prefix + k + '.', keys) - else - keys << prefix + k - end - end - keys - end - def run timestamp = Time.now.to_i - acquire_rabbitmq_exchanges.each do |exchange| + acquire_rabbitmq_info(:exchanges).each do |exchange| if config[:filter] next unless exchange['name'].match(config[:filter]) end diff --git a/bin/metrics-rabbitmq-queue.rb b/bin/metrics-rabbitmq-queue.rb index 76f97d5..f633f97 100755 --- a/bin/metrics-rabbitmq-queue.rb +++ b/bin/metrics-rabbitmq-queue.rb @@ -28,44 +28,10 @@ # Released under the same terms as Sensu (the MIT license); see LICENSE # for details. -require 'sensu-plugin/metric/cli' -require 'socket' -require 'carrot-top' -require 'inifile' +require 'sensu-plugins-rabbitmq' # main plugin class -class RabbitMQMetrics < Sensu::Plugin::Metric::CLI::Graphite - option :host, - description: 'RabbitMQ management API host', - long: '--host HOST', - default: 'localhost' - - option :port, - description: 'RabbitMQ management API port', - long: '--port PORT', - proc: proc(&:to_i), - default: 15_672 - - option :vhost, - description: 'Regular expression for filtering the RabbitMQ vhost', - short: '-v', - long: '--vhost VHOST' - - option :username, - description: 'RabbitMQ management API user', - long: '--username USER', - default: 'guest' - - option :password, - description: 'RabbitMQ management API password', - long: '--password PASSWORD', - default: 'guest' - - option :scheme, - description: 'Metric naming scheme, text to prepend to $queue_name.$metric', - long: '--scheme SCHEME', - default: "#{Socket.gethostname}.rabbitmq" - +class RabbitMQQueueMetrics < Sensu::Plugin::RabbitMQ::Metrics option :filter, description: 'Regular expression for filtering queues', long: '--filter REGEX' @@ -75,61 +41,9 @@ class RabbitMQMetrics < Sensu::Plugin::Metric::CLI::Graphite long: '--metrics REGEX', default: '^messages$|consumers|drain_time|avg_egress' - option :ssl, - description: 'Enable SSL for connection to the API', - long: '--ssl', - boolean: true, - default: false - - option :ini, - description: 'Configuration ini file', - short: '-i', - long: '--ini VALUE' - - def acquire_rabbitmq_queues - begin - if config[:ini] - ini = IniFile.load(config[:ini]) - section = ini['auth'] - username = section['username'] - password = section['password'] - else - username = config[:username] - password = config[:password] - end - - rabbitmq_info = CarrotTop.new( - host: config[:host], - port: config[:port], - user: username, - password: password, - ssl: config[:ssl] - ) - rescue - warning 'could not get rabbitmq queue info' - end - - if config[:vhost] - return rabbitmq_info.queues.select { |x| x['vhost'].match(config[:vhost]) } - end - - rabbitmq_info.queues - end - - def dotted_keys(hash, prefix = '', keys = []) - hash.each do |k, v| - if v.is_a? Hash - keys = dotted_keys(v, prefix + k + '.', keys) - else - keys << prefix + k - end - end - keys - end - def run timestamp = Time.now.to_i - acquire_rabbitmq_queues.each do |queue| + acquire_rabbitmq_info(:queues).each do |queue| if config[:filter] next unless queue['name'].match(config[:filter]) end diff --git a/lib/sensu-plugins-rabbitmq.rb b/lib/sensu-plugins-rabbitmq.rb index c927d80..61767d2 100644 --- a/lib/sensu-plugins-rabbitmq.rb +++ b/lib/sensu-plugins-rabbitmq.rb @@ -1 +1,5 @@ +require 'ruby_dig' + require 'sensu-plugins-rabbitmq/version' +require 'sensu-plugins-rabbitmq/check' +require 'sensu-plugins-rabbitmq/metrics' diff --git a/lib/sensu-plugins-rabbitmq/check.rb b/lib/sensu-plugins-rabbitmq/check.rb new file mode 100644 index 0000000..19b8d7e --- /dev/null +++ b/lib/sensu-plugins-rabbitmq/check.rb @@ -0,0 +1,17 @@ +require 'sensu-plugin/check/cli' +require 'sensu-plugins-rabbitmq/rabbitmq' + +module Sensu + module Plugin + module RabbitMQ + class Check < Sensu::Plugin::Check::CLI + include Sensu::Plugin::RabbitMQ::Common + + # To avoid complaints from mother class at the end of tests (at_exit handler) + def run + ok + end + end + end + end +end diff --git a/lib/sensu-plugins-rabbitmq/metrics.rb b/lib/sensu-plugins-rabbitmq/metrics.rb new file mode 100644 index 0000000..8d300e0 --- /dev/null +++ b/lib/sensu-plugins-rabbitmq/metrics.rb @@ -0,0 +1,34 @@ +require 'sensu-plugin/metric/cli' +require 'socket' +require 'sensu-plugins-rabbitmq/rabbitmq' + +module Sensu + module Plugin + module RabbitMQ + class Metrics < Sensu::Plugin::Metric::CLI::Graphite + include Sensu::Plugin::RabbitMQ::Common + + option :scheme, + description: 'Metric naming scheme', + long: '--scheme SCHEME', + default: "#{Socket.gethostname}.rabbitmq" + + def dotted_keys(hash, prefix = '', keys = []) + hash.each do |k, v| + if v.is_a? Hash + keys = dotted_keys(v, prefix + k + '.', keys) + else + keys << prefix + k + end + end + keys + end + + # To avoid complaints from mother class at the end of tests (at_exit handler) + def run + ok + end + end + end + end +end diff --git a/lib/sensu-plugins-rabbitmq/rabbitmq.rb b/lib/sensu-plugins-rabbitmq/rabbitmq.rb new file mode 100644 index 0000000..7f94aa5 --- /dev/null +++ b/lib/sensu-plugins-rabbitmq/rabbitmq.rb @@ -0,0 +1,91 @@ +require 'carrot-top' +require 'inifile' + +module Sensu + module Plugin + module RabbitMQ + module Options + def add_common_options + option :host, + description: 'RabbitMQ management API host', + long: '--host HOST', + default: 'localhost' + + option :port, + description: 'RabbitMQ management API port', + long: '--port PORT', + proc: proc(&:to_i), + default: 15_672 + + option :vhost, + description: 'Regular expression for filtering the RabbitMQ vhost', + short: '-v', + long: '--vhost VHOST', + default: '' + + option :username, + description: 'RabbitMQ management API user', + long: '--username USER', + default: 'guest' + + option :password, + description: 'RabbitMQ management API password', + long: '--password PASSWORD', + default: 'guest' + + option :ssl, + description: 'Enable SSL for connection to the API', + long: '--ssl', + boolean: true, + default: false + + option :ini, + description: 'Configuration ini file', + short: '-i', + long: '--ini VALUE' + end + end + + module Common + def acquire_rabbitmq_info(property = nil) + begin + if config[:ini] + ini = IniFile.load(config[:ini]) + section = ini['auth'] + username = section['username'] + password = section['password'] + else + username = config[:username] + password = config[:password] + end + + rabbitmq_info = CarrotTop.new( + host: config[:host], + port: config[:port], + user: username, + password: password, + ssl: config[:ssl] + ) + rescue + warning 'could not get rabbitmq info' + end + + result_info = rabbitmq_info + unless property.nil? + result_info = rabbitmq_info.send property + if config[:vhost] != '' + result_info.select! { |x| x['vhost'].match(config[:vhost]) } + end + end + + result_info + end + + def self.included(receiver) + receiver.extend(Sensu::Plugin::RabbitMQ::Options) + receiver.add_common_options + end + end + end + end +end diff --git a/sensu-plugins-rabbitmq.gemspec b/sensu-plugins-rabbitmq.gemspec index cc4c705..3226e25 100644 --- a/sensu-plugins-rabbitmq.gemspec +++ b/sensu-plugins-rabbitmq.gemspec @@ -2,7 +2,7 @@ lib = File.expand_path('../lib', __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'date' -require_relative 'lib/sensu-plugins-rabbitmq' +require 'sensu-plugins-rabbitmq/version' Gem::Specification.new do |s| s.authors = ['Sensu-Plugins and contributors'] @@ -39,6 +39,7 @@ Gem::Specification.new do |s| s.add_runtime_dependency 'bunny', '2.5.0' s.add_runtime_dependency 'amq-protocol', '2.0.1' s.add_runtime_dependency 'inifile', '3.0.0' + s.add_runtime_dependency 'ruby_dig', '0.0.2' s.add_development_dependency 'bundler', '~> 1.7' s.add_development_dependency 'codeclimate-test-reporter', '~> 0.4' diff --git a/test/check-rabbitmq-queue_spec.rb b/test/check-rabbitmq-queue_spec.rb new file mode 100644 index 0000000..6a521d2 --- /dev/null +++ b/test/check-rabbitmq-queue_spec.rb @@ -0,0 +1,96 @@ +#!/usr/bin/env ruby +# +# check-rabbitmq-queue_spec +# +# DESCRIPTION: +# Tests for check-rabbitmq-queue.rb +# +# OUTPUT: +# +# PLATFORMS: +# +# DEPENDENCIES: +# +# USAGE: +# bundle install +# rake spec +# +# NOTES: +# +# LICENSE: +# Copyright 2017 Romain Thouvenin +# Released under the same terms as Sensu (the MIT license); see LICENSE for details. + +require_relative './spec_helper.rb' +require_relative '../bin/check-rabbitmq-queue.rb' + +def cq1 + { + 'name' => 'q1', + 'messages' => 700 + } +end + +def cq2 + { + 'name' => 'q2', + 'messages' => 300 + } +end + +class RabbitInfoStub +end + +describe CheckRabbitMQMessages, 'run' do + let(:check) do + CheckRabbitMQMessages.new ['--queue', 'q1,q2'] + end + + it 'should return a warning when the queue does not exist' do + info_stub = RabbitInfoStub.new + allow(info_stub).to receive(:method_missing).and_return [] + allow(check).to receive(:acquire_rabbitmq_info).and_return info_stub + + expect(check).not_to receive(:ok) + expect(check).to receive(:warning) + + check.run + end + + it 'should return a warning when the number of messages exceeds the default threshold of 250' do + check.config[:ignore] = true + info_stub = RabbitInfoStub.new + allow(info_stub).to receive(:method_missing).and_return [cq2] + allow(check).to receive(:acquire_rabbitmq_info).and_return info_stub + + expect(check).not_to receive(:ok) + expect(check).to receive(:warning) + + check.run + end + + it 'should return a critical when the number of messages exceeds the default threshold of 500' do + check.config[:ignore] = true + info_stub = RabbitInfoStub.new + allow(info_stub).to receive(:method_missing).and_return [cq1] + allow(check).to receive(:acquire_rabbitmq_info).and_return info_stub + + expect(check).not_to receive(:ok) + expect(check).to receive(:critical) + + check.run + end + + it 'should return a critical when below is true and the number of messages is below the threshold' do + check.config[:ignore] = true + check.config[:below] = true + info_stub = RabbitInfoStub.new + allow(info_stub).to receive(:method_missing).and_return [cq2] + allow(check).to receive(:acquire_rabbitmq_info).and_return info_stub + + expect(check).not_to receive(:ok) + expect(check).to receive(:critical) + + check.run + end +end diff --git a/test/metrics-rabbitmq-exchange_spec.rb b/test/metrics-rabbitmq-exchange_spec.rb new file mode 100644 index 0000000..bfa5c38 --- /dev/null +++ b/test/metrics-rabbitmq-exchange_spec.rb @@ -0,0 +1,90 @@ +#!/usr/bin/env ruby +# +# metrics-rabbitmq-exchange_spec +# +# DESCRIPTION: +# Tests for metrics-rabbitmq-exchange.rb +# +# OUTPUT: +# +# PLATFORMS: +# +# DEPENDENCIES: +# +# USAGE: +# bundle install +# rake spec +# +# NOTES: +# +# LICENSE: +# Copyright 2017 Romain Thouvenin +# Released under the same terms as Sensu (the MIT license); see LICENSE for details. + +require_relative './spec_helper.rb' +require_relative '../bin/metrics-rabbitmq-exchange.rb' + +def e1 + { + 'name' => 'e1', + 'type' => 'topic', + 'durable' => 'true', + 'message_stats' => { 'publish' => 100, 'ack' => 50 } + } +end + +def e2 + { + 'name' => 'e2', + 'type' => 'fanout', + 'durable' => 'false' + } +end + +describe RabbitMQExchangeMetrics, 'run' do + let(:check) do + RabbitMQExchangeMetrics.new + end + + it 'should output nothing and return ok when there are no exchanges' do + allow(check).to receive(:acquire_rabbitmq_info).and_return [] + expect(check).not_to receive(:output) + expect(check).to receive(:ok) + check.run + end + + it 'should by default output all exchanges and all metrics' do + allow(check).to receive(:acquire_rabbitmq_info).and_return [e1, e2] + + expect(check).to receive(:output).with(/.+.rabbitmq.e1.name$/, 'e1', timestamp) + expect(check).to receive(:output).with(/.+.rabbitmq.e1.type$/, 'topic', timestamp) + expect(check).to receive(:output).with(/.+.rabbitmq.e1.durable$/, 'true', timestamp) + expect(check).to receive(:output).with(/.+.rabbitmq.e1.message_stats.publish$/, 100, timestamp) + expect(check).to receive(:output).with(/.+.rabbitmq.e1.message_stats.ack$/, 50, timestamp) + + expect(check).to receive(:output).with(/.+.rabbitmq.e2.name$/, 'e2', timestamp) + expect(check).to receive(:output).with(/.+.rabbitmq.e2.type$/, 'fanout', timestamp) + expect(check).to receive(:output).with(/.+.rabbitmq.e2.durable$/, 'false', timestamp) + + expect(check).to receive(:ok) + check.run + end + + it 'should output only the exchanges specified by the filter option' do + check.config[:filter] = '.2' + allow(check).to receive(:acquire_rabbitmq_info).and_return [e1, e2] + expect(check).not_to receive(:output).with(/e1/, any_args) + expect(check).to receive(:output).with(/e2/, any_args).exactly(e2.size).times + expect(check).to receive(:ok) + check.run + end + + it 'should output only the metrics specified by the metrics option' do + check.config[:metrics] = 'message_stats' + allow(check).to receive(:acquire_rabbitmq_info).and_return [e1] + expect(check).to receive(:output).with(/e1.message_stats.publish$/, 100, timestamp) + expect(check).to receive(:output).with(/e1.message_stats.ack$/, 50, timestamp) + expect(check).to receive(:ok) + check.run + end +end diff --git a/test/metrics-rabbitmq-queue_spec.rb b/test/metrics-rabbitmq-queue_spec.rb new file mode 100644 index 0000000..47cc1bf --- /dev/null +++ b/test/metrics-rabbitmq-queue_spec.rb @@ -0,0 +1,94 @@ +#!/usr/bin/env ruby +# +# metrics-rabbitmq-queue_spec +# +# DESCRIPTION: +# Tests for metrics-rabbitmq-queue.rb +# +# OUTPUT: +# +# PLATFORMS: +# +# DEPENDENCIES: +# +# USAGE: +# bundle install +# rake spec +# +# NOTES: +# +# LICENSE: +# Copyright 2017 Romain Thouvenin +# Released under the same terms as Sensu (the MIT license); see LICENSE for details. + +require_relative './spec_helper.rb' +require_relative '../bin/metrics-rabbitmq-queue.rb' + +def q1 + { + 'name' => 'q1', + 'messages' => 42, + 'consumers' => 1, + 'backing_queue_status' => { 'avg_egress_rate' => 4.2 } + } +end + +def q2 + { + 'name' => 'q2', + 'memory' => 666, + 'state' => 'running', + 'messages' => 0, + 'consumers' => 0, + 'backing_queue_status' => { 'avg_egress_rate' => 0.123456e-123 }, + 'message_details' => { 'messages_ready' => 0 } + } +end + +describe RabbitMQQueueMetrics, 'run' do + let(:check) do + RabbitMQQueueMetrics.new + end + + it 'should output nothing and return ok when there are no queues' do + allow(check).to receive(:acquire_rabbitmq_info).and_return [] + expect(check).not_to receive(:output) + expect(check).to receive(:ok) + check.run + end + + it 'should by default output all queues and a spefific set of metrics' do + allow(check).to receive(:acquire_rabbitmq_info).and_return [q1, q2] + + expect(check).to receive(:output).with(/.+.rabbitmq.q1.messages$/, 42, timestamp) + expect(check).to receive(:output).with(/.+.rabbitmq.q1.consumers$/, 1, timestamp) + expect(check).to receive(:output).with(/.+.rabbitmq.q1.avg_egress_rate$/, '4.2000', timestamp) + expect(check).to receive(:output).with(/.+.rabbitmq.q1.drain_time$/, 10, timestamp) + + expect(check).to receive(:output).with(/.+.rabbitmq.q2.messages$/, 0, timestamp) + expect(check).to receive(:output).with(/.+.rabbitmq.q2.consumers$/, 0, timestamp) + expect(check).to receive(:output).with(/.+.rabbitmq.q2.avg_egress_rate$/, '0.0000', timestamp) + expect(check).to receive(:output).with(/.+.rabbitmq.q2.drain_time$/, 0, timestamp) + + expect(check).to receive(:ok) + check.run + end + + it 'should output only the queues specified by the filter option' do + check.config[:filter] = '.2' + allow(check).to receive(:acquire_rabbitmq_info).and_return [q1, q2] + expect(check).not_to receive(:output).with(/q1/, any_args) + expect(check).to receive(:output).with(/q2/, any_args).exactly(4).times + expect(check).to receive(:ok) + check.run + end + + it 'should output only the metrics specified by the metrics option' do + check.config[:metrics] = 'message_details|consumers' + allow(check).to receive(:acquire_rabbitmq_info).and_return [q2] + expect(check).to receive(:output).with(/q2.consumers$/, 0, timestamp) + expect(check).to receive(:output).with(/q2.message_details.messages_ready$/, 0, timestamp) + expect(check).to receive(:ok) + check.run + end +end diff --git a/test/spec_helper.rb b/test/spec_helper.rb index 9797982..c975740 100644 --- a/test/spec_helper.rb +++ b/test/spec_helper.rb @@ -1,2 +1,29 @@ require 'codeclimate-test-reporter' + +RSpec.configure do |c| + # Sensu plugins run in the context of an at_exit handler. This prevents + # code-under-test from being run at the end of the rspec suite. + c.before(:each) do + Sensu::Plugin::CLI.class_eval do + # PluginStub + class PluginStub + def run; end + + def ok(*); end + + def warning(*); end + + def critical(*); end + + def unknown(*); end + end + class_variable_set(:@@autorun, PluginStub) + end + end +end + CodeClimate::TestReporter.start + +def timestamp + kind_of Numeric +end