test/plugin/base_test.rb in fluent-plugin-google-cloud-0.7.5 vs test/plugin/base_test.rb in fluent-plugin-google-cloud-0.7.6

- old
+ new

@@ -534,10 +534,82 @@ assert !fields.key?('data'), 'Did not expect data' assert !fields.key?('some_null_field'), 'Did not expect some_null_field' end end + # TODO(qingling128): Fix the inconsistent behavior of 'message', 'log' and + # 'msg' in the next major version 1.0.0. + def test_structured_payload_json_log_detect_json_with_hash_input + hash_value = { + 'msg' => 'test log entry 0', + 'tag2' => 'test', + 'data' => 5000, + 'some_null_field' => nil + } + [ + { + config: APPLICATION_DEFAULT_CONFIG, + field_name: 'log', + expected_payload: 'jsonPayload' + }, + { + config: APPLICATION_DEFAULT_CONFIG, + field_name: 'msg', + expected_payload: 'jsonPayload' + }, + { + config: APPLICATION_DEFAULT_CONFIG, + field_name: 'message', + expected_payload: 'textPayload' + }, + { + config: DETECT_JSON_CONFIG, + field_name: 'log', + expected_payload: 'jsonPayload' + }, + { + config: DETECT_JSON_CONFIG, + field_name: 'msg', + expected_payload: 'jsonPayload' + }, + { + config: DETECT_JSON_CONFIG, + field_name: 'message', + expected_payload: 'textPayload' + } + ].each do |test_params| + new_stub_context do + setup_gce_metadata_stubs + setup_logging_stubs do + d = create_driver(test_params[:config]) + d.emit(test_params[:field_name] => hash_value) + d.run + end + if test_params[:expected_payload] == 'textPayload' + verify_log_entries(1, COMPUTE_PARAMS, 'textPayload') do |entry| + text_payload = entry['textPayload'] + assert_equal '{"msg"=>"test log entry 0", "tag2"=>"test", ' \ + '"data"=>5000, "some_null_field"=>nil}', + text_payload, entry + end + else + verify_log_entries(1, COMPUTE_PARAMS, 'jsonPayload') do |entry| + json_payload = get_fields(entry['jsonPayload']) + assert_equal 1, json_payload.size, entry + fields = get_fields( + get_struct(json_payload[test_params[:field_name]])) + assert_equal 4, fields.size, entry + assert_equal 'test log entry 0', get_string(fields['msg']), entry + assert_equal 'test', get_string(fields['tag2']), entry + assert_equal 5000, get_number(fields['data']), entry + assert_equal null_value, fields['some_null_field'], entry + end + end + end + end + end + def test_structured_payload_json_log_detect_json_parsed setup_gce_metadata_stubs json_string = '{"msg": "test log entry 0", "tag2": "test", ' \ '"data": 5000, "some_null_field": null}' setup_logging_stubs do @@ -1440,10 +1512,19 @@ custom_key: 'custom_trace_key', custom_key_config: CONFIG_CUSTOM_TRACE_KEY_SPECIFIED, sample_value: TRACE) end + def test_log_entry_trace_sampled_field + verify_field_key('traceSampled', + default_key: DEFAULT_TRACE_SAMPLED_KEY, + custom_key: 'custom_trace_sampled_key', + custom_key_config: + CONFIG_CUSTOM_TRACE_SAMPLED_KEY_SPECIFIED, + sample_value: TRACE_SAMPLED) + end + # Verify the cascading JSON detection of LogEntry fields. def test_cascading_json_detection_with_log_entry_insert_id_field verify_cascading_json_detection_with_log_entry_fields( 'insertId', DEFAULT_INSERT_ID_KEY, @@ -1488,10 +1569,19 @@ 'trace', DEFAULT_TRACE_KEY, root_level_value: TRACE, nested_level_value: TRACE2) end + def test_cascading_json_detection_with_log_entry_trace_sampled_field + verify_cascading_json_detection_with_log_entry_fields( + 'traceSampled', DEFAULT_TRACE_SAMPLED_KEY, + root_level_value: TRACE_SAMPLED, + nested_level_value: TRACE_SAMPLED2, + default_value_from_root: false, + default_value_from_nested: false) + end + # Verify that labels present in multiple inputs respect the expected priority # order: # 1. Labels from the field "logging.googleapis.com/labels" in payload. # 2. Labels from the config "label_map". # 3. Labels from the config "labels". @@ -2504,10 +2594,14 @@ nested_level_value = expectation[:nested_level_value] expected_value_from_root = expectation.fetch( :expected_value_from_root, root_level_value) expected_value_from_nested = expectation.fetch( :expected_value_from_nested, nested_level_value) + default_value_from_root = expectation.fetch( + :default_value_from_root, nil) + default_value_from_nested = expectation.fetch( + :default_value_from_nested, nil) setup_gce_metadata_stubs # { # "logging.googleapis.com/XXX' => 'sample value' @@ -2541,24 +2635,37 @@ # } # } log_entry_with_both_level_fields = log_entry_with_nested_level_field.merge( default_key => root_level_value) - { - log_entry_with_root_level_field => expected_value_from_root, - log_entry_with_nested_level_field => expected_value_from_nested, - log_entry_with_both_level_fields => expected_value_from_nested - }.each_with_index do |(input_log_entry, expected_value), index| + [ + [ + log_entry_with_root_level_field, + expected_value_from_root, + default_value_from_root + ], + [ + log_entry_with_nested_level_field, + expected_value_from_nested, + default_value_from_nested + ], + [ + log_entry_with_both_level_fields, + expected_value_from_nested, + default_value_from_nested + ] + ].each_with_index do |(log_entry, expected_value, default_value), index| setup_logging_stubs do @logs_sent = [] d = create_driver(DETECT_JSON_CONFIG) - d.emit(input_log_entry) + d.emit(log_entry) d.run end verify_log_entries(1, COMPUTE_PARAMS, 'jsonPayload', false) do |entry| - assert_equal expected_value, entry[log_entry_field], - "Index #{index} failed. #{expected_value} is expected" \ - " for #{log_entry_field} field." + assert_equal_with_default \ + entry[log_entry_field], expected_value, default_value, + "Index #{index} failed. #{expected_value} is expected for " \ + "#{log_entry_field} field." payload_fields = get_fields(entry['jsonPayload']) assert_equal structured_log_entry.size, payload_fields.size payload_fields.each do |key, value| assert_equal structured_log_entry[key], get_string(value) end