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

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

require 'spec_helper'

RSpec.describe Ci::RefDeleteUnlockArtifactsWorker do
  describe '#perform' do
    subject(:perform) { worker.perform(project_id, user_id, ref) }

    let(:worker) { described_class.new }

    let(:ref) { 'refs/heads/master' }

    let(:project) { create(:project) }

    include_examples 'an idempotent worker' do
      subject(:idempotent_perform) { perform_multiple([project_id, user_id, ref], exec_times: 2) }

      let(:project_id) { project.id }
      let(:user_id) { project.creator.id }

      let(:pipeline) { create(:ci_pipeline, ref: 'master', project: project, locked: :artifacts_locked) }

      it 'unlocks the artifacts from older pipelines' do
        expect { idempotent_perform }.to change { pipeline.reload.locked }.from('artifacts_locked').to('unlocked')
      end
    end

    context 'when project exists' do
      let(:project_id) { project.id }

      context 'when user exists' do
        let(:user_id) { project.creator.id }

        context 'when ci ref exists for project' do
          let!(:ci_ref) { create(:ci_ref, ref_path: ref, project: project) }

          it 'calls the service' do
            service = spy(Ci::UnlockArtifactsService)
            expect(Ci::UnlockArtifactsService).to receive(:new).and_return(service)

            perform

            expect(service).to have_received(:execute).with(ci_ref)
          end

          context 'when a locked pipeline with persisted artifacts exists' do
            let!(:pipeline) { create(:ci_pipeline, :with_persisted_artifacts, ref: 'master', project: project, locked: :artifacts_locked) }

            context 'with ci_update_unlocked_job_artifacts disabled' do
              before do
                stub_feature_flags(ci_update_unlocked_job_artifacts: false)
              end

              it 'logs the correct extra metadata' do
                expect(worker).to receive(:log_extra_metadata_on_done).with(:unlocked_pipelines, 1)
                expect(worker).to receive(:log_extra_metadata_on_done).with(:unlocked_job_artifacts, 0)

                perform
              end
            end

            context 'with ci_update_unlocked_job_artifacts enabled' do
              before do
                stub_feature_flags(ci_update_unlocked_job_artifacts: true)
              end

              it 'logs the correct extra metadata' do
                expect(worker).to receive(:log_extra_metadata_on_done).with(:unlocked_pipelines, 1)
                expect(worker).to receive(:log_extra_metadata_on_done).with(:unlocked_job_artifacts, 2)

                perform
              end
            end
          end
        end

        context 'when ci ref does not exist for the given project' do
          let!(:another_ci_ref) { create(:ci_ref, ref_path: ref) }

          it 'does not call the service' do
            expect(Ci::UnlockArtifactsService).not_to receive(:new)

            perform
          end
        end

        context 'when same ref path exists for a different project' do
          let!(:another_ci_ref) { create(:ci_ref, ref_path: ref) }
          let!(:ci_ref) { create(:ci_ref, ref_path: ref, project: project) }

          it 'calls the service with the correct ref_id' do
            service = spy(Ci::UnlockArtifactsService)
            expect(Ci::UnlockArtifactsService).to receive(:new).and_return(service)

            perform

            expect(service).to have_received(:execute).with(ci_ref)
          end
        end
      end

      context 'when user does not exist' do
        let(:user_id) { non_existing_record_id }

        it 'does not call service' do
          expect(Ci::UnlockArtifactsService).not_to receive(:new)

          perform
        end
      end
    end

    context 'when project does not exist' do
      let(:project_id) { non_existing_record_id }
      let(:user_id) { project.creator.id }

      it 'does not call service' do
        expect(Ci::UnlockArtifactsService).not_to receive(:new)

        perform
      end
    end
  end
end