forked from ManageIQ/manageiq
/
classification_spec.rb
549 lines (436 loc) · 20.6 KB
/
classification_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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
describe Classification do
context ".hash_all_by_type_and_name" do
it "with entries duped across categories should return both entries" do
clergy = FactoryGirl.create(:classification, :name => "clergy", :single_value => 1)
clergy_bishop = FactoryGirl.create(:classification_tag, :name => "bishop", :parent => clergy)
chess = FactoryGirl.create(:classification, :name => "chess", :single_value => 1)
chess_bishop = FactoryGirl.create(:classification_tag, :name => "bishop", :parent => chess)
expect(Classification.hash_all_by_type_and_name).to include(
"clergy" => {
:category => clergy,
:entry => {"bishop" => clergy_bishop}
},
"chess" => {
:category => chess,
:entry => {"bishop" => chess_bishop}
}
)
end
end
context "enforce_policy" do
let(:tag) { FactoryGirl.build(:classification_tag, :parent => FactoryGirl.build(:classification)) }
it "enforce_policy on sub-classed vm" do
allow(MiqEvent).to receive(:raise_evm_event).and_return(true)
vm = FactoryGirl.build(:vm_vmware, :name => "VM1")
tag.enforce_policy(vm, "fake_event")
end
it "enforce_policy on sub-classed host" do
allow(MiqEvent).to receive(:raise_evm_event).and_return(true)
host = FactoryGirl.build(:host_vmware_esx)
tag.enforce_policy(host, "fake_event")
end
end
context "with hierarchy" do
let(:host1) { FactoryGirl.create(:host, :name => "HOST1") }
let(:host2) { FactoryGirl.create(:host, :name => "HOST2") }
let(:host3) do
FactoryGirl.create(:host, :name => "HOST3").tap do |host|
Classification.find_by_name("test_category").entries.each { |ent| ent.assign_entry_to(host) }
end
end
let(:sti_inst) { FactoryGirl.create(:template_vmware) }
before do
parent = FactoryGirl.create(:classification, :name => "test_category")
FactoryGirl.create(:classification_tag, :name => "test_entry", :parent => parent)
FactoryGirl.create(:classification_tag, :name => "another_test_entry", :parent => parent)
parent = FactoryGirl.create(:classification, :name => "test_single_value_category", :single_value => 1)
FactoryGirl.create(:classification_tag, :name => "single_entry_1", :parent => parent)
FactoryGirl.create(:classification_tag, :name => "single_entry_2", :parent => parent)
parent = FactoryGirl.create(:classification, :name => "test_multi_value_category", :single_value => 0)
FactoryGirl.create(:classification_tag, :name => "multi_entry_1", :parent => parent)
FactoryGirl.create(:classification_tag, :name => "multi_entry_2", :parent => parent)
end
context "#destroy" do
it "a category deletes all entries" do
cat = Classification.find_by_name("test_category")
expect(cat).to_not be_nil
entries = cat.entries
expect(entries.length).to eq(2)
cat.destroy
entries.each { |e| expect(Classification.find_by(:id => e.id)).to be_nil }
end
it "a category deletes assignments referenced by its entries" do
cat = Classification.find_by_name("test_category")
assignment_tag = "/chargeback_rate/assigned_to/vm/tag/managed/test_category/test_entry"
Tag.create!(:name => assignment_tag)
expect(Tag.exists?(:name => assignment_tag)).to be true
cat.destroy
expect(Tag.exists?(:name => assignment_tag)).to be false
end
it "a category does not delete assignments that are close but do not match its tag" do
cat = Classification.find_by_name("test_category")
assignment_tag = "/chargeback_rate/assigned_to/vm/tag/managed/test_category/test_entry"
another_tag1 = Tag.create(:name => "/policy_set/assigned_to/vm/tag/managed/test_category/test_entry1")
another_tag2 = Tag.create(:name => "/chargeback_rate/assigned_to/vm/tag/managed/test_category1/test_entry")
Tag.create!(:name => assignment_tag)
cat.destroy
expect(Tag.exists?(:name => assignment_tag)).to be false
expect(Tag.exists?(:name => another_tag1.name)).to be true
expect(Tag.exists?(:name => another_tag2.name)).to be true
end
it "an entry deletes assignments where its tag is referenced" do
cat = Classification.find_by_name("test_category")
ent = cat.entries.find { |e| e.name == "test_entry" }
assignment_tag = "/chargeback_rate/assigned_to/vm/tag/managed/test_category/test_entry"
Tag.create!(:name => assignment_tag)
expect(Tag.exists?(:name => assignment_tag)).to be true
ent.destroy
expect(Tag.exists?(:name => assignment_tag)).to be false
end
end
it "should test setup data" do
cat = Classification.find_by_name("test_category")
expect(cat).to_not be_nil
expect(cat.tag).to_not be_nil
expect(File.split(cat.tag.name).last).to_not be_nil
end
it "should test add entry" do
cat = Classification.find_by_name("test_category")
ent = FactoryGirl.create(:classification_tag, :name => "test_add_entry", :parent => cat)
expect(ent).to be_valid
expect(cat.id.to_i).to eq(ent.parent_id)
expect(cat.entries.length).to eq(3)
end
it "should test add entry directly (without calling add_entry)" do
cat = Classification.find_by_name("test_category")
ent = cat.children.new(:name => "test_add_entry_1")
ent.save(:validate => false)
cat.reload
expect(cat.id.to_i).to eq(ent.parent_id)
expect(cat.entries.length).to eq(3)
end
it "should test categories" do
# Find all classification categories
expect(Classification.categories.length).to eq(3)
end
it "should test create duplicate category" do
expect do
FactoryGirl.create(:classification, :name => "test_category")
end.to raise_error(ActiveRecord::RecordInvalid)
end
it "should test update category" do
cat = Classification.find_by_name("test_category")
cat.name = "test_update_entry"
expect(cat).to be_valid
expect(cat.save).to be_truthy
end
it "should test add duplicate entry" do
cat = Classification.find_by_name("test_category")
ent = FactoryGirl.create(:classification_tag, :name => "test_add_dup_entry", :parent => cat)
expect(ent).to be_valid
expect do
FactoryGirl.create(:classification_tag, :name => "test_add_dup_entry", :parent => cat)
end.to raise_error(ActiveRecord::RecordInvalid)
end
it "should test update entry" do
cat = Classification.find_by_name("test_category")
ent = cat.entries[0]
ent.name = "test_update_entry"
expect(ent).to be_valid
expect(ent.save).to be_truthy
end
it "should test update entry to duplicate" do
cat = Classification.find_by_name("test_category")
ent = cat.entries[0]
ent.name = cat.entries[1].name
expect(ent).to_not be_valid
expect(ent.errors[:name]).to eq(["has already been taken"])
end
it "should test invalid name" do
['<My_Name>',
'My Name',
'My_Name_is...',
'123456789_123456789_123456789_123456789_123456789_1'
].each do |name|
cat = Classification.new(:name => name, :parent_id => 0)
expect(cat).to_not be_valid
expect(cat.errors[:name].size).to eq(1)
end
end
it "should be able to produce valid names" do
['<My_Name>',
'My Name',
'My_Name_is...',
'123456789_123456789_123456789_123456789_123456789_1'
].each do |name|
good_name = Classification.sanitize_name(name)
cat = Classification.new(:name => good_name, :description => name, :parent_id => 0)
expect(cat).to be_valid
end
end
it "should test assign single entry to" do
cat = Classification.find_by_name "test_single_value_category"
ent1 = cat.entries[0]
ent2 = cat.entries[1]
ent1.assign_entry_to(host1)
expect(any_tagged_with(Host, ent1.name, ent1.parent.name)).to eq([host1])
expect(any_tagged_with(Host, full_tag_name(ent1))).to eq([host1])
expect(all_tagged_with(Host, ent1.name, ent1.parent.name)).to eq([host1])
ent2.assign_entry_to(host1)
expect(any_tagged_with(Host, ent2.name, ent2.parent.name)).to eq([host1])
expect(any_tagged_with(Host, ent1.name, ent1.parent.name)).not_to eq([host1])
end
it "should test assign multi entry to" do
cat = Classification.find_by_name "test_multi_value_category"
ent1 = cat.entries[0]
ent2 = cat.entries[1]
ent1.assign_entry_to(host2)
expect(any_tagged_with(Host, ent1.name, ent1.parent.name)).to eq([host2])
expect(all_tagged_with(Host, ent1.name, ent1.parent.name)).to eq([host2])
expect(any_tagged_with(Host, [ent1.name, ent2.name], ent1.parent.name)).to eq([host2])
expect(all_tagged_with(Host, [ent1.name, ent2.name], ent1.parent.name)).to be_empty
ent2.assign_entry_to(host2)
expect(any_tagged_with(Host, ent2.name, ent2.parent.name)).to eq([host2])
expect(any_tagged_with(Host, ent1.name, ent1.parent.name)).to eq([host2])
expect(all_tagged_with(Host, "#{ent1.name} #{ent2.name}", ent1.parent.name)).to eq([host2])
expect(any_tagged_with(Host, [ent2.name, ent1.name], ent2.parent.name)).to eq([host2])
expect(any_tagged_with(Host, [full_tag_name(ent2), full_tag_name(ent1)])).to eq([host2])
end
it "find with multiple tags" do
cat1 = Classification.find_by_name "test_single_value_category"
ent11 = cat1.entries[0]
ent12 = cat1.entries[1]
cat2 = Classification.find_by_name "test_multi_value_category"
ent21 = cat2.entries[0]
ent22 = cat2.entries[1]
ent11.assign_entry_to(host2)
ent21.assign_entry_to(host2)
# success
expect(any_tagged_with(Host, [[full_tag_name(ent12), full_tag_name(ent11)], [full_tag_name(ent21)]])).to eq([host2])
expect(all_tagged_with(Host, [[full_tag_name(ent11)], [full_tag_name(ent11)]])).to eq([host2])
# failure
expect(all_tagged_with(Host, [[full_tag_name(ent12), full_tag_name(ent11)], [full_tag_name(ent21)]])
).not_to eq([host2])
expect(all_tagged_with(Host, [[full_tag_name(ent11)], [full_tag_name(ent22)]])).not_to eq([host2])
expect(all_tagged_with(Host, [[full_tag_name(ent12)], [full_tag_name(ent21)]])).not_to eq([host2])
end
it "finds tagged items with order clause" do
cat1 = Classification.find_by_name "test_single_value_category"
ent11 = cat1.entries[0]
ent11.assign_entry_to(host1)
expect(all_tagged_with(Host.order('name'), ent11.name, ent11.parent.name)).to eq([host1])
expect(any_tagged_with(Host.order('name'), ent11.name, ent11.parent.name)).to eq([host1])
expect(all_tagged_with(Host.order('lower(name)'), ent11.name, ent11.parent.name)).to eq([host1])
expect(any_tagged_with(Host.order('lower(name)'), ent11.name, ent11.parent.name)).to eq([host1])
expect(all_tagged_with(Host.order(Host.arel_table[:name].lower), ent11.name, ent11.parent.name)).to eq([host1])
expect(any_tagged_with(Host.order(Host.arel_table[:name].lower), ent11.name, ent11.parent.name)).to eq([host1])
end
it "should test find by entry" do
cat = Classification.find_by_name "test_multi_value_category"
ent1 = cat.entries[0]
ent1.assign_entry_to(host2)
expect(ent1.find_by_entry("Host")[0].name).to eq(host2.name)
end
it "should test remove entry from" do
cat = Classification.find_by_name "test_multi_value_category"
ent1 = cat.entries[0]
ent2 = cat.entries[1]
ent1.assign_entry_to(host2)
ent2.assign_entry_to(host2)
expect(all_tagged_with(Host, "#{ent1.name} #{ent2.name}", ent1.parent.name)).to_not be_empty
ent1.remove_entry_from(host2)
expect(any_tagged_with(Host, ent2.name, ent2.parent.name)).to_not be_empty
expect(any_tagged_with(Host, ent1.name, ent1.parent.name)).to be_empty
end
it "should test find assigned entries" do
expect(Classification.find_assigned_entries(host3).length).to eq(2)
end
it "should test to_tag" do
cat = Classification.find_by_name("test_category")
expect(cat).to_not be_nil
expect(cat.entries.length).to eq(2)
cat.entries.each { |ent| expect(ent.to_tag).to eq("/managed/#{cat.name}/#{ent.name}") }
end
it "should test assign single entry to an STI instance" do
cat = Classification.find_by_name "test_single_value_category"
ent1 = cat.entries[0]
ent2 = cat.entries[1]
ent1.assign_entry_to(sti_inst)
expect(any_tagged_with(MiqTemplate, ent1.name, ent1.parent.name)).to_not be_empty
ent2.assign_entry_to(sti_inst)
expect(any_tagged_with(MiqTemplate, ent2.name, ent2.parent.name)).to_not be_empty
expect(any_tagged_with(MiqTemplate, ent1.name, ent1.parent.name)).to be_empty
end
it "should test find by entry for an STI instance" do
cat = Classification.find_by_name "test_multi_value_category"
ent1 = cat.entries[0]
ent1.assign_entry_to(sti_inst)
expect(ent1.find_by_entry("MiqTemplate")[0]).to eq(sti_inst)
end
it "should test remove entry from an STI instance" do
cat = Classification.find_by_name "test_multi_value_category"
ent1 = cat.entries[0]
ent2 = cat.entries[1]
ent1.assign_entry_to(sti_inst)
ent2.assign_entry_to(sti_inst)
expect(all_tagged_with(MiqTemplate, "#{ent1.name} #{ent2.name}", ent1.parent.name)).to_not be_empty
ent1.remove_entry_from(sti_inst)
expect(any_tagged_with(MiqTemplate, ent2.name, ent2.parent.name)).to_not be_empty
expect(any_tagged_with(MiqTemplate, ent1.name, ent1.parent.name)).to be_empty
end
context "#bulk_assignemnt" do
before do
@options = {:model => "Host"}
targets = [host1, host2, host3]
@options[:object_ids] = targets.collect(&:id)
cat = Classification.find_by_name("test_category")
@dels = cat.entries.collect
@options[:delete_ids] = @dels.collect(&:id)
cat = Classification.find_by_name("test_multi_value_category")
@adds = cat.entries.collect
@options[:add_ids] = @adds.collect(&:id)
end
it "normal case" do
Classification.bulk_reassignment(@options)
@dels.each { |d| expect(any_tagged_with(Host, d.name, d.parent.name)).to be_empty }
@adds.each { |a| expect(all_tagged_with(Host, a.name, a.parent.name)).to_not be_empty }
end
it "with some errors" do
allow(MiqEvent).to receive(:raise_evm_event).and_raise
expect { Classification.bulk_reassignment(@options) }
.to raise_error(RuntimeError, /Failures occurred during bulk reassignment/)
@dels.each { |d| expect(any_tagged_with(Host, d.name, d.parent.name)).to_not be_empty }
@adds.each { |a| expect(all_tagged_with(Host, a.name, a.parent.name)).to be_empty }
end
end
context "#category" do
it "for tag" do
tag = Classification.find_by_name("test_category").children.first
expect(tag.category).to eq("test_category")
end
it "for category" do
expect(Classification.find_by_name("test_category").category).to be_nil
end
end
end
describe ".seed" do
before do
allow(YAML).to receive(:load_file).and_return([
{:name => "cc",
:description => "Cost Center",
:example_text => "Cost Center",
:read_only => "0",
:syntax => "string",
:show => true,
:parent_id => 0,
:default => true,
:single_value => "1",
:entries => [{:description => "Cost Center 001", :name => "001"},
{:description => "Cost Center 002", :name => "002"}]
}]
)
end
context "after seeding" do
before { Classification.seed }
it "loads categories and tags" do
expect(Classification.categories.count).to eq(1)
expect(Classification.categories.first.entries.count).to eq(2)
end
it "re-seeds deleted categories" do
Classification.categories.first.destroy
expect(Classification.count).to eq(0)
Classification.seed
expect(Classification.count).to eq(3)
end
it "does not re-seed deleted tags" do
Classification.where("parent_id != 0").destroy_all
expect(Classification.count).to eq(1)
Classification.seed
expect(Classification.count).to eq(1)
end
end
it "does not re-seed existing categories" do
category = FactoryGirl.create(:classification_cost_center,
:description => "user defined",
:example_text => "user defined",
:show => false,
:single_value => "0")
category_attrs = category.attributes
Classification.seed
category.reload
expect(category_attrs).to eq(category.attributes)
end
end
describe '.find_by_name' do
let(:my_region_number) { Classification.my_region_number }
let(:other_region) { Classification.my_region_number + 1 }
let(:other_region_id) { other_region * Classification.rails_sequence_factor + 1 }
before do
@local = FactoryGirl.create(:classification, :name => "test_category1")
FactoryGirl.create(:classification, :name => "test_category3")
FactoryGirl.create(:tag, :name => "/managed/test_category2", :id => other_region_id)
@remote = FactoryGirl.create(:classification, :name => "test_category2", :id => other_region_id)
end
it "created classification in other region" do
expect(@remote.region_id).to eq(other_region)
expect(@remote.reload.id).to eq(other_region_id)
expect(@remote.tag_id).to eq(other_region_id)
expect(@remote.tag.region_id).to eq(other_region)
end
it "finds in region" do
local = Classification.find_by_name("test_category1", my_region_number)
expect(local).to eq(@local)
remote = Classification.find_by_name("test_category2", other_region)
expect(remote).to eq(@remote)
end
it "filters out wrong region" do
expect(Classification.find_by_name("test_category1", other_region)).to be_nil
expect(Classification.find_by_name("test_category2", my_region_number)).to be_nil
end
it "finds in all regions" do
expect(Classification.find_by_name("test_category1", nil)).to eq(@local)
expect(Classification.find_by_name("test_category2", nil)).to eq(@remote)
end
it "finds in my region" do
expect(Classification.find_by_name("test_category1")).to eq(@local)
expect(Classification.find_by_name("test_category2")).to be_nil
end
end
describe '.find_by_names' do
let(:my_region_number) { Classification.my_region_number }
let(:other_region) { Classification.my_region_number + 1 }
let(:other_region_id) { other_region * Classification.rails_sequence_factor + 1 }
before do
@local = FactoryGirl.create(:classification, :name => "test_category1")
FactoryGirl.create(:tag, :name => Classification.name2tag("test_category2"), :id => other_region_id)
@remote = FactoryGirl.create(:classification, :name => "test_category2", :id => other_region_id)
FactoryGirl.create(:classification, :name => "test_category3")
end
it "finds in region" do
expect(Classification.find_by_names(%w(test_category1 test_category2), my_region_number)).to eq([@local])
expect(Classification.find_by_names(%w(test_category1 test_category2), other_region)).to eq([@remote])
end
it "finds in all regions" do
expect(Classification.find_by_names(%w(test_category1 test_category2), nil)).to match_array([@local, @remote])
end
it "finds in my region" do
Classification.find_by_name(%w(test_category1 test_category2))
expect(Classification.find_by_names(%w(test_category1 test_category2))).to eq([@local])
end
end
def all_tagged_with(target, all, category = nil)
tagged_with(target, :all => all, :cat => category)
end
def any_tagged_with(target, any, category = nil)
tagged_with(target, :any => any, :cat => category)
end
def tagged_with(target, options)
target.find_tagged_with(options.merge!(:ns => Classification::DEFAULT_NAMESPACE))
end
def grouped_with(target, options)
target.find_tags_by_grouping(options.merge!(:ns => Classification::DEFAULT_NAMESPACE))
end
def full_tag_name(tag)
Classification.name2tag(tag.name, tag.parent, "") # avoid "managed", it will get added later
end
end