2019-12-04 20:38:33 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Gitlab::SidekiqLogging::JSONFormatter do
|
|
|
|
let(:message) { 'This is a test' }
|
2020-03-13 15:44:24 +05:30
|
|
|
let(:now) { Time.now }
|
|
|
|
let(:timestamp) { now.utc.to_f }
|
|
|
|
let(:timestamp_iso8601) { now.iso8601(3) }
|
|
|
|
|
|
|
|
describe 'with a Hash' do
|
|
|
|
let(:hash_input) do
|
|
|
|
{
|
|
|
|
foo: 1,
|
|
|
|
'bar' => 'test',
|
|
|
|
'created_at' => timestamp,
|
|
|
|
'enqueued_at' => timestamp,
|
|
|
|
'started_at' => timestamp,
|
|
|
|
'retried_at' => timestamp,
|
|
|
|
'failed_at' => timestamp,
|
|
|
|
'completed_at' => timestamp_iso8601
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'properly formats timestamps into ISO 8601 form' do
|
|
|
|
result = subject.call('INFO', now, 'my program', hash_input)
|
|
|
|
|
|
|
|
data = JSON.parse(result)
|
|
|
|
expected_output = hash_input.stringify_keys.merge!(
|
|
|
|
{
|
|
|
|
'severity' => 'INFO',
|
|
|
|
'time' => timestamp_iso8601,
|
|
|
|
'created_at' => timestamp_iso8601,
|
|
|
|
'enqueued_at' => timestamp_iso8601,
|
|
|
|
'started_at' => timestamp_iso8601,
|
|
|
|
'retried_at' => timestamp_iso8601,
|
|
|
|
'failed_at' => timestamp_iso8601,
|
|
|
|
'completed_at' => timestamp_iso8601
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(data).to eq(expected_output)
|
|
|
|
end
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'wraps a String' do
|
2020-03-13 15:44:24 +05:30
|
|
|
result = subject.call('DEBUG', now, 'my string', message)
|
2018-05-09 12:01:36 +05:30
|
|
|
|
|
|
|
data = JSON.parse(result)
|
|
|
|
expected_output = {
|
|
|
|
severity: 'DEBUG',
|
2020-03-13 15:44:24 +05:30
|
|
|
time: timestamp_iso8601,
|
2018-05-09 12:01:36 +05:30
|
|
|
message: message
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(data).to eq(expected_output.stringify_keys)
|
|
|
|
end
|
|
|
|
end
|