summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ci/pipeline/seed/deployment_spec.rb
blob: 51185be3e74bd8b3e713124dd3ce656df3aaa34a (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Ci::Pipeline::Seed::Deployment do
  let_it_be(:project, refind: true) { create(:project, :repository) }

  let(:pipeline) do
    create(:ci_pipeline, project: project,
           sha: 'b83d6e391c22777fca1ed3012fce84f633d7fed0')
  end

  let(:job) { build(:ci_build, project: project, pipeline: pipeline) }
  let(:environment) { Gitlab::Ci::Pipeline::Seed::Environment.new(job).to_resource }
  let(:seed) { described_class.new(job, environment) }
  let(:attributes) { {} }

  before do
    job.assign_attributes(**attributes)
  end

  describe '#to_resource' do
    subject { seed.to_resource }

    context 'when job has environment attribute' do
      let(:attributes) do
        {
          environment: 'production',
          options: { environment: { name: 'production', **kubernetes_options } }
        }
      end

      let(:kubernetes_options) { {} }

      it 'returns a deployment object with environment' do
        expect(subject).to be_a(Deployment)
        expect(subject.iid).to be_present
        expect(subject.environment.name).to eq('production')
        expect(subject.cluster).to be_nil
        expect(subject.deployment_cluster).to be_nil
      end

      context 'when environment has deployment platform' do
        let!(:cluster) { create(:cluster, :provided_by_gcp, projects: [project], managed: managed_cluster) }
        let(:managed_cluster) { true }

        it 'sets the cluster and deployment_cluster' do
          expect(subject.cluster).to eq(cluster) # until we stop double writing in 12.9: https://gitlab.com/gitlab-org/gitlab/issues/202628
          expect(subject.deployment_cluster.cluster).to eq(cluster)
        end

        context 'when a custom namespace is given' do
          let(:kubernetes_options) { { kubernetes: { namespace: 'the-custom-namespace' } } }

          context 'when cluster is managed' do
            it 'does not set the custom namespace' do
              expect(subject.deployment_cluster.kubernetes_namespace).not_to eq('the-custom-namespace')
            end
          end

          context 'when cluster is not managed' do
            let(:managed_cluster) { false }

            it 'sets the custom namespace' do
              expect(subject.deployment_cluster.kubernetes_namespace).to eq('the-custom-namespace')
            end
          end
        end
      end

      context 'when environment has an invalid URL' do
        let(:attributes) do
          {
            environment: '!!!',
            options: { environment: { name: '!!!' } }
          }
        end

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

      context 'when job has already deployment' do
        let(:job) { build(:ci_build, :with_deployment, project: project, environment: 'production') }

        it 'returns the persisted deployment' do
          is_expected.to eq(job.deployment)
        end
      end
    end

    context 'when job does not start environment' do
      where(:action) do
        %w(stop prepare verify access)
      end

      with_them do
        let(:attributes) do
          {
            environment: 'production',
            options: { environment: { name: 'production', action: action } }
          }
        end

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

    context 'when job does not have environment attribute' do
      let(:attributes) { { name: 'test' } }

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