summaryrefslogtreecommitdiff
path: root/spec/services/ci/pipeline_processing/atomic_processing_service/status_collection_spec.rb
blob: 46ea0036e491527f23087d236ec2c9956374bafb (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Ci::PipelineProcessing::AtomicProcessingService::StatusCollection,
  feature_category: :continuous_integration do
  using RSpec::Parameterized::TableSyntax

  let_it_be(:pipeline) { create(:ci_pipeline) }
  let_it_be(:build_stage) { create(:ci_stage, name: 'build', pipeline: pipeline) }
  let_it_be(:test_stage) { create(:ci_stage, name: 'test', pipeline: pipeline) }
  let_it_be(:deploy_stage) { create(:ci_stage, name: 'deploy', pipeline: pipeline) }
  let_it_be(:build_a) do
    create(:ci_build, :success, name: 'build-a', ci_stage: build_stage, stage_idx: 0, pipeline: pipeline)
  end

  let_it_be(:build_b) do
    create(:ci_build, :failed, name: 'build-b', ci_stage: build_stage, stage_idx: 0, pipeline: pipeline)
  end

  let_it_be(:test_a) do
    create(:ci_build, :running, name: 'test-a', ci_stage: test_stage, stage_idx: 1, pipeline: pipeline)
  end

  let_it_be(:test_b) do
    create(:ci_build, :pending, name: 'test-b', ci_stage: test_stage, stage_idx: 1, pipeline: pipeline)
  end

  let_it_be(:deploy) do
    create(:ci_build, :created, name: 'deploy', ci_stage: deploy_stage, stage_idx: 2, pipeline: pipeline)
  end

  let(:collection) { described_class.new(pipeline) }

  describe '#set_processable_status' do
    it 'does update existing status of processable' do
      collection.set_processable_status(test_a.id, 'success', 100)

      expect(collection.status_of_processables(['test-a'], dag: false)).to eq('success')
    end

    it 'ignores a missing processable' do
      collection.set_processable_status(-1, 'failed', 100)
    end
  end

  describe '#status_of_all' do
    it 'returns composite status of the collection' do
      expect(collection.status_of_all).to eq('running')
    end
  end

  describe '#status_of_processables' do
    where(:names, :status, :dag) do
      %w[build-a]         | 'success' | false
      %w[build-a build-b] | 'failed'  | false
      %w[build-a test-a]  | 'running' | false
      %w[build-a]         | 'success' | true
      %w[build-a build-b] | 'failed'  | true
      %w[build-a test-a]  | 'pending' | true
    end

    with_them do
      it 'returns composite status of given names' do
        expect(collection.status_of_processables(names, dag: dag)).to eq(status)
      end
    end
  end

  describe '#status_of_processables_prior_to_stage' do
    where(:stage, :status) do
      0 | 'success'
      1 | 'failed'
      2 | 'running'
    end

    with_them do
      it 'returns composite status for processables in prior stages' do
        expect(collection.status_of_processables_prior_to_stage(stage)).to eq(status)
      end
    end
  end

  describe '#status_of_stage' do
    where(:stage, :status) do
      0 | 'failed'
      1 | 'running'
      2 | 'created'
    end

    with_them do
      it 'returns composite status for processables at a given stages' do
        expect(collection.status_of_stage(stage)).to eq(status)
      end
    end
  end

  describe '#created_processable_ids_in_stage' do
    it 'returns IDs of processables at a given stage position' do
      expect(collection.created_processable_ids_in_stage(0)).to be_empty
      expect(collection.created_processable_ids_in_stage(1)).to be_empty
      expect(collection.created_processable_ids_in_stage(2)).to contain_exactly(deploy.id)
    end
  end

  describe '#processing_processables' do
    it 'returns processables marked as processing' do
      expect(collection.processing_processables.map { |processable| processable[:id] })
        .to contain_exactly(build_a.id, build_b.id, test_a.id, test_b.id, deploy.id)
    end
  end
end