Commit 27111e29 authored by Shinya Maeda's avatar Shinya Maeda

Restructure spec

parent 40c6af54
...@@ -2,188 +2,257 @@ require 'spec_helper' ...@@ -2,188 +2,257 @@ require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20171124104327_migrate_kubernetes_service_to_new_clusters_architectures.rb') require Rails.root.join('db', 'post_migrate', '20171124104327_migrate_kubernetes_service_to_new_clusters_architectures.rb')
describe MigrateKubernetesServiceToNewClustersArchitectures, :migration do describe MigrateKubernetesServiceToNewClustersArchitectures, :migration do
context 'when user configured kubernetes from CI/CD > Clusters' do context 'when unique KubernetesService exists' do
let(:project) { create(:project) } shared_examples 'KubernetesService migration' do
let(:user) { create(:user) } let(:sample_num) { 2 }
let(:projects) { create_list(:project, sample_num) }
# Platforms::Kubernetes (New archtecture)
let!(:cluster) do
create(:cluster,
projects: [project],
user: user,
provider_type: :gcp,
platform_type: :kubernetes,
provider_gcp: provider_gcp,
platform_kubernetes: platform_kubernetes)
end
let(:provider_gcp) { create(:cluster_provider_gcp, :created) }
let(:platform_kubernetes) { create(:cluster_platform_kubernetes, :configured) }
# KubernetesService (Automatically synchronized when Platforms::Kubernetes created)
let!(:kubernetes_service) { create(:kubernetes_service, project: project) }
context 'when user is using the cluster' do let!(:kubernetes_services) do
it 'migrates' do projects.map do |project|
expect{ migrate! }.not_to change { Clusters::Cluster.count } create(:kubernetes_service,
expect(cluster).to be_active project: project,
expect(kubernetes_service).not_to be_active active: active,
api_url: "https://kubernetes#{project.id}.com",
token: defined?(token) ? token : "token#{project.id}",
ca_pem: "ca_pem#{project.id}")
end end
end end
context 'when user disabled cluster' do it 'migrates the KubernetesService to Platform::Kubernetes' do
before do expect{ migrate! }.to change { Clusters::Cluster.count }.by(sample_num)
disable_cluster!
end
context 'when user configured kubernetes from Integration > Kubernetes' do projects.each do |project|
before do project.clusters.last.tap do |cluster|
kubernetes_service.update( expect(cluster.enabled).to eq(active)
active: true, expect(cluster.platform_kubernetes.api_url).to eq(project.kubernetes_service.api_url)
api_url: 'http://new.kube.com', expect(cluster.platform_kubernetes.ca_pem).to eq(project.kubernetes_service.ca_pem)
ca_pem: nil, expect(cluster.platform_kubernetes.token).to eq(project.kubernetes_service.token)
token: 'z' * 40).reload expect(project.kubernetes_service).not_to be_active
expect(project.kubernetes_service.properties['migrated']).to be_truthy
end end
context 'when user is using the kubernetes service' do
it 'migrates' do
expect{ migrate! }.to change { Clusters::Cluster.count }.by(1)
Clusters::Cluster.last.tap do |c|
expect(c).to be_active
expect(c.platform_kubernetes.api_url).to eq(kubernetes_service.api_url)
expect(c.platform_kubernetes.ca_pem).to eq(kubernetes_service.ca_pem)
expect(c.platform_kubernetes.token).to eq(kubernetes_service.token)
end end
expect(kubernetes_service).not_to be_active
end end
end end
context 'when user stopped using the kubernetes service' do context 'when KubernetesService is active' do
before do let(:active) { true }
kubernetes_service.update(active: false)
it_behaves_like 'KubernetesService migration'
end end
it 'migrates' do context 'when KubernetesService is not active' do
expect{ migrate! }.to change { Clusters::Cluster.count }.by(1) let(:active) { false }
Clusters::Cluster.last.tap do |c| # Platforms::Kubernetes validates `token` reagdless of the activeness
expect(c).not_to be_active # KubernetesService validates `token` if only it's activated
expect(c.platform_kubernetes.api_url).to eq(kubernetes_service.api_url) # However, in this migration file, there are no validations because of the migration specific model class
expect(c.platform_kubernetes.ca_pem).to eq(kubernetes_service.ca_pem) # therefore, Validation Error will not happen in this case and just migrate data
expect(c.platform_kubernetes.token).to eq(kubernetes_service.token) let(:token) { '' }
end
expect(kubernetes_service).not_to be_active it_behaves_like 'KubernetesService migration'
end
end
end end
end end
context 'when user deleted cluster' do context 'when unique KubernetesService spawned from Service Template' do
before do it 'migrates the KubernetesService to Platform::Kubernetes' do
destory_cluster!
end
context 'when user configured kubernetes from Integration > Kubernetes' do
let!(:new_kubernetes_service) do
project.create_kubernetes_service(
active: true,
api_url: 'http://123.123.123.123',
ca_pem: nil,
token: 'a' * 40)
end end
context 'when user is using the kubernetes service' do
it 'migrates' do
expect{ migrate! }.to change { Clusters::Cluster.count }.by(1)
Clusters::Cluster.last.tap do |c|
expect(c).to be_active
expect(c.platform_kubernetes.api_url).to eq(new_kubernetes_service.api_url)
expect(c.platform_kubernetes.ca_pem).to eq(new_kubernetes_service.ca_pem)
expect(c.platform_kubernetes.token).to eq(new_kubernetes_service.token)
end end
expect(new_kubernetes_service).not_to be_active context 'when synced KubernetesService exists' do
end it 'does not migrate the KubernetesService' do # Because the corresponding Platform::Kubernetes already exists
end
context 'when user stopped using the kubernetes service' do
before do
new_kubernetes_service.update(active: false)
end end
it 'migrates' do
expect{ migrate! }.to change { Clusters::Cluster.count }.by(1)
Clusters::Cluster.last.tap do |c|
expect(c).not_to be_active
expect(c.platform_kubernetes.api_url).to eq(new_kubernetes_service.api_url)
expect(c.platform_kubernetes.ca_pem).to eq(new_kubernetes_service.ca_pem)
expect(c.platform_kubernetes.token).to eq(new_kubernetes_service.token)
end end
expect(new_kubernetes_service).not_to be_active context 'when KubernetesService does not exist' do
end it 'does not migrate the KubernetesService' do
end
end
end end
end end
context 'when user configured kubernetes from Integration > Kubernetes' do # context 'when user configured kubernetes from CI/CD > Clusters' do
let(:project) { create(:project) } # let(:project) { create(:project) }
let!(:kubernetes_service) { create(:kubernetes_service, project: project) } # let(:user) { create(:user) }
context 'when user is using the kubernetes service' do # # Platforms::Kubernetes (New archtecture)
it 'migrates' do # let!(:cluster) do
expect{ migrate! }.to change { Clusters::Cluster.count }.by(1) # create(:cluster,
# projects: [project],
# user: user,
# provider_type: :gcp,
# platform_type: :kubernetes,
# provider_gcp: provider_gcp,
# platform_kubernetes: platform_kubernetes)
# end
Clusters::Cluster.last.tap do |c| # let(:provider_gcp) { create(:cluster_provider_gcp, :created) }
expect(c).to be_active # let(:platform_kubernetes) { create(:cluster_platform_kubernetes, :configured) }
expect(c.platform_kubernetes.api_url).to eq(kubernetes_service.api_url)
expect(c.platform_kubernetes.ca_pem).to eq(kubernetes_service.ca_pem)
expect(c.platform_kubernetes.token).to eq(kubernetes_service.token)
end
expect(kubernetes_service).not_to be_active # # KubernetesService (Automatically synchronized when Platforms::Kubernetes created)
end # let!(:kubernetes_service) { create(:kubernetes_service, project: project) }
end
context 'when user stopped using the kubernetes service' do # context 'when user is using the cluster' do
before do # it 'migrates' do
kubernetes_service.update(active: false) # expect{ migrate! }.not_to change { Clusters::Cluster.count }
end # expect(cluster).to be_active
# expect(kubernetes_service).not_to be_active
# end
# end
it 'migrates' do # context 'when user disabled cluster' do
expect{ migrate! }.to change { Clusters::Cluster.count }.by(1) # before do
# disable_cluster!
# end
Clusters::Cluster.last.tap do |c| # context 'when user configured kubernetes from Integration > Kubernetes' do
expect(c).not_to be_active # before do
expect(c.platform_kubernetes.api_url).to eq(kubernetes_service.api_url) # kubernetes_service.update(
expect(c.platform_kubernetes.ca_pem).to eq(kubernetes_service.ca_pem) # active: true,
expect(c.platform_kubernetes.token).to eq(kubernetes_service.token) # api_url: 'http://new.kube.com',
end # ca_pem: nil,
# token: 'z' * 40).reload
# end
expect(kubernetes_service).not_to be_active # context 'when user is using the kubernetes service' do
end # it 'migrates' do
end # expect{ migrate! }.to change { Clusters::Cluster.count }.by(1)
end
context 'when nothing is configured' do # Clusters::Cluster.last.tap do |c|
it 'migrates' do # expect(c).to be_active
expect{ migrate! }.not_to change { Clusters::Cluster.count } # expect(c.platform_kubernetes.api_url).to eq(kubernetes_service.api_url)
end # expect(c.platform_kubernetes.ca_pem).to eq(kubernetes_service.ca_pem)
end # expect(c.platform_kubernetes.token).to eq(kubernetes_service.token)
# end
def disable_cluster! # expect(kubernetes_service).not_to be_active
cluster.update!(enabled: false) # end
kubernetes_service.update!(active: false) # end
end
def destory_cluster! # context 'when user stopped using the kubernetes service' do
cluster.destroy! # before do
kubernetes_service.destroy! # kubernetes_service.update(active: false)
end # end
# it 'migrates' do
# expect{ migrate! }.to change { Clusters::Cluster.count }.by(1)
# Clusters::Cluster.last.tap do |c|
# expect(c).not_to be_active
# expect(c.platform_kubernetes.api_url).to eq(kubernetes_service.api_url)
# expect(c.platform_kubernetes.ca_pem).to eq(kubernetes_service.ca_pem)
# expect(c.platform_kubernetes.token).to eq(kubernetes_service.token)
# end
# expect(kubernetes_service).not_to be_active
# end
# end
# end
# end
# context 'when user deleted cluster' do
# before do
# destory_cluster!
# end
# context 'when user configured kubernetes from Integration > Kubernetes' do
# let!(:new_kubernetes_service) do
# project.create_kubernetes_service(
# active: true,
# api_url: 'http://123.123.123.123',
# ca_pem: nil,
# token: 'a' * 40)
# end
# context 'when user is using the kubernetes service' do
# it 'migrates' do
# expect{ migrate! }.to change { Clusters::Cluster.count }.by(1)
# Clusters::Cluster.last.tap do |c|
# expect(c).to be_active
# expect(c.platform_kubernetes.api_url).to eq(new_kubernetes_service.api_url)
# expect(c.platform_kubernetes.ca_pem).to eq(new_kubernetes_service.ca_pem)
# expect(c.platform_kubernetes.token).to eq(new_kubernetes_service.token)
# end
# expect(new_kubernetes_service).not_to be_active
# end
# end
# context 'when user stopped using the kubernetes service' do
# before do
# new_kubernetes_service.update(active: false)
# end
# it 'migrates' do
# expect{ migrate! }.to change { Clusters::Cluster.count }.by(1)
# Clusters::Cluster.last.tap do |c|
# expect(c).not_to be_active
# expect(c.platform_kubernetes.api_url).to eq(new_kubernetes_service.api_url)
# expect(c.platform_kubernetes.ca_pem).to eq(new_kubernetes_service.ca_pem)
# expect(c.platform_kubernetes.token).to eq(new_kubernetes_service.token)
# end
# expect(new_kubernetes_service).not_to be_active
# end
# end
# end
# end
# end
# context 'when user configured kubernetes from Integration > Kubernetes' do
# let(:project) { create(:project) }
# let!(:kubernetes_service) { create(:kubernetes_service, project: project) }
# context 'when user is using the kubernetes service' do
# it 'migrates' do
# expect{ migrate! }.to change { Clusters::Cluster.count }.by(1)
# Clusters::Cluster.last.tap do |c|
# expect(c).to be_active
# expect(c.platform_kubernetes.api_url).to eq(kubernetes_service.api_url)
# expect(c.platform_kubernetes.ca_pem).to eq(kubernetes_service.ca_pem)
# expect(c.platform_kubernetes.token).to eq(kubernetes_service.token)
# end
# expect(kubernetes_service).not_to be_active
# end
# end
# context 'when user stopped using the kubernetes service' do
# before do
# kubernetes_service.update(active: false)
# end
# it 'migrates' do
# expect{ migrate! }.to change { Clusters::Cluster.count }.by(1)
# Clusters::Cluster.last.tap do |c|
# expect(c).not_to be_active
# expect(c.platform_kubernetes.api_url).to eq(kubernetes_service.api_url)
# expect(c.platform_kubernetes.ca_pem).to eq(kubernetes_service.ca_pem)
# expect(c.platform_kubernetes.token).to eq(kubernetes_service.token)
# end
# expect(kubernetes_service).not_to be_active
# end
# end
# end
# context 'when nothing is configured' do
# it 'migrates' do
# expect{ migrate! }.not_to change { Clusters::Cluster.count }
# end
# end
# def disable_cluster!
# cluster.update!(enabled: false)
# kubernetes_service.update!(active: false)
# end
# def destory_cluster!
# cluster.destroy!
# kubernetes_service.destroy!
# end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment