spec/outputs/icinga_spec.rb in logstash-output-icinga-1.0.0 vs spec/outputs/icinga_spec.rb in logstash-output-icinga-1.1.0

- old
+ new

@@ -18,11 +18,12 @@ 'user' => user, 'password' => password, 'action' => action, 'action_config' => action_config, 'icinga_host' => icinga_host, - 'icinga_service' => icinga_service + 'icinga_service' => icinga_service, + 'create_object' => true } } let(:event) { LogStash::Event.new({ 'message' => 'This is a dummy message.' }) } let(:request) { "https://#{host[0]}/v1/actions/#{action}?service=#{icinga_host}!#{icinga_service}" } let(:request_body) { "{\"author\":\"#{action_config['author']}\",\"comment\":\"#{action_config['comment']}\"}" } @@ -62,13 +63,13 @@ it 'should retry with the next host' do stub_request(:post, request). with(:body => request_body, :basic_auth => [user, password], :headers => {'Accept'=>'application/json'}). - to_raise(StandardError) + to_raise(Timeout::Error) - expect(logger).to receive(:error).with("Request failed", instance_of(Hash)) + expect(logger).to receive(:warn).with("Request failed", instance_of(Hash)) expect(logger).to receive(:info).with("Retrying request with '#{host[1]}:5665'") stub_request(:post, "https://#{host[1]}:5665/v1/actions/#{action}?service=#{icinga_host}!#{icinga_service}"). with(:body => request_body, @@ -87,33 +88,33 @@ it 'should try each host only once per request' do stub_request(:post, request). with(:body => request_body, :basic_auth => [user, password], :headers => {'Accept'=>'application/json'}). - to_raise(StandardError) + to_raise(Timeout::Error) - expect(logger).to receive(:error).with("Request failed", instance_of(Hash)) + expect(logger).to receive(:warn).with("Request failed", instance_of(Hash)) expect(logger).to receive(:info).with("Retrying request with '#{host[1]}:5665'") stub_request(:post, "https://#{host[1]}:5665/v1/actions/#{action}?service=#{icinga_host}!#{icinga_service}"). with(:body => request_body, :basic_auth => [user, password], :headers => {'Accept'=>'application/json'}). - to_raise(StandardError) + to_raise(Timeout::Error) - expect(logger).to receive(:error).with("Request failed", instance_of(Hash)) + expect(logger).to receive(:warn).with("Request failed", instance_of(Hash)) expect(logger).to receive(:info).with("Retrying request with '#{host[2]}:5665'") stub_request(:post, "https://#{host[2]}:5665/v1/actions/#{action}?service=#{icinga_host}!#{icinga_service}"). with(:body => request_body, :basic_auth => [user, password], :headers => {'Accept'=>'application/json'}). - to_raise(StandardError) + to_raise(Timeout::Error) - expect(logger).to receive(:error).with("Request failed", instance_of(Hash)) + expect(logger).to receive(:warn).with("Request failed", instance_of(Hash)) output.receive(event) end end @@ -149,9 +150,36 @@ } plugin = LogStash::Plugin.lookup('output', 'icinga').new(options) logger = output.logger expect(logger).to receive(:warn).with("Unknown setting 'mysetting' for action '#{action}'") plugin.register + end + end + + context 'with create_object => true' do + it 'should create object and retry action' do + stub_request(:post, request). + with(:body => request_body, + :basic_auth => [user, password], + :headers => {'Accept'=>'application/json'}). + to_return(body: 'Object not found', status: 404, :headers => {}). + to_raise(StandardError) + + expect(logger).to receive(:warn).with("Request failed", instance_of(Hash)) + + stub_request(:put, "https://#{host[0]}/v1/objects/services/#{icinga_host}!#{icinga_service}"). + with(:body => '{"templates":["logstash-service"],"attrs":{"vars.created_by":"logstash"}}', + :basic_auth => [user, password], + :headers => {'Accept'=>'application/json'}). + to_return(:status => 200, :body => '', :headers => {}) + + stub_request(:post, request). + with(:body => request_body, + :basic_auth => [user, password], + :headers => {'Accept'=>'application/json'}). + to_return(body: '', status: 200, :headers => {}) + + output.receive(event) end end end