summaryrefslogtreecommitdiff
path: root/spec/models/concerns/deployment_platform_spec.rb
blob: 2bb6aa27e2158dd22d7a5f572a5b03afe43afe50 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe DeploymentPlatform do
  let(:project) { create(:project) }

  describe '#deployment_platform' do
    subject { project.deployment_platform }

    context 'multiple clusters' do
      let(:group) { create(:group) }
      let(:project) { create(:project, group: group) }

      shared_examples 'matching environment scope' do
        it 'returns environment specific cluster' do
          is_expected.to eq(cluster.platform_kubernetes)
        end
      end

      shared_examples 'not matching environment scope' do
        it 'returns default cluster' do
          is_expected.to eq(default_cluster.platform_kubernetes)
        end
      end

      context 'multiple clusters use the same management project' do
        let(:management_project) { create(:project, group: group) }

        let!(:default_cluster) do
          create(:cluster_for_group, groups: [group], environment_scope: '*', management_project: management_project)
        end

        let!(:cluster) do
          create(:cluster_for_group, groups: [group], environment_scope: 'review/*', management_project: management_project)
        end

        let(:environment) { 'review/name' }

        subject { management_project.deployment_platform(environment: environment) }

        it_behaves_like 'matching environment scope'
      end

      context 'when project does not have a cluster but has group clusters' do
        let!(:default_cluster) do
          create(:cluster, :provided_by_user,
                cluster_type: :group_type, groups: [group], environment_scope: '*')
        end

        let!(:cluster) do
          create(:cluster, :provided_by_user,
                cluster_type: :group_type, environment_scope: 'review/*', groups: [group])
        end

        let(:environment) { 'review/name' }

        subject { project.deployment_platform(environment: environment) }

        context 'when environment scope is exactly matched' do
          before do
            cluster.update!(environment_scope: 'review/name')
          end

          it_behaves_like 'matching environment scope'
        end

        context 'when environment scope is matched by wildcard' do
          before do
            cluster.update!(environment_scope: 'review/*')
          end

          it_behaves_like 'matching environment scope'
        end

        context 'when environment scope does not match' do
          before do
            cluster.update!(environment_scope: 'review/*/special')
          end

          it_behaves_like 'not matching environment scope'
        end

        context 'when group belongs to a parent group' do
          let(:parent_group) { create(:group) }
          let(:group) { create(:group, parent: parent_group) }

          context 'when parent_group has a cluster with default scope' do
            let!(:parent_group_cluster) do
              create(:cluster, :provided_by_user,
                    cluster_type: :group_type, environment_scope: '*', groups: [parent_group])
            end

            it_behaves_like 'matching environment scope'
          end

          context 'when parent_group has a cluster that is an exact match' do
            let!(:parent_group_cluster) do
              create(:cluster, :provided_by_user,
                    cluster_type: :group_type, environment_scope: 'review/name', groups: [parent_group])
            end

            it_behaves_like 'matching environment scope'
          end
        end
      end

      context 'with instance clusters' do
        let!(:default_cluster) do
          create(:cluster, :provided_by_user, :instance, environment_scope: '*')
        end

        let!(:cluster) do
          create(:cluster, :provided_by_user, :instance, environment_scope: 'review/*')
        end

        let(:environment) { 'review/name' }

        subject { project.deployment_platform(environment: environment) }

        context 'when environment scope is exactly matched' do
          before do
            cluster.update!(environment_scope: 'review/name')
          end

          it_behaves_like 'matching environment scope'
        end

        context 'when environment scope is matched by wildcard' do
          before do
            cluster.update!(environment_scope: 'review/*')
          end

          it_behaves_like 'matching environment scope'
        end

        context 'when environment scope does not match' do
          before do
            cluster.update!(environment_scope: 'review/*/special')
          end

          it_behaves_like 'not matching environment scope'
        end
      end

      context 'when environment is specified' do
        let!(:default_cluster) { create(:cluster, :provided_by_user, projects: [project], environment_scope: '*') }
        let!(:cluster) { create(:cluster, :provided_by_user, environment_scope: 'review/*', projects: [project]) }

        let!(:group_default_cluster) do
          create(:cluster, :provided_by_user,
                cluster_type: :group_type, groups: [group], environment_scope: '*')
        end

        let(:environment) { 'review/name' }

        subject { project.deployment_platform(environment: environment) }

        context 'when environment scope is exactly matched' do
          before do
            cluster.update!(environment_scope: 'review/name')
          end

          it_behaves_like 'matching environment scope'
        end

        context 'when environment scope is matched by wildcard' do
          before do
            cluster.update!(environment_scope: 'review/*')
          end

          it_behaves_like 'matching environment scope'
        end

        context 'when environment scope does not match' do
          before do
            cluster.update!(environment_scope: 'review/*/special')
          end

          it_behaves_like 'not matching environment scope'
        end

        context 'when environment scope has _' do
          it 'does not treat it as wildcard' do
            cluster.update!(environment_scope: 'foo_bar/*')

            is_expected.to eq(default_cluster.platform_kubernetes)
          end

          context 'when environment name contains an underscore' do
            let(:environment) { 'foo_bar/test' }

            it 'matches literally for _' do
              cluster.update!(environment_scope: 'foo_bar/*')

              is_expected.to eq(cluster.platform_kubernetes)
            end
          end
        end

        # The environment name and scope cannot have % at the moment,
        # but we're considering relaxing it and we should also make sure
        # it doesn't break in case some data sneaked in somehow as we're
        # not checking this integrity in database level.
        context 'when environment scope has %' do
          it 'does not treat it as wildcard' do
            cluster.update_attribute(:environment_scope, '*%*')

            is_expected.to eq(default_cluster.platform_kubernetes)
          end

          context 'when environment name contains a percent char' do
            let(:environment) { 'foo%bar/test' }

            it 'matches literally for %' do
              cluster.update_attribute(:environment_scope, 'foo%bar/*')

              is_expected.to eq(cluster.platform_kubernetes)
            end
          end
        end

        context 'when perfectly matched cluster exists' do
          let!(:perfectly_matched_cluster) { create(:cluster, :provided_by_user, projects: [project], environment_scope: 'review/name') }

          it 'returns perfectly matched cluster as highest precedence' do
            is_expected.to eq(perfectly_matched_cluster.platform_kubernetes)
          end
        end
      end

      context 'with multiple clusters and multiple environments' do
        let!(:cluster_1) { create(:cluster, :provided_by_user, projects: [project], environment_scope: 'staging/*') }
        let!(:cluster_2) { create(:cluster, :provided_by_user, projects: [project], environment_scope: 'test/*') }

        let(:environment_1) { 'staging/name' }
        let(:environment_2) { 'test/name' }

        it 'returns the appropriate cluster' do
          expect(project.deployment_platform(environment: environment_1)).to eq(cluster_1.platform_kubernetes)
          expect(project.deployment_platform(environment: environment_2)).to eq(cluster_2.platform_kubernetes)
        end
      end
    end

    context 'with no Kubernetes configuration on CI/CD, no Kubernetes Service' do
      it { is_expected.to be_nil }
    end

    context 'when project is the cluster\'s management project ' do
      let(:another_project) { create(:project, namespace: project.namespace) }

      let!(:cluster_with_management_project) do
        create(:cluster, :provided_by_user, projects: [another_project], management_project: project)
      end

      context 'cluster_management_project feature is enabled' do
        it 'returns the cluster with management project' do
          is_expected.to eq(cluster_with_management_project.platform_kubernetes)
        end
      end

      context 'cluster_management_project feature is disabled' do
        before do
          stub_feature_flags(cluster_management_project: false)
        end

        it 'returns nothing' do
          is_expected.to be_nil
        end
      end
    end

    context 'when project has configured kubernetes from CI/CD > Clusters' do
      let!(:cluster) { create(:cluster, :provided_by_gcp, projects: [project]) }
      let(:platform_kubernetes) { cluster.platform_kubernetes }

      it 'returns the Kubernetes platform' do
        expect(subject).to eq(platform_kubernetes)
      end

      context 'with a group level kubernetes cluster' do
        let(:group_cluster) { create(:cluster, :provided_by_gcp, :group) }

        before do
          project.update!(group: group_cluster.group)
        end

        it 'returns the Kubernetes platform from the project cluster' do
          expect(subject).to eq(platform_kubernetes)
        end
      end
    end

    context 'when group has configured kubernetes cluster' do
      let!(:group_cluster) { create(:cluster, :provided_by_gcp, :group) }
      let(:group) { group_cluster.group }

      before do
        project.update!(group: group)
      end

      it 'returns the Kubernetes platform' do
        is_expected.to eq(group_cluster.platform_kubernetes)
      end

      context 'when project is the cluster\'s management project ' do
        let(:another_project) { create(:project, namespace: project.namespace) }

        let!(:cluster_with_management_project) do
          create(:cluster, :provided_by_user, projects: [another_project], management_project: project)
        end

        context 'cluster_management_project feature is enabled' do
          it 'returns the cluster with management project' do
            is_expected.to eq(cluster_with_management_project.platform_kubernetes)
          end
        end

        context 'cluster_management_project feature is disabled' do
          before do
            stub_feature_flags(cluster_management_project: false)
          end

          it 'returns the group cluster' do
            is_expected.to eq(group_cluster.platform_kubernetes)
          end
        end
      end

      context 'when project is not the cluster\'s management project' do
        let(:another_project) { create(:project, group: group) }
        let!(:cluster_with_management_project) { create(:cluster, :provided_by_user, management_project: another_project) }

        it 'returns the group cluster' do
          is_expected.to eq(group_cluster.platform_kubernetes)
        end
      end

      context 'when child group has configured kubernetes cluster' do
        let(:child_group1) { create(:group, parent: group) }
        let!(:child_group1_cluster) { create(:cluster_for_group, groups: [child_group1]) }

        before do
          project.update!(group: child_group1)
        end

        it 'returns the Kubernetes platform for the child group' do
          is_expected.to eq(child_group1_cluster.platform_kubernetes)
        end

        context 'deeply nested group' do
          let(:child_group2) { create(:group, parent: child_group1) }
          let!(:child_group2_cluster) { create(:cluster_for_group, groups: [child_group2]) }

          before do
            project.update!(group: child_group2)
          end

          it 'returns most nested group cluster Kubernetes platform' do
            is_expected.to eq(child_group2_cluster.platform_kubernetes)
          end

          context 'cluster in the middle of hierarchy is disabled' do
            before do
              child_group2_cluster.update!(enabled: false)
            end

            it 'returns closest enabled Kubenetes platform' do
              is_expected.to eq(child_group1_cluster.platform_kubernetes)
            end
          end
        end
      end
    end

    context 'when instance has configured kubernetes cluster' do
      let!(:instance_cluster) { create(:cluster, :provided_by_user, :instance) }

      it 'returns the Kubernetes platform' do
        is_expected.to eq(instance_cluster.platform_kubernetes)
      end
    end
  end
end