-
Notifications
You must be signed in to change notification settings - Fork 72
/
provision_workflow_spec.rb
277 lines (238 loc) · 12.5 KB
/
provision_workflow_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
silence_warnings { ManageIQ::Providers::Vmware::InfraManager::ProvisionWorkflow.const_set("DIALOGS_VIA_AUTOMATE", false) }
describe ManageIQ::Providers::Vmware::InfraManager::ProvisionWorkflow do
include Spec::Support::WorkflowHelper
let(:admin) { FactoryGirl.create(:user_with_group) }
let(:template) { FactoryGirl.create(:template_vmware) }
before do
EvmSpecHelper.local_miq_server
end
describe "#new" do
it "pass platform attributes to automate" do
stub_dialog(:get_dialogs)
assert_automate_dialog_lookup(admin, "infra", "vmware", "get_pre_dialog_name", nil)
described_class.new({}, admin.userid)
end
it "sets up workflow" do
stub_dialog(:get_pre_dialogs)
stub_dialog(:get_dialogs)
workflow = described_class.new(values = {}, admin.userid)
expect(workflow.requester).to eq(admin)
expect(values).to eq({})
end
end
describe "#make_request" do
let(:alt_user) { FactoryGirl.create(:user_with_group) }
it "creates and update a request" do
stub_dialog(:get_pre_dialogs)
stub_dialog(:get_dialogs)
# if running_pre_dialog is set, it will run 'continue_request'
workflow = described_class.new(values = {:running_pre_dialog => false}, admin)
expect(AuditEvent).to receive(:success).with(
:event => "vm_provision_request_created",
:target_class => "Vm",
:userid => admin.userid,
:message => "VM Provisioning requested by <#{admin.userid}> for Vm:#{template.id}"
)
# creates a request
stub_get_next_vm_name
# the dialogs populate this
values.merge!(:src_vm_id => template.id, :vm_tags => [])
request = workflow.make_request(nil, values)
expect(request).to be_valid
expect(request).to be_a_kind_of(MiqProvisionRequest)
expect(request.request_type).to eq("template")
expect(request.description).to eq("Provision from [#{template.name}] to [New VM]")
expect(request.requester).to eq(admin)
expect(request.userid).to eq(admin.userid)
expect(request.requester_name).to eq(admin.name)
# updates a request
stub_get_next_vm_name
workflow = described_class.new(values, alt_user)
expect(AuditEvent).to receive(:success).with(
:event => "vm_provision_request_updated",
:target_class => "Vm",
:userid => alt_user.userid,
:message => "VM Provisioning request updated by <#{alt_user.userid}> for Vm:#{template.id}"
)
workflow.make_request(request, values)
end
end
context 'provisioning a VM' do
let(:workflow) { described_class.new({}, admin.userid) }
before do
@ems = FactoryGirl.create(:ems_vmware)
@host1 = FactoryGirl.create(:host_vmware, :ems_id => @ems.id)
@host2 = FactoryGirl.create(:host_vmware, :ems_id => @ems.id)
@src_vm = FactoryGirl.create(:vm_vmware, :host => @host1, :ems_id => @ems.id)
stub_dialog(:get_dialogs)
workflow.instance_variable_set(:@values, :vm_tags => [], :src_vm_id => @src_vm.id)
workflow.instance_variable_set(:@target_resource, nil)
end
context '#allowed_storage_profiles' do
let(:profile) { FactoryGirl.create(:storage_profile, :name => 'Gold') }
it 'when storage_profile selection is set, will not touch storage_profile selection value' do
selected = []
workflow.instance_variable_set(:@values, :src_vm_id => template.id, :placement_storage_profile => selected)
workflow.allowed_storage_profiles
values = workflow.instance_variable_get(:@values)
expect(values[:placement_storage_profile]).to eq(selected)
end
context 'when storage_profile selection is not set' do
it 'set storage_profile selection to [nil, nil] if template has no storage_profile' do
template = FactoryGirl.create(:vm_vmware, :host => @host1, :ems_id => @ems.id)
workflow.instance_variable_set(:@values, :src_vm_id => template.id, :placement_storage_profile => nil)
workflow.allowed_storage_profiles
values = workflow.instance_variable_get(:@values)
expect(values[:placement_storage_profile]).to eq([nil, nil])
end
it 'set storage_profile selection to that of template if template has one' do
template = FactoryGirl.create(:vm_vmware, :host => @host1, :ems_id => @ems.id, :storage_profile => profile)
workflow.instance_variable_set(:@values, :src_vm_id => template.id, :placement_storage_profile => nil)
workflow.allowed_storage_profiles
values = workflow.instance_variable_get(:@values)
expect(values[:placement_storage_profile]).to eq([profile.id, profile.name])
end
end
context 'storage_profile filter' do
let(:ems) { FactoryGirl.create(:ems_vmware, :storage_profiles => [profile]) }
let(:template) { FactoryGirl.create(:vm_vmware, :ems_id => ems.id) }
it 'list storage_profiles associated with ems' do
workflow.instance_variable_set(:@values, :src_vm_id => template.id, :src_ems_id => ems.id)
workflow.allowed_storage_profiles
filters = workflow.instance_variable_get(:@filters)
expect(filters[:StorageProfile]).to eq(profile.id => profile.name)
end
end
end
context '#set_on_vm_id_changed' do
before(:each) do
workflow.instance_variable_set(:@filters, :Host => {21 => "ESX 6.0"}, :StorageProfile => {1 => "Tag 1"})
workflow.instance_variable_set(:@values, :src_vm_id => @src_vm.id, :placement_storage_profile => [])
allow(workflow).to receive(:set_or_default_hardware_field_values).with(@src_vm)
end
it 'clears StorageProfile filter' do
workflow.set_on_vm_id_changed
filters = workflow.instance_variable_get(:@filters)
expect(filters).to eq(:Host => {21=>"ESX 6.0"}, :StorageProfile => nil)
end
it 'clears :placement_storage_profile value' do
workflow.set_on_vm_id_changed
values = workflow.instance_variable_get(:@values)
expect(values[:placement_storage_profile]).to be_nil
end
end
context 'network selection' do
let(:s11) { FactoryGirl.create(:switch, :name => "A") }
let(:s12) { FactoryGirl.create(:switch, :name => "B") }
let(:s13) { FactoryGirl.create(:switch, :name => "C") }
let(:s14) { FactoryGirl.create(:switch, :name => "DVS14", :shared => true) }
let(:s15) { FactoryGirl.create(:switch, :name => "DVS15", :shared => true) }
let(:s21) { FactoryGirl.create(:switch, :name => "DVS21", :shared => true) }
let(:s22) { FactoryGirl.create(:switch, :name => "DVS22", :shared => true) }
before do
@lan11 = FactoryGirl.create(:lan, :name => "lan_A", :switch_id => s11.id)
@lan12 = FactoryGirl.create(:lan, :name => "lan_B", :switch_id => s12.id)
@lan13 = FactoryGirl.create(:lan, :name => "lan_C", :switch_id => s13.id)
@lan14 = FactoryGirl.create(:lan, :name => "lan_DVS", :switch_id => s14.id)
@lan15 = FactoryGirl.create(:lan, :name => "lan_DVS", :switch_id => s15.id)
@lan21 = FactoryGirl.create(:lan, :name => "lan_DVS", :switch_id => s21.id)
@lan22 = FactoryGirl.create(:lan, :name => "lan_A", :switch_id => s22.id)
end
it '#allowed_vlans' do
@host1.switches = [s11, s12, s13]
allow(workflow).to receive(:allowed_hosts).with(no_args).and_return([workflow.host_to_hash_struct(@host1)])
vlans, _hosts = workflow.allowed_vlans(:vlans => true, :dvs => true)
lan_keys = [@lan11.name, @lan13.name, @lan12.name]
lan_values = [@lan11.name, @lan13.name, @lan12.name]
expect(vlans.keys).to match_array(lan_keys)
expect(vlans.values).to match_array(lan_values)
end
it 'concatenates dvswitches of the same portgroup name' do
@host1.switches = [s11, s12, s13, s14, s15]
allow(workflow).to receive(:allowed_hosts).with(no_args).and_return([workflow.host_to_hash_struct(@host1)])
vlans, _hosts = workflow.allowed_vlans(:vlans => true, :dvs => true)
lan_keys = [@lan11.name, @lan13.name, @lan12.name, "dvs_#{@lan14.name}"]
switches = [s14.name, s15.name].sort.join('/')
lan_values = [@lan11.name, @lan13.name, @lan12.name, "#{@lan14.name} (#{switches})"]
expect(vlans.keys).to match_array(lan_keys)
expect(vlans.values).to match_array(lan_values)
end
it 'concatenates dvswitches of the same portgroup name from different hosts' do
@host1.switches = [s11, s12, s13, s14, s15]
@host2.switches = [s15, s21]
allow(workflow).to receive(:allowed_hosts).with(no_args).and_return(
[workflow.host_to_hash_struct(@host1), workflow.host_to_hash_struct(@host2)]
)
vlans, _hosts = workflow.allowed_vlans(:vlans => true, :dvs => true)
lan_keys = [@lan11.name, @lan13.name, @lan12.name, "dvs_#{@lan14.name}"]
switches = [s14.name, s15.name, s21.name].sort.join('/')
lan_values = [@lan11.name, @lan13.name, @lan12.name, "#{@lan14.name} (#{switches})"]
expect(vlans.keys).to match_array(lan_keys)
expect(vlans.values).to match_array(lan_values)
end
it 'excludes dvs if told so' do
@host1.switches = [s11, s12, s13, s14, s15]
@host2.switches = [s15, s21]
allow(workflow).to receive(:allowed_hosts).with(no_args).and_return(
[workflow.host_to_hash_struct(@host1), workflow.host_to_hash_struct(@host2)]
)
vlans, _hosts = workflow.allowed_vlans(:vlans => true, :dvs => false)
lan_keys = [@lan11.name, @lan13.name, @lan12.name]
expect(vlans.keys).to match_array(lan_keys)
expect(vlans.values).to match_array(lan_keys)
end
it 'concatenates dvswitches of the same portgroup name from different hosts when autoplacement is on' do
@host1.switches = [s11, s12, s13, s14, s15]
@host2.switches = [s21]
workflow.instance_variable_set(:@values, :vm_tags => [], :src_vm_id => @src_vm.id, :placement_auto => true)
vlans, _hosts = workflow.allowed_vlans(:vlans => true, :dvs => true)
lan_keys = [@lan11.name, @lan13.name, @lan12.name, "dvs_#{@lan14.name}"]
switches = [s14.name, s15.name, s21.name].sort.join('/')
lan_values = [@lan11.name, @lan13.name, @lan12.name, "#{@lan14.name} (#{switches})"]
expect(vlans.keys).to match_array(lan_keys)
expect(vlans.values).to match_array(lan_values)
end
it 'returns no vlans when autoplacement is off and no allowed_hosts' do
@host1.switches = [s11, s12, s13, s14, s15]
@host2.switches = [s21]
workflow.instance_variable_set(:@values, :vm_tags => [], :src_vm_id => @src_vm.id, :placement_auto => false)
vlans, _hosts = workflow.allowed_vlans(:vlans => true, :dvs => true)
expect(vlans.keys).to match_array([])
expect(vlans.values).to match_array([])
end
it 'Returns both dvportgroup and lan with the same name' do
@host1.switches = [s11, s22]
allow(workflow).to receive(:allowed_hosts).with(no_args).and_return([workflow.host_to_hash_struct(@host1)])
vlans, _hosts = workflow.allowed_vlans(:vlans => true, :dvs => true)
lan_keys = [@lan11.name, "dvs_#{@lan22.name}"]
lan_values = [@lan11.name, "#{@lan22.name} (#{s22.name})"]
expect(vlans.keys).to match_array(lan_keys)
expect(vlans.values).to match_array(lan_values)
end
context '#allowed_hosts_obj' do
before do
allow(workflow).to receive(:find_all_ems_of_type).and_return([@host1, @host2])
allow(Rbac).to receive(:search) do |hash|
[Array.wrap(hash[:targets])]
end
end
it 'finds all hosts with no selected network' do
workflow.instance_variable_set(:@values, :src_vm_id => @src_vm.id)
expect(workflow.allowed_hosts_obj).to match_array([@host1, @host2])
end
it 'finds only the hosts that can access the selected vSwitch network' do
@host1.switches = [s11]
@host2.switches = [s22]
workflow.instance_variable_set(:@values, :src_vm_id => @src_vm.id, :vlan => [@lan11.name, @lan11.name])
expect(workflow.allowed_hosts_obj).to match_array([@host1])
end
it 'finds only the hosts that can access the selected dvSwitch network' do
@host1.switches = [s11]
@host2.switches = [s22]
workflow.instance_variable_set(:@values, :src_vm_id => @src_vm.id, :vlan => ["dvs_#{@lan22.name}", @lan22.name])
expect(workflow.allowed_hosts_obj).to match_array([@host2])
end
end
end
end
end