-
Notifications
You must be signed in to change notification settings - Fork 899
/
service_template_transformation_plan_spec.rb
314 lines (275 loc) · 16.5 KB
/
service_template_transformation_plan_spec.rb
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
RSpec.describe ServiceTemplateTransformationPlan, :v2v do
subject { FactoryBot.create(:service_template_transformation_plan) }
describe '#request_class' do
it { expect(subject.request_class).to eq(ServiceTemplateTransformationPlanRequest) }
end
describe '#request_type' do
it { expect(subject.request_type).to eq("transformation_plan") }
end
let(:transformation_mapping) { FactoryBot.create(:transformation_mapping) }
let(:transformation_mapping2) { FactoryBot.create(:transformation_mapping) }
let(:apst) { FactoryBot.create(:service_template_ansible_playbook) }
let(:vm1) { FactoryBot.create(:vm_or_template) }
let(:vm2) { FactoryBot.create(:vm_or_template) }
let(:vm3) { FactoryBot.create(:vm_or_template) }
let(:security_group1) { FactoryBot.create(:security_group, :name => "default") }
let(:flavor1) { FactoryBot.create(:flavor, :name => "large") }
let(:security_group2) { FactoryBot.create(:security_group, :name => "default") }
let(:flavor2) { FactoryBot.create(:flavor, :name => "medium") }
let(:catalog_item_options) do
{
:name => 'Transformation Plan',
:description => 'a description',
:config_info => {
:transformation_mapping_id => transformation_mapping.id,
:pre_service_id => apst.id,
:post_service_id => apst.id,
:actions => [
{:vm_id => vm1.id.to_s, :pre_service => true, :post_service => false, :osp_security_group_id => security_group1.id, :osp_flavor_id => flavor1.id},
{:vm_id => vm2.id.to_s, :pre_service => true, :post_service => true, :osp_security_group_id => security_group1.id, :osp_flavor_id => flavor1.id}
],
}
}
end
let(:updated_catalog_item_options_with_vms_added) do
{
:name => 'Transformation Plan Updated',
:description => 'an updated description',
:config_info => {
:transformation_mapping_id => transformation_mapping.id,
:pre_service_id => apst.id,
:post_service_id => apst.id,
:actions => [
{:vm_id => vm1.id.to_s, :pre_service => true, :post_service => false, :osp_security_group_id => security_group1.id, :osp_flavor_id => flavor1.id},
{:vm_id => vm2.id.to_s, :pre_service => true, :post_service => true, :osp_security_group_id => security_group1.id, :osp_flavor_id => flavor1.id},
{:vm_id => vm3.id.to_s, :pre_service => true, :post_service => true, :osp_security_group_id => security_group2.id, :osp_flavor_id => flavor2.id}
],
}
}
end
let(:updated_catalog_item_options_with_vms_removed) do
{
:name => 'Transformation Plan Updated',
:description => 'an updated description',
:config_info => {
:transformation_mapping_id => transformation_mapping.id,
:pre_service_id => apst.id,
:post_service_id => apst.id,
:actions => [
{:vm_id => vm1.id.to_s, :pre_service => true, :post_service => false}
],
}
}
end
let(:updated_catalog_item_options_with_vms_added_and_removed) do
{
:name => 'Transformation Plan Updated',
:description => 'an updated description',
:config_info => {
:transformation_mapping_id => transformation_mapping.id,
:pre_service_id => apst.id,
:post_service_id => apst.id,
:actions => [
{:vm_id => vm1.id.to_s, :pre_service => true, :post_service => false},
{:vm_id => vm3.id.to_s, :pre_service => true, :post_service => true}
],
}
}
end
let(:updated_options_with_updated_transformation_mapping) do
{
:name => 'Transformation Plan Updated',
:description => 'an updated description',
:config_info => {
:transformation_mapping_id => transformation_mapping2.id,
:pre_service_id => apst.id,
:post_service_id => apst.id,
:actions => [
{:vm_id => vm1.id.to_s, :pre_service => true, :post_service => false},
{:vm_id => vm2.id.to_s, :pre_service => true, :post_service => true}
],
}
}
end
let(:miq_requests) { [FactoryBot.create(:service_template_transformation_plan_request, :request_state => "finished")] }
let(:miq_requests_with_in_progress_request) { [FactoryBot.create(:service_template_transformation_plan_request, :request_state => "active")] }
describe '#validate_order' do
let(:service_template) { described_class.create_catalog_item(catalog_item_options) }
it 'allows a plan to be ordered if all VMs have not been migrated' do
expect(service_template.validate_order).to be_truthy
expect(service_template.orderable?).to be_truthy # alias
end
it 'denies a plan from bring ordered if all VMs have been migrated' do
vm1.tag_add('transformation_status/migrated', :ns => '/managed')
vm2.tag_add('transformation_status/migrated', :ns => '/managed')
expect(service_template.validate_order).to be_falsey
expect(service_template.orderable?).to be_falsey # alias
end
end
describe '.public_service_templates' do
it 'display public service templates' do
st1 = FactoryBot.create(:service_template_transformation_plan)
st2 = FactoryBot.create(:service_template)
expect(st1.internal?).to be_truthy
expect(st2.internal?).to be_falsey
expect(ServiceTemplate.public_service_templates).to match_array([st2])
end
end
describe '.create_catalog_item' do
it 'creates and returns a transformation plan' do
service_template = described_class.create_catalog_item(catalog_item_options)
expect(service_template.name).to eq('Transformation Plan')
expect(service_template.transformation_mapping).to eq(transformation_mapping)
expect(service_template.vm_resources.collect(&:resource)).to match_array([vm1, vm2])
expect(service_template.vm_resources.collect(&:status)).to eq([ServiceResource::STATUS_QUEUED, ServiceResource::STATUS_QUEUED])
expect(service_template.vm_resources.find_by(:resource_id => vm1.id).options)
.to eq("pre_ansible_playbook_service_template_id" => apst.id, "osp_security_group_id" => security_group1.id, "osp_flavor_id" => flavor1.id)
expect(service_template.vm_resources.find_by(:resource_id => vm2.id).options)
.to eq("pre_ansible_playbook_service_template_id" => apst.id, "post_ansible_playbook_service_template_id" => apst.id, "osp_security_group_id" => security_group1.id, "osp_flavor_id" => flavor1.id)
expect(service_template.config_info).to eq(catalog_item_options[:config_info])
expect(service_template.resource_actions.first).to have_attributes(
:action => 'Provision',
:fqname => described_class.default_provisioning_entry_point(nil)
)
end
it 'requires a transformation mapping' do
catalog_item_options[:config_info].delete(:transformation_mapping_id)
expect do
described_class.create_catalog_item(catalog_item_options)
end.to raise_error(StandardError, 'Must provide an existing transformation mapping')
end
it 'requires selected vms' do
catalog_item_options[:config_info].delete(:actions)
expect do
described_class.create_catalog_item(catalog_item_options)
end.to raise_error(StandardError, 'Must select a list of valid vms')
end
it 'validates unique name' do
described_class.create_catalog_item(catalog_item_options)
expect { described_class.create_catalog_item(catalog_item_options) }.to raise_error(
ActiveRecord::RecordInvalid, 'Validation failed: ServiceTemplateTransformationPlan: Name has already been taken'
)
end
end
describe '#update_catalog_item' do
it 'updates the associated transformation mapping' do
service_template = described_class.create_catalog_item(catalog_item_options)
service_template.miq_requests = []
service_template.update_catalog_item(updated_options_with_updated_transformation_mapping)
expect(service_template.name).to eq('Transformation Plan Updated')
expect(service_template.transformation_mapping).to eq(transformation_mapping2)
end
it 'updates by adding new VMs to existing VMs and returns a transformation plan' do
service_template = described_class.create_catalog_item(catalog_item_options)
service_template.miq_requests = []
service_template.update_catalog_item(updated_catalog_item_options_with_vms_added)
expect(service_template.name).to eq('Transformation Plan Updated')
expect(service_template.transformation_mapping).to eq(transformation_mapping)
expect(service_template.vm_resources.collect(&:resource)).to match_array([vm1, vm2, vm3])
expect(service_template.vm_resources.collect(&:status)).to eq([ServiceResource::STATUS_QUEUED, ServiceResource::STATUS_QUEUED, ServiceResource::STATUS_QUEUED])
expect(service_template.vm_resources.find_by(:resource_id => vm1.id).options)
.to eq("pre_ansible_playbook_service_template_id" => apst.id, "osp_security_group_id" => security_group1.id, "osp_flavor_id" => flavor1.id)
expect(service_template.vm_resources.find_by(:resource_id => vm2.id).options)
.to eq("pre_ansible_playbook_service_template_id" => apst.id, "post_ansible_playbook_service_template_id" => apst.id, "osp_security_group_id" => security_group1.id, "osp_flavor_id" => flavor1.id)
expect(service_template.vm_resources.find_by(:resource_id => vm3.id).options)
.to eq("pre_ansible_playbook_service_template_id" => apst.id, "post_ansible_playbook_service_template_id" => apst.id, "osp_security_group_id" => security_group2.id, "osp_flavor_id" => flavor2.id)
expect(service_template.config_info).to eq(updated_catalog_item_options_with_vms_added[:config_info])
expect(service_template.resource_actions.first).to have_attributes(
:action => 'Provision',
:fqname => described_class.default_provisioning_entry_point(nil)
)
end
it 'updates by removing some VMs from the existing VMs and returns a transformation plan' do
service_template = described_class.create_catalog_item(catalog_item_options)
service_template.miq_requests = []
service_template.update_catalog_item(updated_catalog_item_options_with_vms_removed)
expect(service_template.name).to eq('Transformation Plan Updated')
expect(service_template.transformation_mapping).to eq(transformation_mapping)
expect(service_template.vm_resources.collect(&:resource)).to match_array([vm1])
expect(service_template.vm_resources.collect(&:status)).to eq([ServiceResource::STATUS_QUEUED])
expect(service_template.vm_resources.find_by(:resource_id => vm1.id).options)
.to eq("pre_ansible_playbook_service_template_id" => apst.id)
expect(service_template.config_info).to eq(updated_catalog_item_options_with_vms_removed[:config_info])
expect(service_template.resource_actions.first).to have_attributes(
:action => 'Provision',
:fqname => described_class.default_provisioning_entry_point(nil)
)
end
it 'updates by adding new VMs to the existing VMs and removing some VMs from the existing VMs and returns a transformation plan' do
service_template = described_class.create_catalog_item(catalog_item_options)
service_template.miq_requests = []
service_template.update_catalog_item(updated_catalog_item_options_with_vms_added_and_removed)
expect(service_template.name).to eq('Transformation Plan Updated')
expect(service_template.transformation_mapping).to eq(transformation_mapping)
expect(service_template.vm_resources.collect(&:resource)).to match_array([vm1, vm3])
expect(service_template.vm_resources.collect(&:status)).to eq([ServiceResource::STATUS_QUEUED, ServiceResource::STATUS_QUEUED])
expect(service_template.vm_resources.find_by(:resource_id => vm1.id).options)
.to eq("pre_ansible_playbook_service_template_id" => apst.id)
expect(service_template.vm_resources.find_by(:resource_id => vm3.id).options)
.to eq("pre_ansible_playbook_service_template_id" => apst.id, "post_ansible_playbook_service_template_id" => apst.id)
expect(service_template.config_info).to eq(updated_catalog_item_options_with_vms_added_and_removed[:config_info])
expect(service_template.resource_actions.first).to have_attributes(
:action => 'Provision',
:fqname => described_class.default_provisioning_entry_point(nil)
)
end
it 'updates only the basic attributes' do
service_template = described_class.create_catalog_item(catalog_item_options)
service_template.miq_requests = []
service_template.update_catalog_item(:name => "Name updated", :description => "description updated")
expect(service_template.name).to eq('Name updated')
expect(service_template.description).to eq('description updated')
expect(service_template.transformation_mapping).to eq(transformation_mapping)
expect(service_template.vm_resources.collect(&:resource)).to match_array([vm1, vm2])
expect(service_template.vm_resources.collect(&:status)).to eq([ServiceResource::STATUS_QUEUED, ServiceResource::STATUS_QUEUED])
expect(service_template.vm_resources.find_by(:resource_id => vm1.id).options)
.to eq("pre_ansible_playbook_service_template_id" => apst.id, "osp_security_group_id" => security_group1.id, "osp_flavor_id" => flavor1.id)
expect(service_template.vm_resources.find_by(:resource_id => vm2.id).options)
.to eq("pre_ansible_playbook_service_template_id" => apst.id, "post_ansible_playbook_service_template_id" => apst.id, "osp_security_group_id" => security_group1.id, "osp_flavor_id" => flavor1.id)
expect(service_template.config_info).to eq(catalog_item_options[:config_info])
expect(service_template.resource_actions.first).to have_attributes(
:action => 'Provision',
:fqname => described_class.default_provisioning_entry_point(nil)
)
end
it 'updates only the basic attributes when completed miq_requests are present' do
service_template = described_class.create_catalog_item(catalog_item_options)
service_template.miq_requests = miq_requests
service_template.update_catalog_item(updated_catalog_item_options_with_vms_added)
expect(service_template.name).to eq('Transformation Plan Updated')
expect(service_template.description).to eq('an updated description')
expect(service_template.transformation_mapping).to eq(transformation_mapping)
expect(service_template.vm_resources.collect(&:resource)).to match_array([vm1, vm2])
expect(service_template.vm_resources.find_by(:resource_id => vm1.id).options)
.to eq("pre_ansible_playbook_service_template_id" => apst.id, "osp_security_group_id" => security_group1.id, "osp_flavor_id" => flavor1.id)
expect(service_template.vm_resources.find_by(:resource_id => vm2.id).options)
.to eq("pre_ansible_playbook_service_template_id" => apst.id, "post_ansible_playbook_service_template_id" => apst.id, "osp_security_group_id" => security_group1.id, "osp_flavor_id" => flavor1.id)
expect(service_template.config_info).to eq(catalog_item_options[:config_info])
expect(service_template.resource_actions.first).to have_attributes(
:action => 'Provision',
:fqname => described_class.default_provisioning_entry_point(nil)
)
end
it 'raises an exception when miq_request is in progress' do
service_template = described_class.create_catalog_item(catalog_item_options)
service_template.miq_requests = miq_requests_with_in_progress_request
expect do
service_template.update_catalog_item(updated_catalog_item_options_with_vms_added)
end.to raise_error(StandardError, 'Editing a plan in progress is prohibited')
end
it 'requires a transformation mapping' do
updated_catalog_item_options_with_vms_added[:config_info].delete(:transformation_mapping_id)
service_template = described_class.create_catalog_item(catalog_item_options)
expect do
service_template.update_catalog_item(updated_catalog_item_options_with_vms_added)
end.to raise_error(StandardError, 'Must provide an existing transformation mapping')
end
it 'requires selected vms' do
updated_catalog_item_options_with_vms_added[:config_info].delete(:actions)
service_template = described_class.create_catalog_item(catalog_item_options)
expect do
service_template.update_catalog_item(updated_catalog_item_options_with_vms_added)
end.to raise_error(StandardError, 'Must select a list of valid vms')
end
end
end