Module: LogStashHelper

Defined in:
lib/logstash/devutils/rspec/logstash_helpers.rb

Constant Summary collapse

DEFAULT_NUMBER_OF_TRY =
5
DEFAULT_EXCEPTIONS_FOR_TRY =
[RSpec::Expectations::ExpectationNotMetError]

Instance Method Summary collapse

Instance Method Details

#agent(&block) ⇒ Object



102
103
104
105
106
107
108
109
# File 'lib/logstash/devutils/rspec/logstash_helpers.rb', line 102

def agent(&block)

  it("agent(#{caller[0].gsub(/ .*/, "")}) runs") do
    pipeline = LogStash::Pipeline.new(config)
    pipeline.run
    block.call
  end
end

#config(configstr) ⇒ Object



16
17
18
# File 'lib/logstash/devutils/rspec/logstash_helpers.rb', line 16

def config(configstr)
  let(:config) { configstr }
end

#input(config, &block) ⇒ Object

def sample



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/logstash/devutils/rspec/logstash_helpers.rb', line 64

def input(config, &block)
  pipeline = LogStash::Pipeline.new(config)
  queue = Queue.new

  pipeline.instance_eval do
    # create closure to capture queue
    @output_func = lambda { |event| queue << event }

    # output_func is now a method, call closure
    def output_func(event)
      @output_func.call(event)
    end
  end

  pipeline_thread = Thread.new { pipeline.run }
  sleep 0.1 while !pipeline.ready?

  result = block.call(pipeline, queue)

  pipeline.shutdown
  pipeline_thread.join

  result
end

#plugin_input(plugin, &block) ⇒ Object

def input



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/logstash/devutils/rspec/logstash_helpers.rb', line 89

def plugin_input(plugin, &block)
  queue = Queue.new

  input_thread = Thread.new do
    plugin.run(queue)
  end
  result = block.call(queue)

  plugin.do_stop
  input_thread.join
  result
end

#sample(sample_event, &block) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/logstash/devutils/rspec/logstash_helpers.rb', line 29

def sample(sample_event, &block)
  name = sample_event.is_a?(String) ? sample_event : LogStash::Json.dump(sample_event)
  name = name[0..50] + "..." if name.length > 50

  describe "\"#{name}\"" do
    let(:pipeline) { LogStash::Pipeline.new(config) }
    let(:event) do
      sample_event = [sample_event] unless sample_event.is_a?(Array)
      next sample_event.collect do |e|
        e = { "message" => e } if e.is_a?(String)
        next LogStash::Event.new(e)
      end
    end

    let(:results) do
      results = []
      pipeline.instance_eval { @filters.each(&:register) }

      event.each do |e|
        # filter call the block on all filtered events, included new events added by the filter
        pipeline.filter(e) { |filtered_event| results << filtered_event }
      end

      # flush makes sure to empty any buffered events in the filter
      pipeline.flush_filters(:final => true) { |flushed_event| results << flushed_event }

      results.select { |e| !e.cancelled? }
    end

    subject { results.length > 1 ? results : results.first }

    it("when processed", &block)
  end
end

#tags(*tags) ⇒ Object



24
25
26
27
# File 'lib/logstash/devutils/rspec/logstash_helpers.rb', line 24

def tags(*tags)
  let(:default_tags) { tags }
  puts "Setting default tags: #{tags}"
end

#try(number_of_try = DEFAULT_NUMBER_OF_TRY, &block) ⇒ Object



12
13
14
# File 'lib/logstash/devutils/rspec/logstash_helpers.rb', line 12

def try(number_of_try = DEFAULT_NUMBER_OF_TRY, &block)
  Stud.try(number_of_try.times, DEFAULT_EXCEPTIONS_FOR_TRY, &block)
end

#type(default_type) ⇒ Object

def config



20
21
22
# File 'lib/logstash/devutils/rspec/logstash_helpers.rb', line 20

def type(default_type)
  let(:default_type) { default_type }
end