summaryrefslogtreecommitdiff
path: root/spec/services/clusters/build_kubernetes_namespace_service_spec.rb
blob: 36c0546954251a30e7108185a5d8b5ce3b2e3fab (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
# frozen_string_literal: true

require 'spec_helper'

describe Clusters::BuildKubernetesNamespaceService do
  let(:cluster) { create(:cluster, :project, :provided_by_gcp) }
  let(:environment) { create(:environment) }
  let(:project) { environment.project }

  let(:namespace_generator) { double(from_environment_slug: namespace) }
  let(:namespace) { 'namespace' }

  subject { described_class.new(cluster, environment: environment).execute }

  before do
    allow(Gitlab::Kubernetes::DefaultNamespace).to receive(:new).and_return(namespace_generator)
  end

  shared_examples 'shared attributes' do
    it 'initializes a new namespace and sets default values' do
      expect(subject).to be_new_record
      expect(subject.cluster).to eq cluster
      expect(subject.project).to eq project
      expect(subject.namespace).to eq namespace
      expect(subject.service_account_name).to eq "#{namespace}-service-account"
    end
  end

  include_examples 'shared attributes'

  it 'sets cluster_project and environment' do
    expect(subject.cluster_project).to eq cluster.cluster_project
    expect(subject.environment).to eq environment
  end

  context 'namespace per environment is disabled' do
    let(:cluster) { create(:cluster, :project, :provided_by_gcp, :namespace_per_environment_disabled) }

    include_examples 'shared attributes'

    it 'does not set environment' do
      expect(subject.cluster_project).to eq cluster.cluster_project
      expect(subject.environment).to be_nil
    end
  end

  context 'group cluster' do
    let(:cluster) { create(:cluster, :group, :provided_by_gcp) }

    include_examples 'shared attributes'

    it 'does not set cluster_project' do
      expect(subject.cluster_project).to be_nil
      expect(subject.environment).to eq environment
    end
  end
end