migrate_gcp_clusters_to_new_clusters_architectures_spec.rb 9.28 KB
Newer Older
1
require 'spec_helper'
2
require Rails.root.join('db', 'post_migrate', '20171013104327_migrate_gcp_clusters_to_new_clusters_architectures.rb')
3 4

describe MigrateGcpClustersToNewClustersArchitectures, :migration do
5 6
  let(:projects) { table(:projects) }
  let(:project) { projects.create }
7 8 9 10 11 12 13 14
  let(:users) { table(:users) }
  let(:user) { users.create! }
  let(:service) { GcpMigrationSpec::KubernetesService.create!(project_id: project.id) }

  module GcpMigrationSpec
    class KubernetesService < ActiveRecord::Base
      self.table_name = 'services'

Lin Jen-Shin's avatar
Lin Jen-Shin committed
15
      serialize :properties, JSON
16 17 18 19 20 21 22 23 24

      default_value_for :active, true
      default_value_for :type, 'KubernetesService'
      default_value_for :properties, {
        api_url: 'https://kubernetes.example.com',
        token: 'a' * 40
      }
    end
  end
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
  context 'when cluster is being created' do
    let(:project_id) { project.id }
    let(:user_id) { user.id }
    let(:service_id) { service.id }
    let(:status) { 2 } # creating
    let(:gcp_cluster_size) { 1 }
    let(:created_at) { "'2017-10-17 20:24:02'" }
    let(:updated_at) { "'2017-10-17 20:28:44'" }
    let(:enabled) { true }
    let(:status_reason) { "''" }
    let(:project_namespace) { "'sample-app'" }
    let(:endpoint) { 'NULL' }
    let(:ca_cert) { 'NULL' }
    let(:encrypted_kubernetes_token) { 'NULL' }
    let(:encrypted_kubernetes_token_iv) { 'NULL' }
    let(:username) { 'NULL' }
    let(:encrypted_password) { 'NULL' }
    let(:encrypted_password_iv) { 'NULL' }
    let(:gcp_project_id) { "'gcp_project_id'" }
    let(:gcp_cluster_zone) { "'gcp_cluster_zone'" }
    let(:gcp_cluster_name) { "'gcp_cluster_name'" }
    let(:gcp_machine_type) { "'gcp_machine_type'" }
    let(:gcp_operation_id) { 'NULL' }
    let(:encrypted_gcp_token) { "'encrypted_gcp_token'" }
    let(:encrypted_gcp_token_iv) { "'encrypted_gcp_token_iv'" }

52
    let(:cluster) { described_class::Cluster.last }
53 54 55 56 57 58 59 60 61 62 63 64
    let(:cluster_id) { cluster.id }

    before do
      ActiveRecord::Base.connection.execute <<-SQL
        INSERT INTO gcp_clusters (project_id, user_id, service_id, status, gcp_cluster_size, created_at, updated_at, enabled, status_reason, project_namespace, endpoint, ca_cert, encrypted_kubernetes_token, encrypted_kubernetes_token_iv, username, encrypted_password, encrypted_password_iv, gcp_project_id, gcp_cluster_zone, gcp_cluster_name, gcp_machine_type, gcp_operation_id, encrypted_gcp_token, encrypted_gcp_token_iv)
        VALUES (#{project_id}, #{user_id}, #{service_id}, #{status}, #{gcp_cluster_size}, #{created_at}, #{updated_at}, #{enabled}, #{status_reason}, #{project_namespace}, #{endpoint}, #{ca_cert}, #{encrypted_kubernetes_token}, #{encrypted_kubernetes_token_iv}, #{username}, #{encrypted_password}, #{encrypted_password_iv}, #{gcp_project_id}, #{gcp_cluster_zone}, #{gcp_cluster_name}, #{gcp_machine_type}, #{gcp_operation_id}, #{encrypted_gcp_token}, #{encrypted_gcp_token_iv});
      SQL
    end

    it 'correctly migrate to new clusters architectures' do
      migrate!

65 66 67 68
      expect(described_class::Cluster.count).to eq(1)
      expect(described_class::ClustersProject.count).to eq(1)
      expect(described_class::ProvidersGcp.count).to eq(1)
      expect(described_class::PlatformsKubernetes.count).to eq(1)
69

70
      expect(cluster.user_id).to eq(user.id)
71 72 73 74 75
      expect(cluster.enabled).to be_truthy
      expect(cluster.name).to eq(gcp_cluster_name.delete!("'"))
      expect(cluster.provider_type).to eq('gcp')
      expect(cluster.platform_type).to eq('kubernetes')

76
      expect(cluster.project_ids).to include(project.id)
77

78
      expect(cluster.provider_gcp.cluster_id).to eq(cluster.id)
79 80 81 82 83 84 85 86 87 88 89
      expect(cluster.provider_gcp.status).to eq(status)
      expect(cluster.provider_gcp.status_reason).to eq(tr(status_reason))
      expect(cluster.provider_gcp.gcp_project_id).to eq(tr(gcp_project_id))
      expect(cluster.provider_gcp.zone).to eq(tr(gcp_cluster_zone))
      expect(cluster.provider_gcp.num_nodes).to eq(gcp_cluster_size)
      expect(cluster.provider_gcp.machine_type).to eq(tr(gcp_machine_type))
      expect(cluster.provider_gcp.operation_id).to be_nil
      expect(cluster.provider_gcp.endpoint).to be_nil
      expect(cluster.provider_gcp.encrypted_access_token).to eq(tr(encrypted_gcp_token))
      expect(cluster.provider_gcp.encrypted_access_token_iv).to eq(tr(encrypted_gcp_token_iv))

90
      expect(cluster.platform_kubernetes.cluster_id).to eq(cluster.id)
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
      expect(cluster.platform_kubernetes.api_url).to be_nil
      expect(cluster.platform_kubernetes.ca_cert).to be_nil
      expect(cluster.platform_kubernetes.namespace).to eq(tr(project_namespace))
      expect(cluster.platform_kubernetes.username).to be_nil
      expect(cluster.platform_kubernetes.encrypted_password).to be_nil
      expect(cluster.platform_kubernetes.encrypted_password_iv).to be_nil
      expect(cluster.platform_kubernetes.encrypted_token).to be_nil
      expect(cluster.platform_kubernetes.encrypted_token_iv).to be_nil
    end
  end

  context 'when cluster has been created' do
    let(:project_id) { project.id }
    let(:user_id) { user.id }
    let(:service_id) { service.id }
    let(:status) { 3 } # created
    let(:gcp_cluster_size) { 1 }
    let(:created_at) { "'2017-10-17 20:24:02'" }
    let(:updated_at) { "'2017-10-17 20:28:44'" }
    let(:enabled) { true }
    let(:status_reason) { "'general error'" }
    let(:project_namespace) { "'sample-app'" }
    let(:endpoint) { "'111.111.111.111'" }
    let(:ca_cert) { "'ca_cert'" }
    let(:encrypted_kubernetes_token) { "'encrypted_kubernetes_token'" }
    let(:encrypted_kubernetes_token_iv) { "'encrypted_kubernetes_token_iv'" }
    let(:username) { "'username'" }
    let(:encrypted_password) { "'encrypted_password'" }
    let(:encrypted_password_iv) { "'encrypted_password_iv'" }
    let(:gcp_project_id) { "'gcp_project_id'" }
    let(:gcp_cluster_zone) { "'gcp_cluster_zone'" }
    let(:gcp_cluster_name) { "'gcp_cluster_name'" }
    let(:gcp_machine_type) { "'gcp_machine_type'" }
    let(:gcp_operation_id) { "'gcp_operation_id'" }
    let(:encrypted_gcp_token) { "'encrypted_gcp_token'" }
    let(:encrypted_gcp_token_iv) { "'encrypted_gcp_token_iv'" }

128
    let(:cluster) { described_class::Cluster.last }
129 130 131 132 133 134 135 136 137 138 139 140
    let(:cluster_id) { cluster.id }

    before do
      ActiveRecord::Base.connection.execute <<-SQL
        INSERT INTO gcp_clusters (project_id, user_id, service_id, status, gcp_cluster_size, created_at, updated_at, enabled, status_reason, project_namespace, endpoint, ca_cert, encrypted_kubernetes_token, encrypted_kubernetes_token_iv, username, encrypted_password, encrypted_password_iv, gcp_project_id, gcp_cluster_zone, gcp_cluster_name, gcp_machine_type, gcp_operation_id, encrypted_gcp_token, encrypted_gcp_token_iv)
        VALUES (#{project_id}, #{user_id}, #{service_id}, #{status}, #{gcp_cluster_size}, #{created_at}, #{updated_at}, #{enabled}, #{status_reason}, #{project_namespace}, #{endpoint}, #{ca_cert}, #{encrypted_kubernetes_token}, #{encrypted_kubernetes_token_iv}, #{username}, #{encrypted_password}, #{encrypted_password_iv}, #{gcp_project_id}, #{gcp_cluster_zone}, #{gcp_cluster_name}, #{gcp_machine_type}, #{gcp_operation_id}, #{encrypted_gcp_token}, #{encrypted_gcp_token_iv});
      SQL
    end

    it 'correctly migrate to new clusters architectures' do
      migrate!

141 142 143 144
      expect(described_class::Cluster.count).to eq(1)
      expect(described_class::ClustersProject.count).to eq(1)
      expect(described_class::ProvidersGcp.count).to eq(1)
      expect(described_class::PlatformsKubernetes.count).to eq(1)
145

146
      expect(cluster.user_id).to eq(user.id)
147 148 149 150 151
      expect(cluster.enabled).to be_truthy
      expect(cluster.name).to eq(tr(gcp_cluster_name))
      expect(cluster.provider_type).to eq('gcp')
      expect(cluster.platform_type).to eq('kubernetes')

152
      expect(cluster.project_ids).to include(project.id)
153

154
      expect(cluster.provider_gcp.cluster_id).to eq(cluster.id)
155 156 157 158 159 160 161 162 163 164 165
      expect(cluster.provider_gcp.status).to eq(status)
      expect(cluster.provider_gcp.status_reason).to eq(tr(status_reason))
      expect(cluster.provider_gcp.gcp_project_id).to eq(tr(gcp_project_id))
      expect(cluster.provider_gcp.zone).to eq(tr(gcp_cluster_zone))
      expect(cluster.provider_gcp.num_nodes).to eq(gcp_cluster_size)
      expect(cluster.provider_gcp.machine_type).to eq(tr(gcp_machine_type))
      expect(cluster.provider_gcp.operation_id).to eq(tr(gcp_operation_id))
      expect(cluster.provider_gcp.endpoint).to eq(tr(endpoint))
      expect(cluster.provider_gcp.encrypted_access_token).to eq(tr(encrypted_gcp_token))
      expect(cluster.provider_gcp.encrypted_access_token_iv).to eq(tr(encrypted_gcp_token_iv))

166
      expect(cluster.platform_kubernetes.cluster_id).to eq(cluster.id)
167 168 169 170 171 172 173 174 175
      expect(cluster.platform_kubernetes.api_url).to eq('https://' + tr(endpoint))
      expect(cluster.platform_kubernetes.ca_cert).to eq(tr(ca_cert))
      expect(cluster.platform_kubernetes.namespace).to eq(tr(project_namespace))
      expect(cluster.platform_kubernetes.username).to eq(tr(username))
      expect(cluster.platform_kubernetes.encrypted_password).to eq(tr(encrypted_password))
      expect(cluster.platform_kubernetes.encrypted_password_iv).to eq(tr(encrypted_password_iv))
      expect(cluster.platform_kubernetes.encrypted_token).to eq(tr(encrypted_kubernetes_token))
      expect(cluster.platform_kubernetes.encrypted_token_iv).to eq(tr(encrypted_kubernetes_token_iv))
    end
176 177
  end

178 179
  def tr(str)
    str.delete("'")
180 181
  end
end