summaryrefslogtreecommitdiff
path: root/spec/initializers/diagnostic_reports_spec.rb
blob: 70574194916ec290013e6a9a77c0e1cc9e6f4a55 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'diagnostic reports' do
  subject(:load_initializer) do
    load Rails.root.join('config/initializers/diagnostic_reports.rb')
  end

  shared_examples 'does not modify worker startup hooks' do
    it do
      expect(Gitlab::Cluster::LifecycleEvents).not_to receive(:on_worker_start)
      expect(Gitlab::Memory::ReportsDaemon).not_to receive(:instance)

      load_initializer
    end
  end

  context 'when GITLAB_DIAGNOSTIC_REPORTS_ENABLED is set to true' do
    before do
      stub_env('GITLAB_DIAGNOSTIC_REPORTS_ENABLED', true)
    end

    context 'when run in application context' do
      before do
        allow(::Gitlab::Runtime).to receive(:application?).and_return(true)
      end

      it 'modifies worker startup hooks' do
        report_daemon = instance_double(Gitlab::Memory::ReportsDaemon)

        expect(Gitlab::Cluster::LifecycleEvents).to receive(:on_worker_start).and_call_original
        expect(Gitlab::Memory::ReportsDaemon).to receive(:instance).and_return(report_daemon)
        expect(report_daemon).to receive(:start)

        load_initializer
      end
    end

    context 'when run in non-application context, such as rails console or tests' do
      before do
        allow(::Gitlab::Runtime).to receive(:application?).and_return(false)
      end

      include_examples 'does not modify worker startup hooks'
    end
  end

  context 'when GITLAB_DIAGNOSTIC_REPORTS_ENABLED is not set' do
    before do
      allow(::Gitlab::Runtime).to receive(:application?).and_return(true)
    end

    include_examples 'does not modify worker startup hooks'
  end

  context 'when GITLAB_DIAGNOSTIC_REPORTS_ENABLED is set to false' do
    before do
      stub_env('GITLAB_DIAGNOSTIC_REPORTS_ENABLED', false)
      allow(::Gitlab::Runtime).to receive(:application?).and_return(true)
    end

    include_examples 'does not modify worker startup hooks'
  end
end