summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/background_migration/populate_cluster_kubernetes_namespace_table_spec.rb
blob: 4f1b01eed410694219834cd98dedef6d6d3a24e7 (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::BackgroundMigration::PopulateClusterKubernetesNamespaceTable, :migration, schema: 20181022173835 do
  let(:migration) { described_class.new }
  let(:clusters) { create_list(:cluster, 10, :project, :provided_by_gcp) }

  before do
    clusters
  end

  shared_examples 'consistent kubernetes namespace attributes' do
    it 'should populate namespace and service account information' do
      subject

      clusters_with_namespace.each do |cluster|
        project = cluster.project
        cluster_project = cluster.cluster_projects.first
        namespace = "#{project.path}-#{project.id}"
        kubernetes_namespace = cluster.reload.kubernetes_namespace

        expect(kubernetes_namespace).to be_present
        expect(kubernetes_namespace.cluster_project).to eq(cluster_project)
        expect(kubernetes_namespace.project).to eq(cluster_project.project)
        expect(kubernetes_namespace.cluster).to eq(cluster_project.cluster)
        expect(kubernetes_namespace.namespace).to eq(namespace)
        expect(kubernetes_namespace.service_account_name).to eq("#{namespace}-service-account")
      end
    end
  end

  subject { migration.perform }

  context 'when no Clusters::Project has a Clusters::KubernetesNamespace' do
    let(:cluster_projects) { Clusters::Project.all }

    it 'should create a Clusters::KubernetesNamespace per Clusters::Project' do
      expect do
        subject
      end.to change(Clusters::KubernetesNamespace, :count).by(cluster_projects.count)
    end

    it_behaves_like 'consistent kubernetes namespace attributes' do
      let(:clusters_with_namespace) { clusters }
    end
  end

  context 'when every Clusters::Project has Clusters::KubernetesNamespace' do
    before do
      clusters.each do |cluster|
        create(:cluster_kubernetes_namespace,
               cluster_project: cluster.cluster_projects.first,
               cluster: cluster,
               project: cluster.project)
      end
    end

    it 'should not create any Clusters::KubernetesNamespace' do
      expect do
        subject
      end.not_to change(Clusters::KubernetesNamespace, :count)
    end
  end

  context 'when only some Clusters::Project have Clusters::KubernetesNamespace related' do
    let(:with_kubernetes_namespace) { clusters.first(6) }
    let(:with_no_kubernetes_namespace) { clusters.last(4) }

    before do
      with_kubernetes_namespace.each do |cluster|
        create(:cluster_kubernetes_namespace,
               cluster_project: cluster.cluster_projects.first,
               cluster: cluster,
               project: cluster.project)
      end
    end

    it 'creates limited number of Clusters::KubernetesNamespace' do
      expect do
        subject
      end.to change(Clusters::KubernetesNamespace, :count).by(with_no_kubernetes_namespace.count)
    end

    it 'should not modify clusters with Clusters::KubernetesNamespace' do
      subject

      with_kubernetes_namespace.each do |cluster|
        expect(cluster.kubernetes_namespaces.count).to eq(1)
      end
    end

    it_behaves_like 'consistent kubernetes namespace attributes' do
      let(:clusters_with_namespace) { with_no_kubernetes_namespace }
    end
  end
end