kubernetes_namespace_spec.rb 5.39 KB
Newer Older
1 2 3 4 5 6
# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::Ci::Build::Prerequisite::KubernetesNamespace do
  describe '#unmet?' do
7 8
    let(:build) { create(:ci_build) }

9 10 11 12 13 14 15 16 17 18
    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

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

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

25
        it { is_expected.to be_truthy }
26

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

          it { is_expected.to be_falsey }
        end

33
        context 'and a namespace is already created for this project' do
34 35 36 37 38 39
          let(:kubernetes_namespace) { instance_double(Clusters::KubernetesNamespace, service_account_token: 'token') }

          before do
            allow(Clusters::KubernetesNamespaceFinder).to receive(:new)
              .and_return(double(execute: kubernetes_namespace))
          end
40

41
          it { is_expected.to be_falsey }
42

43 44
          context 'and the service_account_token is blank' do
            let(:kubernetes_namespace) { instance_double(Clusters::KubernetesNamespace, service_account_token: nil) }
45

46
            it { is_expected.to be_truthy }
47
          end
48
        end
49
      end
50

51
      context 'and no cluster to deploy to' do
52 53
        let(:cluster) { nil }

54 55
        it { is_expected.to be_falsey }
      end
56 57 58 59
    end
  end

  describe '#complete!' do
60 61
    let(:build) { create(:ci_build) }
    let(:prerequisite) { described_class.new(build) }
62

63
    subject { prerequisite.complete! }
64 65

    context 'completion is required' do
66
      let(:cluster) { create(:cluster, :group) }
67 68 69 70 71 72 73 74 75 76 77
      let(:deployment) { create(:deployment, cluster: cluster) }
      let(:service) { double(execute: true) }
      let(:kubernetes_namespace) { double }

      before do
        allow(prerequisite).to receive(:unmet?).and_return(true)
        allow(build).to receive(:deployment).and_return(deployment)
      end

      context 'kubernetes namespace does not exist' do
        let(:namespace_builder) { double(execute: kubernetes_namespace)}
78

79 80 81 82
        before do
          allow(Clusters::KubernetesNamespaceFinder).to receive(:new)
            .and_return(double(execute: nil))
        end
83

84 85 86 87 88
        it 'creates a namespace using a new record' do
          expect(Clusters::BuildKubernetesNamespaceService)
            .to receive(:new)
            .with(cluster, environment: deployment.environment)
            .and_return(namespace_builder)
89

90
          expect(Clusters::Kubernetes::CreateOrUpdateNamespaceService)
91 92 93 94 95 96 97 98
            .to receive(:new)
            .with(cluster: cluster, kubernetes_namespace: kubernetes_namespace)
            .and_return(service)

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

          subject
        end
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

        context 'the build has a namespace configured via CI template' do
          let(:kubernetes_namespace) { double(namespace: existing_namespace) }

          before do
            allow(build).to receive(:expanded_kubernetes_namespace)
              .and_return(requested_namespace)
          end

          context 'the requested namespace matches the default' do
            let(:requested_namespace) { 'production' }
            let(:existing_namespace) { requested_namespace }

            it 'creates a namespace' do
              expect(Clusters::BuildKubernetesNamespaceService)
                .to receive(:new)
                .with(cluster, environment: deployment.environment)
                .and_return(namespace_builder)

              expect(Clusters::Kubernetes::CreateOrUpdateNamespaceService)
                .to receive(:new)
                .with(cluster: cluster, kubernetes_namespace: kubernetes_namespace)
                .and_return(service)

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

              subject
            end
          end

          context 'the requested namespace differs from the default' do
            let(:requested_namespace) { 'production' }
            let(:existing_namespace) { 'other-namespace' }

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

              subject
            end
          end
        end
140 141 142 143 144 145 146 147 148 149 150
      end

      context 'kubernetes namespace exists (but has no service_account_token)' do
        before do
          allow(Clusters::KubernetesNamespaceFinder).to receive(:new)
            .and_return(double(execute: kubernetes_namespace))
        end

        it 'creates a namespace using the tokenless record' do
          expect(Clusters::BuildKubernetesNamespaceService).not_to receive(:new)

151
          expect(Clusters::Kubernetes::CreateOrUpdateNamespaceService)
152 153 154 155 156 157
            .to receive(:new)
            .with(cluster: cluster, kubernetes_namespace: kubernetes_namespace)
            .and_return(service)

          subject
        end
158 159 160 161
      end
    end

    context 'completion is not required' do
162 163 164 165
      before do
        allow(prerequisite).to receive(:unmet?).and_return(false)
      end

166
      it 'does not create a namespace' do
167
        expect(Clusters::Kubernetes::CreateOrUpdateNamespaceService).not_to receive(:new)
168 169 170 171 172 173

        subject
      end
    end
  end
end