test/plugin/base_test.rb in fluent-plugin-google-cloud-0.6.10 vs test/plugin/base_test.rb in fluent-plugin-google-cloud-0.6.11.pre.memory.1

- old
+ new

@@ -175,28 +175,29 @@ end def test_ec2_metadata_requires_project_id setup_ec2_metadata_stubs exception_count = 0 - Fluent::GoogleCloudOutput::CredentialsInfo.stubs(:project_id).returns(nil) begin create_driver rescue Fluent::ConfigError => error assert error.message.include? 'Unable to obtain metadata parameters:' assert error.message.include? 'project_id' exception_count += 1 end assert_equal 1, exception_count end - def test_ec2_metadata_project_id_from_credentials - setup_ec2_metadata_stubs - [IAM_CREDENTIALS, LEGACY_CREDENTIALS].each do |creds| - ENV['GOOGLE_APPLICATION_CREDENTIALS'] = creds[:path] - d = create_driver - d.run - assert_equal creds[:project_id], d.instance.project_id + def test_project_id_from_credentials + %w(gce ec2).each do |platform| + send("setup_#{platform}_metadata_stubs") + [IAM_CREDENTIALS, LEGACY_CREDENTIALS].each do |creds| + ENV['GOOGLE_APPLICATION_CREDENTIALS'] = creds[:path] + d = create_driver + d.run + assert_equal creds[:project_id], d.instance.project_id + end end end def test_one_log setup_gce_metadata_stubs @@ -214,22 +215,21 @@ setup_logging_stubs do d = create_driver d.emit('message' => log_entry(0)) d.run end - verify_log_entries(1, COMPUTE_PARAMS) + verify_log_entries(1, COMPUTE_PARAMS.merge( + project_id: IAM_CREDENTIALS[:project_id])) end - def test_one_log_with_invalid_json_credentials - setup_gce_metadata_stubs - ENV['GOOGLE_APPLICATION_CREDENTIALS'] = INVALID_CREDENTIALS[:path] - setup_logging_stubs do - d = create_driver - d.emit('message' => log_entry(0)) + def test_invalid_json_credentials + %w(gce_metadata ec2_metadata no_metadata_service).each do |platform| + send("setup_#{platform}_stubs") exception_count = 0 + ENV['GOOGLE_APPLICATION_CREDENTIALS'] = INVALID_CREDENTIALS[:path] begin - d.run + create_driver rescue RuntimeError => error assert error.message.include? 'Unable to read the credential file' exception_count += 1 end assert_equal 1, exception_count @@ -605,35 +605,50 @@ end end def test_timestamps setup_gce_metadata_stubs - [Time.at(123_456.789), Time.at(0), Time.now].each do |ts| + current_time = Time.now + next_year = Time.mktime(current_time.year + 1) + one_second_before_next_year = next_year - 1 + adjusted_to_last_year = + one_second_before_next_year.to_datetime.prev_year.to_time + one_second_into_next_year = next_year + 1 + one_day_into_next_year = next_year.to_date.next_day.to_time + { + Time.at(123_456.789) => Time.at(123_456.789), + Time.at(0) => Time.at(0), + current_time => current_time, + one_second_before_next_year => adjusted_to_last_year, + next_year => Time.at(0), + one_second_into_next_year => Time.at(0), + one_day_into_next_year => Time.at(0) + }.each do |ts, adjusted_ts| expected_ts = [] emit_index = 0 setup_logging_stubs do @logs_sent = [] d = create_driver # Test the "native" fluentd timestamp as well as our nanosecond tags. d.emit({ 'message' => log_entry(emit_index) }, ts.to_f) - expected_ts.push(ts) + expected_ts.push(adjusted_ts) emit_index += 1 d.emit('message' => log_entry(emit_index), 'timeNanos' => ts.tv_sec * 1_000_000_000 + ts.tv_nsec) - expected_ts.push(ts) + expected_ts.push(adjusted_ts) emit_index += 1 d.emit('message' => log_entry(emit_index), 'timestamp' => { 'seconds' => ts.tv_sec, 'nanos' => ts.tv_nsec }) - expected_ts.push(ts) + expected_ts.push(adjusted_ts) emit_index += 1 d.emit('message' => log_entry(emit_index), 'timestampSeconds' => ts.tv_sec, 'timestampNanos' => ts.tv_nsec) - expected_ts.push(ts) + expected_ts.push(adjusted_ts) emit_index += 1 d.emit('message' => log_entry(emit_index), 'timestampSeconds' => ts.tv_sec.to_s, 'timestampNanos' => ts.tv_nsec.to_s) - expected_ts.push(ts) + expected_ts.push(adjusted_ts) emit_index += 1 d.run verify_index = 0 verify_log_entries(emit_index, COMPUTE_PARAMS) do |entry, i| verify_default_log_entry_text(entry['textPayload'], i, entry)