summaryrefslogtreecommitdiff
path: root/spec/services/ci/create_pipeline_service/parallel_spec.rb
blob: 5e34a67d376bcf74c5881cdd2a4707f7cbc34ed6 (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
# frozen_string_literal: true
require 'spec_helper'

RSpec.describe Ci::CreatePipelineService do
  let_it_be(:project) { create(:project, :repository) }
  let_it_be(:user)    { project.owner }

  let(:service)  { described_class.new(project, user, { ref: 'master' }) }
  let(:pipeline) { service.execute(:push) }

  before do
    stub_ci_pipeline_yaml_file(config)
  end

  context 'job:parallel' do
    context 'numeric' do
      let(:config) do
        <<-EOY
        job:
          script: "echo job"
          parallel: 3
        EOY
      end

      it 'creates the pipeline' do
        expect(pipeline).to be_created_successfully
      end

      it 'creates 3 jobs' do
        expect(pipeline.processables.pluck(:name)).to contain_exactly(
          'job 1/3', 'job 2/3', 'job 3/3'
        )
      end
    end

    context 'matrix' do
      let(:config) do
        <<-EOY
        job:
          script: "echo job"
          parallel:
            matrix:
              - PROVIDER: ovh
                STACK: [monitoring, app]
              - PROVIDER: [gcp, vultr]
                STACK: [data]
        EOY
      end

      it 'creates the pipeline' do
        expect(pipeline).to be_created_successfully
      end

      it 'creates 4 builds with the corresponding matrix variables' do
        expect(pipeline.processables.pluck(:name)).to contain_exactly(
          'job: [gcp, data]', 'job: [ovh, app]', 'job: [ovh, monitoring]', 'job: [vultr, data]'
        )

        job1 = find_job('job: [gcp, data]')
        job2 = find_job('job: [ovh, app]')
        job3 = find_job('job: [ovh, monitoring]')
        job4 = find_job('job: [vultr, data]')

        expect(job1.scoped_variables.to_hash).to include('PROVIDER' => 'gcp', 'STACK' => 'data')
        expect(job2.scoped_variables.to_hash).to include('PROVIDER' => 'ovh', 'STACK' => 'app')
        expect(job3.scoped_variables.to_hash).to include('PROVIDER' => 'ovh', 'STACK' => 'monitoring')
        expect(job4.scoped_variables.to_hash).to include('PROVIDER' => 'vultr', 'STACK' => 'data')
      end

      context 'when a bridge is using parallel:matrix' do
        let(:config) do
          <<-EOY
          job:
            stage: test
            script: "echo job"

          deploy:
            stage: deploy
            trigger:
              include: child.yml
            parallel:
              matrix:
                - PROVIDER: ovh
                  STACK: [monitoring, app]
                - PROVIDER: [gcp, vultr]
                  STACK: [data]
          EOY
        end

        it 'creates the pipeline' do
          expect(pipeline).to be_created_successfully
        end

        it 'creates 1 build and 4 bridges with the corresponding matrix variables' do
          expect(pipeline.processables.pluck(:name)).to contain_exactly(
            'job', 'deploy: [gcp, data]', 'deploy: [ovh, app]', 'deploy: [ovh, monitoring]', 'deploy: [vultr, data]'
          )

          bridge1 = find_job('deploy: [gcp, data]')
          bridge2 = find_job('deploy: [ovh, app]')
          bridge3 = find_job('deploy: [ovh, monitoring]')
          bridge4 = find_job('deploy: [vultr, data]')

          expect(bridge1.scoped_variables.to_hash).to include('PROVIDER' => 'gcp', 'STACK' => 'data')
          expect(bridge2.scoped_variables.to_hash).to include('PROVIDER' => 'ovh', 'STACK' => 'app')
          expect(bridge3.scoped_variables.to_hash).to include('PROVIDER' => 'ovh', 'STACK' => 'monitoring')
          expect(bridge4.scoped_variables.to_hash).to include('PROVIDER' => 'vultr', 'STACK' => 'data')
        end
      end
    end
  end

  private

  def find_job(name)
    pipeline.processables.find { |job| job.name == name }
  end
end