summaryrefslogtreecommitdiff
path: root/spec/services/ci/create_pipeline_service/evaluate_runner_tags_spec.rb
blob: 9add096d782f0359f9282d35934000893d8e016a (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
144
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Ci::CreatePipelineService do
  let_it_be(:group)          { create(:group, :private) }
  let_it_be(:group_variable) { create(:ci_group_variable, group: group, key: 'RUNNER_TAG', value: 'group')}
  let_it_be(:project)        { create(:project, :repository, group: group) }
  let_it_be(:user)           { create(:user) }

  let(:service)  { described_class.new(project, user, ref: 'master') }
  let(:pipeline) { service.execute(:push).payload }
  let(:job)      { pipeline.builds.find_by(name: 'job') }

  before do
    project.add_developer(user)
    stub_ci_pipeline_yaml_file config
  end

  context 'when the variable is set' do
    let(:config) do
      <<~EOS
        variables:
          KUBERNETES_RUNNER: kubernetes

        job:
          tags:
            - docker
            - $KUBERNETES_RUNNER
          script:
            - echo "Hello runner selector feature"
      EOS
    end

    it 'uses the evaluated variable' do
      expect(pipeline).to be_created_successfully
      expect(job.tags.pluck(:name)).to match_array(%w[docker kubernetes])
    end
  end

  context 'when the tag is composed by two variables' do
    let(:config) do
      <<~EOS
        variables:
          CLOUD_PROVIDER: aws
          KUBERNETES_RUNNER: kubernetes
          ENVIRONMENT_NAME: prod

        job:
          tags:
            - docker
            - $CLOUD_PROVIDER-$KUBERNETES_RUNNER-$ENVIRONMENT_NAME
          script:
            - echo "Hello runner selector feature"
      EOS
    end

    it 'uses the evaluated variables' do
      expect(pipeline).to be_created_successfully
      expect(job.tags.pluck(:name)).to match_array(%w[docker aws-kubernetes-prod])
    end
  end

  context 'when the variable is not set' do
    let(:config) do
      <<~EOS
        job:
          tags:
            - docker
            - $KUBERNETES_RUNNER
          script:
            - echo "Hello runner selector feature"
      EOS
    end

    it 'uses the variable as a regular string' do
      expect(pipeline).to be_created_successfully
      expect(job.tags.pluck(:name)).to match_array(%w[docker $KUBERNETES_RUNNER])
    end
  end

  context 'when the tag uses group variables' do
    let(:config) do
      <<~EOS
        job:
          tags:
            - docker
            - $RUNNER_TAG
          script:
            - echo "Hello runner selector feature"
      EOS
    end

    it 'uses the evaluated variables' do
      expect(pipeline).to be_created_successfully
      expect(job.tags.pluck(:name)).to match_array(%w[docker group])
    end
  end

  context 'when the tag has the same variable name defined for both group and project' do
    let_it_be(:project_variable) { create(:ci_variable, project: project, key: 'RUNNER_TAG', value: 'project') }

    let(:config) do
      <<~EOS
        variables:
          RUNNER_TAG: pipeline
        job:
          tags:
            - docker
            - $RUNNER_TAG
          script:
            - echo "Hello runner selector feature"
      EOS
    end

    it 'uses the project variable instead of group due to variable precedence' do
      expect(pipeline).to be_created_successfully
      expect(job.tags.pluck(:name)).to match_array(%w[docker project])
    end
  end

  context 'with parallel:matrix config' do
    let(:tags) { pipeline.builds.map(&:tags).flatten.pluck(:name) }

    let(:config) do
      <<~EOS
        job:
          parallel:
            matrix:
              - PROVIDER: [aws, gcp]
                STACK: [monitoring, backup, app]
          tags:
            - ${PROVIDER}-${STACK}
          script:
            - echo "Hello runner selector feature"
      EOS
    end

    it 'uses the evaluated variables' do
      expect(pipeline).to be_created_successfully
      expect(tags).to match_array(%w[aws-monitoring aws-backup aws-app gcp-monitoring gcp-backup gcp-app])
    end
  end
end