summaryrefslogtreecommitdiff
path: root/spec/migrations/migrate_auto_dev_ops_domain_to_cluster_domain_spec.rb
blob: 2ffc0e65feec8b9e2508efc9c4f8041eab00c68f (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
98
99
100
101
102
103
104
105
106
# frozen_string_literal: true

require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20190204115450_migrate_auto_dev_ops_domain_to_cluster_domain.rb')

describe MigrateAutoDevOpsDomainToClusterDomain, :migration do
  include MigrationHelpers::ClusterHelpers

  let(:migration) { described_class.new }
  let(:project_auto_devops_table) { table(:project_auto_devops) }
  let(:clusters_table) { table(:clusters) }
  let(:cluster_projects_table) { table(:cluster_projects) }

  # Following lets are needed by MigrationHelpers::ClusterHelpers
  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
    setup_cluster_projects_with_domain(quantity: 20, domain: domain)
  end

  context 'with ProjectAutoDevOps with no domain' do
    let(:domain) { nil }

    it 'should not update cluster project' do
      migrate!

      expect(clusters_without_domain.count).to eq(clusters_table.count)
    end
  end

  context 'with ProjectAutoDevOps with domain' do
    let(:domain) { 'example-domain.com' }

    it 'should update all cluster projects' do
      migrate!

      expect(clusters_with_domain.count).to eq(clusters_table.count)
    end
  end

  context 'when only some ProjectAutoDevOps have domain set' do
    let(:domain) { 'example-domain.com' }

    before do
      setup_cluster_projects_with_domain(quantity: 25, domain: nil)
    end

    it 'should only update specific cluster projects' do
      migrate!

      expect(clusters_with_domain.count).to eq(20)

      project_auto_devops_with_domain.each do |project_auto_devops|
        cluster_project = Clusters::Project.find_by(project_id: project_auto_devops.project_id)
        cluster = Clusters::Cluster.find(cluster_project.cluster_id)

        expect(cluster.domain).to be_present
      end

      expect(clusters_without_domain.count).to eq(25)

      project_auto_devops_without_domain.each do |project_auto_devops|
        cluster_project = Clusters::Project.find_by(project_id: project_auto_devops.project_id)
        cluster = Clusters::Cluster.find(cluster_project.cluster_id)

        expect(cluster.domain).not_to be_present
      end
    end
  end

  def setup_cluster_projects_with_domain(quantity:, domain:)
    create_cluster_project_list(quantity)

    cluster_projects = cluster_projects_table.last(quantity)

    cluster_projects.each do |cluster_project|
      specific_domain = "#{cluster_project.id}-#{domain}" if domain

      project_auto_devops_table.create(
        project_id: cluster_project.project_id,
        enabled: true,
        domain: specific_domain
      )
    end
  end

  def project_auto_devops_with_domain
    project_auto_devops_table.where.not("domain IS NULL OR domain = ''")
  end

  def project_auto_devops_without_domain
    project_auto_devops_table.where("domain IS NULL OR domain = ''")
  end

  def clusters_with_domain
    clusters_table.where.not("domain IS NULL OR domain = ''")
  end

  def clusters_without_domain
    clusters_table.where("domain IS NULL OR domain = ''")
  end
end