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



116
117
118
119
120
121
122
123
# File 'lib/logstash/devutils/rspec/logstash_helpers.rb', line 116

def agent(&block)

  it("agent(#{caller[0].gsub(/ .*/, "")}) runs") do
    pipeline = new_pipeline_from_string(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



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/logstash/devutils/rspec/logstash_helpers.rb', line 74

def input(config, &block)
  pipeline = new_pipeline_from_string(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)
      # We want to return nil or [] since outputs aren't used here
      # NOTE: In Ruby 1.9.x, Queue#<< returned nil, but in 2.x it returns the queue itself
      # So we need to be explicit about the return
      []
    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

#new_pipeline_from_string(string) ⇒ Object

def agent



125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/logstash/devutils/rspec/logstash_helpers.rb', line 125

def new_pipeline_from_string(string)
  if TestPipeline.instance_methods.include?(:pipeline_config)
    settings = ::LogStash::SETTINGS.clone

    config_part = org.logstash.common.SourceWithMetadata.new("config_string", "config_string", string)

    pipeline_config = LogStash::Config::PipelineConfig.new(LogStash::Config::Source::Local, :main, config_part, settings)
    TestPipeline.new(pipeline_config)
  else
    TestPipeline.new(string)
  end
end

#plugin_input(plugin, &block) ⇒ Object

def input



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

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
68
69
70
71
72
# 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) { new_pipeline_from_string(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

    # starting at logstash-core 5.3 an initialized pipeline need to be closed
    after do
      pipeline.close if pipeline.respond_to?(:close)
    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