From 556a1decbac0da8c3045c1db4173a3cba68995c4 Mon Sep 17 00:00:00 2001 From: Rainmaker Date: Sat, 29 Jul 2017 23:43:18 -0400 Subject: [PATCH] add unit tests for GenericMonitor#create_build --- lib/ragios/monitors/generic_monitor.rb | 64 +++++------ lib/ragios/notifications/notify_worker.rb | 3 +- lib/ragios/ragios_exception.rb | 5 +- spec/lib/monitors/generic_monitor_spec.rb | 130 ++++++++++++++++++++-- 4 files changed, 152 insertions(+), 50 deletions(-) diff --git a/lib/ragios/monitors/generic_monitor.rb b/lib/ragios/monitors/generic_monitor.rb index 2508cf11..7cf98c85 100644 --- a/lib/ragios/monitors/generic_monitor.rb +++ b/lib/ragios/monitors/generic_monitor.rb @@ -38,7 +38,7 @@ def find(monitor_id) def build_plugin(plugin_name) module_name = "Plugin" - plugin_class = Module.const_get("Ragios").const_get(module_name).const_get(plugin_name.camelize) + plugin_class = Module.const_get("Ragios").const_get(module_name).const_get(plugin_name.to_s.camelize) plugin_class.new rescue => e raise $!, "Cannot Create Plugin #{plugin_name}: #{$!}", $!.backtrace @@ -95,11 +95,6 @@ def push_event(state) true end - def validate_plugin - validate_plugin_test_command - validate_plugin_test_result - end - def has_failed push_event("failed") end @@ -118,47 +113,44 @@ def create_notifiers end def create_plugin - validate_plugin_in_options - @plugin = GenericMonitor.build_plugin(@options[:plugin]) - @plugin.init(@options) - validate_plugin - @plugin + raise_plugin_not_found unless @options.has_key?(:plugin) + plugin = GenericMonitor.build_plugin(@options[:plugin]) + validate_plugin(plugin) + plugin.init(@options) + @plugin = plugin end - def validate_plugin_test_command - if @plugin.respond_to?(:test_command?) - true + private + + def validate_plugin(plugin) + if !plugin.respond_to?(:test_command?) + error_message = "test_command? not implemented in #{plugin.class} plugin" + raise Ragios::PluginTestCommandNotImplemented.new(error: error_message), error_message + elsif !plugin.respond_to?(:init) + error_message = "init not implemented in #{plugin.class} plugin" + raise Ragios::PluginInitNotImplemented.new(error: error_message), error_message + elsif !defined?(plugin.test_result) + error_message = "test_result not defined in #{plugin.class} plugin" + raise Ragios::PluginTestResultNotDefined.new(error: error_message), error_message else - error_message = "No test_command? found for #{@plugin.class} plugin" - raise Ragios::PluginTestCommandNotFound.new(error: error_message), error_message - end - end - - def validate_plugin_test_result - if defined?(@plugin.test_result) true - else - error_message = "No test_result found for #{@plugin.class} plugin" - raise Ragios::PluginTestResultNotFound.new(error: error_message), error_message end end def validate_notifiers_in_options - if @options.has_key?(:via) - true - else - error_message = "No Notifier Found in #{@options}" - raise Ragios::NotifierNotFound.new(error: error_message), error_message + unless @options.has_key?(:via) + raise_notifier_not_found_error end end - def validate_plugin_in_options - if @options.has_key?(:plugin) - true - else - error_message = "No Plugin Found in #{@options}" - raise Ragios::PluginNotFound.new(error: error_message), error_message - end + def raise_notifier_not_found_error + error_message = "No Notifier Found in #{@options}" + raise Ragios::NotifierNotFound.new(error: error_message), error_message + end + + def raise_plugin_not_found + error_message = "No Plugin Found in #{@options}" + raise Ragios::PluginNotFound.new(error: error_message), error_message end end end diff --git a/lib/ragios/notifications/notify_worker.rb b/lib/ragios/notifications/notify_worker.rb index aebf98cb..9d0c4160 100644 --- a/lib/ragios/notifications/notify_worker.rb +++ b/lib/ragios/notifications/notify_worker.rb @@ -21,12 +21,13 @@ def perform(event, monitor, test_result, notifier) publisher.async.log_event!( event_details.merge(event: occurred) ) - + terminate rescue Exception => exception occurred = {"notifier error" => exception.message} publisher.async.log_event!( event_details.merge(event: occurred) ) + raise exception end private diff --git a/lib/ragios/ragios_exception.rb b/lib/ragios/ragios_exception.rb index ddf8a27e..782d3145 100644 --- a/lib/ragios/ragios_exception.rb +++ b/lib/ragios/ragios_exception.rb @@ -10,7 +10,8 @@ class EventNotFound < StandardError; end class MonitorNotFound < StandardError; end class NotifierNotFound < StandardError; end class PluginNotFound < StandardError; end - class PluginTestCommandNotFound < StandardError; end - class PluginTestResultNotFound < StandardError; end + class PluginTestCommandNotImplemented < StandardError; end + class PluginTestResultNotDefined < StandardError; end + class PluginInitNotImplemented < StandardError; end class CannotEditSystemSettings < StandardError; end end diff --git a/spec/lib/monitors/generic_monitor_spec.rb b/spec/lib/monitors/generic_monitor_spec.rb index 02be7ef9..f163a02d 100644 --- a/spec/lib/monitors/generic_monitor_spec.rb +++ b/spec/lib/monitors/generic_monitor_spec.rb @@ -1,22 +1,130 @@ require 'spec_base.rb' describe Ragios::Monitors::GenericMonitor do - describe "#validate_plugin_in_options" do - context "when options has no plugin" do - it "raises a No plugin found exception" do + describe "#create_plugin" do + context "when plugin is included in options" do + context "when plugin defines test_result" do + context "when plugin implements test_command" do + context "when plugin implements init(options)" do + it "creates the plugin" do + module Ragios + module Plugin + class GoodPlugin + attr_reader :test_result + def init(options); end + def test_command?; end + end + end + end + options = {plugin: "good_plugin"} + generic_monitor = Ragios::Monitors::GenericMonitor.new(options, true) + expect(generic_monitor.create_plugin).to be_a(Ragios::Plugin::GoodPlugin) + expect(generic_monitor.plugin).to be_a(Ragios::Plugin::GoodPlugin) + end + end + end + end + end + context "when plugin does not implement test_command?" do + context "when plugin defines test_result" do + context "when plugin is included in options" do + context "when plugin implements init(options)" do + it "raises a PluginTestCommandNotImplemented exception, plugin not is created" do + module Ragios + module Plugin + class PluginWithNoTestCmd + attr_reader :test_result + def init(options); end + end + end + end + + options = {plugin: "plugin_with_no_test_cmd"} + generic_monitor = Ragios::Monitors::GenericMonitor.new(options, true) + expect{generic_monitor.create_plugin}.to raise_error(Ragios::PluginTestCommandNotImplemented) + expect(generic_monitor.plugin).to be_nil + end + end + end + end + end + context "when plugin does not define test_result" do + context "when plugin implements test_command?" do + context "when plugin is included in options" do + context "when plugin implements init(options)" do + it "raises a PluginTestResultNotDefined exception, plugin is not created" do + module Ragios + module Plugin + class PluginWithNoTestResult + def init(options); end + def test_command?; end + end + end + end + + options = {plugin: "plugin_with_no_test_result"} + generic_monitor = Ragios::Monitors::GenericMonitor.new(options, true) + expect{generic_monitor.create_plugin}.to raise_error(Ragios::PluginTestResultNotDefined) + expect(generic_monitor.plugin).to be_nil + end + end + end + end + end + context "when plugin does not implement init(options) metho" do + context "when plugin defines test_result" do + context "when plugin implements test_command" do + context "when plugin is included in options" do + it "raises a PluginInitNotImplemented exception, plugin is not created" do + module Ragios + module Plugin + class PluginWithNoInit + attr_reader :test_result + def test_command?; end + end + end + end + options = {plugin: "plugin_with_no_init"} + generic_monitor = Ragios::Monitors::GenericMonitor.new(options, true) + expect{generic_monitor.create_plugin}.to raise_error(Ragios::PluginInitNotImplemented) + expect(generic_monitor.plugin).to be_nil + end + end + end + end + end + context "when there is no plugin in options" do + it "raises a PluginNotFound exception, plugin is not created" do options = {} generic_monitor = Ragios::Monitors::GenericMonitor.new(options, true) - expect(generic_monitor.options).to eq(options) - expect{generic_monitor.validate_plugin_in_options}.to raise_error(Ragios::PluginNotFound) + expect{generic_monitor.create_plugin}.to raise_error(Ragios::PluginNotFound) + expect(generic_monitor.plugin).to be_nil end end - - context "when options has a plugin" do - it "Returns true" do - options = {plugin: :plugin} + context "when plugin key in options is not a symbol" do + it "raises a PluginNotFound exception, plugin is not created" do + options = {"plugin" => "plugin" } + generic_monitor = Ragios::Monitors::GenericMonitor.new(options, true) + expect{generic_monitor.create_plugin}.to raise_error(Ragios::PluginNotFound) + expect(generic_monitor.plugin).to be_nil + end + end + end + describe "#create_notifiers" do + context "when notifiers are not included in options" do + it "raises a NotifierNotFound exception" do + options = {} + generic_monitor = Ragios::Monitors::GenericMonitor.new(options, true) + expect{generic_monitor.create_notifiers}.to raise_error(Ragios::NotifierNotFound) + expect(generic_monitor.notifiers).to be_nil + end + end + context "when notifiers list is empty in options" do + it "raises a NotifierNotFound exception" do + options = {via: []} generic_monitor = Ragios::Monitors::GenericMonitor.new(options, true) - expect(generic_monitor.options).to eq(options) - expect(generic_monitor.validate_plugin_in_options).to eq(true) + expect{generic_monitor.create_notifiers}.to raise_error(Ragios::NotifierNotFound) + expect(generic_monitor.notifiers).to be_nil end end end