Welcome to mirror list, hosted at ThFree Co, Russian Federation.

collect_error_service_spec.rb « error_tracking « services « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: faca3c12a483401fb05f6f1a7e80100b8268eaef (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe ErrorTracking::CollectErrorService do
  let_it_be(:project) { create(:project) }

  let(:parsed_event_file) { 'error_tracking/parsed_event.json' }
  let(:parsed_event) { parse_valid_event(parsed_event_file) }

  subject { described_class.new(project, nil, event: parsed_event) }

  describe '#execute' do
    it 'creates Error and creates ErrorEvent' do
      expect { subject.execute }
        .to change { ErrorTracking::Error.count }.by(1)
        .and change { ErrorTracking::ErrorEvent.count }.by(1)
    end

    it 'updates Error and created ErrorEvent on second hit' do
      subject.execute

      expect { subject.execute }.not_to change { ErrorTracking::Error.count }
      expect { subject.execute }.to change { ErrorTracking::ErrorEvent.count }.by(1)
    end

    it 'has correct values set' do
      subject.execute

      event = ErrorTracking::ErrorEvent.last
      error = event.error

      expect(error.name).to eq 'ActionView::MissingTemplate'
      expect(error.description).to start_with 'Missing template posts/error2'
      expect(error.actor).to eq 'PostsController#error2'
      expect(error.platform).to eq 'ruby'
      expect(error.last_seen_at).to eq '2021-07-08T12:59:16Z'

      expect(event.description).to start_with 'Missing template posts/error2'
      expect(event.occurred_at).to eq '2021-07-08T12:59:16Z'
      expect(event.level).to eq 'error'
      expect(event.environment).to eq 'development'
      expect(event.payload).to eq parsed_event
    end

    context 'python sdk event' do
      let(:parsed_event_file) { 'error_tracking/python_event.json' }

      it 'creates a valid event' do
        expect { subject.execute }.to change { ErrorTracking::ErrorEvent.count }.by(1)
      end
    end

    context 'with unusual payload' do
      let(:modified_event) { parsed_event }
      let(:event) { described_class.new(project, nil, event: modified_event).execute }

      context 'when transaction is missing' do
        it 'builds actor from stacktrace' do
          modified_event.delete('transaction')

          expect(event.error.actor).to eq 'find()'
        end
      end

      context 'when transaction is an empty string' do \
        it 'builds actor from stacktrace' do
          modified_event['transaction'] = ''

          expect(event.error.actor).to eq 'find()'
        end
      end

      context 'when timestamp is numeric' do
        it 'parses timestamp' do
          modified_event['timestamp'] = '1631015580.50'

          expect(event.occurred_at).to eq '2021-09-07T11:53:00.5'
        end
      end
    end

    context 'go payload' do
      let(:parsed_event_file) { 'error_tracking/go_parsed_event.json' }

      it 'has correct values set' do
        subject.execute

        event = ErrorTracking::ErrorEvent.last
        error = event.error

        expect(error.name).to eq '*errors.errorString'
        expect(error.description).to start_with 'Hello world'
        expect(error.platform).to eq 'go'

        expect(event.description).to start_with 'Hello world'
        expect(event.level).to eq 'error'
        expect(event.environment).to eq 'Accumulate'
        expect(event.payload).to eq parsed_event
      end

      context 'with two exceptions' do
        let(:parsed_event_file) { 'error_tracking/go_two_exception_event.json' }

        it 'reports using second exception', :aggregate_failures do
          subject.execute

          event = ErrorTracking::ErrorEvent.last
          error = event.error

          expect(error.name).to eq '*url.Error'
          expect(error.description).to eq(%(Get \"foobar\": unsupported protocol scheme \"\"))
          expect(error.platform).to eq 'go'
          expect(error.actor).to eq('main(main)')

          expect(event.description).to eq(%(Get \"foobar\": unsupported protocol scheme \"\"))
          expect(event.payload).to eq parsed_event
        end
      end
    end
  end

  private

  def parse_valid_event(parsed_event_file)
    parsed_event = Gitlab::Json.parse(fixture_file(parsed_event_file))

    validator = ErrorTracking::Collector::PayloadValidator.new
    # This a precondition for all specs to verify that
    # submitted JSON payload is valid.
    expect(validator).to be_valid(parsed_event)

    parsed_event
  end
end