/
custom_button_spec.rb
388 lines (335 loc) · 15.1 KB
/
custom_button_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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
describe CustomButton do
describe '.with_array_order' do
context 'order by array' do
let!(:custom_button_1) { FactoryGirl.create(:custom_button, :name => 'AAA', :applies_to_id => 100, :applies_to_class => Vm) }
let!(:custom_button_2) { FactoryGirl.create(:custom_button, :name => 'BBB', :applies_to_id => 200, :applies_to_class => Vm) }
let!(:custom_button_3) { FactoryGirl.create(:custom_button, :name => 'CCC', :applies_to_id => 300, :applies_to_class => Vm) }
context 'by bigint column' do
it 'orders by memory_shares column' do
expect(CustomButton.with_array_order(%w(300 100 200), :applies_to_id).ids).to eq([custom_button_3.id, custom_button_1.id, custom_button_2.id])
end
end
context 'by string column' do
it 'orders by name column' do
expect(CustomButton.with_array_order(%w(BBB AAA CCC), :name, :text).ids).to eq([custom_button_2.id, custom_button_1.id, custom_button_3.id])
end
end
end
end
context "with no buttons" do
describe '#evaluate_enablement_expression_for' do
let(:miq_expression) { MiqExpression.new('EQUAL' => {'field' => 'Vm-name', 'value' => 'vm_1'}) }
let(:vm) { FactoryGirl.create(:vm_vmware, :name => 'vm_1') }
let(:custom_button) do
FactoryGirl.create(:custom_button, :applies_to => vm.class, :name => "foo", :description => "foo foo")
end
it 'evaluates enablement expression when expression is not set' do
expect(custom_button.evaluate_enablement_expression_for(vm)).to be_truthy
end
it 'evaluates enablement expression when expression is set and method is called for list' do
custom_button.enablement_expression = miq_expression
expect(custom_button.evaluate_enablement_expression_for(nil)).to be_falsey
end
it 'evaluates enablement expression when expression is set and evaluated to true' do
custom_button.enablement_expression = miq_expression
expect(custom_button.evaluate_enablement_expression_for(vm)).to be_truthy
end
it 'evaluates enablement expression when expression is set and evaluated to false' do
custom_button.enablement_expression = miq_expression
vm.name = 'XXX'
expect(custom_button.evaluate_enablement_expression_for(vm)).to be_falsey
end
end
it "should validate there are no buttons" do
expect(described_class.count).to eq(0)
end
context "when I create a button via save_as_button class method" do
before do
@button_name = "Power ON"
@button_text = "Power ON during Business Hours ONLY"
@button_number = 3
@button_class = "Vm"
@target_id = 2
@ae_name = 'Automation'
@ae_attributes = {'phrase' => 'Hello World'}
@ae_uri = MiqAeEngine.create_automation_object(@ae_name, @ae_attributes)
@userid = "guest"
uri_path, uri_attributes, uri_message = CustomButton.parse_uri(@ae_uri)
@button = FactoryGirl.create(:custom_button,
:name => @button_name,
:description => @button_text,
:applies_to_class => @button_class,
:applies_to_id => @target_id,
:uri => @ae_uri,
:uri_path => uri_path,
:uri_attributes => uri_attributes,
:uri_message => uri_message,
:userid => @userid
)
end
it "creates the proper button" do
expect(described_class.count).to eq(1)
expect(@button.uri_path).to eq('/System/Process/Automation')
expect(@button.applies_to_id).to eq(@target_id)
expect(@button.uri_object_name).to eq(@ae_name)
@ae_attributes.each { |key, value| expect(@button.uri_attributes[key]).to eq(value.to_s) }
# These attributes are not longer stored with the button
expect(@button.uri_attributes['User::user']).to be_nil
expect(@button.uri_attributes['MiqServer::miq_server']).to be_nil
end
context "when invoking for a particular VM" do
before do
@vm = FactoryGirl.create(:vm_vmware)
@user2 = FactoryGirl.create(:user_with_group)
EvmSpecHelper.local_miq_server(:is_master => true, :zone => Zone.seed)
end
it "calls automate without saved User and MiqServer" do
User.with_user(@user2) { @button.invoke(@vm) }
expect(MiqQueue.count).to eq(1)
q = MiqQueue.first
expect(q.class_name).to eq("MiqAeEngine")
expect(q.method_name).to eq("deliver")
expect(q.role).to eq("automate")
expect(q.zone).to eq("default")
expect(q.priority).to eq(MiqQueue::HIGH_PRIORITY)
a = q.args
expect(a).to be_kind_of(Array)
h = a.first
expect(h).to be_kind_of(Hash)
expect(h[:user_id]).to eq(@user2.id)
expect(h[:object_type]).to eq(@vm.class.base_class.name)
expect(h[:object_id]).to eq(@vm.id)
expect(h[:attrs]).to include(@ae_attributes)
expect(h[:instance_name]).to eq(@ae_name)
end
end
end
end
it ".buttons_for" do
vm = FactoryGirl.create(:vm_vmware)
vm_other = FactoryGirl.create(:vm_vmware)
button1all = FactoryGirl.create(:custom_button,
:applies_to => vm.class,
:name => "foo",
:description => "foo foo")
button1vm = FactoryGirl.create(:custom_button,
:applies_to => vm,
:name => "bar",
:description => "bar bar")
button2vm = FactoryGirl.create(:custom_button,
:applies_to => vm,
:name => "foo",
:description => "foo foo")
expect(described_class.buttons_for(Host)).to eq([])
expect(described_class.buttons_for(Vm)).to eq([button1all])
expect(described_class.buttons_for(vm)).to match_array([button1vm, button2vm])
expect(described_class.buttons_for(vm_other)).to eq([])
end
it "#save" do
ra = FactoryGirl.create(:resource_action, :ae_namespace => 'SYSTEM', :ae_class => 'PROCESS', :ae_message => 'create')
button = FactoryGirl.create(:custom_button, :name => "My test button", :applies_to => Vm, :resource_action => ra)
button.save
ra.ae_message = "new message"
button.save
expect(button.reload.resource_action.ae_message).to eq('new message')
end
context "validates uniqueness" do
before do
@vm = FactoryGirl.create(:vm_vmware)
@default_name = @default_description = "boom"
end
it "applies_to_class" do
button_for_all_vms = FactoryGirl.create(:custom_button,
:applies_to_class => 'Vm',
:name => @default_name,
:description => @default_description)
expect(button_for_all_vms).to be_valid
new_host_button = described_class.new(
:applies_to_class => 'Host',
:name => @default_name,
:description => @default_description)
expect(new_host_button).to be_valid
dup_vm_button = described_class.new(
:applies_to_class => 'Vm',
:name => @default_name,
:description => @default_description)
expect(dup_vm_button).not_to be_valid
dup_vm_name_button = described_class.new(
:applies_to_class => 'Vm',
:name => @default_name,
:description => "hello world")
expect(dup_vm_name_button).not_to be_valid
dup_vm_desc_button = described_class.new(
:applies_to_class => 'Vm',
:name => "hello",
:description => @default_description)
expect(dup_vm_desc_button).not_to be_valid
new_vm_button = described_class.new(
:applies_to_class => 'Vm',
:name => "hello",
:description => "hello world")
expect(new_vm_button).to be_valid
end
it "applies_to_instance" do
vm_other = FactoryGirl.create(:vm_vmware)
button_for_single_vm = FactoryGirl.create(:custom_button,
:applies_to => @vm,
:name => @default_name,
:description => @default_description)
expect(button_for_single_vm).to be_valid
# For same VM
dup_vm_button = described_class.new(
:applies_to => @vm,
:name => @default_name,
:description => @default_description)
expect(dup_vm_button).not_to be_valid
dup_vm_name_button = described_class.new(
:applies_to => @vm,
:name => @default_name,
:description => "hello world")
expect(dup_vm_name_button).not_to be_valid
dup_vm_desc_button = described_class.new(
:applies_to => @vm,
:name => "hello",
:description => @default_description)
expect(dup_vm_desc_button).not_to be_valid
new_vm_button = described_class.new(
:applies_to => @vm,
:name => "hello",
:description => "hello world")
expect(new_vm_button).to be_valid
# For other VM
dup_vm_button = described_class.new(
:applies_to => vm_other,
:name => @default_name,
:description => @default_description)
expect(dup_vm_button).to be_valid
dup_vm_name_button = described_class.new(
:applies_to => vm_other,
:name => @default_name,
:description => "hello world")
expect(dup_vm_name_button).to be_valid
dup_vm_desc_button = described_class.new(
:applies_to => vm_other,
:name => "hello",
:description => @default_description)
expect(dup_vm_desc_button).to be_valid
new_vm_button = described_class.new(
:applies_to => vm_other,
:name => "hello",
:description => "hello world")
expect(new_vm_button).to be_valid
end
end
describe "#expanded_serializable_hash" do
let(:test_button) { described_class.new(:resource_action => resource_action) }
let(:expected_hash) do
{
"id" => nil,
"guid" => nil,
"description" => nil,
"disabled_text" => nil,
"enablement_expression" => nil,
"applies_to_class" => nil,
"options" => {},
"userid" => nil,
"wait_for_complete" => nil,
"created_on" => nil,
"updated_on" => nil,
"name" => nil,
"visibility" => nil,
"visibility_expression" => nil,
"applies_to_id" => nil
}
end
context "when a resource action exists" do
let(:resource_action) { ResourceAction.new }
before do
allow(resource_action).to receive(:serializable_hash).and_return("resource_action_hash")
end
it "returns the button as a serializable hash with the resource action serialized hash" do
expect(test_button.expanded_serializable_hash).to eq(
expected_hash.merge(:resource_action => "resource_action_hash")
)
end
end
context "when a resource action does not exist" do
let(:resource_action) { nil }
it "returns the button as a serializable hash" do
expect(test_button.expanded_serializable_hash).to eq(expected_hash)
end
end
end
it "#copy" do
service_template1 = FactoryGirl.create(:service_template)
service_template2 = FactoryGirl.create(:service_template)
button = FactoryGirl.create(:custom_button, :applies_to => service_template1)
expect { button.copy(:applies_to => service_template2) }.to change { CustomButton.count }.by(1)
end
describe "publish custom button event" do
let(:vm) { FactoryGirl.create(:vm_vmware) }
let(:vm2) { FactoryGirl.create(:vm_vmware) }
let(:vm3) { FactoryGirl.create(:vm_vmware) }
let(:user) { FactoryGirl.create(:user_with_group) }
let(:resource_action) { FactoryGirl.create(:resource_action, :ae_namespace => 'SYSTEM', :ae_class => 'PROCESS', :ae_instance => 'Request') }
let(:custom_button) { FactoryGirl.create(:custom_button, :applies_to => vm.class, :resource_action => resource_action) }
before do
EvmSpecHelper.local_miq_server(:is_master => true, :zone => Zone.seed)
end
%i(invoke invoke_async).each do |method|
describe "##{method}", "publishes CustomButtonEvent(s)" do
it "with a single VM" do
Timecop.freeze(Time.now.utc) do
User.with_user(user) { custom_button.send(method, vm, 'UI') }
expect(CustomButtonEvent.first.timestamp).to be_within(0.01).of(Time.now.utc)
end
expect(CustomButtonEvent.count).to eq(1)
expect(CustomButtonEvent.first).to have_attributes(
:source => 'UI',
:target_id => vm.id,
:target_type => 'VmOrTemplate',
:type => 'CustomButtonEvent',
:event_type => 'button.trigger.start',
:user_id => user.id,
:full_data => a_hash_including(:automate_entry_point => "/SYSTEM/PROCESS/Request")
)
end
describe "multiple vms" do
it "with an array" do
Timecop.freeze(Time.now.utc) do
User.with_user(user) { custom_button.send(method, [vm, vm2, vm3], 'UI') }
expect(CustomButtonEvent.first.timestamp).to be_within(0.01).of(Time.now.utc)
end
expect(CustomButtonEvent.count).to eq(3)
expect(CustomButtonEvent.first).to have_attributes(
:source => 'UI',
:target_id => vm.id,
:target_type => 'VmOrTemplate',
:type => 'CustomButtonEvent',
:event_type => 'button.trigger.start',
:user_id => user.id,
:full_data => a_hash_including(:automate_entry_point => "/SYSTEM/PROCESS/Request")
)
end
it "with an ActiveRecord::Relation" do
vm && vm2 && vm3
Timecop.freeze(Time.now.utc) do
User.with_user(user) { custom_button.send(method, Vm.all, 'UI') }
expect(CustomButtonEvent.first.timestamp).to be_within(0.01).of(Time.now.utc)
end
expect(CustomButtonEvent.count).to eq(3)
expect(CustomButtonEvent.first).to have_attributes(
:source => 'UI',
:target_id => vm.id,
:target_type => 'VmOrTemplate',
:type => 'CustomButtonEvent',
:event_type => 'button.trigger.start',
:user_id => user.id,
:full_data => a_hash_including(:automate_entry_point => "/SYSTEM/PROCESS/Request")
)
end
end
end
end
end
end