spec/ragios-client_spec.rb in ragios-client-0.0.4 vs spec/ragios-client_spec.rb in ragios-client-0.0.5

- old
+ new

@@ -1,398 +1,229 @@ require 'rubygems' require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib/ragios-client')) -#use for fast testing +#use for fast tests plugin = "mock_plugin" -#using the url_monitor plugin will perform a real http request on each check leading to slow tests -#plugin = "url_monitor" +def generate_json(str) + JSON.generate(str) +end + admin = 'admin' password = 'ragios' describe "Ragios Client" do before(:each) do - @ragios = Ragios::Client.new(username: admin,password: password) + #@ragios = Ragios::Client.new(username: admin, password: password) + @ragios = Ragios::Client.new end + describe "#add" do + it "adds a monitor" do + monitor = { + monitor: "Google", + url: "http://google.com", + every: "5m", + contact: "admin@mail.com", + via: ["gmail_notifier","twitter_notifier"], + plugin: plugin + } - it "adds a monitor" do - monitors = [{monitor: "Google", - url: "http://google.com", - every: "5m", - contact: "admin@mail.com", - via: ["gmail_notifier","twitter_notifier"], - plugin: plugin }] + returned_monitor = @ragios.add(monitor) + returned_monitor.should include(monitor) + monitor_id = returned_monitor[:_id] - returned_monitors = @ragios.add monitors - returned_monitors.first.should include(monitors.first) - monitor_id = returned_monitors.first[:_id] - - #teardown - hash = @ragios.delete(monitor_id) - hash[:ok].should == true - end - - it "cannot add a monitor with no plugin" do - monitors = [{monitor: "Google", - url: "http://google.com", - every: "5m", - contact: "admin@mail.com", - via: "gmail_notifier"}] - begin - @ragios.add monitors - rescue => e - e.should be_an_instance_of Ragios::ClientException - e.message.should include("No Plugin Found") + #teardown + @ragios.delete(monitor_id) end - end - it "cannot add a monitor with no notifier" do - monitors = [{monitor: "Google", - url: "http://google.com", - every: "5m", - contact: "admin@mail.com", - plugin: plugin }] - begin - @ragios.add monitors - rescue Exception => e - e.should be_an_instance_of Ragios::ClientException - e.message.should include("No Notifier Found") + it "cannot add a monitor with no plugin" do + monitor = { + monitor: "Google", + url: "http://google.com", + every: "5m", + contact: "admin@mail.com", + via: "gmail_notifier" + } + begin + @ragios.add monitor + rescue => e + e.should be_an_instance_of Ragios::ClientException + e.message.should include("No Plugin Found") + end end - end - it "cannot add a badly formed monitor" do - expect{@ragios.add("bad data")}.to raise_error(JSON::GeneratorError) - end - - it "should retrieve a monitor by id" do - #setup starts - unique_name = "Google #{Time.now.to_i}" - monitors = [{monitor: unique_name, - url: "http://google.com", - every: "5m", - contact: "admin@mail.com", - via: ["gmail_notifier"], - plugin: plugin }] - - returned_monitors = @ragios.add monitors - monitor_id = returned_monitors.first[:_id] - #setup ends - - received_monitor = @ragios.find(monitor_id) - received_monitor.should include(monitors.first) - #teardown - hash = @ragios.delete(monitor_id) - hash[:ok].should == true - end - - it "cannot retrieve a monitor that doesnt exist" do - begin - @ragios.find('dont_exist') - rescue => e - e.should be_an_instance_of Ragios::ClientException - e.message.should include("No monitor found with id = dont_exist") + it "cannot add a monitor with no notifier" do + monitor = { + monitor: "Google", + url: "http://google.com", + every: "5m", + contact: "admin@mail.com", + plugin: plugin + } + begin + @ragios.add monitor + rescue Exception => e + e.should be_an_instance_of Ragios::ClientException + e.message.should include("No Notifier Found") + end end - end - it "should find monitors that match multiple key/value pairs" do - #setup starts - unique_name = "Google #{Time.now.to_i}" - monitors = [{monitor: unique_name, - url: "http://google.com", - every: "5m", - contact: "admin@mail.com", - via: ["gmail_notifier"], - plugin: plugin, - tag: "test" }] - returned_monitors = @ragios.add monitors - monitor_id = returned_monitors.first[:_id] - #setup ends - - options = {tag: 'test', every: '5m', monitor: unique_name} - found_monitors = @ragios.where(options) - found_monitors.first.should include(monitors.first) - - #teardown - hash = @ragios.delete(monitor_id) - hash[:ok].should == true - end - - it "returns an empty array when no monitor matches multiple key/value pairs" do - options = {something: "dont_exist", every: "5m", monitor: "dont_exist"} - @ragios.where(options).should == [] - end - - it "should update a monitor" do - #setup starts - unique_name = "Google #{Time.now.to_i}" - monitors = [{monitor: unique_name, - url: "http://google.com", - every: "5m", - contact: "admin@mail.com", - via: ["gmail_notifier"], - plugin: plugin }] - - returned_monitors = @ragios.add monitors - monitor_id = returned_monitors.first[:_id] - #setup ends - - update_options = {every: "10m", via: ["twitter_notifier"]} - updated_monitor = @ragios.update(monitor_id, update_options) - updated_monitor.should include(update_options) - - #teardown - hash = @ragios.delete(monitor_id) - hash[:ok].should == true - end - - it "cannot update a monitor with bad data" do - #setup starts - unique_name = "Google #{Time.now.to_i}" - monitors = [{monitor: unique_name, - url: "http://google.com", - every: "5m", - contact: "admin@mail.com", - via: ["gmail_notifier"], - plugin: plugin }] - - returned_monitors = @ragios.add monitors - monitor_id = returned_monitors.first[:_id] - #setup ends - - expect { @ragios.update(monitor_id,"bad data") }.to raise_error(JSON::GeneratorError) - - #teardown - hash = @ragios.delete(monitor_id) - hash[:ok].should == true - end - - it "cannot update a monitor that don't exist" do - update_options = {every: "5m", via: ["twitter_notifier"]} - monitor_id = "dont_exist" - begin - @ragios.update(monitor_id,update_options) - rescue => e - e.should be_an_instance_of Ragios::ClientException - e.message.should include("No monitor found with id = dont_exist") + it "cannot add a badly formed monitor" do + expect{@ragios.add("bad data")}.to raise_error(JSON::GeneratorError) end end + describe "More API calls" do + before(:each) do + @unique_name = "Google #{Time.now.to_i}" + @monitor = { + monitor: @unique_name, + url: "http://google.com", + every: "5m", + contact: "admin@mail.com", + via: ["gmail_notifier"], + plugin: plugin, + tag: "test" + } - it "tests a monitor" do - #setup starts - unique_name = "Google #{Time.now.to_i}" - monitors = [{monitor: unique_name, - url: "http://google.com", - every: "5m", - contact: "admin@mail.com", - via: ["gmail_notifier"], - plugin: plugin }] - - returned_monitors = @ragios.add monitors - monitor_id = returned_monitors.first[:_id] - #setup ends - - hash = @ragios.test(monitor_id) - hash[:ok].should == true - - #teardown - hash = @ragios.delete(monitor_id) - hash[:ok].should == true - end - - it "cannot test a monitor that don't exist" do - monitor_id = "dont_exist" - begin - @ragios.test(monitor_id) - rescue => e - e.should be_an_instance_of Ragios::ClientException - e.message.should include("No monitor found with id = dont_exist") + returned_monitor = @ragios.add(@monitor) + @monitor_id = returned_monitor[:_id] end - end - - it "deletes a monitor" do - #setup starts - unique_name = "Google #{Time.now.to_i}" - monitors = [{monitor: unique_name, - url: "http://google.com", - every: "5m", - contact: "admin@mail.com", - via: ["gmail_notifier"], - plugin: plugin }] - - returned_monitors = @ragios.add monitors - monitor_id = returned_monitors.first[:_id] - #setup ends - - hash = @ragios.delete(monitor_id) - hash[:ok].should == true - begin - @ragios.find(monitor_id) - rescue => e - e.should be_an_instance_of Ragios::ClientException - e.message.should include("No monitor found with id = #{monitor_id}") + describe "#find" do + it "should retrieve a monitor by id" do + received_monitor = @ragios.find(@monitor_id) + received_monitor.should include(@monitor) + end + it "cannot retrieve a monitor that doesnt exist" do + expect{ @ragios.find('dont_exist') }.to raise_error(Ragios::ClientException, generate_json(error: "No monitor found with id = dont_exist")) + end end - end - - it "cannot delete a monitor that doesnt exist" do - monitor_id = "dont_exist" - begin - @ragios.find(monitor_id) - rescue => e - e.should be_an_instance_of Ragios::ClientException - e.message.should include("No monitor found with id = #{monitor_id}") + describe "#where" do + it "should find monitors that match multiple key/value pairs" do + options = {tag: 'test', every: '5m', monitor: @unique_name} + found_monitors = @ragios.where(options) + found_monitors.first.should include(@monitor) + end + it "returns an empty array when no monitor matches multiple key/value pairs" do + options = {something: "dont_exist", every: "5m", monitor: "dont_exist"} + @ragios.where(options).should == [] + end end - end + describe "#update" do + it "should update a monitor" do + update_options = {every: "10m", via: ["twitter_notifier"]} + @ragios.update(@monitor_id, update_options).should == {ok: true} + end + it "cannot update a monitor with bad data" do + expect { @ragios.update(@monitor_id,"bad data") }.to raise_error(JSON::GeneratorError) + end + it "cannot update a monitor that don't exist" do + update_options = {every: "5m", via: ["twitter_notifier"]} + expect { @ragios.update("dont_exist", update_options) }.to raise_error(Ragios::ClientException, generate_json(error: "No monitor found with id = dont_exist")) + end + end + describe "#test" do + it "tests a monitor" do + @ragios.test(@monitor_id).should == {ok: true} + end + it "cannot test a monitor that don't exist" do + expect { @ragios.test("dont_exist") }.to raise_error(Ragios::ClientException, generate_json(error: "No monitor found with id = dont_exist")) + end + end + describe "#delete" do + it "deletes a monitor" do + new_monitor_id = @ragios.add(@monitor)[:_id] + @ragios.delete(new_monitor_id).should == {ok: true} + end + it "cannot delete a monitor that doesnt exist" do + this_monitor_id = "dont_exist" + expect{ @ragios.find(this_monitor_id) }.to raise_error(Ragios::ClientException, generate_json(error: "No monitor found with id = #{this_monitor_id}")) + end + end + describe "#stop" do + it "stops an active monitor" do + active_monitor = @ragios.find(@monitor_id) + active_monitor[:status_].should == "active" - it "stops an active monitor" do - #setup starts - unique_name = "Google #{Time.now.to_i}" - monitors = [{monitor: unique_name, - url: "http://google.com", - every: "5m", - contact: "admin@mail.com", - via: ["gmail_notifier"], - plugin: plugin }] + @ragios.stop(@monitor_id).should == {ok: true} - returned_monitors = @ragios.add monitors - monitor_id = returned_monitors.first[:_id] - #setup ends + stopped_monitor = @ragios.find(@monitor_id) + stopped_monitor[:status_].should == "stopped" - active_monitor = @ragios.find(monitor_id) - active_monitor[:status_].should == "active" - - hash = @ragios.stop(monitor_id) - hash[:ok].should == true - - stopped_monitor = @ragios.find(monitor_id) - stopped_monitor[:status_].should == "stopped" - - #stop monitor is idempotent - hash = @ragios.stop(monitor_id) - hash[:ok].should == true - - #teardown - hash = @ragios.delete(monitor_id) - hash[:ok].should == true - end - - it "cannot stop a monitor that dont exist" do - monitor_id = "dont_exist" - begin - @ragios.stop(monitor_id) - rescue => e - e.should be_an_instance_of Ragios::ClientException - e.message.should include("No monitor found with id = #{monitor_id}") + #stop monitor is idempotent + @ragios.stop(@monitor_id).should == {ok: true} + end + it "cannot stop a monitor that dont exist" do + this_monitor_id = "dont_exist" + expect { @ragios.stop(this_monitor_id) }.to raise_error(Ragios::ClientException, generate_json(error: "No monitor found with id = #{this_monitor_id}")) + end end - end + describe "#restart" do + it "restarts a stopped monitor" do + @ragios.stop(@monitor_id) + stopped_monitor = @ragios.find(@monitor_id) + stopped_monitor[:status_].should == "stopped" - it "restarts a stopped monitor" do - #setup starts - unique_name = "Google #{Time.now.to_i}" - monitors = [{monitor: unique_name, - url: "http://google.com", - every: "5m", - contact: "admin@mail.com", - via: ["gmail_notifier"], - plugin: plugin }] + @ragios.restart(@monitor_id).should == {ok: true} - returned_monitors = @ragios.add monitors - monitor_id = returned_monitors.first[:_id] - #setup ends + active_monitor = @ragios.find(@monitor_id) + active_monitor[:status_].should == "active" - @ragios.stop(monitor_id) - stopped_monitor = @ragios.find(monitor_id) - stopped_monitor[:status_].should == "stopped" - - hash = @ragios.restart(monitor_id) - hash[:ok].should == true - - active_monitor = @ragios.find(monitor_id) - active_monitor[:status_].should == "active" - - #restart monitor is idempotent - hash = @ragios.restart(monitor_id) - hash[:ok].should == true - - #teardown - hash = @ragios.delete(monitor_id) - hash[:ok].should == true - end - - it "cannot restart a monitor that dont exist" do - monitor_id = "dont_exist" - begin - @ragios.restart(monitor_id) - rescue => e - e.should be_an_instance_of Ragios::ClientException - e.message.should include("No monitor found with id = #{monitor_id}") + #restart monitor is idempotent + @ragios.restart(@monitor_id).should == {ok: true} + end + it "cannot restart a monitor that dont exist" do + this_monitor_id = "dont_exist" + expect { @ragios.restart(this_monitor_id) }.to raise_error(Ragios::ClientException, generate_json(error: "No monitor found with id = #{this_monitor_id}")) + end end + describe "#all" do + it "retrieves all monitors" do + retrieved_monitors = @ragios.all + retrieved_monitors.should_not be_empty + retrieved_monitors.should be_an_instance_of Array + end + end + after(:each) do + @ragios.delete(@monitor_id) + end end it "accepts correct login credentials" do expect{ @ragios.login(admin, password) }.to_not raise_error end it "rejects incorrect login credentials" do - begin - @ragios.login('incorret','incorret') - rescue => e - e.should be_an_instance_of Ragios::ClientException - e.message.should include("You are not authorized to access this resource") - end + expect { @ragios.login('incorret','incorret') }.to raise_error(Ragios::ClientException, generate_json(error: "You are not authorized to access this resource")) end - it "retrieves all monitors" do - #setup starts - unique_name = "Google #{Time.now.to_i}" - monitors = [{monitor: unique_name, - url: "http://google.com", - every: "5m", - contact: "admin@mail.com", - via: ["gmail_notifier"], - plugin: plugin }] - - returned_monitors = @ragios.add monitors - monitor_id = returned_monitors.first[:_id] - #setup ends - - retrieved_monitors = @ragios.all - retrieved_monitors.should_not be_empty - retrieved_monitors.should be_an_instance_of Array - - #teardown - hash = @ragios.delete(monitor_id) - hash[:ok].should == true - end - it "has default settings" do ragios_client = Ragios::Client.new ragios_client.address.should == "http://127.0.0.1" ragios_client.port.should == "5041" - ragios_client.username.should == "" - ragios_client.password.should == "" + ragios_client.username.should == nil + ragios_client.password.should == nil end it "accepts new default address" do ragios_client = Ragios::Client.new(address: 'https://mysite.com', port: '6802') ragios_client.address.should == "https://mysite.com" ragios_client.port.should == "6802" - ragios_client.username.should == "" - ragios_client.password.should == "" + ragios_client.username.should == nil + ragios_client.password.should == nil end it "accepts new default login credentials" do ragios_client = Ragios::Client.new(username: 'superadmin', password: 'abc123') ragios_client.address.should == "http://127.0.0.1" ragios_client.port.should == "5041" ragios_client.username.should == "superadmin" ragios_client.password.should == "abc123" end - it "accepts new default settings" do + it "accepts new default settings" do ragios_client = Ragios::Client.new(username: 'superadmin', password: 'abc123', address: 'https://mysite.com', port: '6802') ragios_client.address.should == "https://mysite.com" ragios_client.port.should == "6802" ragios_client.username.should == "superadmin" ragios_client.password.should == "abc123" end - end