summaryrefslogtreecommitdiff
path: root/spec/serializers/ci/dag_pipeline_entity_spec.rb
blob: fab8798effca0243fe0656c0f3a6f737fdd25440 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# frozen_string_literal: true

require 'spec_helper'

describe Ci::DagPipelineEntity do
  let_it_be(:request) { double(:request) }

  let(:pipeline) { create(:ci_pipeline) }
  let(:entity) { described_class.new(pipeline, request: request) }

  describe '#as_json' do
    subject { entity.as_json }

    context 'when pipeline is empty' do
      it 'contains stages' do
        expect(subject).to include(:stages)

        expect(subject[:stages]).to be_empty
      end
    end

    context 'when pipeline has jobs' do
      let!(:build_job)  { create(:ci_build, stage: 'build',  pipeline: pipeline) }
      let!(:test_job)   { create(:ci_build, stage: 'test',   pipeline: pipeline) }
      let!(:deploy_job) { create(:ci_build, stage: 'deploy', pipeline: pipeline) }

      it 'contains 3 stages' do
        stages = subject[:stages]

        expect(stages.size).to eq 3
        expect(stages.map { |s| s[:name] }).to contain_exactly('build', 'test', 'deploy')
      end
    end

    context 'when pipeline has parallel jobs, DAG needs and GenericCommitStatus' do
      let!(:stage_build)  { create(:ci_stage_entity, name: 'build',  position: 1, pipeline: pipeline) }
      let!(:stage_test)   { create(:ci_stage_entity, name: 'test',   position: 2, pipeline: pipeline) }
      let!(:stage_deploy) { create(:ci_stage_entity, name: 'deploy', position: 3, pipeline: pipeline) }

      let!(:job_build_1)   { create(:ci_build, name: 'build 1', stage: 'build', pipeline: pipeline) }
      let!(:job_build_2)   { create(:ci_build, name: 'build 2', stage: 'build', pipeline: pipeline) }
      let!(:commit_status) { create(:generic_commit_status, stage: 'build', pipeline: pipeline) }

      let!(:job_rspec_1) { create(:ci_build, name: 'rspec 1/2', stage: 'test', pipeline: pipeline) }
      let!(:job_rspec_2) { create(:ci_build, name: 'rspec 2/2', stage: 'test', pipeline: pipeline) }

      let!(:job_jest) do
        create(:ci_build, name: 'jest', stage: 'test', scheduling_type: 'dag', pipeline: pipeline).tap do |job|
          create(:ci_build_need, name: 'build 1', build: job)
        end
      end

      let!(:job_deploy_ruby) do
        create(:ci_build, name: 'deploy_ruby', stage: 'deploy', scheduling_type: 'dag', pipeline: pipeline).tap do |job|
          create(:ci_build_need, name: 'rspec 1/2', build: job)
          create(:ci_build_need, name: 'rspec 2/2', build: job)
        end
      end

      let!(:job_deploy_js) do
        create(:ci_build, name: 'deploy_js', stage: 'deploy', scheduling_type: 'dag', pipeline: pipeline).tap do |job|
          create(:ci_build_need, name: 'jest', build: job)
        end
      end

      it 'performs the smallest number of queries' do
        log = ActiveRecord::QueryRecorder.new { subject }

        # stages, project, builds, build_needs
        expect(log.count).to eq 4
      end

      it 'contains all the data' do
        expected_result = {
          stages: [
            {
              name: 'build',
              groups: [
                {
                  name: 'build 1', size: 1, jobs: [
                    { name: 'build 1', scheduling_type: 'stage' }
                  ]
                },
                {
                  name: 'build 2', size: 1, jobs: [
                    { name: 'build 2', scheduling_type: 'stage' }
                  ]
                },
                {
                  name: 'generic', size: 1, jobs: [
                    { name: 'generic', scheduling_type: nil }
                  ]
                }
              ]
            },
            {
              name: 'test',
              groups: [
                {
                  name: 'jest', size: 1, jobs: [
                    { name: 'jest', scheduling_type: 'dag', needs: ['build 1'] }
                  ]
                },
                {
                  name: 'rspec', size: 2, jobs: [
                    { name: 'rspec 1/2', scheduling_type: 'stage' },
                    { name: 'rspec 2/2', scheduling_type: 'stage' }
                  ]
                }
              ]
            },
            {
              name: 'deploy',
              groups: [
                {
                  name: 'deploy_js', size: 1, jobs: [
                    { name: 'deploy_js', scheduling_type: 'dag', needs: ['jest'] }
                  ]
                },
                {
                  name: 'deploy_ruby', size: 1, jobs: [
                    { name: 'deploy_ruby', scheduling_type: 'dag', needs: ['rspec 1/2', 'rspec 2/2'] }
                  ]
                }
              ]
            }
          ]
        }

        expect(subject.fetch(:stages)).not_to be_empty

        expect(subject.fetch(:stages)[0].fetch(:name)).to eq 'build'
        expect(subject.fetch(:stages)[0]).to eq expected_result.fetch(:stages)[0]

        expect(subject.fetch(:stages)[1].fetch(:name)).to eq 'test'
        expect(subject.fetch(:stages)[1]).to eq expected_result.fetch(:stages)[1]

        expect(subject.fetch(:stages)[2].fetch(:name)).to eq 'deploy'
        expect(subject.fetch(:stages)[2]).to eq expected_result.fetch(:stages)[2]
      end
    end
  end
end