forked from ManageIQ/manageiq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
target_spec.rb
288 lines (245 loc) · 8.66 KB
/
target_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
describe ManagerRefresh::Target do
before(:each) do
@zone = FactoryGirl.create(:zone)
@ems = FactoryGirl.create(:ems_cloud, :zone => @zone)
@vm_1 = FactoryGirl.create(
:vm_cloud,
:ext_management_system => @ems,
:ems_ref => "vm_1"
)
@vm_2 = FactoryGirl.create(
:vm_cloud,
:ext_management_system => @ems,
:ems_ref => "vm_2"
)
end
context ".load" do
it "intializes correct ManagerRefresh::Target.object with a :manager_id" do
target_1 = ManagerRefresh::Target.load(
:manager_id => @ems.id,
:association => :vms,
:manager_ref => {:ems_ref => @vm_1.ems_ref},
:options => {:opt1 => "opt1", :opt2 => "opt2"}
)
expect(target_1).to(
have_attributes(
:manager => @ems,
:manager_id => @ems.id,
:association => :vms,
:manager_ref => {:ems_ref => @vm_1.ems_ref},
:options => {:opt1 => "opt1", :opt2 => "opt2"}
)
)
end
it "intializes correct ManagerRefresh::Target.object with a :manager " do
target_1 = ManagerRefresh::Target.load(
:manager => @ems,
:association => :vms,
:manager_ref => {:ems_ref => @vm_1.ems_ref},
:options => {:opt1 => "opt1", :opt2 => "opt2"}
)
expect(target_1).to(
have_attributes(
:manager => @ems,
:manager_id => @ems.id,
:association => :vms,
:manager_ref => {:ems_ref => @vm_1.ems_ref},
:options => {:opt1 => "opt1", :opt2 => "opt2"}
)
)
end
it "intializes correct ManagerRefresh::Target.object with a :payload_id " do
expected_payload = "abcd"
payload_id = BinaryBlob.create!(:binary => expected_payload.dup).id
target_1 = ManagerRefresh::Target.load(
:manager => @ems,
:association => :vms,
:manager_ref => {:ems_ref => @vm_1.ems_ref},
:payload_id => payload_id,
:options => {:opt1 => "opt1", :opt2 => "opt2"}
)
expect(target_1).to(
have_attributes(
:manager => @ems,
:manager_id => @ems.id,
:association => :vms,
:manager_ref => {:ems_ref => @vm_1.ems_ref},
:payload_id => payload_id,
:options => {:opt1 => "opt1", :opt2 => "opt2"}
)
)
expect(target_1.payload).to eq(expected_payload)
end
it "raises exception when manager not provided in any form" do
data = {
:association => :vms,
:manager_ref => {:ems_ref => @vm_1.ems_ref},
:options => {:opt1 => "opt1", :opt2 => "opt2"}
}
expect { ManagerRefresh::Target.load(data) }.to raise_error("Provide either :manager or :manager_id argument")
end
end
context ".dump" do
it "intializes correct ManagerRefresh::Target.object with a :manager_id" do
target_1 = ManagerRefresh::Target.load(
:manager_id => @ems.id,
:association => :vms,
:manager_ref => {:ems_ref => @vm_1.ems_ref},
:options => {:opt1 => "opt1", :opt2 => "opt2"}
)
expect(target_1.dump).to(
eq(
:manager_id => @ems.id,
:event_id => nil,
:association => :vms,
:payload_id => nil,
:manager_ref => {:ems_ref => @vm_1.ems_ref},
:options => {:opt1 => "opt1", :opt2 => "opt2"}
)
)
end
it "intializes correct ManagerRefresh::Target.object with a :manager " do
target_1 = ManagerRefresh::Target.load(
:manager => @ems,
:association => :vms,
:manager_ref => {:ems_ref => @vm_1.ems_ref},
:options => {:opt1 => "opt1", :opt2 => "opt2"}
)
expect(target_1.dump).to(
eq(
:manager_id => @ems.id,
:event_id => nil,
:association => :vms,
:payload_id => nil,
:manager_ref => {:ems_ref => @vm_1.ems_ref},
:options => {:opt1 => "opt1", :opt2 => "opt2"}
)
)
end
it "class method .dump returns the same as an instance method .dump " do
target_1 = ManagerRefresh::Target.load(
:manager => @ems,
:association => :vms,
:manager_ref => {:ems_ref => @vm_1.ems_ref},
:options => {:opt1 => "opt1", :opt2 => "opt2"}
)
expect(target_1.dump).to(
eq(
:manager_id => @ems.id,
:event_id => nil,
:association => :vms,
:payload_id => nil,
:manager_ref => {:ems_ref => @vm_1.ems_ref},
:options => {:opt1 => "opt1", :opt2 => "opt2"}
)
)
end
it "class method .dump returns the same as an instance method .dump " do
target_1 = ManagerRefresh::Target.load(
:manager => @ems,
:association => :vms,
:manager_ref => {:ems_ref => @vm_1.ems_ref},
:options => {:opt1 => "opt1", :opt2 => "opt2"}
)
expect(target_1.dump).to eq ManagerRefresh::Target.dump(target_1)
end
end
context ".load_from_db" do
it "loads ManagerRefresh::Target from the db" do
target_1 = ManagerRefresh::Target.load(
:manager => @ems,
:association => :vms,
:manager_ref => {:ems_ref => @vm_1.ems_ref},
:options => {:opt1 => "opt1", :opt2 => "opt2"}
)
expect(target_1.load_from_db).to eq @vm_1
end
end
context ".id" do
it "checks that .id is an alias for .dump" do
target_1 = ManagerRefresh::Target.load(
:manager => @ems,
:association => :vms,
:manager_ref => {:ems_ref => @vm_1.ems_ref},
:options => {:opt1 => "opt1", :opt2 => "opt2"}
)
expect(target_1.dump).to eq target_1.id
end
end
context ".name" do
it "checks that .name is an alias for .association" do
target_1 = ManagerRefresh::Target.load(
:manager => @ems,
:association => :vms,
:manager_ref => {:ems_ref => @vm_1.ems_ref},
:options => {:opt1 => "opt1", :opt2 => "opt2"}
)
expect(target_1.name).to eq target_1.manager_ref
end
context ".payload" do
it "returns the payload from binary_blob" do
expected_payload = @vm_1.inspect
payload_id = BinaryBlob.create!(:binary => expected_payload.dup)
target_1 = ManagerRefresh::Target.new(
:manager => @ems,
:association => :vms,
:manager_ref => {:ems_ref => @vm_1.ems_ref},
:payload_id => payload_id,
:options => {:opt1 => "opt1", :opt2 => "opt2"}
)
expect(target_1.payload).to eq(expected_payload)
end
it "returns nil if no payload_id" do
target_1 = ManagerRefresh::Target.new(
:manager => @ems,
:association => :vms,
:manager_ref => {:ems_ref => @vm_1.ems_ref},
:options => {:opt1 => "opt1", :opt2 => "opt2"}
)
expect(target_1.payload).to be_nil
end
end
context ".payload=" do
it "creates a binary blob" do
expected_payload = @vm_1.inspect
target_1 = ManagerRefresh::Target.new(
:manager => @ems,
:association => :vms,
:manager_ref => {:ems_ref => @vm_1.ems_ref},
:options => {:opt1 => "opt1", :opt2 => "opt2"}
)
target_1.payload = expected_payload.dup
expect(target_1.payload_id).to_not be_nil
expect(BinaryBlob.find(target_1.payload_id).binary).to eq(expected_payload)
end
it "overwrites a binary blob" do
first_payload = "abcd"
expected_payload = @vm_1.inspect
target_1 = ManagerRefresh::Target.new(
:manager => @ems,
:association => :vms,
:manager_ref => {:ems_ref => @vm_1.ems_ref},
:options => {:opt1 => "opt1", :opt2 => "opt2"}
)
target_1.payload = first_payload
payload_id = target_1.payload_id
target_1.payload = expected_payload.dup
expect(target_1.payload).to eq(expected_payload)
expect { BinaryBlob.find(payload_id) }.to raise_exception(ActiveRecord::RecordNotFound)
end
it "deletes the binary blob when the payload is cleared" do
target_1 = ManagerRefresh::Target.new(
:manager => @ems,
:association => :vms,
:manager_ref => {:ems_ref => @vm_1.ems_ref},
:options => {:opt1 => "opt1", :opt2 => "opt2"}
)
target_1.payload = "abcd"
payload_id = target_1.payload_id
target_1.payload = nil
expect(target_1.payload).to be_nil
expect { BinaryBlob.find(payload_id) }.to raise_exception(ActiveRecord::RecordNotFound)
end
end
end
end