forked from ManageIQ/manageiq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinders_spec.rb
123 lines (99 loc) · 5.67 KB
/
finders_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
require_relative '../helpers/spec_parsed_data'
require_relative 'test_persister'
require_relative 'targeted_refresh_spec_helper'
describe ManagerRefresh::Inventory::Persister do
include SpecParsedData
include TargetedRefreshSpecHelper
######################################################################################################################
# Spec scenarios for asserts giving hints to developers
######################################################################################################################
#
before :each do
@zone = FactoryGirl.create(:zone)
@ems = FactoryGirl.create(:ems_cloud,
:zone => @zone,
:network_manager => FactoryGirl.create(:ems_network, :zone => @zone))
allow(@ems.class).to receive(:ems_type).and_return(:mock)
allow(Settings.ems_refresh).to receive(:mock).and_return({})
end
let(:persister) { create_persister }
it "raises an exception when relation object is needed, but something else is provided" do
expected_error = "Wrong index for key :vm_or_template, the value must be of type Nil or InventoryObject or InventoryObjectLazy, got: not_allowed_string"
expect do
persister.hardwares.lazy_find(:vm_or_template => "not_allowed_string")
end.to(raise_error(expected_error))
expect do
persister.hardwares.lazy_find("not_allowed_string")
end.to(raise_error(expected_error))
end
it "raises an exception when composite index is expected by finder, but 1 value is passed instead" do
expected_error = "The index :manager_ref has composite index, finder has to be called as: collection.find(:hardware => 'X', :device_name => 'X')"
expect do
persister.disks.lazy_find("unknown__public", :key => :device_name, :default => 'default_device_name')
end.to(raise_error(expected_error))
end
it "raises an exception passing bad primary index used by finder" do
expected_error = "Finder has missing keys for index :manager_ref, missing indexes are: [:hardware]"
expect do
persister.networks.lazy_find(:hardwares => "something", :description => "public")
end.to(raise_error(expected_error))
expected_error = "Finder has missing keys for index :manager_ref, missing indexes are: [:ems_ref]"
expect do
persister.vms.lazy_find(:ems_ruf => "some_ems_ref")
end.to(raise_error(expected_error))
end
it "raises an exception passing bad secondsry index used by finder" do
expected_error = "Finder has missing keys for index :by_name, missing indexes are: [:name]"
expect do
persister.vms.lazy_find({:names => "name"}, {:ref => :by_name})
end.to(raise_error(expected_error))
expected_error = "Finder has missing keys for index :by_uid_ems_and_name, missing indexes are: [:uid_ems, :name]"
expect do
persister.vms.lazy_find({:names => "name", :uida_ems => "uid_ems"}, {:ref => :by_uid_ems_and_name})
end.to(raise_error(expected_error))
end
it "checks passing more keys to index passes just fine" do
# There is not need to force exact match as long as all keys of the index are passed
vm_lazy_1 = persister.vms.lazy_find({:name => "name", :uid_ems => "uid_ems", :ems_ref => "ems_ref"}, {:ref => :by_uid_ems_and_name})
expect(vm_lazy_1.reference.full_reference).to eq(:name => "name", :uid_ems => "uid_ems", :ems_ref => "ems_ref")
expect(vm_lazy_1.ref).to eq(:by_uid_ems_and_name)
expect(vm_lazy_1.to_s).to eq("uid_ems__name")
vm_lazy_2 = persister.vms.lazy_find(:name => "name", :uid_ems => "uid_ems", :ems_ref => "ems_ref")
expect(vm_lazy_2.reference.full_reference).to eq(:name => "name", :uid_ems => "uid_ems", :ems_ref => "ems_ref")
expect(vm_lazy_2.ref).to eq(:manager_ref)
expect(vm_lazy_2.to_s).to eq("ems_ref")
end
it "checks passing composite index doesn't depend on order" do
lazy_find_vm = persister.vms.lazy_find(:ems_ref => "ems_ref_1")
lazy_find_hardware = persister.hardwares.lazy_find(:vm_or_template => lazy_find_vm)
lazy_find_network_1 = persister.networks.lazy_find(:hardware => lazy_find_hardware, :description => "public")
lazy_find_network_2 = persister.networks.lazy_find(:description => "public", :hardware => lazy_find_hardware)
expect(lazy_find_network_1.to_s).to eq("ems_ref_1__public")
expect(lazy_find_network_1.to_s).to eq(lazy_find_network_2.to_s)
end
it "checks non composite index is allowed as non hash" do
ems_ref = "vm_ems_ref_1"
vm_lazy_1 = persister.vms.lazy_find(:ems_ref => ems_ref)
vm_lazy_2 = persister.vms.lazy_find(ems_ref)
# Check the stringified reference matches
expect(vm_lazy_1.to_s).to eq ems_ref
expect(vm_lazy_1.stringified_reference).to eq ems_ref
expect(vm_lazy_1.reference.stringified_reference).to eq ems_ref
expect(vm_lazy_1.to_s).to eq vm_lazy_2.to_s
# Check the full reference matches
expect(vm_lazy_1.reference.full_reference).to eq(:ems_ref => ems_ref)
expect(vm_lazy_1.reference.full_reference).to eq(vm_lazy_2.reference.full_reference)
end
it "checks non composite relation index is allowed as non hash" do
ems_ref = "vm_ems_ref_1"
vm_lazy = persister.vms.lazy_find(:ems_ref => ems_ref)
hardware_lazy_1 = persister.hardwares.lazy_find(vm_lazy)
hardware_lazy_2 = persister.hardwares.lazy_find(:vm_or_template => vm_lazy)
# Check the stringified reference matches
expect(hardware_lazy_1.to_s).to eq ems_ref
expect(hardware_lazy_1.to_s).to eq hardware_lazy_2.to_s
# Check the full reference matches
expect(hardware_lazy_1.reference.full_reference).to eq(:vm_or_template => vm_lazy)
expect(hardware_lazy_2.reference.full_reference).to eq hardware_lazy_2.reference.full_reference
end
end