Skip to content

Commit

Permalink
fluent-cat: add basic cat test case
Browse files Browse the repository at this point in the history
Signed-off-by: Kentaro Hayashi <hayashi@clear-code.com>
  • Loading branch information
kenhys committed May 12, 2021
1 parent 7a1abc9 commit b15409a
Showing 1 changed file with 99 additions and 0 deletions.
99 changes: 99 additions & 0 deletions test/command/test_cat.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
require_relative '../helper'

require 'test-unit'
require 'fluent/command/cat'
require 'fluent/plugin/output'
require 'fluent/plugin/in_forward'
require 'fluent/plugin/out_secondary_file'
require 'fluent/test/driver/output'
require 'fluent/test/driver/input'

class TestFluentCat < ::Test::Unit::TestCase
def setup
Fluent::Test.setup
FileUtils.mkdir_p(TMP_DIR)
@record = { 'key' => 'value' }
@time = event_time
@es = Fluent::OneEventStream.new(@time, @record)
@primary = create_primary
metadata = @primary.buffer.new_metadata
@chunk = create_chunk(@primary, metadata, @es)
end

def teardown
FileUtils.rm_rf(TMP_DIR)
end

TMP_DIR = File.expand_path(File.dirname(__FILE__) + "/../tmp/command/fluent_cat#{ENV['TEST_ENV_NUMBER']}")

PORT = unused_port
CONFIG = %[
port #{PORT}
bind 127.0.0.1
]

SECONDARY_CONFIG = %[
directory #{TMP_DIR}
]

class DummyOutput < Fluent::Plugin::Output
def write(chunk); end
end

def create_driver(conf=CONFIG)
Fluent::Test::Driver::Input.new(Fluent::Plugin::ForwardInput).configure(conf)
end

def create_primary(buffer_cofig = config_element('buffer'))
DummyOutput.new.configure(config_element('ROOT','',{}, [buffer_cofig]))
end

def create_secondary_driver(conf=SECONDARY_CONFIG)
c = Fluent::Test::Driver::Output.new(Fluent::Plugin::SecondaryFileOutput)
c.instance.acts_as_secondary(@primary)
c.configure(conf)
end

def create_chunk(primary, metadata, es)
primary.buffer.generate_chunk(metadata).tap do |c|
c.concat(es.to_msgpack_stream, es.size)
c.commit
end
end

sub_test_case "json" do
def test_cat_secondary_file
d = create_driver
assert_raise Fluent::Test::Driver::TestTimedOut do
d.run(timeout: 0.5) do
r, w = IO.pipe
$stdin = r
w.puts(%({"key":"value"}))
Fluent::Cat.new(["--port", "#{PORT}", "dummy"]).call
end
end
event = d.events.first
assert_equal([1, "dummy", @record],
[d.events.size, event.first, event.last])
end
end

sub_test_case "msgpack" do
def test_cat_secondary_file
d = create_secondary_driver
path = d.instance.write(@chunk)
d = create_driver
assert_raise Fluent::Test::Driver::TestTimedOut do
d.run(timeout: 0.5) do
r, w = IO.pipe
$stdin = r
w.write(File.read(path))
Fluent::Cat.new(["--format", "msgpack", "--port", "#{PORT}", "dummy"]).call
end
end
event = d.events.first
assert_equal([1, "dummy", @record],
[d.events.size, event.first, event.last])
end
end
end

0 comments on commit b15409a

Please sign in to comment.