Skip to content

Commit

Permalink
impl fuzzy_assert since we didn't interested in the value of metrics
Browse files Browse the repository at this point in the history
Signed-off-by: Yuta Iwama <[email protected]>
  • Loading branch information
ganmacs committed Jun 21, 2019
1 parent 5edaca8 commit 11105cf
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 77 deletions.
1 change: 1 addition & 0 deletions test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def to_masked_element
require 'fluent/msgpack_factory'
require 'fluent/time'
require 'serverengine'
require 'helpers/fuzzy_assert'

module Fluent
module Plugin
Expand Down
89 changes: 89 additions & 0 deletions test/helpers/fuzzy_assert.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
require 'test/unit'

class FuzzyIncludeAssertion
include Test::Unit::Assertions

def self.assert(expected, actual, message = nil)
new(expected, actual, message).assert
end

def initialize(expected, actual, message)
@expected = expected
@actual = actual
@message = message
end

def assert
if collection?
assert_same_collection
else
assert_same_value
end
end

private

def assert_same_value
m = "expected(#{@expected}) !== actual(#{@actual.inspect})"
if @message
m = "#{@message}: #{m}"
end
assert_true(@expected === @actual, m)
end

def assert_same_class
if @expected.class != @actual.class
if (@expected.class.ancestors | @actual.class.ancestors).empty?
assert_equal(@expected.class, @actual.class, @message)
end
end
end

def assert_same_collection
assert_same_class
assert_same_values
end

def assert_same_values
if @expected.is_a?(Array)
@expected.each_with_index do |val, i|
self.class.assert(val, @actual[i], @message)
end
else
@expected.each do |key, val|
self.class.assert(val, @actual[key], "#{key}: ")
end
end
end

def collection?
@actual.is_a?(Array) || @actual.is_a?(Hash)
end
end

class FuzzyAssertion < FuzzyIncludeAssertion
private

def assert_same_collection
super
assert_same_keys
end

def assert_same_keys
if @expected.is_a?(Array)
assert_equal(@expected.size, @actual.size, "expected.size(#{@expected}) != actual.size(#{@expected})")
else
assert_equal(@expected.keys.sort, @actual.keys.sort)
end
end
end

module FuzzyAssert
def assert_fuzzy_include(left, right, message = nil)
FuzzyIncludeAssertion.new(left, right, message).assert
end

def assert_fuzzy_equal(left, right, message = nil)
FuzzyAssertion.new(left, right, message).assert
end
end
156 changes: 79 additions & 77 deletions test/plugin/test_in_monitor_agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
require_relative '../test_plugin_classes'

class MonitorAgentInputTest < Test::Unit::TestCase
include FuzzyAssert

def setup
Fluent::Test.setup
end
Expand Down Expand Up @@ -119,12 +121,12 @@ def test_configure
"plugin_id" => "test_out",
"retry_count" => 0,
"type" => "test_out",
"emit_count" => 0,
"emit_records" => 0,
"write_count" => 0,
"rollback_count" => 0,
"slow_flush_count" => 0,
"flush_time_count" => 0,
"emit_count" => Integer,
"emit_records" => Integer,
"write_count" => Integer,
"rollback_count" => Integer,
"slow_flush_count" => Integer,
"flush_time_count" => Integer,
}
output_info.merge!("config" => {"@id" => "test_out", "@type" => "test_out"}) if with_config
error_label_info = {
Expand All @@ -136,23 +138,23 @@ def test_configure
"plugin_id" => "null",
"retry_count" => 0,
"type" => "null",
"buffer_available_buffer_space_ratios" => 100.0,
"buffer_queue_byte_size" => 0,
"buffer_stage_byte_size" => 0,
"buffer_stage_length" => 0,
"emit_count" => 0,
"emit_records" => 0,
"write_count" => 0,
"rollback_count" => 0,
"slow_flush_count" => 0,
"flush_time_count" => 0,
"buffer_available_buffer_space_ratios" => Float,
"buffer_queue_byte_size" => Integer,
"buffer_stage_byte_size" => Integer,
"buffer_stage_length" => Integer,
"emit_count" => Integer,
"emit_records" => Integer,
"write_count" => Integer,
"rollback_count" => Integer,
"slow_flush_count" => Integer,
"flush_time_count" => Integer,
}
error_label_info.merge!("config" => {"@id"=>"null", "@type" => "null"}) if with_config
opts = {with_config: with_config}
assert_equal(input_info, d.instance.get_monitor_info(@ra.inputs.first, opts))
assert_equal(filter_info, d.instance.get_monitor_info(@ra.filters.first, opts))
assert_equal(output_info, d.instance.get_monitor_info(test_label.outputs.first, opts))
assert_equal(error_label_info, d.instance.get_monitor_info(error_label.outputs.first, opts))
assert_fuzzy_equal(filter_info, d.instance.get_monitor_info(@ra.filters.first, opts))
assert_fuzzy_equal(output_info, d.instance.get_monitor_info(test_label.outputs.first, opts))
assert_fuzzy_equal(error_label_info, d.instance.get_monitor_info(error_label.outputs.first, opts))
end

test "fluentd opts" do
Expand Down Expand Up @@ -203,28 +205,28 @@ def test_configure
"type" => "relabel",
"output_plugin" => true,
"retry_count" => 0,
"emit_count" => 0,
"emit_records" => 0,
"write_count" => 0,
"rollback_count" => 0,
"slow_flush_count" => 0,
"flush_time_count" => 0,
"emit_count" => Integer,
"emit_records" => Integer,
"write_count" => Integer,
"rollback_count" => Integer,
"slow_flush_count" => Integer,
"flush_time_count" => Integer,
}
expect_test_out_record = {
"plugin_id" => "test_out",
"plugin_category" => "output",
"type" => "test_out",
"output_plugin" => true,
"retry_count" => 0,
"emit_count" => 0,
"emit_records" => 0,
"write_count" => 0,
"rollback_count" => 0,
"slow_flush_count" => 0,
"flush_time_count" => 0,
"emit_count" => Integer,
"emit_records" => Integer,
"write_count" => Integer,
"rollback_count" => Integer,
"slow_flush_count" => Integer,
"flush_time_count" => Integer,
}
assert_equal(expect_relabel_record, d.events[1][2])
assert_equal(expect_test_out_record, d.events[3][2])
assert_fuzzy_equal(expect_relabel_record, d.events[1][2])
assert_fuzzy_equal(expect_test_out_record, d.events[3][2])
end
end

Expand Down Expand Up @@ -333,24 +335,24 @@ def get(uri, header = {})
"plugin_id" => "null",
"retry_count" => 0,
"type" => "null",
"buffer_available_buffer_space_ratios" => 100.0,
"buffer_queue_byte_size" => 0,
"buffer_stage_byte_size" => 0,
"buffer_stage_length" => 0,
"emit_count" => 0,
"emit_records" => 0,
"write_count" => 0,
"rollback_count" => 0,
"slow_flush_count" => 0,
"flush_time_count" => 0,
"buffer_available_buffer_space_ratios" => Float,
"buffer_queue_byte_size" => Integer,
"buffer_stage_byte_size" => Integer,
"buffer_stage_length" => Integer,
"emit_count" => Integer,
"emit_records" => Integer,
"write_count" => Integer,
"rollback_count" => Integer,
"slow_flush_count" => Integer,
"flush_time_count" => Integer,
}
expected_null_response.merge!("config" => {"@id" => "null", "@type" => "null"}) if with_config
expected_null_response.merge!("retry" => {}) if with_retry
response = JSON.parse(get("http://127.0.0.1:#{@port}/api/plugins.json").body)
test_in_response = response["plugins"][0]
null_response = response["plugins"][5]
assert_equal(expected_test_in_response, test_in_response)
assert_equal(expected_null_response, null_response)
assert_fuzzy_equal(expected_null_response, null_response)
end

test "/api/plugins.json/not_found" do
Expand Down Expand Up @@ -395,24 +397,24 @@ def get(uri, header = {})
"plugin_id" => "null",
"retry_count" => 0,
"type" => "null",
"buffer_available_buffer_space_ratios" => 100.0,
"buffer_queue_byte_size" => 0,
"buffer_stage_byte_size" => 0,
"buffer_stage_length" => 0,
"emit_count" => 0,
"emit_records" => 0,
"write_count" => 0,
"rollback_count" => 0,
"slow_flush_count" => 0,
"flush_time_count" => 0,
"buffer_available_buffer_space_ratios" => Float,
"buffer_queue_byte_size" => Integer,
"buffer_stage_byte_size" => Integer,
"buffer_stage_length" => Integer,
"emit_count" => Integer,
"emit_records" => Integer,
"write_count" => Integer,
"rollback_count" => Integer,
"slow_flush_count" => Integer,
"flush_time_count" => Integer,
}
expected_null_response.merge!("config" => {"@id" => "null", "@type" => "null"}) if with_config
expected_null_response.merge!("retry" => {}) if with_retry
response = JSON.parse(get("http://127.0.0.1:#{@port}/api/plugins.json#{query_param}").body)
test_in_response = response["plugins"][0]
null_response = response["plugins"][5]
assert_equal(expected_test_in_response, test_in_response)
assert_equal(expected_null_response, null_response)
assert_fuzzy_include(expected_null_response, null_response)
end

test "/api/plugins.json with 'with_ivars'. response contains specified instance variables of each plugin" do
Expand Down Expand Up @@ -441,22 +443,22 @@ def get(uri, header = {})
"retry_count" => 0,
"type" => "null",
"instance_variables" => {"id" => "null", "num_errors" => 0},
"buffer_available_buffer_space_ratios" => 100.0,
"buffer_queue_byte_size" => 0,
"buffer_stage_byte_size" => 0,
"buffer_stage_length" => 0,
"emit_count" => 0,
"emit_records" => 0,
"write_count" => 0,
"rollback_count" => 0,
"slow_flush_count" => 0,
"flush_time_count" => 0,
"buffer_available_buffer_space_ratios" => Float,
"buffer_queue_byte_size" => Integer,
"buffer_stage_byte_size" => Integer,
"buffer_stage_length" => Integer,
"emit_count" => Integer,
"emit_records" => Integer,
"write_count" => Integer,
"rollback_count" => Integer,
"slow_flush_count" => Integer,
"flush_time_count" => Integer,
}
response = JSON.parse(get("http://127.0.0.1:#{@port}/api/plugins.json?with_config=no&with_retry=no&with_ivars=id,num_errors").body)
test_in_response = response["plugins"][0]
null_response = response["plugins"][5]
assert_equal(expected_test_in_response, test_in_response)
assert_equal(expected_null_response, null_response)
assert_fuzzy_equal(expected_null_response, null_response)
end

test "/api/config" do
Expand Down Expand Up @@ -571,16 +573,16 @@ def write(chunk)
"type" => "test_out_fail_write",
"buffer_newest_timekey" => output.calculate_timekey(event_time),
"buffer_oldest_timekey" => output.calculate_timekey(event_time),
"buffer_available_buffer_space_ratios" => 100.0,
"buffer_queue_byte_size" => 40,
"buffer_stage_byte_size" => 0,
"buffer_stage_length" => 0,
"emit_count" => 1,
"emit_records" => 1,
"write_count" => 2,
"rollback_count" => 0,
'slow_flush_count' => 0,
'flush_time_count' => 0,
"buffer_available_buffer_space_ratios" => Float,
"buffer_queue_byte_size" => Integer,
"buffer_stage_byte_size" => Integer,
"buffer_stage_length" => Integer,
"emit_count" => Integer,
"emit_records" => Integer,
"write_count" => Integer,
"rollback_count" => Integer,
'slow_flush_count' => Integer,
'flush_time_count' => Integer,
}
output.emit_events('test.tag', Fluent::ArrayEventStream.new([[event_time, {"message" => "test failed flush 1"}]]))
# flush few times to check steps
Expand All @@ -596,7 +598,7 @@ def write(chunk)
# remove dynamic keys
response_retry_count = test_out_fail_write_response.delete("retry_count")
response_retry = test_out_fail_write_response.delete("retry")
assert_equal(expected_test_out_fail_write_response, test_out_fail_write_response)
assert_fuzzy_equal(expected_test_out_fail_write_response, test_out_fail_write_response)
assert{ response_retry.has_key?("steps") }
# it's very hard to check exact retry count (because retries are called by output flush thread scheduling)
assert{ response_retry_count >= 1 && response_retry["steps"] >= 0 }
Expand Down

0 comments on commit 11105cf

Please sign in to comment.