spec/lib/flapjack/gateways/pagerduty_spec.rb in flapjack-1.5.0 vs spec/lib/flapjack/gateways/pagerduty_spec.rb in flapjack-1.6.0rc1

- old
+ new

@@ -30,11 +30,11 @@ end it "looks for acknowledgements if the search is not already running" do expect(redis).to receive(:get).with('sem_pagerduty_acks_running').and_return(nil) expect(redis).to receive(:set).with('sem_pagerduty_acks_running', 'true') - expect(redis).to receive(:expire).with('sem_pagerduty_acks_running', 300) + expect(redis).to receive(:expire).with('sem_pagerduty_acks_running', 3600) expect(redis).to receive(:del).with('sem_pagerduty_acks_running') expect(Flapjack::RedisPool).to receive(:new).and_return(redis) fp = Flapjack::Gateways::Pagerduty.new(:config => config, :logger => @logger) @@ -67,20 +67,60 @@ "total"=>1} stub_request(:get, "https://flpjck.pagerduty.com/api/v1/incidents?" + "fields=incident_number,status,last_status_change_by&incident_key=#{check}&" + "since=#{since}&status=acknowledged&until=#{unt}"). - with(:headers => {'Authorization'=>['flapjack', 'password123']}). + with(:headers => {'Authorization'=>'Token token=token123'}). to_return(:status => 200, :body => response.to_json, :headers => {}) expect(Flapjack::RedisPool).to receive(:new).and_return(redis) fp = Flapjack::Gateways::Pagerduty.new(:config => config, :logger => @logger) + EM.synchrony do + result = fp.send(:pagerduty_acknowledged?, 'subdomain' => 'flpjck', 'token' => 'token123', + 'check' => check) + expect(result).to be_a(Hash) + expect(result).to have_key(:pg_acknowledged_by) + expect(result[:pg_acknowledged_by]).to be_a(Hash) + expect(result[:pg_acknowledged_by]).to have_key('id') + expect(result[:pg_acknowledged_by]['id']).to eq('ABCDEFG') + EM.stop + end + + end + + it "looks for acknowledgements via the PagerDuty API with basic auth" do + check = 'PING' + expect(Time).to receive(:now).and_return(time) + since = (time.utc - (60*60*24*7)).iso8601 # the last week + unt = (time.utc + (60*60*24)).iso8601 # 1 day in the future + + response = {"incidents" => + [{"incident_number" => 12, + "status" => "acknowledged", + "last_status_change_by" => {"id"=>"ABCDEFG", "name"=>"John Smith", + "email"=>"johns@example.com", + "html_url"=>"http://flpjck.pagerduty.com/users/ABCDEFG"} + } + ], + "limit"=>100, + "offset"=>0, + "total"=>1} + + stub_request(:get, "https://flpjck.pagerduty.com/api/v1/incidents?" + + "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 => {}) + + expect(Flapjack::RedisPool).to 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 = fp.send(:pagerduty_acknowledged?, 'subdomain' => 'flpjck', + 'username' => 'flapjack', 'password' => 'password123', 'check' => check) expect(result).to be_a(Hash) expect(result).to have_key(:pg_acknowledged_by) expect(result[:pg_acknowledged_by]).to be_a(Hash) expect(result[:pg_acknowledged_by]).to have_key('id') @@ -96,22 +136,23 @@ contact = double('contact') expect(contact).to receive(:pagerduty_credentials).and_return({ 'service_key' => '12345678', 'subdomain"' => 'flpjck', - 'username' => 'flapjack', - 'password' => 'password123' + 'token' => 'token123' }) entity_check = double('entity_check') - expect(entity_check).to receive(:check).exactly(2).times.and_return('PING') + expect(entity_check).to receive(:check).exactly(1).times.and_return('PING') expect(entity_check).to receive(:contacts).and_return([contact]) - expect(entity_check).to receive(:entity_name).exactly(2).times.and_return('foo-app-01.bar.net') + expect(entity_check).to receive(:entity_name).exactly(1).times.and_return('foo-app-01.bar.net') + expect(entity_check).to receive(:in_unscheduled_maintenance?).exactly(1).times.and_return(false) + expect(entity_check).to receive(:failed?).exactly(1).times.and_return(true) expect(Flapjack::Data::Event).to receive(:create_acknowledgement).with('foo-app-01.bar.net', 'PING', :summary => 'Acknowledged on PagerDuty', :duration => 14400, :redis => redis) - expect(Flapjack::Data::EntityCheck).to receive(:unacknowledged_failing).exactly(2).times.and_return([entity_check]) + expect(Flapjack::Data::EntityCheck).to receive(:unacknowledged_failing).exactly(1).times.and_return([entity_check]) expect(fp).to receive(:pagerduty_acknowledged?).and_return({}) fp.send(:find_pagerduty_acknowledgements) end @@ -190,10 +231,9 @@ end end it "does not look for acknowledgements if all required credentials are not present" do creds = {'subdomain' => 'example', - 'username' => 'sausage', 'check' => 'PING'} expect(Flapjack::RedisPool).to receive(:new).and_return(redis) fp = Flapjack::Gateways::Pagerduty.new(:config => config, :logger => @logger) EM.synchrony do