2017-11-28 17:38:55 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
require Rails.root.join('db', 'post_migrate', '20171124104327_migrate_kubernetes_service_to_new_clusters_architectures.rb')
|
|
|
|
|
|
|
|
describe MigrateKubernetesServiceToNewClustersArchitectures, :migration do
|
2017-12-14 17:56:23 +00:00
|
|
|
context 'when unique KubernetesService exists' do
|
|
|
|
shared_examples 'KubernetesService migration' do
|
|
|
|
let(:sample_num) { 2 }
|
2018-01-08 07:17:35 +00:00
|
|
|
|
|
|
|
let(:projects) do
|
|
|
|
(1..sample_num).each_with_object([]) do |n, array|
|
|
|
|
array << MigrateKubernetesServiceToNewClustersArchitectures::Project.create!
|
|
|
|
end
|
|
|
|
end
|
2017-12-14 17:56:23 +00:00
|
|
|
|
|
|
|
let!(:kubernetes_services) do
|
|
|
|
projects.map do |project|
|
2018-01-08 07:17:35 +00:00
|
|
|
MigrateKubernetesServiceToNewClustersArchitectures::Service.create!(
|
|
|
|
project: project,
|
|
|
|
active: active,
|
|
|
|
category: 'deployment',
|
|
|
|
type: 'KubernetesService',
|
|
|
|
properties: "{\"namespace\":\"prod\",\"api_url\":\"https://kubernetes#{project.id}.com\",\"ca_pem\":\"ca_pem#{project.id}\",\"token\":\"token#{project.id}\"}")
|
2017-11-28 17:38:55 +00:00
|
|
|
end
|
2017-12-14 17:56:23 +00:00
|
|
|
end
|
2017-11-28 17:38:55 +00:00
|
|
|
|
2017-12-14 17:56:23 +00:00
|
|
|
it 'migrates the KubernetesService to Platform::Kubernetes' do
|
2018-01-08 07:17:35 +00:00
|
|
|
expect { migrate! }.to change { MigrateKubernetesServiceToNewClustersArchitectures::Cluster.count }.by(sample_num)
|
2017-12-14 17:56:23 +00:00
|
|
|
|
|
|
|
projects.each do |project|
|
|
|
|
project.clusters.last.tap do |cluster|
|
|
|
|
expect(cluster.enabled).to eq(active)
|
|
|
|
expect(cluster.platform_kubernetes.api_url).to eq(project.kubernetes_service.api_url)
|
2018-01-08 07:17:35 +00:00
|
|
|
expect(cluster.platform_kubernetes.ca_cert).to eq(project.kubernetes_service.ca_pem)
|
2017-12-14 17:56:23 +00:00
|
|
|
expect(cluster.platform_kubernetes.token).to eq(project.kubernetes_service.token)
|
|
|
|
expect(project.kubernetes_service).not_to be_active
|
2017-11-28 17:38:55 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-12-14 17:56:23 +00:00
|
|
|
context 'when KubernetesService is active' do
|
|
|
|
let(:active) { true }
|
2017-11-28 17:38:55 +00:00
|
|
|
|
2017-12-14 17:56:23 +00:00
|
|
|
it_behaves_like 'KubernetesService migration'
|
|
|
|
end
|
2017-11-28 17:38:55 +00:00
|
|
|
end
|
|
|
|
|
2017-12-14 17:56:23 +00:00
|
|
|
context 'when unique KubernetesService spawned from Service Template' do
|
2017-12-15 13:43:25 +00:00
|
|
|
let(:sample_num) { 2 }
|
2018-01-08 07:17:35 +00:00
|
|
|
|
|
|
|
let(:projects) do
|
|
|
|
(1..sample_num).each_with_object([]) do |n, array|
|
|
|
|
array << MigrateKubernetesServiceToNewClustersArchitectures::Project.create!
|
|
|
|
end
|
|
|
|
end
|
2017-12-15 13:43:25 +00:00
|
|
|
|
|
|
|
let!(:kubernetes_service_template) do
|
2018-01-08 07:17:35 +00:00
|
|
|
MigrateKubernetesServiceToNewClustersArchitectures::Service.create!(
|
|
|
|
template: true,
|
|
|
|
category: 'deployment',
|
|
|
|
type: 'KubernetesService',
|
|
|
|
properties: "{\"namespace\":\"prod\",\"api_url\":\"https://sample.kubernetes.com\",\"ca_pem\":\"ca_pem-sample\",\"token\":\"token-sample\"}")
|
2017-12-15 13:43:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
let!(:kubernetes_services) do
|
|
|
|
projects.map do |project|
|
2018-01-08 07:17:35 +00:00
|
|
|
MigrateKubernetesServiceToNewClustersArchitectures::Service.create!(
|
|
|
|
project: project,
|
|
|
|
category: 'deployment',
|
|
|
|
type: 'KubernetesService',
|
|
|
|
properties: "{\"namespace\":\"prod\",\"api_url\":\"#{kubernetes_service_template.api_url}\",\"ca_pem\":\"#{kubernetes_service_template.ca_pem}\",\"token\":\"#{kubernetes_service_template.token}\"}")
|
2017-12-15 13:43:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'migrates the KubernetesService to Platform::Kubernetes without template' do
|
2018-01-08 07:17:35 +00:00
|
|
|
expect { migrate! }.to change { MigrateKubernetesServiceToNewClustersArchitectures::Cluster.count }.by(sample_num)
|
2017-11-28 17:38:55 +00:00
|
|
|
|
2017-12-15 13:43:25 +00:00
|
|
|
projects.each do |project|
|
|
|
|
project.clusters.last.tap do |cluster|
|
|
|
|
expect(cluster.platform_kubernetes.api_url).to eq(project.kubernetes_service.api_url)
|
2018-01-08 07:17:35 +00:00
|
|
|
expect(cluster.platform_kubernetes.ca_cert).to eq(project.kubernetes_service.ca_pem)
|
2017-12-15 13:43:25 +00:00
|
|
|
expect(cluster.platform_kubernetes.token).to eq(project.kubernetes_service.token)
|
|
|
|
expect(project.kubernetes_service).not_to be_active
|
|
|
|
end
|
|
|
|
end
|
2017-11-28 17:38:55 +00:00
|
|
|
end
|
2017-12-14 17:56:23 +00:00
|
|
|
end
|
2017-11-28 17:38:55 +00:00
|
|
|
|
2018-01-03 11:58:54 +00:00
|
|
|
context 'when managed KubernetesService exists' do
|
2018-01-08 07:17:35 +00:00
|
|
|
let(:project) { MigrateKubernetesServiceToNewClustersArchitectures::Project.create! }
|
|
|
|
|
|
|
|
let(:cluster) do
|
|
|
|
MigrateKubernetesServiceToNewClustersArchitectures::Cluster.create!(
|
2018-01-08 08:01:29 +00:00
|
|
|
projects: [project],
|
|
|
|
name: 'sample-cluster',
|
|
|
|
platform_type: :kubernetes,
|
|
|
|
provider_type: :user,
|
|
|
|
platform_kubernetes_attributes: {
|
|
|
|
api_url: 'https://sample.kubernetes.com',
|
|
|
|
ca_cert: 'ca_pem-sample',
|
|
|
|
token: 'token-sample'
|
|
|
|
} )
|
2018-01-08 07:17:35 +00:00
|
|
|
end
|
2017-12-15 13:43:25 +00:00
|
|
|
|
|
|
|
let!(:kubernetes_service) do
|
2018-01-08 07:17:35 +00:00
|
|
|
MigrateKubernetesServiceToNewClustersArchitectures::Service.create!(
|
|
|
|
project: project,
|
|
|
|
active: cluster.enabled,
|
|
|
|
category: 'deployment',
|
|
|
|
type: 'KubernetesService',
|
|
|
|
properties: "{\"api_url\":\"#{cluster.platform_kubernetes.api_url}\"}")
|
2017-12-15 13:43:25 +00:00
|
|
|
end
|
|
|
|
|
2018-01-03 11:58:54 +00:00
|
|
|
it 'does not migrate the KubernetesService and disables the kubernetes_service' do # Because the corresponding Platform::Kubernetes already exists
|
2018-01-08 07:17:35 +00:00
|
|
|
expect { migrate! }.not_to change { MigrateKubernetesServiceToNewClustersArchitectures::Cluster.count }
|
2017-11-28 17:38:55 +00:00
|
|
|
|
2018-01-03 11:58:54 +00:00
|
|
|
kubernetes_service.reload
|
|
|
|
expect(kubernetes_service).not_to be_active
|
2017-11-28 17:38:55 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-03 11:58:54 +00:00
|
|
|
context 'when production cluster has already been existed' do # i.e. There are no environment_scope conflicts
|
2018-01-08 07:17:35 +00:00
|
|
|
let(:project) { MigrateKubernetesServiceToNewClustersArchitectures::Project.create! }
|
|
|
|
|
|
|
|
let(:cluster) do
|
|
|
|
MigrateKubernetesServiceToNewClustersArchitectures::Cluster.create!(
|
|
|
|
projects: [project],
|
|
|
|
name: 'sample-cluster',
|
|
|
|
platform_type: :kubernetes,
|
|
|
|
provider_type: :user,
|
|
|
|
environment_scope: 'production/*',
|
|
|
|
platform_kubernetes_attributes: {
|
|
|
|
api_url: 'https://sample.kubernetes.com',
|
|
|
|
ca_cert: 'ca_pem-sample',
|
|
|
|
token: 'token-sample'
|
|
|
|
} )
|
|
|
|
end
|
|
|
|
|
|
|
|
let!(:kubernetes_service) do
|
|
|
|
MigrateKubernetesServiceToNewClustersArchitectures::Service.create!(
|
|
|
|
project: project,
|
|
|
|
active: true,
|
|
|
|
category: 'deployment',
|
|
|
|
type: 'KubernetesService',
|
|
|
|
properties: "{\"api_url\":\"https://debug.kube.com\"}")
|
|
|
|
end
|
2017-12-18 07:32:41 +00:00
|
|
|
|
|
|
|
it 'migrates the KubernetesService to Platform::Kubernetes' do
|
2018-01-08 07:17:35 +00:00
|
|
|
expect { migrate! }.to change { MigrateKubernetesServiceToNewClustersArchitectures::Cluster.count }.by(1)
|
2017-12-18 07:32:41 +00:00
|
|
|
|
|
|
|
kubernetes_service.reload
|
|
|
|
project.clusters.last.tap do |cluster|
|
2018-01-03 11:58:54 +00:00
|
|
|
expect(cluster.environment_scope).to eq('*')
|
2017-12-18 07:32:41 +00:00
|
|
|
expect(cluster.platform_kubernetes.api_url).to eq(kubernetes_service.api_url)
|
2018-01-08 07:17:35 +00:00
|
|
|
expect(cluster.platform_kubernetes.ca_cert).to eq(kubernetes_service.ca_pem)
|
2017-12-18 07:32:41 +00:00
|
|
|
expect(cluster.platform_kubernetes.token).to eq(kubernetes_service.token)
|
|
|
|
expect(kubernetes_service).not_to be_active
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-03 11:58:54 +00:00
|
|
|
context 'when default cluster has already been existed' do
|
2018-01-08 07:17:35 +00:00
|
|
|
let(:project) { MigrateKubernetesServiceToNewClustersArchitectures::Project.create! }
|
|
|
|
|
|
|
|
let!(:cluster) do
|
|
|
|
MigrateKubernetesServiceToNewClustersArchitectures::Cluster.create!(
|
|
|
|
projects: [project],
|
|
|
|
name: 'sample-cluster',
|
|
|
|
platform_type: :kubernetes,
|
|
|
|
provider_type: :user,
|
|
|
|
environment_scope: '*',
|
|
|
|
platform_kubernetes_attributes: {
|
|
|
|
api_url: 'https://sample.kubernetes.com',
|
|
|
|
ca_cert: 'ca_pem-sample',
|
|
|
|
token: 'token-sample'
|
|
|
|
} )
|
|
|
|
end
|
|
|
|
|
|
|
|
let!(:kubernetes_service) do
|
|
|
|
MigrateKubernetesServiceToNewClustersArchitectures::Service.create!(
|
|
|
|
project: project,
|
|
|
|
active: true,
|
|
|
|
category: 'deployment',
|
|
|
|
type: 'KubernetesService',
|
|
|
|
properties: "{\"api_url\":\"https://debug.kube.com\"}")
|
|
|
|
end
|
2017-12-18 07:32:41 +00:00
|
|
|
|
2018-01-03 11:58:54 +00:00
|
|
|
it 'migrates the KubernetesService to Platform::Kubernetes with dedicated environment_scope' do # Because environment_scope is duplicated
|
2018-01-08 07:17:35 +00:00
|
|
|
expect { migrate! }.to change { MigrateKubernetesServiceToNewClustersArchitectures::Cluster.count }.by(1)
|
2018-01-03 11:58:54 +00:00
|
|
|
|
|
|
|
kubernetes_service.reload
|
|
|
|
project.clusters.last.tap do |cluster|
|
|
|
|
expect(cluster.environment_scope).to eq('migrated/*')
|
|
|
|
expect(cluster.platform_kubernetes.api_url).to eq(kubernetes_service.api_url)
|
2018-01-08 07:17:35 +00:00
|
|
|
expect(cluster.platform_kubernetes.ca_cert).to eq(kubernetes_service.ca_pem)
|
2018-01-03 11:58:54 +00:00
|
|
|
expect(cluster.platform_kubernetes.token).to eq(kubernetes_service.token)
|
|
|
|
expect(kubernetes_service).not_to be_active
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-12-18 07:32:41 +00:00
|
|
|
|
2018-01-03 11:58:54 +00:00
|
|
|
context 'when default cluster and migrated cluster has already been existed' do
|
2018-01-08 07:17:35 +00:00
|
|
|
let(:project) { MigrateKubernetesServiceToNewClustersArchitectures::Project.create! }
|
|
|
|
|
|
|
|
let!(:cluster) do
|
|
|
|
MigrateKubernetesServiceToNewClustersArchitectures::Cluster.create!(
|
|
|
|
projects: [project],
|
|
|
|
name: 'sample-cluster',
|
|
|
|
platform_type: :kubernetes,
|
|
|
|
provider_type: :user,
|
|
|
|
environment_scope: '*',
|
|
|
|
platform_kubernetes_attributes: {
|
|
|
|
api_url: 'https://sample.kubernetes.com',
|
|
|
|
ca_cert: 'ca_pem-sample',
|
|
|
|
token: 'token-sample'
|
|
|
|
} )
|
|
|
|
end
|
|
|
|
|
|
|
|
let!(:migrated_cluster) do
|
|
|
|
MigrateKubernetesServiceToNewClustersArchitectures::Cluster.create!(
|
|
|
|
projects: [project],
|
|
|
|
name: 'sample-cluster',
|
|
|
|
platform_type: :kubernetes,
|
|
|
|
provider_type: :user,
|
|
|
|
environment_scope: 'migrated/*',
|
|
|
|
platform_kubernetes_attributes: {
|
|
|
|
api_url: 'https://sample.kubernetes.com',
|
|
|
|
ca_cert: 'ca_pem-sample',
|
|
|
|
token: 'token-sample'
|
|
|
|
} )
|
|
|
|
end
|
|
|
|
|
|
|
|
let!(:kubernetes_service) do
|
|
|
|
MigrateKubernetesServiceToNewClustersArchitectures::Service.create!(
|
|
|
|
project: project,
|
|
|
|
active: true,
|
|
|
|
category: 'deployment',
|
|
|
|
type: 'KubernetesService',
|
|
|
|
properties: "{\"api_url\":\"https://debug.kube.com\"}")
|
|
|
|
end
|
2018-01-03 11:58:54 +00:00
|
|
|
|
|
|
|
it 'migrates the KubernetesService to Platform::Kubernetes with dedicated environment_scope' do # Because environment_scope is duplicated
|
2018-01-08 07:17:35 +00:00
|
|
|
expect { migrate! }.to change { MigrateKubernetesServiceToNewClustersArchitectures::Cluster.count }.by(1)
|
2018-01-03 11:58:54 +00:00
|
|
|
|
|
|
|
kubernetes_service.reload
|
|
|
|
project.clusters.last.tap do |cluster|
|
|
|
|
expect(cluster.environment_scope).to eq('migrated0/*')
|
|
|
|
expect(cluster.platform_kubernetes.api_url).to eq(kubernetes_service.api_url)
|
2018-01-08 07:17:35 +00:00
|
|
|
expect(cluster.platform_kubernetes.ca_cert).to eq(kubernetes_service.ca_pem)
|
2018-01-03 11:58:54 +00:00
|
|
|
expect(cluster.platform_kubernetes.token).to eq(kubernetes_service.token)
|
|
|
|
expect(kubernetes_service).not_to be_active
|
|
|
|
end
|
2017-12-18 07:32:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-05 15:37:40 +00:00
|
|
|
context 'when KubernetesService has nullified parameters' do
|
2018-01-08 07:17:35 +00:00
|
|
|
let(:project) { MigrateKubernetesServiceToNewClustersArchitectures::Project.create! }
|
2018-01-05 15:37:40 +00:00
|
|
|
|
|
|
|
before do
|
2018-01-08 07:17:35 +00:00
|
|
|
MigrateKubernetesServiceToNewClustersArchitectures::Service.create!(
|
|
|
|
project: project,
|
|
|
|
active: false,
|
|
|
|
category: 'deployment',
|
|
|
|
type: 'KubernetesService',
|
|
|
|
properties: "{}")
|
2018-01-05 15:37:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not migrate the KubernetesService and disables the kubernetes_service' do
|
2018-01-08 07:17:35 +00:00
|
|
|
expect { migrate! }.not_to change { MigrateKubernetesServiceToNewClustersArchitectures::Cluster.count }
|
2018-01-05 15:37:40 +00:00
|
|
|
|
|
|
|
expect(project.kubernetes_service).not_to be_active
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-05 16:11:06 +00:00
|
|
|
# Platforms::Kubernetes validates `token` reagdless of the activeness,
|
|
|
|
# whereas KubernetesService validates `token` if only it's activated
|
|
|
|
# However, in this migration file, there are no validations because of the re-defined model class
|
|
|
|
# therefore, we should safely add this raw to Platform::Kubernetes
|
|
|
|
context 'when KubernetesService has empty token' do
|
2018-01-08 07:17:35 +00:00
|
|
|
let(:project) { MigrateKubernetesServiceToNewClustersArchitectures::Project.create! }
|
2018-01-05 16:11:06 +00:00
|
|
|
|
|
|
|
before do
|
2018-01-08 07:17:35 +00:00
|
|
|
MigrateKubernetesServiceToNewClustersArchitectures::Service.create!(
|
|
|
|
project: project,
|
|
|
|
active: false,
|
|
|
|
category: 'deployment',
|
|
|
|
type: 'KubernetesService',
|
|
|
|
properties: "{\"namespace\":\"prod\",\"api_url\":\"http://111.111.111.111\",\"ca_pem\":\"a\",\"token\":\"\"}")
|
2018-01-05 16:11:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not migrate the KubernetesService and disables the kubernetes_service' do
|
2018-01-08 07:17:35 +00:00
|
|
|
expect { migrate! }.to change { MigrateKubernetesServiceToNewClustersArchitectures::Cluster.count }.by(1)
|
2018-01-05 16:11:06 +00:00
|
|
|
|
|
|
|
project.clusters.last.tap do |cluster|
|
|
|
|
expect(cluster.environment_scope).to eq('*')
|
|
|
|
expect(cluster.platform_kubernetes.namespace).to eq('prod')
|
|
|
|
expect(cluster.platform_kubernetes.api_url).to eq('http://111.111.111.111')
|
2018-01-08 07:17:35 +00:00
|
|
|
expect(cluster.platform_kubernetes.ca_cert).to eq('a')
|
2018-01-05 16:11:06 +00:00
|
|
|
expect(cluster.platform_kubernetes.token).to be_empty
|
|
|
|
expect(project.kubernetes_service).not_to be_active
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-12-14 17:56:23 +00:00
|
|
|
context 'when KubernetesService does not exist' do
|
2018-01-08 07:17:35 +00:00
|
|
|
let!(:project) { MigrateKubernetesServiceToNewClustersArchitectures::Project.create! }
|
2017-11-28 17:38:55 +00:00
|
|
|
|
2017-12-15 13:43:25 +00:00
|
|
|
it 'does not migrate the KubernetesService' do
|
2018-01-08 07:17:35 +00:00
|
|
|
expect { migrate! }.not_to change { MigrateKubernetesServiceToNewClustersArchitectures::Cluster.count }
|
2017-12-14 17:56:23 +00:00
|
|
|
end
|
2017-11-28 17:38:55 +00:00
|
|
|
end
|
|
|
|
end
|