summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/badge/pipeline/status_spec.rb
blob: b5dabca047715829b4cff003017f6ce457dbd36d (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
125
126
127
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Badge::Pipeline::Status do
  let(:project) { create(:project, :repository) }
  let(:sha) { project.commit.sha }
  let(:branch) { 'master' }
  let(:badge) { described_class.new(project, branch) }

  describe '#entity' do
    it 'always says pipeline' do
      expect(badge.entity).to eq 'pipeline'
    end
  end

  describe '#template' do
    it 'returns badge template' do
      expect(badge.template.key_text).to eq 'pipeline'
    end
  end

  describe '#metadata' do
    it 'returns badge metadata' do
      expect(badge.metadata.image_url).to include 'badges/master/pipeline.svg'
    end
  end

  context 'pipeline exists', :sidekiq_might_not_need_inline do
    let!(:pipeline) { create_pipeline(project, sha, branch) }

    context 'pipeline success' do
      before do
        pipeline.success!
      end

      describe '#status' do
        it 'is successful' do
          expect(badge.status).to eq 'success'
        end
      end
    end

    context 'pipeline failed' do
      before do
        pipeline.drop!
      end

      describe '#status' do
        it 'failed' do
          expect(badge.status).to eq 'failed'
        end
      end
    end

    context 'when outdated pipeline for given ref exists' do
      before do
        pipeline.success!

        old_pipeline = create_pipeline(project, '11eeffdd', branch)
        old_pipeline.drop!
      end

      it 'does not take outdated pipeline into account' do
        expect(badge.status).to eq 'success'
      end
    end

    context 'when multiple pipelines exist for given sha' do
      before do
        pipeline.drop!

        new_pipeline = create_pipeline(project, sha, branch)
        new_pipeline.success!
      end

      it 'does not take outdated pipeline into account' do
        expect(badge.status).to eq 'success'
      end
    end

    context 'when ignored_skipped is set to true' do
      let(:new_badge) { described_class.new(project, branch, opts: { ignore_skipped: true }) }

      before do
        pipeline.skip!
      end

      describe '#status' do
        it 'uses latest non-skipped status' do
          expect(new_badge.status).not_to eq 'skipped'
        end
      end
    end

    context 'when ignored_skipped is set to false' do
      let(:new_badge) { described_class.new(project, branch, opts: { ignore_skipped: false }) }

      before do
        pipeline.skip!
      end

      describe '#status' do
        it 'uses latest status' do
          expect(new_badge.status).to eq 'skipped'
        end
      end
    end
  end

  context 'build does not exist' do
    describe '#status' do
      it 'is unknown' do
        expect(badge.status).to eq 'unknown'
      end
    end
  end

  def create_pipeline(project, sha, branch)
    pipeline = create(:ci_empty_pipeline,
                      project: project,
                      sha: sha,
                      ref: branch)

    create(:ci_build, pipeline: pipeline, stage: 'notify')
  end
end