Skip to content
This repository has been archived by the owner on Mar 27, 2021. It is now read-only.

Commit

Permalink
add unit tests for GenericMonitor#create_build
Browse files Browse the repository at this point in the history
  • Loading branch information
obi-a committed Jul 30, 2017
1 parent fb374be commit 556a1de
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 50 deletions.
64 changes: 28 additions & 36 deletions lib/ragios/monitors/generic_monitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/ragios/notifications/notify_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions lib/ragios/ragios_exception.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
130 changes: 119 additions & 11 deletions spec/lib/monitors/generic_monitor_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 556a1de

Please sign in to comment.