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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20171124104327_migrate_kubernetes_service_to_new_clusters_architectures.rb')
describe MigrateKubernetesServiceToNewClustersArchitectures, :migration do
context 'when unique KubernetesService exists' do
shared_examples 'KubernetesService migration' do
let(:sample_num) { 2 }
let(:projects) { create_list(:project, sample_num) }
let!(:kubernetes_services) do
projects.map do |project|
create(:kubernetes_service,
project: project,
active: active,
api_url: "https://kubernetes#{project.id}.com",
token: defined?(token) ? token : "token#{project.id}",
ca_pem: "ca_pem#{project.id}")
end
end
it 'migrates the KubernetesService to Platform::Kubernetes' do
expect { migrate! }.to change { Clusters::Cluster.count }.by(sample_num)
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)
expect(cluster.platform_kubernetes.ca_pem).to eq(project.kubernetes_service.ca_pem)
expect(cluster.platform_kubernetes.token).to eq(project.kubernetes_service.token)
expect(project.kubernetes_service).not_to be_active
end
end
end
end
context 'when KubernetesService is active' do
let(:active) { true }
it_behaves_like 'KubernetesService migration'
end
end
context 'when unique KubernetesService spawned from Service Template' do
let(:sample_num) { 2 }
let(:projects) { create_list(:project, sample_num) }
let!(:kubernetes_service_template) do
create(:kubernetes_service,
project: nil,
template: true,
api_url: "https://sample.kubernetes.com",
token: "token-sample",
ca_pem: "ca_pem-sample")
end
let!(:kubernetes_services) do
projects.map do |project|
create(:kubernetes_service,
project: project,
api_url: kubernetes_service_template.api_url,
token: kubernetes_service_template.token,
ca_pem: kubernetes_service_template.ca_pem)
end
end
it 'migrates the KubernetesService to Platform::Kubernetes without template' do
expect { migrate! }.to change { Clusters::Cluster.count }.by(sample_num)
projects.each do |project|
project.clusters.last.tap do |cluster|
expect(cluster.platform_kubernetes.api_url).to eq(project.kubernetes_service.api_url)
expect(cluster.platform_kubernetes.ca_pem).to eq(project.kubernetes_service.ca_pem)
expect(cluster.platform_kubernetes.token).to eq(project.kubernetes_service.token)
expect(project.kubernetes_service).not_to be_active
end
end
end
end
context 'when managed KubernetesService exists' do
let(:project) { create(:project) }
let(:cluster) { create(:cluster, :provided_by_gcp, projects: [project]) }
let!(:platform_kubernetes) { cluster.platform_kubernetes }
let!(:kubernetes_service) do
create(:kubernetes_service,
project: project,
active: cluster.enabled,
api_url: platform_kubernetes.api_url,
token: platform_kubernetes.token,
ca_pem: platform_kubernetes.ca_cert)
end
it 'does not migrate the KubernetesService and disables the kubernetes_service' do # Because the corresponding Platform::Kubernetes already exists
expect { migrate! }.not_to change { Clusters::Cluster.count }
kubernetes_service.reload
expect(kubernetes_service).not_to be_active
end
end
context 'when production cluster has already been existed' do # i.e. There are no environment_scope conflicts
let(:project) { create(:project) }
let!(:cluster) { create(:cluster, :provided_by_gcp, environment_scope: 'production/*', projects: [project]) }
let!(:kubernetes_service) { create(:kubernetes_service, api_url: 'https://debug.kube.com', active: true, project: project) }
it 'migrates the KubernetesService to Platform::Kubernetes' do
expect { migrate! }.to change { Clusters::Cluster.count }.by(1)
kubernetes_service.reload
project.clusters.last.tap do |cluster|
expect(cluster.environment_scope).to eq('*')
expect(cluster.platform_kubernetes.api_url).to eq(kubernetes_service.api_url)
expect(cluster.platform_kubernetes.ca_pem).to eq(kubernetes_service.ca_pem)
expect(cluster.platform_kubernetes.token).to eq(kubernetes_service.token)
expect(kubernetes_service).not_to be_active
end
end
end
context 'when default cluster has already been existed' do
let(:project) { create(:project) }
let!(:cluster) { create(:cluster, :provided_by_gcp, environment_scope: '*', projects: [project]) }
let!(:kubernetes_service) { create(:kubernetes_service, api_url: 'https://debug.kube.com', active: true, project: project) }
it 'migrates the KubernetesService to Platform::Kubernetes with dedicated environment_scope' do # Because environment_scope is duplicated
expect { migrate! }.to change { Clusters::Cluster.count }.by(1)
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)
expect(cluster.platform_kubernetes.ca_pem).to eq(kubernetes_service.ca_pem)
expect(cluster.platform_kubernetes.token).to eq(kubernetes_service.token)
expect(kubernetes_service).not_to be_active
end
end
end
context 'when default cluster and migrated cluster has already been existed' do
let(:project) { create(:project) }
let!(:cluster) { create(:cluster, :provided_by_gcp, environment_scope: '*', projects: [project]) }
let!(:migrated_cluster) { create(:cluster, :provided_by_gcp, environment_scope: 'migrated/*', projects: [project]) }
let!(:kubernetes_service) { create(:kubernetes_service, api_url: 'https://debug.kube.com', active: true, project: project) }
it 'migrates the KubernetesService to Platform::Kubernetes with dedicated environment_scope' do # Because environment_scope is duplicated
expect { migrate! }.to change { Clusters::Cluster.count }.by(1)
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)
expect(cluster.platform_kubernetes.ca_pem).to eq(kubernetes_service.ca_pem)
expect(cluster.platform_kubernetes.token).to eq(kubernetes_service.token)
expect(kubernetes_service).not_to be_active
end
end
end
context 'when KubernetesService has nullified parameters' do
let(:project) { create(:project) }
before do
ActiveRecord::Base.connection.execute <<~SQL
INSERT INTO services (project_id, active, category, type, properties)
VALUES (#{project.id}, false, 'deployment', 'KubernetesService', '{}');
SQL
end
it 'does not migrate the KubernetesService and disables the kubernetes_service' do
expect { migrate! }.not_to change { Clusters::Cluster.count }
expect(project.kubernetes_service).not_to be_active
end
end
# 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
let(:project) { create(:project) }
before do
ActiveRecord::Base.connection.execute <<~SQL
INSERT INTO services (project_id, active, category, type, properties)
VALUES (#{project.id}, false, 'deployment', 'KubernetesService', '{"namespace":"prod","api_url":"http://111.111.111.111","ca_pem":"a","token":""}');
SQL
end
it 'does not migrate the KubernetesService and disables the kubernetes_service' do
expect { migrate! }.to change { Clusters::Cluster.count }.by(1)
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')
expect(cluster.platform_kubernetes.ca_pem).to eq('a')
expect(cluster.platform_kubernetes.token).to be_empty
expect(project.kubernetes_service).not_to be_active
end
end
end
context 'when KubernetesService does not exist' do
let!(:project) { create(:project) }
it 'does not migrate the KubernetesService' do
expect { migrate! }.not_to change { Clusters::Cluster.count }
end
end
end
|