summaryrefslogtreecommitdiff
path: root/spec/models/clusters/kubernetes_namespace_spec.rb
blob: dea58fa26c7c2a86747cbd8d4d582860aa391300 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Clusters::KubernetesNamespace, type: :model do
  it { is_expected.to belong_to(:cluster_project) }
  it { is_expected.to belong_to(:project) }
  it { is_expected.to belong_to(:cluster) }
  it { is_expected.to have_one(:platform_kubernetes) }

  describe 'namespace uniqueness validation' do
    let(:cluster_project) { create(:cluster_project) }

    let(:kubernetes_namespace) do
      build(:cluster_kubernetes_namespace,
            cluster: cluster_project.cluster,
            project: cluster_project.project,
            cluster_project: cluster_project)
    end

    subject  { kubernetes_namespace }

    context 'when cluster is using the namespace' do
      before do
        create(:cluster_kubernetes_namespace,
               cluster: cluster_project.cluster,
               project: cluster_project.project,
               cluster_project: cluster_project,
               namespace: kubernetes_namespace.namespace)
      end

      it { is_expected.not_to be_valid }
    end

    context 'when cluster is not using the namespace' do
      it { is_expected.to be_valid }
    end
  end

  describe '#set_namespace_and_service_account_to_default' do
    let(:cluster) { platform.cluster }
    let(:cluster_project) { create(:cluster_project, cluster: cluster) }
    let(:kubernetes_namespace) do
      create(:cluster_kubernetes_namespace,
            cluster: cluster_project.cluster,
            project: cluster_project.project,
            cluster_project: cluster_project)
    end

    describe 'namespace' do
      let(:platform) { create(:cluster_platform_kubernetes, namespace: namespace) }

      subject { kubernetes_namespace.namespace }

      context 'when platform has a namespace assigned' do
        let(:namespace) { 'platform-namespace' }

        it 'should copy the namespace' do
          is_expected.to eq('platform-namespace')
        end
      end

      context 'when platform does not have namespace assigned' do
        let(:namespace) { nil }

        it 'should set default namespace' do
          project_slug = "#{cluster_project.project.path}-#{cluster_project.project_id}"

          is_expected.to eq(project_slug)
        end
      end
    end

    describe 'service_account_name' do
      let(:platform) { create(:cluster_platform_kubernetes) }

      subject { kubernetes_namespace.service_account_name }

      it 'should set a service account name based on namespace' do
        is_expected.to eq("#{kubernetes_namespace.namespace}-service-account")
      end
    end
  end
end