File: deployment_metrics_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (132 lines) | stat: -rw-r--r-- 4,351 bytes parent folder | download
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe DeploymentMetrics do
  describe '#has_metrics?' do
    subject { described_class.new(deployment.project, deployment).has_metrics? }

    context 'when deployment is failed' do
      let(:deployment) { create(:deployment, :failed) }

      it { is_expected.to be_falsy }
    end

    context 'when deployment is nil' do
      let_it_be(:project) { create(:project) }

      subject { described_class.new(project, nil).has_metrics? }

      it { is_expected.to be_falsy }
    end

    context 'when deployment is success' do
      let(:deployment) { create(:deployment, :success) }

      context 'without a monitoring integration' do
        it { is_expected.to be_falsy }
      end

      context 'with a Prometheus integration' do
        let(:prometheus_integration) { instance_double(::Integrations::Prometheus, can_query?: true, configured?: true) }

        before do
          allow(deployment.project).to receive(:find_or_initialize_integration).with('prometheus').and_return prometheus_integration
        end

        it { is_expected.to be_truthy }
      end

      context 'with a Prometheus integration that cannot query' do
        let(:prometheus_integration) { instance_double(::Integrations::Prometheus, configured?: true, can_query?: false) }

        before do
          allow(deployment.project).to receive(:find_or_initialize_integration).with('prometheus').and_return prometheus_integration
        end

        it { is_expected.to be_falsy }
      end

      context 'with a Prometheus integration that is not configured' do
        let(:prometheus_integration) { instance_double(::Integrations::Prometheus, configured?: false, can_query?: false) }

        before do
          allow(deployment.project).to receive(:find_or_initialize_integration).with('prometheus').and_return prometheus_integration
        end

        it { is_expected.to be_falsy }
      end

      context 'with a cluster Prometheus' do
        let(:deployment) { create(:deployment, :success, :on_cluster) }
        let!(:prometheus) { create(:clusters_integrations_prometheus, cluster: deployment.cluster) }

        before do
          expect(deployment.cluster.integration_prometheus).to receive(:configured?).and_return(true)
        end

        it { is_expected.to be_truthy }
      end
    end
  end

  describe '#metrics' do
    let(:deployment) { create(:deployment, :success) }
    let(:prometheus_adapter) { instance_double(::Integrations::Prometheus, can_query?: true, configured?: true) }
    let(:deployment_metrics) { described_class.new(deployment.project, deployment) }

    subject { deployment_metrics.metrics }

    context 'metrics are disabled' do
      it { is_expected.to eq({}) }
    end

    context 'metrics are enabled' do
      let(:simple_metrics) do
        {
          success: true,
          metrics: {},
          last_update: 42
        }
      end

      before do
        allow(deployment_metrics).to receive(:prometheus_adapter).and_return(prometheus_adapter)
        expect(prometheus_adapter).to receive(:query).with(:deployment, deployment).and_return(simple_metrics)
      end

      it { is_expected.to eq(simple_metrics.merge({ deployment_time: deployment.finished_at.to_i })) }
    end
  end

  describe '#additional_metrics' do
    let(:project) { create(:project, :repository) }
    let(:deployment) { create(:deployment, :succeed, project: project) }
    let(:deployment_metrics) { described_class.new(deployment.project, deployment) }

    subject { deployment_metrics.additional_metrics }

    context 'metrics are disabled' do
      it { is_expected.to eq({}) }
    end

    context 'metrics are enabled' do
      let(:simple_metrics) do
        {
          success: true,
          metrics: {},
          last_update: 42
        }
      end

      let(:prometheus_adapter) { instance_double(::Integrations::Prometheus, can_query?: true, configured?: true) }

      before do
        allow(deployment_metrics).to receive(:prometheus_adapter).and_return(prometheus_adapter)
        expect(prometheus_adapter).to receive(:query).with(:additional_metrics_deployment, deployment).and_return(simple_metrics)
      end

      it { is_expected.to eq(simple_metrics.merge({ deployment_time: deployment.finished_at.to_i })) }
    end
  end
end