Skip to content

Commit 2641a5d

Browse files
committed
Add tests
Signed-off-by: Shizuo Fujita <fujita@clear-code.com>
1 parent 1970ed5 commit 2641a5d

1 file changed

Lines changed: 117 additions & 0 deletions

File tree

test/test_out_s3.rb

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,123 @@ def test_write_with_zstd
495495
FileUtils.rm_f(s3_local_file_path)
496496
end
497497

498+
def test_no_compress_by_gzip_with_compressed_buffer
499+
setup_mocks(true)
500+
s3_local_file_path = "/tmp/s3-test.gz"
501+
setup_s3_object_mocks(s3_local_file_path: s3_local_file_path)
502+
503+
d = create_time_sliced_driver(CONFIG_TIME_SLICE + <<~EOF)
504+
<buffer tag,time>
505+
@type memory
506+
compress gzip
507+
timekey 3600
508+
timekey_use_utc true
509+
</buffer>
510+
EOF
511+
512+
# GzipCompressor should not use Zlib::GzipWriter
513+
dont_allow(Zlib::GzipWriter).new
514+
515+
time = event_time("2011-01-02 13:14:15 UTC")
516+
d.run(default_tag: "test") do
517+
d.feed(time, {"a"=>1})
518+
d.feed(time, {"a"=>2})
519+
end
520+
521+
data = ""
522+
File.open(s3_local_file_path, "rb") do |f|
523+
until f.eof?
524+
gz = Zlib::GzipReader.new(f)
525+
data << gz.read
526+
527+
unused = gz.unused
528+
gz.finish
529+
f.pos -= unused.length if unused
530+
end
531+
end
532+
assert_equal %[2011-01-02T13:14:15Z\ttest\t{"a":1}\n] +
533+
%[2011-01-02T13:14:15Z\ttest\t{"a":2}\n],
534+
data
535+
FileUtils.rm_f(s3_local_file_path)
536+
end
537+
538+
def test_no_compress_by_gzip_command_with_compressed_buffer
539+
setup_mocks(true)
540+
s3_local_file_path = "/tmp/s3-test.gz"
541+
setup_s3_object_mocks(s3_local_file_path: s3_local_file_path)
542+
543+
d = create_time_sliced_driver(CONFIG_TIME_SLICE + <<~EOF)
544+
store_as gzip_command
545+
<buffer tag,time>
546+
@type memory
547+
compress gzip
548+
timekey 3600
549+
timekey_use_utc true
550+
</buffer>
551+
EOF
552+
553+
# GzipCommandCompressor should not use Kernel.system and Zlib::GzipWriter
554+
dont_allow(Kernel).system
555+
dont_allow(Zlib::GzipWriter).new
556+
557+
time = event_time("2011-01-02 13:14:15 UTC")
558+
d.run(default_tag: "test") do
559+
d.feed(time, {"a"=>1})
560+
d.feed(time, {"a"=>2})
561+
end
562+
563+
data = ""
564+
File.open(s3_local_file_path, "rb") do |f|
565+
until f.eof?
566+
gz = Zlib::GzipReader.new(f)
567+
data << gz.read
568+
569+
unused = gz.unused
570+
gz.finish
571+
f.pos -= unused.length if unused
572+
end
573+
end
574+
assert_equal %[2011-01-02T13:14:15Z\ttest\t{"a":1}\n] +
575+
%[2011-01-02T13:14:15Z\ttest\t{"a":2}\n],
576+
data
577+
FileUtils.rm_f(s3_local_file_path)
578+
end
579+
580+
def test_no_compress_by_zstd_with_compressed_buffer
581+
setup_mocks(true)
582+
s3_local_file_path = "/tmp/s3-test.zst"
583+
expected_s3path = "log/events/ts=20110102-13/events_0-#{Socket.gethostname}.zst"
584+
setup_s3_object_mocks(s3_local_file_path: s3_local_file_path, s3path: expected_s3path)
585+
586+
d = create_time_sliced_driver(CONFIG_TIME_SLICE + <<~EOF)
587+
store_as zstd
588+
<buffer tag,time>
589+
@type memory
590+
compress zstd
591+
timekey 3600
592+
timekey_use_utc true
593+
</buffer>
594+
EOF
595+
596+
# ZstdCompressor should not use Zstd.compress
597+
dont_allow(Zstd).compress
598+
599+
time = event_time("2011-01-02 13:14:15 UTC")
600+
d.run(default_tag: "test") do
601+
d.feed(time, {"a"=>1})
602+
d.feed(time, {"a"=>2})
603+
end
604+
605+
File.open(s3_local_file_path, 'rb') do |file|
606+
compressed_data = file.read
607+
uncompressed_data = Zstd.decompress(compressed_data)
608+
expected_data = %[2011-01-02T13:14:15Z\ttest\t{"a":1}\n] +
609+
%[2011-01-02T13:14:15Z\ttest\t{"a":2}\n]
610+
assert_equal expected_data, uncompressed_data
611+
end
612+
FileUtils.rm_f(s3_local_file_path)
613+
end
614+
498615
class MockResponse
499616
attr_reader :data
500617

0 commit comments

Comments
 (0)