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
- #agent(&block) ⇒ Object
- #config(configstr) ⇒ Object
-
#input(config, &block) ⇒ Object
def sample.
-
#new_pipeline_from_string(string) ⇒ Object
def agent.
-
#plugin_input(plugin, &block) ⇒ Object
def input.
- #sample(sample_event, &block) ⇒ Object
- #tags(*tags) ⇒ Object
- #try(number_of_try = DEFAULT_NUMBER_OF_TRY, &block) ⇒ Object
-
#type(default_type) ⇒ Object
def config.
Instance Method Details
#agent(&block) ⇒ Object
112 113 114 115 116 117 118 119 |
# File 'lib/logstash/devutils/rspec/logstash_helpers.rb', line 112 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 |
# File 'lib/logstash/devutils/rspec/logstash_helpers.rb', line 74 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 |
#new_pipeline_from_string(string) ⇒ Object
def agent
121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/logstash/devutils/rspec/logstash_helpers.rb', line 121 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
99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/logstash/devutils/rspec/logstash_helpers.rb', line 99 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 (*) let(:default_tags) { } puts "Setting default 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 |