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

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

require 'spec_helper'

RSpec.describe Ci::EnqueueJobService, '#execute', feature_category: :continuous_integration do
  let_it_be(:project) { create(:project) }
  let(:user) { create(:user, developer_projects: [project]) }
  let(:pipeline) { create(:ci_pipeline, project: project) }
  let(:build) { create(:ci_build, :manual, pipeline: pipeline) }

  let(:service) do
    described_class.new(build, current_user: user)
  end

  subject(:execute) { service.execute }

  it 'assigns the user to the job' do
    expect { execute }.to change { build.reload.user }.to(user)
  end

  it 'calls enqueue!' do
    expect(build).to receive(:enqueue!)
    execute
  end

  it 'calls Ci::ResetSkippedJobsService' do
    expect_next_instance_of(Ci::ResetSkippedJobsService) do |service|
      expect(service).to receive(:execute).with(build)
    end

    execute
  end

  it 'returns the job' do
    expect(execute).to eq(build)
  end

  context 'when variables are supplied' do
    let(:job_variables) do
      [{ key: 'first', secret_value: 'first' },
       { key: 'second', secret_value: 'second' }]
    end

    let(:service) do
      described_class.new(build, current_user: user, variables: job_variables)
    end

    it 'assigns the variables to the job' do
      execute
      expect(build.reload.job_variables.map(&:key)).to contain_exactly('first', 'second')
    end
  end

  context 'when the job transition is invalid' do
    let(:bridge) { create(:ci_bridge, :failed, pipeline: pipeline, project: project) }

    let(:service) do
      described_class.new(bridge, current_user: user)
    end

    it 'raises StateMachines::InvalidTransition' do
      expect { execute }.to raise_error StateMachines::InvalidTransition
    end
  end

  context 'when a transition block is supplied' do
    let(:bridge) { create(:ci_bridge, :playable, pipeline: pipeline) }

    let(:service) do
      described_class.new(bridge, current_user: user)
    end

    subject(:execute) { service.execute(&:pending!) }

    it 'calls the transition block instead of enqueue!' do
      expect(bridge).to receive(:pending!)
      expect(bridge).not_to receive(:enqueue!)
      execute
    end
  end
end