Skip to content

Commit 05d5f4a

Browse files
committed
Change EvmServer#find_vms_by... tests
1 parent ee5beba commit 05d5f4a

File tree

1 file changed

+68
-25
lines changed

1 file changed

+68
-25
lines changed

spec/lib/workers/evm_server_spec.rb

Lines changed: 68 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -189,46 +189,89 @@
189189
end
190190
end
191191

192+
let(:hostname) { "ABCDEFG" }
193+
let(:address) { "AB:CD:EF:GH" }
194+
let(:vm1_network) { FactoryBot.create(:network, :hostname => hostname) }
195+
let(:vm2_network) { FactoryBot.create(:network) }
196+
let(:vm3_device) { FactoryBot.create(:guest_device, :address => address, :device_type => "ethernet") }
197+
let(:vm1) { FactoryBot.create(:vm_vmware, :hardware => FactoryBot.create(:hardware, :networks => [vm1_network])) }
198+
let(:vm2) { FactoryBot.create(:vm_vmware, :hardware => FactoryBot.create(:hardware, :networks => [vm2_network])) }
199+
let(:vm3) { FactoryBot.create(:vm_vmware, :hardware => FactoryBot.create(:hardware, :guest_devices => [vm3_device])) }
200+
192201
context "#find_vms_by_mac_address_and_hostname_and_ipaddress (private)" do
193202
subject { described_class.new }
194-
before do
195-
@hardware1 = FactoryBot.create(:hardware)
196-
@vm1 = FactoryBot.create(:vm_vmware, :hardware => @hardware1)
197-
198-
@hardware2 = FactoryBot.create(:hardware)
199-
@vm2 = FactoryBot.create(:vm_vmware, :hardware => @hardware2)
200-
end
201203

202204
it "mac_address" do
203-
address = "ABCDEFG"
204-
guest_device = FactoryBot.create(:guest_device, :address => address, :device_type => "ethernet")
205-
@hardware1.guest_devices << guest_device
206-
207-
expect(subject.send(:find_vms_by_mac_address_and_hostname_and_ipaddress, address, nil, nil))
208-
.to eql([@vm1])
205+
vm1
206+
vm2
207+
vm3
208+
subject
209+
210+
expect do
211+
expect(subject.send(:find_vms_by_mac_address_and_hostname_and_ipaddress, address, nil, nil)).to eq([vm3])
212+
end.to make_database_queries(:count => 1)
209213
end
210214

211215
it "hostname" do
212-
hostname = "ABCDEFG"
213-
network = FactoryBot.create(:network, :hostname => hostname)
214-
@hardware1.networks << network
216+
vm1
217+
vm2
218+
subject
215219

216-
expect(subject.send(:find_vms_by_mac_address_and_hostname_and_ipaddress, nil, hostname, nil))
217-
.to eql([@vm1])
220+
expect do
221+
expect(subject.send(:find_vms_by_mac_address_and_hostname_and_ipaddress, nil, hostname, nil)).to eq([vm1])
222+
end.to make_database_queries(:count => 1)
218223
end
219224

220225
it "ipaddress" do
221-
ipaddress = "127.0.0.1"
222-
network = FactoryBot.create(:network, :ipaddress => ipaddress)
223-
@hardware1.networks << network
226+
vm1
227+
vm2
228+
subject
229+
230+
expect do
231+
expect(subject.send(:find_vms_by_mac_address_and_hostname_and_ipaddress, nil, nil, vm1_network.ipaddress)).to eq([vm1])
232+
end.to make_database_queries(:count => 1)
233+
end
234+
235+
it "hostname and ipaddress" do
236+
vm1
237+
vm2
238+
subject
224239

225-
expect(subject.send(:find_vms_by_mac_address_and_hostname_and_ipaddress, nil, nil, ipaddress))
226-
.to eql([@vm1])
240+
expect do
241+
expect(subject.send(:find_vms_by_mac_address_and_hostname_and_ipaddress, nil, vm1_network.hostname, vm1_network.ipaddress)).to eq([vm1])
242+
end.to make_database_queries(:count => 1)
243+
end
244+
245+
it "hostname and different ipaddress" do
246+
vm1
247+
vm2
248+
subject
249+
250+
expect do
251+
expect(subject.send(:find_vms_by_mac_address_and_hostname_and_ipaddress, nil, vm1_network.hostname, vm2_network.ipaddress)).to be_empty
252+
end.to make_database_queries(:count => 1)
253+
end
254+
255+
# vm must match both mac address and a hostname from a single server
256+
# not sure if that was the original intent, but how the code currently works
257+
it "mac address and different hostname" do
258+
vm1
259+
vm3
260+
subject
261+
262+
expect do
263+
expect(subject.send(:find_vms_by_mac_address_and_hostname_and_ipaddress, address, vm1_network.hostname, nil)).to be_empty
264+
end.to make_database_queries(:count => 1)
227265
end
228266

229267
it "returns an empty list when all are blank" do
230-
expect(subject.send(:find_vms_by_mac_address_and_hostname_and_ipaddress, nil, nil, nil)).to eq([])
231-
expect(subject.send(:find_vms_by_mac_address_and_hostname_and_ipaddress, '', '', '')).to eq([])
268+
vm1
269+
subject
270+
271+
expect do
272+
expect(subject.send(:find_vms_by_mac_address_and_hostname_and_ipaddress, nil, nil, nil)).to eq([])
273+
expect(subject.send(:find_vms_by_mac_address_and_hostname_and_ipaddress, '', '', '')).to eq([])
274+
end.not_to make_database_queries
232275
end
233276
end
234277
end

0 commit comments

Comments
 (0)