/
vm_spec.rb
310 lines (259 loc) · 12 KB
/
vm_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
describe Vm do
include_examples "OwnershipMixin"
it "#corresponding_model" do
expect(Vm.corresponding_model).to eq(MiqTemplate)
expect(ManageIQ::Providers::Vmware::InfraManager::Vm.corresponding_model).to eq(ManageIQ::Providers::Vmware::InfraManager::Template)
expect(ManageIQ::Providers::Redhat::InfraManager::Vm.corresponding_model).to eq(ManageIQ::Providers::Redhat::InfraManager::Template)
end
it "#corresponding_template_model" do
expect(Vm.corresponding_template_model).to eq(MiqTemplate)
expect(ManageIQ::Providers::Vmware::InfraManager::Vm.corresponding_template_model).to eq(ManageIQ::Providers::Vmware::InfraManager::Template)
expect(ManageIQ::Providers::Redhat::InfraManager::Vm.corresponding_template_model).to eq(ManageIQ::Providers::Redhat::InfraManager::Template)
end
context "#template=" do
before(:each) { @vm = FactoryGirl.create(:vm_vmware) }
it "false" do
@vm.update_attribute(:template, false)
expect(@vm.type).to eq("ManageIQ::Providers::Vmware::InfraManager::Vm")
expect(@vm.template).to eq(false)
expect(@vm.state).to eq("on")
expect { @vm.reload }.not_to raise_error
expect { ManageIQ::Providers::Vmware::InfraManager::Template.find(@vm.id) }.to raise_error ActiveRecord::RecordNotFound
end
it "true" do
@vm.update_attribute(:template, true)
expect(@vm.type).to eq("ManageIQ::Providers::Vmware::InfraManager::Template")
expect(@vm.template).to eq(true)
expect(@vm.state).to eq("never")
expect { @vm.reload }.to raise_error ActiveRecord::RecordNotFound
expect { ManageIQ::Providers::Vmware::InfraManager::Template.find(@vm.id) }.not_to raise_error
end
end
it "#validate_remote_console_vmrc_support only suppored on vmware" do
vm = FactoryGirl.create(:vm_redhat, :vendor => "redhat")
expect { vm.validate_remote_console_vmrc_support }.to raise_error MiqException::RemoteConsoleNotSupportedError
end
context ".find_all_by_mac_address_and_hostname_and_ipaddress" do
before do
@hardware1 = FactoryGirl.create(:hardware)
@vm1 = FactoryGirl.create(:vm_vmware, :hardware => @hardware1)
@hardware2 = FactoryGirl.create(:hardware)
@vm2 = FactoryGirl.create(:vm_vmware, :hardware => @hardware2)
end
it "mac_address" do
address = "ABCDEFG"
guest_device = FactoryGirl.create(:guest_device, :address => address, :device_type => "ethernet")
@hardware1.guest_devices << guest_device
expect(described_class.find_all_by_mac_address_and_hostname_and_ipaddress(address, nil, nil))
.to eql([@vm1])
end
it "hostname" do
hostname = "ABCDEFG"
network = FactoryGirl.create(:network, :hostname => hostname)
@hardware1.networks << network
expect(described_class.find_all_by_mac_address_and_hostname_and_ipaddress(nil, hostname, nil))
.to eql([@vm1])
end
it "ipaddress" do
ipaddress = "127.0.0.1"
network = FactoryGirl.create(:network, :ipaddress => ipaddress)
@hardware1.networks << network
expect(described_class.find_all_by_mac_address_and_hostname_and_ipaddress(nil, nil, ipaddress))
.to eql([@vm1])
end
it "returns an empty list when all are blank" do
expect(described_class.find_all_by_mac_address_and_hostname_and_ipaddress(nil, nil, nil)).to eq([])
expect(described_class.find_all_by_mac_address_and_hostname_and_ipaddress('', '', '')).to eq([])
end
end
context "with relationships of multiple types" do
before(:each) do
@rp = FactoryGirl.create(:resource_pool, :name => "RP")
@parent_vm = FactoryGirl.create(:vm_vmware, :name => "Parent VM")
@vm = FactoryGirl.create(:vm_vmware, :name => "VM")
@child_vm = FactoryGirl.create(:vm_vmware, :name => "Child VM")
@rp.with_relationship_type("ems_metadata") { @rp.add_child(@vm) }
@parent_vm.with_relationship_type("genealogy") { @parent_vm.add_child(@vm) }
@vm.with_relationship_type("genealogy") { @vm.add_child(@child_vm) }
[@rp, @parent_vm, @vm, @child_vm].each(&:clear_relationships_cache)
end
context "#destroy" do
before(:each) do
@vm.destroy
end
it "should destroy all relationships" do
expect(Relationship.where(:resource_type => "Vm", :resource_id => @vm.id).count).to eq(0)
expect(Relationship.where(:resource_type => "Vm", :resource_id => @child_vm.id).count).to eq(0)
expect(@parent_vm.children).to eq([])
expect(@child_vm.parents).to eq([])
expect(@rp.children).to eq([])
end
end
end
context "#invoke_tasks_local" do
before(:each) do
EvmSpecHelper.create_guid_miq_server_zone
@host = FactoryGirl.create(:host)
@vm = FactoryGirl.create(:vm_vmware, :host => @host)
end
it "sets up standard callback for non Power Operations" do
options = {:task => "create_snapshot", :invoke_by => :task, :ids => [@vm.id]}
Vm.invoke_tasks_local(options)
expect(MiqTask.count).to eq(1)
task = MiqTask.first
expect(MiqQueue.count).to eq(1)
msg = MiqQueue.first
expect(msg.miq_callback).to eq({:class_name => "MiqTask", :method_name => :queue_callback, :instance_id => task.id, :args => ["Finished"]})
end
it "sets up powerops callback for Power Operations" do
options = {:task => "start", :invoke_by => :task, :ids => [@vm.id]}
Vm.invoke_tasks_local(options)
expect(MiqTask.count).to eq(1)
task = MiqTask.first
expect(MiqQueue.count).to eq(1)
msg = MiqQueue.first
expect(msg.miq_callback).to eq({:class_name => @vm.class.base_class.name, :method_name => :powerops_callback, :instance_id => @vm.id, :args => [task.id]})
allow(Vm).to receive(:start).and_raise(MiqException::MiqVimBrokerUnavailable)
msg.deliver
end
it "retirement passes in userid" do
options = {:task => "retire_now", :invoke_by => :task, :ids => [@vm.id], :userid => "Freddy"}
Vm.invoke_tasks_local(options)
expect(MiqTask.count).to eq(1)
task = MiqTask.first
expect(MiqQueue.count).to eq(1)
msg = MiqQueue.first
expect(msg.miq_callback).to eq({:class_name => "MiqTask", :method_name => :queue_callback,
:instance_id => task.id, :args => ["Finished"]})
expect(msg.args).to eq(["Freddy"])
end
end
context "#start" do
before do
EvmSpecHelper.create_guid_miq_server_zone
@host = FactoryGirl.create(:host_vmware)
@vm = FactoryGirl.create(:vm_vmware,
:host => @host,
:miq_group => FactoryGirl.create(:miq_group)
)
FactoryGirl.create(:miq_event_definition, :name => :request_vm_start)
# admin user is needed to process Events
User.super_admin || FactoryGirl.create(:user_with_group, :userid => "admin")
end
it "policy passes" do
expect_any_instance_of(ManageIQ::Providers::Vmware::InfraManager::Vm).to receive(:raw_start)
allow(MiqAeEngine).to receive_messages(:deliver => ['ok', 'sucess', MiqAeEngine::MiqAeWorkspaceRuntime.new])
@vm.start
status, message, result = MiqQueue.first.deliver
MiqQueue.first.delivered(status, message, result)
end
it "policy prevented" do
expect_any_instance_of(ManageIQ::Providers::Vmware::InfraManager::Vm).to_not receive(:raw_start)
event = {:attributes => {"full_data" => {:policy => {:prevented => true}}}}
allow_any_instance_of(MiqAeEngine::MiqAeWorkspaceRuntime).to receive(:get_obj_from_path).with("/").and_return(:event_stream => event)
allow(MiqAeEngine).to receive_messages(:deliver => ['ok', 'sucess', MiqAeEngine::MiqAeWorkspaceRuntime.new])
@vm.start
status, message, _result = MiqQueue.first.deliver
MiqQueue.first.delivered(status, message, MiqAeEngine::MiqAeWorkspaceRuntime.new)
end
end
context "#scan" do
before do
EvmSpecHelper.create_guid_miq_server_zone
@host = FactoryGirl.create(:host_vmware)
@vm = FactoryGirl.create(
:vm_vmware,
:host => @host,
:miq_group => FactoryGirl.create(:miq_group)
)
FactoryGirl.create(:miq_event_definition, :name => :request_vm_scan)
# admin user is needed to process Events
User.super_admin || FactoryGirl.create(:user_with_group, :userid => "admin")
end
it "policy passes" do
expect_any_instance_of(ManageIQ::Providers::Vmware::InfraManager::Vm).to receive(:raw_scan)
allow(MiqAeEngine).to receive_messages(:deliver => ['ok', 'sucess', MiqAeEngine::MiqAeWorkspaceRuntime.new])
@vm.scan
status, message, result = MiqQueue.first.deliver
MiqQueue.first.delivered(status, message, result)
end
it "policy prevented" do
expect_any_instance_of(ManageIQ::Providers::Vmware::InfraManager::Vm).to_not receive(:raw_scan)
event = {:attributes => {"full_data" => {:policy => {:prevented => true}}}}
allow_any_instance_of(MiqAeEngine::MiqAeWorkspaceRuntime).to receive(:get_obj_from_path).with("/").and_return(:event_stream => event)
allow(MiqAeEngine).to receive_messages(:deliver => ['ok', 'sucess', MiqAeEngine::MiqAeWorkspaceRuntime.new])
@vm.scan
status, message, _result = MiqQueue.first.deliver
MiqQueue.first.delivered(status, message, MiqAeEngine::MiqAeWorkspaceRuntime.new)
end
end
it "#save_drift_state" do
# TODO: Beef up with more data
vm = FactoryGirl.create(:vm_vmware)
vm.save_drift_state
expect(vm.drift_states.size).to eq(1)
expect(DriftState.count).to eq(1)
expect(vm.drift_states.first.data).to eq({
:class => "ManageIQ::Providers::Vmware::InfraManager::Vm",
:id => vm.id,
:location => vm.location,
:name => vm.name,
:vendor => "vmware",
:files => [],
:filesystem_drivers => [],
:groups => [],
:guest_applications => [],
:kernel_drivers => [],
:linux_initprocesses => [],
:patches => [],
:registry_items => [],
:tags => [],
:users => [],
:win32_services => [],
})
end
it '#set_remote_console_url' do
vm = FactoryGirl.create(:vm_vmware)
vm.send(:remote_console_url=, url = 'http://www.redhat.com', 1)
console = SystemConsole.find_by(:vm_id => vm.id)
expect(console.url).to eq(url)
expect(console.url_secret).to be
end
describe '#add_to_service' do
let(:vm) { FactoryGirl.create(:vm_vmware) }
let(:service) { FactoryGirl.create(:service) }
it 'associates the vm to the service' do
vm.add_to_service(service)
expect(service.reload.vms).to include(vm)
end
it 'raise an error if the vm is already part of a service' do
vm.add_to_service(service)
expect { vm.add_to_service(service) }.to raise_error MiqException::Error
end
end
context "#cockpit_url" do
before(:each) do
@csv = <<-CSV.gsub(/^\s+/, "")
name,description,max_concurrent,external_failover,role_scope
cockpit_ws,Cockpit,1,false,zone
CSV
allow(ServerRole).to receive(:seed_data).and_return(@csv)
ServerRole.seed
_, _, @zone = EvmSpecHelper.create_guid_miq_server_zone
@ems = FactoryGirl.create(:ext_management_system, :zone => @zone)
end
it "is direct when no role" do
vm = FactoryGirl.create(:vm_openstack)
allow(vm).to receive_messages(:ipaddresses => ["10.0.0.1"])
expect(vm.cockpit_url).to eq(URI::HTTP.build(:host => "10.0.0.1", :port => 9090))
end
it "uses dashboard redirect when cockpit role is active" do
vm = FactoryGirl.create(:vm_openstack, :ext_management_system => @ems)
allow(vm).to receive_messages(:ipaddresses => ["10.0.0.1"])
server = FactoryGirl.create(:miq_server, :ipaddress => "10.0.0.2", :has_active_cockpit_ws => true, :zone => @zone)
server.assign_role('cockpit_ws', 1)
server.activate_roles('cockpit_ws')
expect(vm.cockpit_url).to eq(URI.parse("https://10.0.0.2/cws/=10.0.0.1"))
end
end
end