Module: LogStashHelper

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

Defined Under Namespace

Classes: TestPipeline

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



107
108
109
110
111
112
113
114
# File 'lib/logstash/devutils/rspec/logstash_helpers.rb', line 107

def agent(&block)

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

#config(configstr) ⇒ Object



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

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

#input(config, &block) ⇒ Object

def sample



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/logstash/devutils/rspec/logstash_helpers.rb', line 69

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



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/logstash/devutils/rspec/logstash_helpers.rb', line 94

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



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
63
64
65
66
67
# File 'lib/logstash/devutils/rspec/logstash_helpers.rb', line 34

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) { TestPipeline.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



29
30
31
32
# File 'lib/logstash/devutils/rspec/logstash_helpers.rb', line 29

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

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



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

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



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

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