summaryrefslogtreecommitdiff
path: root/spec/workers/projects/post_creation_worker_spec.rb
blob: 732dc540fb70113c11e859c54c11db422dc75cfa (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Projects::PostCreationWorker do
  let_it_be(:user) { create :user }

  let(:worker) { described_class.new }
  let(:project) { create(:project) }

  subject { described_class.new.perform(project.id) }

  it_behaves_like 'an idempotent worker' do
    let(:job_args) { [project.id] }

    describe 'Prometheus integration' do
      context 'project is nil' do
        let(:job_args) { [nil] }

        it 'does not create prometheus integration' do
          expect { subject }.not_to change { Integration.count }
        end
      end

      context 'when project has access to shared integration' do
        context 'Prometheus application is shared via group cluster' do
          let(:project) { create(:project, group: group) }
          let(:cluster) { create(:cluster, :group, groups: [group]) }
          let(:group) do
            create(:group).tap do |group|
              group.add_owner(user)
            end
          end

          before do
            create(:clusters_integrations_prometheus, cluster: cluster)
          end

          it 'creates an Integrations::Prometheus record', :aggregate_failures do
            subject

            integration = project.prometheus_integration
            expect(integration.active).to be true
            expect(integration.manual_configuration?).to be false
            expect(integration.persisted?).to be true
          end
        end

        context 'Prometheus application is shared via instance cluster' do
          let(:cluster) { create(:cluster, :instance) }

          before do
            create(:clusters_integrations_prometheus, cluster: cluster)
          end

          it 'creates an Integrations::Prometheus record', :aggregate_failures do
            subject

            integration = project.prometheus_integration
            expect(integration.active).to be true
            expect(integration.manual_configuration?).to be false
            expect(integration.persisted?).to be true
          end

          it 'cleans invalid record and logs warning', :aggregate_failures do
            invalid_integration_record = build(:prometheus_integration, properties: { api_url: nil, manual_configuration: true })
            allow(::Integrations::Prometheus).to receive(:new).and_return(invalid_integration_record)

            expect(Gitlab::ErrorTracking).to receive(:track_exception).with(an_instance_of(ActiveRecord::RecordInvalid), include(extra: { project_id: a_kind_of(Integer) })).twice
            subject

            expect(project.prometheus_integration).to be_nil
          end
        end

        context 'shared Prometheus application is not available' do
          it 'does not persist an Integrations::Prometheus record' do
            subject

            expect(project.prometheus_integration).to be_nil
          end
        end
      end

      describe 'Incident timeline event tags' do
        context 'when project is nil' do
          let(:job_args) { [nil] }

          it 'does not create event tags' do
            expect { subject }.not_to change { IncidentManagement::TimelineEventTag.count }
          end
        end

        context 'when project is created', :aggregate_failures do
          it 'creates tags for the project' do
            expect { subject }.to change { IncidentManagement::TimelineEventTag.count }.by(2)

            expect(project.incident_management_timeline_event_tags.pluck_names).to match_array(
              [
                ::IncidentManagement::TimelineEventTag::START_TIME_TAG_NAME,
                ::IncidentManagement::TimelineEventTag::END_TIME_TAG_NAME
              ]
            )
          end

          it 'raises error if record creation fails' do
            allow_next_instance_of(IncidentManagement::TimelineEventTag) do |tag|
              allow(tag).to receive(:valid?).and_return(false)
            end

            expect(Gitlab::ErrorTracking).to receive(:track_exception).with(an_instance_of(ActiveRecord::RecordInvalid), include(extra: { project_id: a_kind_of(Integer) })).twice
            subject

            expect(project.incident_management_timeline_event_tags).to be_empty
          end
        end
      end
    end
  end
end