summaryrefslogtreecommitdiff
path: root/spec/services/ci/play_manual_stage_service_spec.rb
blob: dd8e037b1290a9bec78e0b9ec78bc53badd1b086 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Ci::PlayManualStageService, '#execute', feature_category: :continuous_integration do
  let(:current_user) { create(:user) }
  let(:pipeline) { create(:ci_pipeline, user: current_user) }
  let(:project) { pipeline.project }
  let(:downstream_project) { create(:project) }
  let(:service) { described_class.new(project, current_user, pipeline: pipeline) }
  let(:stage_status) { 'manual' }

  let(:stage) do
    create(:ci_stage, pipeline: pipeline, project: project, name: 'test')
  end

  before do
    project.add_maintainer(current_user)
    downstream_project.add_maintainer(current_user)
    create_builds_for_stage(status: stage_status)
    create_bridge_for_stage(status: stage_status)
  end

  context 'when pipeline has manual processables' do
    before do
      service.execute(stage)
    end

    it 'starts manual processables from pipeline' do
      expect(pipeline.processables.manual.count).to eq(0)
    end

    it 'updates manual processables' do
      pipeline.processables.each do |processable|
        expect(processable.user).to eq(current_user)
      end
    end
  end

  context 'when pipeline has no manual processables' do
    let(:stage_status) { 'failed' }

    before do
      service.execute(stage)
    end

    it 'does not update the processables' do
      expect(pipeline.processables.failed.count).to eq(4)
    end
  end

  context 'when user does not have permission on a specific processable' do
    before do
      allow_next_instance_of(Ci::Processable) do |instance|
        allow(instance).to receive(:play).and_raise(Gitlab::Access::AccessDeniedError)
      end

      service.execute(stage)
    end

    it 'logs the error' do
      expect(Gitlab::AppLogger).to receive(:error)
        .exactly(stage.processables.manual.count)

      service.execute(stage)
    end
  end

  private

  def create_builds_for_stage(options)
    options.merge!({
      when: 'manual',
      pipeline: pipeline,
      stage_id: stage.id,
      user: pipeline.user
    })

    create_list(:ci_build, 3, options)
  end

  def create_bridge_for_stage(options)
    options.merge!({
      when: 'manual',
      pipeline: pipeline,
      stage_id: stage.id,
      user: pipeline.user,
      downstream: downstream_project
    })

    create(:ci_bridge, options)
  end
end