Welcome to mirror list, hosted at ThFree Co, Russian Federation.

populate_cluster_kubernetes_namespace_table_spec.rb « background_migration « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 128e118ac1700a51b3629426cd4230f61062c148 (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
# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::BackgroundMigration::PopulateClusterKubernetesNamespaceTable, :migration, schema: 20181022173835 do
  include MigrationHelpers::ClusterHelpers

  let(:migration) { described_class.new }
  let(:clusters_table) { table(:clusters) }
  let(:cluster_projects_table) { table(:cluster_projects) }
  let(:cluster_kubernetes_namespaces_table) { table(:clusters_kubernetes_namespaces) }
  let(:projects_table) { table(:projects) }
  let(:namespaces_table) { table(:namespaces) }
  let(:provider_gcp_table) { table(:cluster_providers_gcp) }
  let(:platform_kubernetes_table) { table(:cluster_platforms_kubernetes) }

  before do
    create_cluster_project_list(10)
  end

  shared_examples 'consistent kubernetes namespace attributes' do
    it 'populates namespace and service account information' do
      migration.perform

      clusters_with_namespace.each do |cluster|
        cluster_project = cluster_projects_table.find_by(cluster_id: cluster.id)
        project = projects_table.find(cluster_project.project_id)
        kubernetes_namespace = cluster_kubernetes_namespaces_table.find_by(cluster_id: cluster.id)
        namespace = "#{project.path}-#{project.id}"

        expect(kubernetes_namespace).to be_present
        expect(kubernetes_namespace.cluster_project_id).to eq(cluster_project.id)
        expect(kubernetes_namespace.project_id).to eq(cluster_project.project_id)
        expect(kubernetes_namespace.cluster_id).to eq(cluster_project.cluster_id)
        expect(kubernetes_namespace.namespace).to eq(namespace)
        expect(kubernetes_namespace.service_account_name).to eq("#{namespace}-service-account")
      end
    end
  end

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

    it 'creates a Clusters::KubernetesNamespace per Clusters::Project' do
      expect do
        migration.perform
      end.to change(Clusters::KubernetesNamespace, :count).by(cluster_projects_table.count)
    end

    it_behaves_like 'consistent kubernetes namespace attributes' do
      let(:clusters_with_namespace) { clusters_table.all }
    end
  end

  context 'when every Clusters::Project has Clusters::KubernetesNamespace' do
    before do
      create_kubernetes_namespace(clusters_table.all)
    end

    it 'does not create any Clusters::KubernetesNamespace' do
      expect do
        migration.perform
      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_table.first(6) }
    let(:with_no_kubernetes_namespace) { clusters_table.last(4) }

    before do
      create_kubernetes_namespace(with_kubernetes_namespace)
    end

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

    it 'does not modify clusters with Clusters::KubernetesNamespace' do
      migration.perform

      with_kubernetes_namespace.each do |cluster|
        kubernetes_namespace = cluster_kubernetes_namespaces_table.where(cluster_id: cluster.id)
        expect(kubernetes_namespace.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