logstash/logstash-core/spec/support/helpers.rb
Andrew Cholakian f5c6c5a4b1 Use file hashing where possible in LIR
This commit further improves hashing performance, using SourceWithMetadata
objects where present to determine Vertex IDs and the hash of the source
file. If those objects are not present LIR will revert to graph hashing,
which is slower, but always works. This is still useful for things like
testing, so it makes sense to leave that code in.

In the future it might be nice to extract the hashing code using the
strategy pattern to clean things up.

This commit also improves the Hashable interface, moving hashSource into
its own interface, letting classes that do their own hashing simply
implement uniqueHash instead of requiring them to also implement
hashSource.

A requirement of these changes was also that SourceWithMetadata no
longer accept null or empty arguments. This patch now enforces that
requirement. This also is nicer from an API standpoint, as you now know
that if a SourceWithMetadata object exists, it will actually have all
its fields in use.

Fixes #7408
2017-06-13 14:09:17 +00:00

106 lines
3 KiB
Ruby

# encoding: utf-8
require "stud/task"
def silence_warnings
warn_level = $VERBOSE
$VERBOSE = nil
yield
ensure
$VERBOSE = warn_level
end
def clear_data_dir
if defined?(agent_settings)
data_path = agent_settings.get("path.data")
else
data_path = LogStash::SETTINGS.get("path.data")
end
Dir.foreach(data_path) do |f|
next if f == "." || f == ".." || f == ".gitkeep"
FileUtils.rm_rf(File.join(data_path, f))
end
end
def mock_settings(settings_values={})
settings = LogStash::SETTINGS.clone
settings_values.each do |key, value|
settings.set(key, value)
end
settings
end
def make_test_agent(settings=mock_settings)
sl = LogStash::Config::SourceLoader.new
sl.add_source(LogStash::Config::Source::Local.new(settings))
sl
::LogStash::Agent.new(settings, sl)
end
def mock_pipeline(pipeline_id, reloadable = true, config_hash = nil)
config_string = "input { stdin { id => '#{pipeline_id}' }}"
settings = mock_settings("pipeline.id" => pipeline_id.to_s,
"config.string" => config_string,
"config.reload.automatic" => reloadable)
pipeline_config = mock_pipeline_config(pipeline_id, config_string, settings)
LogStash::Pipeline.new(pipeline_config)
end
def mock_pipeline_from_string(config_string, settings = LogStash::SETTINGS, metric = nil)
pipeline_config = mock_pipeline_config(settings.get("pipeline.id"), config_string, settings)
LogStash::Pipeline.new(pipeline_config, metric)
end
def mock_pipeline_config(pipeline_id, config_string = nil, settings = {})
config_string = "input { stdin { id => '#{pipeline_id}' }}" if config_string.nil?
# This is for older tests when we already have a config
unless settings.is_a?(LogStash::Settings)
settings.merge!({ "pipeline.id" => pipeline_id.to_s })
settings = mock_settings(settings)
end
config_part = org.logstash.common.SourceWithMetadata.new("config_string", "config_string", 0, 0, config_string)
LogStash::Config::PipelineConfig.new(LogStash::Config::Source::Local, pipeline_id, config_part, settings)
end
def start_agent(agent)
agent_task = Stud::Task.new do
begin
agent.execute
rescue => e
raise "Start Agent exception: #{e}"
end
end
sleep(0.1) unless subject.running?
agent_task
end
def temporary_file(content, file_name = Time.now.to_i.to_s, directory = Stud::Temporary.pathname)
FileUtils.mkdir_p(directory)
target = ::File.join(directory, file_name)
File.open(target, "w+") do |f|
f.write(content)
end
target
end
RSpec::Matchers.define :ir_eql do |expected|
match do |actual|
next unless expected.java_kind_of?(org.logstash.config.ir.SourceComponent) && actual.java_kind_of?(org.logstash.config.ir.SourceComponent)
expected.sourceComponentEquals(actual)
end
failure_message do |actual|
"actual value \n#{actual.to_s}\nis not .sourceComponentEquals to the expected value: \n#{expected.to_s}\n"
end
end
SUPPORT_DIR = Pathname.new(::File.join(::File.dirname(__FILE__), "support"))