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

google_cloud_profiler_spec.rb « initializers « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 493d1e0bea5a090d45f23444481056f7ee4648d3 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'google cloud profiler', :aggregate_failures, feature_category: :application_performance do
  subject(:load_initializer) do
    load rails_root_join('config/initializers/google_cloud_profiler.rb')
  end

  shared_examples 'does not call profiler agent' do
    it do
      expect(CloudProfilerAgent::Agent).not_to receive(:new)

      load_initializer
    end
  end

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

    context 'when GITLAB_GOOGLE_CLOUD_PROFILER_PROJECT_ID is not set' do
      include_examples 'does not call profiler agent'
    end

    context 'when GITLAB_GOOGLE_CLOUD_PROFILER_PROJECT_ID is set' do
      let(:project_id) { 'gitlab-staging-1' }
      let(:agent) { instance_double(CloudProfilerAgent::Agent) }

      before do
        stub_env('GITLAB_GOOGLE_CLOUD_PROFILER_PROJECT_ID', project_id)
      end

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

        it 'calls the agent' do
          expect(CloudProfilerAgent::Agent)
            .to receive(:new).with(service: 'gitlab-web', project_id: project_id,
              logger: an_instance_of(::Gitlab::AppJsonLogger),
              log_labels: hash_including(
                message: 'Google Cloud Profiler Ruby',
                pid: be_a(Integer),
                worker_id: be_a(String)
              )).and_return(agent)
          expect(agent).to receive(:start)

          load_initializer
        end
      end

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

        include_examples 'does not call profiler agent'
      end

      context 'when run in another context' do
        before do
          allow(::Gitlab::Runtime).to receive(:puma?).and_return(false)
          allow(::Gitlab::Runtime).to receive(:sidekiq?).and_return(false)
        end

        include_examples 'does not call profiler agent'
      end
    end
  end

  context 'when GITLAB_GOOGLE_CLOUD_PROFILER_ENABLED is not set' do
    include_examples 'does not call profiler agent'
  end

  context 'when GITLAB_GOOGLE_CLOUD_PROFILER_ENABLED is set to false' do
    before do
      stub_env('GITLAB_GOOGLE_CLOUD_PROFILER_ENABLED', false)
    end

    include_examples 'does not call profiler agent'
  end
end