spec/lib/flapjack/gateways/pagerduty_spec.rb in flapjack-0.6.53 vs spec/lib/flapjack/gateways/pagerduty_spec.rb in flapjack-0.6.54

- old
+ new

@@ -2,44 +2,45 @@ require 'yajl/json_gem' require 'flapjack/gateways/pagerduty' -describe Flapjack::Gateways::Pagerduty, :redis => true do +describe Flapjack::Gateways::Pagerduty, :logger => true do let(:config) { {'queue' => 'pagerduty_notifications'} } let(:time) { Time.new } + let(:redis) { mock('redis') } + it "prompts the blocking redis connection to quit" do - redis = mock('redis') redis.should_receive(:rpush).with(config['queue'], %q{{"notification_type":"shutdown"}}) - fp = Flapjack::Gateways::Pagerduty.new - Flapjack::RedisPool.should_receive(:new) - fp.bootstrap(:config => config) - fp.add_shutdown_event(:redis => redis) + Flapjack::RedisPool.should_receive(:new).and_return(redis) + fp = Flapjack::Gateways::Pagerduty.new(:config => config, :logger => @logger) + fp.stop end it "doesn't look for acknowledgements if this search is already running" do - @redis.set(Flapjack::Gateways::Pagerduty::SEM_PAGERDUTY_ACKS_RUNNING, 'true') + redis.should_receive(:get).with('sem_pagerduty_acks_running').and_return('true') + Flapjack::RedisPool.should_receive(:new).and_return(redis) + fp = Flapjack::Gateways::Pagerduty.new(:config => config, :logger => @logger) - fp = Flapjack::Gateways::Pagerduty.new - Flapjack::RedisPool.should_receive(:new) - fp.bootstrap(:config => config) - fp.instance_variable_set("@redis_timer", @redis) - fp.should_not_receive(:find_pagerduty_acknowledgements) fp.find_pagerduty_acknowledgements_if_safe end it "looks for acknowledgements if the search is not already running" do - fp = Flapjack::Gateways::Pagerduty.new - Flapjack::RedisPool.should_receive(:new) - fp.bootstrap(:config => config) - fp.instance_variable_set("@redis_timer", @redis) + redis.should_receive(:get).with('sem_pagerduty_acks_running').and_return(nil) + redis.should_receive(:set).with('sem_pagerduty_acks_running', 'true') + redis.should_receive(:expire).with('sem_pagerduty_acks_running', 300) + redis.should_receive(:del).with('sem_pagerduty_acks_running') + + Flapjack::RedisPool.should_receive(:new).and_return(redis) + fp = Flapjack::Gateways::Pagerduty.new(:config => config, :logger => @logger) + fp.should_receive(:find_pagerduty_acknowledgements) fp.find_pagerduty_acknowledgements_if_safe end # Testing the private PagerDuty methods separately, it's simpler. May be @@ -69,31 +70,31 @@ "fields=incident_number,status,last_status_change_by&incident_key=#{check}&" + "since=#{since}&status=acknowledged&until=#{unt}"). with(:headers => {'Authorization'=>['flapjack', 'password123']}). to_return(:status => 200, :body => response.to_json, :headers => {}) - EM.synchrony do - fp = Flapjack::Gateways::Pagerduty.new - Flapjack::RedisPool.should_receive(:new) - fp.bootstrap(:config => config) + Flapjack::RedisPool.should_receive(:new).and_return(redis) + fp = Flapjack::Gateways::Pagerduty.new(:config => config, :logger => @logger) + + EM.synchrony do result = fp.send(:pagerduty_acknowledged?, 'subdomain' => 'flpjck', 'username' => 'flapjack', 'password' => 'password123', 'check' => check) result.should be_a(Hash) result.should have_key(:pg_acknowledged_by) result[:pg_acknowledged_by].should be_a(Hash) result[:pg_acknowledged_by].should have_key('id') result[:pg_acknowledged_by]['id'].should == 'ABCDEFG' EM.stop end + end it "creates acknowledgements when pagerduty acknowledgements are found" do - fp = Flapjack::Gateways::Pagerduty.new - Flapjack::RedisPool.should_receive(:new) - fp.bootstrap(:config => config) + Flapjack::RedisPool.should_receive(:new).and_return(redis) + fp = Flapjack::Gateways::Pagerduty.new(:config => config, :logger => @logger) contact = mock('contact') contact.should_receive(:pagerduty_credentials).and_return({ 'service_key' => '12345678', 'subdomain"' => 'flpjck', @@ -117,29 +118,31 @@ it "runs a blocking loop listening for notifications" do timer = mock('timer') timer.should_receive(:cancel) EM::Synchrony.should_receive(:add_periodic_timer).with(10).and_return(timer) - redis = mock('redis') redis.should_receive(:del).with('sem_pagerduty_acks_running') - redis.should_receive(:empty!) - fp = Flapjack::Gateways::Pagerduty.new Flapjack::RedisPool.should_receive(:new).and_return(redis) - fp.bootstrap(:config => config) + fp = Flapjack::Gateways::Pagerduty.new(:config => config, :logger => @logger) - fp.should_receive(:should_quit?).exactly(3).times.and_return(false, false, true) - redis.should_receive(:blpop).twice.and_return( - ["pagerduty_notifications", %q{{"notification_type":"problem","event_id":"main-example.com:ping","state":"critical","summary":"!!!"}}], - ["pagerduty_notifications", %q{{"notification_type":"shutdown"}}] - ) + blpop_count = 0 + redis.should_receive(:blpop).twice { + blpop_count += 1 + if blpop_count == 1 + ["pagerduty_notifications", %q{{"notification_type":"problem","event_id":"main-example.com:ping","state":"critical","summary":"!!!"}}] + else + fp.instance_variable_set('@should_quit', true) + ["pagerduty_notifications", %q{{"notification_type":"shutdown"}}] + end + } + fp.should_receive(:test_pagerduty_connection).and_return(true) fp.should_receive(:send_pagerduty_event) - fp.main - fp.cleanup + fp.start end it "tests the pagerduty connection" do evt = { "service_key" => "11111111111111111111111111111111", "incident_key" => "Flapjack is running a NOOP", @@ -148,18 +151,16 @@ body = evt.to_json stub_request(:post, "https://events.pagerduty.com/generic/2010-04-15/create_event.json"). with(:body => body).to_return(:status => 200, :body => '{"status":"success"}', :headers => {}) - EM.synchrony do - fp = Flapjack::Gateways::Pagerduty.new - Flapjack::RedisPool.should_receive(:new) - fp.bootstrap(:config => config) + Flapjack::RedisPool.should_receive(:new).and_return(redis) + fp = Flapjack::Gateways::Pagerduty.new(:config => config, :logger => @logger) + EM.synchrony do ret = fp.send(:test_pagerduty_connection) ret.should be_true - EM.stop end end it "sends an event to pagerduty" do @@ -170,19 +171,16 @@ body = evt.to_json stub_request(:post, "https://events.pagerduty.com/generic/2010-04-15/create_event.json"). with(:body => body).to_return(:status => 200, :body => "", :headers => {}) - EM.synchrony do + Flapjack::RedisPool.should_receive(:new).and_return(redis) + fp = Flapjack::Gateways::Pagerduty.new(:config => config, :logger => @logger) - fp = Flapjack::Gateways::Pagerduty.new - Flapjack::RedisPool.should_receive(:new) - fp.bootstrap(:config => config) - + EM.synchrony do ret = fp.send(:send_pagerduty_event, evt) ret.should_not be_nil ret.should == [200, nil] - EM.stop end end end