summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ci/build/prerequisite/kubernetes_namespace_spec.rb
blob: 51e16c9968838c5a837d512ffbca2a767ac1ae97 (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
# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::Ci::Build::Prerequisite::KubernetesNamespace do
  let(:build) { create(:ci_build) }

  describe '#unmet?' do
    subject { described_class.new(build).unmet? }

    context 'build has no deployment' do
      before do
        expect(build.deployment).to be_nil
      end

      it { is_expected.to be_falsey }
    end

    context 'build has a deployment' do
      let!(:deployment) { create(:deployment, deployable: build) }

      context 'and a cluster to deploy to' do
        let(:cluster) { create(:cluster, :group) }

        before do
          allow(build.deployment).to receive(:deployment_platform_cluster).and_return(cluster)
        end

        it { is_expected.to be_truthy }

        context 'and the cluster is not managed' do
          let(:cluster) { create(:cluster, :not_managed, projects: [build.project]) }

          it { is_expected.to be_falsey }
        end

        context 'and a namespace is already created for this project' do
          let!(:kubernetes_namespace) { create(:cluster_kubernetes_namespace, :with_token, cluster: cluster, project: build.project) }

          it { is_expected.to be_falsey }

          context 'and the service_account_token is blank' do
            let!(:kubernetes_namespace) { create(:cluster_kubernetes_namespace, :without_token, cluster: cluster, project: build.project) }

            it { is_expected.to be_truthy }
          end
        end
      end

      context 'and no cluster to deploy to' do
        before do
          expect(deployment.deployment_platform_cluster).to be_nil
        end

        it { is_expected.to be_falsey }
      end
    end
  end

  describe '#complete!' do
    let!(:deployment) { create(:deployment, deployable: build) }
    let(:service) { double(execute: true) }

    subject { described_class.new(build).complete! }

    context 'completion is required' do
      let(:cluster) { create(:cluster, :group) }

      before do
        allow(build.deployment).to receive(:deployment_platform_cluster).and_return(cluster)
      end

      it 'creates a kubernetes namespace' do
        expect(Clusters::Gcp::Kubernetes::CreateOrUpdateNamespaceService)
          .to receive(:new)
          .with(cluster: cluster, kubernetes_namespace: instance_of(Clusters::KubernetesNamespace))
          .and_return(service)

        expect(service).to receive(:execute).once

        subject
      end
    end

    context 'completion is not required' do
      before do
        expect(deployment.deployment_platform_cluster).to be_nil
      end

      it 'does not create a namespace' do
        expect(Clusters::Gcp::Kubernetes::CreateOrUpdateNamespaceService).not_to receive(:new)

        subject
      end
    end
  end
end