BigW Consortium Gitlab

has_status_spec.rb 7.79 KB
Newer Older
1 2
require 'spec_helper'

3
describe HasStatus do
4
  describe '.status' do
5
    subject { CommitStatus.status }
6

7 8
    shared_examples 'build status summary' do
      context 'all successful' do
9
        let!(:statuses) { Array.new(2) { create(type, status: :success) } }
10 11
        it { is_expected.to eq 'success' }
      end
12

13
      context 'at least one failed' do
14
        let!(:statuses) do
15
          [create(type, status: :success), create(type, status: :failed)]
16
        end
17

18 19 20 21
        it { is_expected.to eq 'failed' }
      end

      context 'at least one running' do
22
        let!(:statuses) do
23
          [create(type, status: :success), create(type, status: :running)]
24
        end
25

26 27 28 29
        it { is_expected.to eq 'running' }
      end

      context 'at least one pending' do
30
        let!(:statuses) do
31
          [create(type, status: :success), create(type, status: :pending)]
32
        end
33

34 35 36 37
        it { is_expected.to eq 'running' }
      end

      context 'success and failed but allowed to fail' do
38
        let!(:statuses) do
39 40
          [create(type, status: :success),
           create(type, status: :failed, allow_failure: true)]
41
        end
42

43 44 45 46
        it { is_expected.to eq 'success' }
      end

      context 'one failed but allowed to fail' do
47 48 49 50
        let!(:statuses) do
          [create(type, status: :failed, allow_failure: true)]
        end

51
        it { is_expected.to eq 'skipped' }
52 53
      end

54
      context 'success and canceled' do
55
        let!(:statuses) do
56 57
          [create(type, status: :success), create(type, status: :canceled)]
        end
58 59 60 61 62

        it { is_expected.to eq 'canceled' }
      end

      context 'one failed and one canceled' do
63
        let!(:statuses) do
64 65 66
          [create(type, status: :failed), create(type, status: :canceled)]
        end

67
        it { is_expected.to eq 'failed' }
68 69 70
      end

      context 'one failed but allowed to fail and one canceled' do
71
        let!(:statuses) do
72 73 74 75 76 77 78 79
          [create(type, status: :failed, allow_failure: true),
           create(type, status: :canceled)]
        end

        it { is_expected.to eq 'canceled' }
      end

      context 'one running one canceled' do
80
        let!(:statuses) do
81 82 83 84
          [create(type, status: :running), create(type, status: :canceled)]
        end

        it { is_expected.to eq 'running' }
85 86 87
      end

      context 'all canceled' do
88
        let!(:statuses) do
89 90
          [create(type, status: :canceled), create(type, status: :canceled)]
        end
91

92 93 94 95
        it { is_expected.to eq 'canceled' }
      end

      context 'success and canceled but allowed to fail' do
96
        let!(:statuses) do
97 98 99 100 101 102 103
          [create(type, status: :success),
           create(type, status: :canceled, allow_failure: true)]
        end

        it { is_expected.to eq 'success' }
      end

104
      context 'one finished and second running but allowed to fail' do
105
        let!(:statuses) do
106 107
          [create(type, status: :success),
           create(type, status: :running, allow_failure: true)]
108
        end
109

110 111 112
        it { is_expected.to eq 'running' }
      end

113
      context 'when one status finished and second is still created' do
114 115 116 117
        let!(:statuses) do
          [create(type, status: :success), create(type, status: :created)]
        end

118
        it { is_expected.to eq 'running' }
119
      end
120

121 122 123 124 125 126 127 128 129 130
      context 'when there is a manual status before created status' do
        let!(:statuses) do
          [create(type, status: :success),
           create(type, status: :manual, allow_failure: false),
           create(type, status: :created)]
        end

        it { is_expected.to eq 'manual' }
      end

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
      context 'when one status is a blocking manual action' do
        let!(:statuses) do
          [create(type, status: :failed),
           create(type, status: :manual, allow_failure: false)]
        end

        it { is_expected.to eq 'manual' }
      end

      context 'when one status is a non-blocking manual action' do
        let!(:statuses) do
          [create(type, status: :failed),
           create(type, status: :manual, allow_failure: true)]
        end

        it { is_expected.to eq 'failed' }
      end
148
    end
149 150 151

    context 'ci build statuses' do
      let(:type) { :ci_build }
152

153 154 155 156 157
      it_behaves_like 'build status summary'
    end

    context 'generic commit statuses' do
      let(:type) { :generic_commit_status }
158

159 160
      it_behaves_like 'build status summary'
    end
161
  end
162 163

  context 'for scope with one status' do
164 165 166 167
    shared_examples 'having a job' do |status|
      %i[ci_build generic_commit_status].each do |type|
        context "when it's #{status} #{type} job" do
          let!(:job) { create(type, status) }
168

169 170 171 172 173
          describe ".#{status}" do
            it 'contains the job' do
              expect(CommitStatus.public_send(status).all).
                to contain_exactly(job)
            end
174
          end
175

176 177 178 179 180 181 182 183 184
          describe '.relevant' do
            if status == :created
              it 'contains nothing' do
                expect(CommitStatus.relevant.all).to be_empty
              end
            else
              it 'contains the job' do
                expect(CommitStatus.relevant.all).to contain_exactly(job)
              end
185 186 187 188 189 190 191 192
            end
          end
        end
      end
    end

    %i[created running pending success
       failed canceled skipped].each do |status|
193
      it_behaves_like 'having a job', status
194 195 196 197
    end
  end

  context 'for scope with more statuses' do
198 199 200 201
    shared_examples 'containing the job' do |status|
      %i[ci_build generic_commit_status].each do |type|
        context "when it's #{status} #{type} job" do
          let!(:job) { create(type, status) }
202

203 204 205
          it 'contains the job' do
            is_expected.to contain_exactly(job)
          end
206 207 208 209
        end
      end
    end

210 211 212 213
    shared_examples 'not containing the job' do |status|
      %i[ci_build generic_commit_status].each do |type|
        context "when it's #{status} #{type} job" do
          let!(:job) { create(type, status) }
214

215 216 217
          it 'contains nothing' do
            is_expected.to be_empty
          end
218 219 220 221 222 223 224
        end
      end
    end

    describe '.running_or_pending' do
      subject { CommitStatus.running_or_pending }

225
      %i[running pending].each do |status|
226
        it_behaves_like 'containing the job', status
227
      end
228

229 230 231
      %i[created failed success].each do |status|
        it_behaves_like 'not containing the job', status
      end
232 233
    end

234 235 236 237 238 239 240 241 242 243 244 245
    describe '.created_or_pending' do
      subject { CommitStatus.created_or_pending }

      %i[created pending].each do |status|
        it_behaves_like 'containing the job', status
      end

      %i[running failed success].each do |status|
        it_behaves_like 'not containing the job', status
      end
    end

246 247 248
    describe '.finished' do
      subject { CommitStatus.finished }

249
      %i[success failed canceled].each do |status|
250
        it_behaves_like 'containing the job', status
251
      end
252

253 254 255
      %i[created running pending].each do |status|
        it_behaves_like 'not containing the job', status
      end
256 257 258 259 260
    end

    describe '.cancelable' do
      subject { CommitStatus.cancelable }

261
      %i[running pending created].each do |status|
262
        it_behaves_like 'containing the job', status
263
      end
264

265 266 267
      %i[failed success skipped canceled].each do |status|
        it_behaves_like 'not containing the job', status
      end
268
    end
269 270 271 272 273 274 275 276 277 278 279 280

    describe '.manual' do
      subject { CommitStatus.manual }

      %i[manual].each do |status|
        it_behaves_like 'containing the job', status
      end

      %i[failed success skipped canceled].each do |status|
        it_behaves_like 'not containing the job', status
      end
    end
281
  end
282 283 284 285 286 287

  describe '::DEFAULT_STATUS' do
    it 'is a status created' do
      expect(described_class::DEFAULT_STATUS).to eq 'created'
    end
  end
288 289 290 291 292 293

  describe '::BLOCKED_STATUS' do
    it 'is a status manual' do
      expect(described_class::BLOCKED_STATUS).to eq 'manual'
    end
  end
294
end