|
189 | 189 | end |
190 | 190 | end |
191 | 191 |
|
| 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 | + |
192 | 201 | context "#find_vms_by_mac_address_and_hostname_and_ipaddress (private)" do |
193 | 202 | 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 |
201 | 203 |
|
202 | 204 | 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) |
209 | 213 | end |
210 | 214 |
|
211 | 215 | it "hostname" do |
212 | | - hostname = "ABCDEFG" |
213 | | - network = FactoryBot.create(:network, :hostname => hostname) |
214 | | - @hardware1.networks << network |
| 216 | + vm1 |
| 217 | + vm2 |
| 218 | + subject |
215 | 219 |
|
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) |
218 | 223 | end |
219 | 224 |
|
220 | 225 | 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 |
224 | 239 |
|
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) |
227 | 265 | end |
228 | 266 |
|
229 | 267 | 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 |
232 | 275 | end |
233 | 276 | end |
234 | 277 | end |
0 commit comments