summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ci/pipeline/chain/sequence_spec.rb
blob: e8eb3333b88e1e533a3e9e424aa80b726a2d6bcf (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Ci::Pipeline::Chain::Sequence do
  let_it_be(:project) { create(:project) }
  let_it_be(:user) { create(:user) }

  let(:pipeline) { build_stubbed(:ci_pipeline) }
  let(:command) { Gitlab::Ci::Pipeline::Chain::Command.new(project: project) }
  let(:first_step) { spy('first step', name: 'FirstStep') }
  let(:second_step) { spy('second step', name: 'SecondStep') }
  let(:sequence) { [first_step, second_step] }
  let(:histogram) { spy('prometheus metric') }

  subject do
    described_class.new(pipeline, command, sequence)
  end

  context 'when one of steps breaks the chain' do
    before do
      allow(first_step).to receive(:break?).and_return(true)
    end

    it 'does not process the second step' do
      subject.build!

      expect(second_step).not_to have_received(:perform!)
    end

    it 'returns a pipeline object' do
      expect(subject.build!).to eq pipeline
    end
  end

  context 'when all chains are executed correctly' do
    before do
      sequence.each do |step|
        allow(step).to receive(:break?).and_return(false)
      end
    end

    it 'iterates through entire sequence' do
      subject.build!

      expect(first_step).to have_received(:perform!)
      expect(second_step).to have_received(:perform!)
    end

    it 'returns a pipeline object' do
      expect(subject.build!).to eq pipeline
    end

    it 'adds sequence duration to duration histogram' do
      allow(command.metrics)
        .to receive(:pipeline_creation_duration_histogram)
        .and_return(histogram)

      subject.build!

      expect(histogram).to have_received(:observe)
    end

    it 'adds step sequence duration to duration histogram' do
      expect(command.metrics)
        .to receive(:pipeline_creation_step_duration_histogram)
        .twice
        .and_return(histogram)
      expect(histogram).to receive(:observe).with({ step: 'FirstStep' }, any_args).ordered
      expect(histogram).to receive(:observe).with({ step: 'SecondStep' }, any_args).ordered

      subject.build!
    end

    it 'records pipeline size by pipeline source in a histogram' do
      allow(command.metrics)
        .to receive(:pipeline_size_histogram)
        .and_return(histogram)

      subject.build!

      expect(histogram).to have_received(:observe)
        .with({ source: 'push' }, 0)
    end

    it 'records active jobs by pipeline plan in a histogram' do
      allow(command.metrics)
        .to receive(:active_jobs_histogram)
        .and_return(histogram)

      pipeline = create(:ci_pipeline, project: project, status: :running)
      create(:ci_build, :finished, project: project, pipeline: pipeline)
      create(:ci_build, :failed, project: project, pipeline: pipeline)
      create(:ci_build, :running, project: project, pipeline: pipeline)
      subject.build!

      expect(histogram).to have_received(:observe)
        .with(hash_including(plan: project.actual_plan_name), 3)
    end
  end
end