Sha256: 4ad7a855b331caf01733cceef4f0a653ce07f602d1ac436ee12a2775c24e6955

Contents?: true

Size: 1.76 KB

Versions: 1

Compression:

Stored size: 1.76 KB

Contents

require "spec_helper"

describe ::LogStasher do
  describe "#log_as_json" do
    it "calls the logger with the payload" do
      expect(::LogStasher.logger).to receive(:<<) do |json|
        expect(::JSON.parse(json)).to eq("yolo" => "brolo")
      end

      ::LogStasher.log_as_json(:yolo => :brolo)
    end

    context "with event" do
      it "calls logger with a logstash event" do
        expect(::LogStasher.logger).to receive(:<<) do |json|
          payload = ::JSON.parse(json)

          expect(payload["@timestamp"]).to_not be_nil
          expect(payload["@version"]).to eq("1")
          expect(payload["yolo"]).to eq("brolo")
        end

        ::LogStasher.log_as_json({:yolo => :brolo}, :as_logstash_event => true)
      end
    end

    context "with metadata" do
      before { ::LogStasher.metadata = { :namespace => :cooldude } }
      after { ::LogStasher.metadata = {} }

      it "calls logger with the metadata" do
        expect(::LogStasher.logger).to receive(:<<) do |json|
          expect(::JSON.parse(json)).to eq("yolo" => "brolo", "metadata" => { "namespace" => "cooldude" })
        end

        ::LogStasher.log_as_json(:yolo => :brolo)
      end

      it "merges metadata for LogStash::Event types" do
        expect(::LogStasher.logger).to receive(:<<) do |json|
          expect(::JSON.parse(json)).to match(a_hash_including("yolo" => "brolo", "metadata" => { "namespace" => "cooldude" }))
        end

        ::LogStasher.log_as_json(::LogStash::Event.new(:yolo => :brolo))
      end

      it "does not merge metadata on an array" do
        expect(::LogStasher.logger).to receive(:<<) do |json|
          expect(::JSON.parse(json)).to eq([{ "yolo" => "brolo" }])
        end

        ::LogStasher.log_as_json([{:yolo => :brolo}])
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
md-logstasher-1.7.0 spec/lib/logstasher/logstasher_spec.rb