test/plugin/base_test.rb in fluent-plugin-google-cloud-0.6.13 vs test/plugin/base_test.rb in fluent-plugin-google-cloud-0.6.14
- old
+ new
@@ -60,10 +60,21 @@
exception_count += 1
end
assert_equal 1, exception_count
end
+ def test_configure_logging_api_url
+ setup_gce_metadata_stubs
+ {
+ APPLICATION_DEFAULT_CONFIG => DEFAULT_LOGGING_API_URL,
+ CUSTOM_LOGGING_API_URL_CONFIG => CUSTOM_LOGGING_API_URL
+ }.each do |(config, url)|
+ d = create_driver(config)
+ assert_equal url, d.instance.instance_variable_get(:@logging_api_url)
+ end
+ end
+
def test_configure_custom_metadata
setup_no_metadata_service_stubs
d = create_driver(CUSTOM_METADATA_CONFIG)
assert_equal CUSTOM_PROJECT_ID, d.instance.project_id
assert_equal CUSTOM_ZONE, d.instance.zone
@@ -161,11 +172,13 @@
CONFIG_MISSING_METADATA_PROJECT_ID =>
['gce', PROJECT_ID, CUSTOM_ZONE, CUSTOM_VM_ID],
CONFIG_EC2_PROJECT_ID =>
['ec2', EC2_PROJECT_ID, EC2_PREFIXED_ZONE, EC2_VM_ID],
CONFIG_EC2_PROJECT_ID_AND_CUSTOM_VM_ID =>
- ['ec2', EC2_PROJECT_ID, EC2_PREFIXED_ZONE, CUSTOM_VM_ID]
+ ['ec2', EC2_PROJECT_ID, EC2_PREFIXED_ZONE, CUSTOM_VM_ID],
+ CONFIG_EC2_PROJECT_ID_USE_REGION =>
+ ['ec2', EC2_PROJECT_ID, EC2_PREFIXED_REGION, EC2_VM_ID]
}.each_with_index do |(config, parts), index|
send("setup_#{parts[0]}_metadata_stubs")
d = create_driver(config)
d.run
assert_equal parts[1], d.instance.project_id, "Index #{index} failed."
@@ -255,13 +268,24 @@
setup_logging_stubs do
d = create_driver(CONFIG_EC2_PROJECT_ID)
d.emit('message' => log_entry(0))
d.run
end
- verify_log_entries(1, EC2_PARAMS)
+ verify_log_entries(1, EC2_ZONE_PARAMS)
end
+ def test_one_log_ec2_region
+ ENV['GOOGLE_APPLICATION_CREDENTIALS'] = IAM_CREDENTIALS[:path]
+ setup_ec2_metadata_stubs
+ setup_logging_stubs do
+ d = create_driver(CONFIG_EC2_PROJECT_ID_USE_REGION)
+ d.emit('message' => log_entry(0))
+ d.run
+ end
+ verify_log_entries(1, EC2_REGION_PARAMS)
+ end
+
def test_structured_payload_log
setup_gce_metadata_stubs
setup_logging_stubs do
d = create_driver
d.emit('msg' => log_entry(0), 'tag2' => 'test', 'data' => 5000,
@@ -1152,59 +1176,18 @@
end
end
end
def test_log_entry_trace_field
- setup_gce_metadata_stubs
- message = log_entry(0)
- trace = 'projects/project-1/traces/1234567890abcdef1234567890abcdef'
- [
- {
- # It leaves trace entry field nil if no trace value sent.
- driver_config: APPLICATION_DEFAULT_CONFIG,
- emitted_log: { 'msg' => message },
- expected_fields: { 'msg' => message },
- expected_trace_value: nil
- },
- {
- # By default, it sets trace via Google-specific key.
- driver_config: APPLICATION_DEFAULT_CONFIG,
- emitted_log: { 'msg' => message, DEFAULT_TRACE_KEY => trace },
- expected_fields: { 'msg' => message },
- expected_trace_value: trace
- },
- {
- # It allows setting the trace via a custom configured key.
- driver_config: CONFIG_CUSTOM_TRACE_KEY_SPECIFIED,
- emitted_log: { 'msg' => message, 'custom_trace_key' => trace },
- expected_fields: { 'msg' => message },
- expected_trace_value: trace
- },
- {
- # It no longer sets trace by the default key if custom key specified.
- driver_config: CONFIG_CUSTOM_TRACE_KEY_SPECIFIED,
- emitted_log: { 'msg' => message, DEFAULT_TRACE_KEY => trace },
- expected_fields: { 'msg' => message, DEFAULT_TRACE_KEY => trace },
- expected_trace_value: nil
- }
- ].each do |input|
- setup_logging_stubs do
- @logs_sent = []
- d = create_driver(input[:driver_config])
- d.emit(input[:emitted_log])
- d.run
- end
- verify_log_entries(1, COMPUTE_PARAMS, 'jsonPayload') do |entry|
- assert_equal input[:expected_trace_value], entry['trace'], input
+ verify_field_key('trace', DEFAULT_TRACE_KEY, 'custom_trace_key',
+ CONFIG_CUSTOM_TRACE_KEY_SPECIFIED,
+ 'projects/proj1/traces/1234567890abcdef1234567890abcdef')
+ end
- fields = get_fields(entry['jsonPayload'])
- assert_equal input[:expected_fields].size, fields.size, input
- fields.each do |key, value|
- assert_equal input[:expected_fields][key], get_string(value), input
- end
- end
- end
+ def test_log_entry_span_id_field
+ verify_field_key('spanId', DEFAULT_SPAN_ID_KEY, 'custom_span_id_key',
+ CONFIG_CUSTOM_SPAN_ID_KEY_SPECIFIED, '000000000000004a')
end
# Metadata Agent related tests.
# Test enable_metadata_agent not set or set to false.
@@ -1760,9 +1743,60 @@
end
verify_log_entries(1, COMPUTE_PARAMS, 'jsonPayload') do |entry|
field = get_fields(entry['jsonPayload'])[payload_key]
assert_equal 'a_string', get_string(field), entry
assert_nil entry[destination_key], entry
+ end
+ end
+
+ def verify_field_key(log_entry_field, default_key, custom_key,
+ custom_key_config, sample_value)
+ setup_gce_metadata_stubs
+ message = log_entry(0)
+ [
+ {
+ # It leaves log entry field nil if no keyed value sent.
+ driver_config: APPLICATION_DEFAULT_CONFIG,
+ emitted_log: { 'msg' => message },
+ expected_payload: { 'msg' => message },
+ expected_field_value: nil
+ },
+ {
+ # By default, it sets log entry field via a default key.
+ driver_config: APPLICATION_DEFAULT_CONFIG,
+ emitted_log: { 'msg' => message, default_key => sample_value },
+ expected_payload: { 'msg' => message },
+ expected_field_value: sample_value
+ },
+ {
+ # It allows setting the log entry field via a custom configured key.
+ driver_config: custom_key_config,
+ emitted_log: { 'msg' => message, custom_key => sample_value },
+ expected_payload: { 'msg' => message },
+ expected_field_value: sample_value
+ },
+ {
+ # It doesn't set log entry field by default key if custom key specified.
+ driver_config: custom_key_config,
+ emitted_log: { 'msg' => message, default_key => sample_value },
+ expected_payload: { 'msg' => message, default_key => sample_value },
+ expected_field_value: nil
+ }
+ ].each do |input|
+ setup_logging_stubs do
+ @logs_sent = []
+ d = create_driver(input[:driver_config])
+ d.emit(input[:emitted_log])
+ d.run
+ end
+ verify_log_entries(1, COMPUTE_PARAMS, 'jsonPayload') do |entry|
+ assert_equal input[:expected_field_value], entry[log_entry_field], input
+ payload_fields = get_fields(entry['jsonPayload'])
+ assert_equal input[:expected_payload].size, payload_fields.size, input
+ payload_fields.each do |key, value|
+ assert_equal input[:expected_payload][key], get_string(value), input
+ end
+ end
end
end
def http_request_message
HTTP_REQUEST_MESSAGE