summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/sidekiq_logging/json_formatter_spec.rb
blob: 8c55cc21f2c917871ca174c9237c2cbeb3e37da7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::SidekiqLogging::JSONFormatter do
  let(:message) { 'This is a test' }
  let(:now) { Time.now }
  let(:timestamp) { now.utc.to_f }
  let(:timestamp_iso8601) { now.iso8601(3) }

  describe 'with a Hash' do
    subject { Gitlab::Json.parse(described_class.new.call('INFO', now, 'my program', hash_input)) }

    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
      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(subject).to eq(expected_output)
    end

    context 'when the job args are bigger than the maximum allowed' do
      it 'keeps args from the front until they exceed the limit' do
        half_limit = Gitlab::Utils::LogLimitedArray::MAXIMUM_ARRAY_LENGTH / 2
        hash_input['args'] = [1, 2, 'a' * half_limit, 'b' * half_limit, 3]

        expected_args = hash_input['args'].take(3).map(&:to_s) + ['...']

        expect(subject['args']).to eq(expected_args)
      end
    end

    it 'properly flattens arguments to a String' do
      hash_input['args'] = [1, "test", 2, { 'test' => 1 }]

      expect(subject['args']).to eq(["1", "test", "2", %({"test"=>1})])
    end
  end

  describe 'with a String' do
    it 'accepts strings with no changes' do
      result = subject.call('DEBUG', now, 'my string', message)

      data = Gitlab::Json.parse(result)
      expected_output = {
        severity: 'DEBUG',
        time: timestamp_iso8601,
        message: message
      }

      expect(data).to eq(expected_output.stringify_keys)
    end
  end
end