forked from ManageIQ/manageiq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_template_spec.rb
1342 lines (1166 loc) · 58.5 KB
/
service_template_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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
describe ServiceTemplate do
include_examples "OwnershipMixin"
let(:service_user) { FactoryBot.build(:user) }
describe "#custom_actions" do
it "returns the custom actions in a hash grouped by buttons and button groups" do
FactoryBot.create(:custom_button, :name => "generic_no_group", :applies_to_class => "Service")
generic_group = FactoryBot.create(:custom_button, :name => "generic_group", :applies_to_class => "Service")
generic_group_set = FactoryBot.create(:custom_button_set, :name => "generic_group_set")
generic_group_set.set_data = {:button_order => [generic_group.id]}
generic_group_set.save!
service_template = FactoryBot.create(:service_template)
FactoryBot.create(
:custom_button,
:name => "assigned_no_group",
:applies_to_class => "ServiceTemplate",
:applies_to_id => service_template.id
)
assigned_group = FactoryBot.create(
:custom_button,
:name => "assigned_group",
:applies_to_class => "ServiceTemplate",
:applies_to_id => service_template.id
)
assigned_group_set = FactoryBot.create(:custom_button_set, :name => "assigned_group_set")
assigned_group_set.set_data = {:button_order => [assigned_group.id]}
assigned_group_set.save!
service_template.update(:custom_button_sets => [assigned_group_set])
expected = {
:buttons => a_collection_containing_exactly(
a_hash_including("name" => "generic_no_group"),
a_hash_including("name" => "assigned_no_group")
),
:button_groups => a_collection_containing_exactly(
a_hash_including(
"name" => "assigned_group_set",
:buttons => [a_hash_including("name" => "assigned_group")]
),
a_hash_including(
"name" => "generic_group_set",
:buttons => [a_hash_including("name" => "generic_group")]
)
)
}
expect(service_template.custom_actions).to match(expected)
end
it "does not show hidden buttons" do
service_template = FactoryBot.create(:service_template)
service = FactoryBot.create(:service, :name => "foo", :service_template => service_template)
true_expression = MiqExpression.new("=" => {"field" => "Service-name", "value" => "foo"})
false_expression = MiqExpression.new("=" => {"field" => "Service-name", "value" => "labar"})
FactoryBot.create(:custom_button,
:name => "visible button",
:applies_to_class => "Service",
:visibility_expression => true_expression)
FactoryBot.create(:custom_button,
:name => "hidden button",
:applies_to_class => "Service",
:visibility_expression => false_expression)
FactoryBot.create(:custom_button_set).tap do |group|
group.add_member(FactoryBot.create(:custom_button,
:name => "visible button in group",
:applies_to_class => "Service",
:visibility_expression => true_expression))
group.add_member(FactoryBot.create(:custom_button,
:name => "hidden button in group",
:applies_to_class => "Service",
:visibility_expression => false_expression))
end
expected = {
:buttons => [
a_hash_including("name" => "visible button")
],
:button_groups => [
a_hash_including(
:buttons => [
a_hash_including("name" => "visible button in group")
]
)
]
}
expect(service_template.custom_actions(service)).to match(expected)
end
context "expression evaluation" do
let(:service_template) { FactoryBot.create(:service_template, :prov_type=> "vmware") }
let(:service) { FactoryBot.create(:service, :name => "foo", :service_template => service_template) }
let(:true_expression_on_template) do
MiqExpression.new("=" => {"field" => "ServiceTemplate-prov_type", "value" => "vmware"})
end
let(:false_expression_on_template) do
MiqExpression.new("=" => {"field" => "ServiceTemplate-prov_type", "value" => "not_vmware"})
end
let(:true_expression_on_service) do
MiqExpression.new("=" => {"field" => "Service-name", "value" => "foo"})
end
let(:false_expression_on_service) do
MiqExpression.new("=" => {"field" => "Service-name", "value" => "not_foo"})
end
before do
FactoryBot.create(:custom_button,
:name => "visible button on service",
:applies_to_class => "Service",
:visibility_expression => true_expression_on_service)
FactoryBot.create(:custom_button,
:name => "hidden button on service",
:applies_to_class => "Service",
:visibility_expression => false_expression_on_service)
FactoryBot.create(:custom_button,
:name => "visible button on template",
:applies_to_class => "ServiceTemplate",
:applies_to_id => service_template.id,
:visibility_expression => true_expression_on_template)
FactoryBot.create(:custom_button,
:name => "hidden visible button on template",
:applies_to_class => "ServiceTemplate",
:applies_to_id => service_template.id,
:visibility_expression => false_expression_on_template)
end
it "uses ServiceTemplate object to evaluate expression defined on Service Template if no parameter passed" do
expected = {
:buttons => [
a_hash_including("name" => "visible button on template")
],
:button_groups => []
}
expect(service_template.custom_actions).to match(expected)
end
it "uses passed object for expression defined on that object and ServiceTemplate for expression on template" do
expected = {
:buttons => a_collection_containing_exactly(
a_hash_including("name" => "visible button on service"),
a_hash_including("name" => "visible button on template")
),
:button_groups => []
}
expect(service_template.custom_actions(service)).to match(expected)
end
end
it "serializes the enablement" do
service_template = FactoryBot.create(:service_template, :name => "foo")
service = FactoryBot.create(:service, :name => "bar", :service_template => service_template)
true_expression = MiqExpression.new("=" => {"field" => "Service-name", "value" => "bar"})
false_expression = MiqExpression.new("=" => {"field" => "Service-name", "value" => "foo"})
FactoryBot.create(:custom_button,
:name => "enabled button",
:applies_to_class => "Service",
:enablement_expression => true_expression)
FactoryBot.create(:custom_button,
:name => "disabled button",
:applies_to_class => "Service",
:enablement_expression => false_expression)
FactoryBot.create(:custom_button_set).tap do |group|
group.add_member(FactoryBot.create(:custom_button,
:name => "enabled button in group",
:applies_to_class => "Service",
:enablement_expression => true_expression))
group.add_member(FactoryBot.create(:custom_button,
:name => "disabled button in group",
:applies_to_class => "Service",
:enablement_expression => false_expression))
end
expected = {
:buttons => a_collection_containing_exactly(
a_hash_including("name" => "enabled button", "enabled" => true),
a_hash_including("name" => "disabled button", "enabled" => false)
),
:button_groups => [
a_hash_including(
:buttons => a_collection_containing_exactly(
a_hash_including("name" => "enabled button in group", "enabled" => true),
a_hash_including("name" => "disabled button in group", "enabled" => false)
)
)
]
}
expect(service_template.custom_actions(service)).to match(expected)
end
end
describe "#custom_action_buttons" do
it "does not show hidden buttons" do
service_template = FactoryBot.create(:service_template, :name => "foo")
true_expression = MiqExpression.new("=" => {"field" => "ServiceTemplate-name", "value" => "foo"})
false_expression = MiqExpression.new("=" => {"field" => "ServiceTemplate-name", "value" => "bar"})
visible_button = FactoryBot.create(:custom_button,
:applies_to_class => "ServiceTemplate",
:applies_to_id => service_template.id,
:visibility_expression => true_expression)
_hidden_button = FactoryBot.create(:custom_button,
:applies_to_class => "ServiceTemplate",
:applies_to_id => service_template.id,
:visibility_expression => false_expression)
visible_button_in_group = FactoryBot.create(:custom_button,
:applies_to_class => "ServiceTemplate",
:applies_to_id => service_template.id,
:visibility_expression => true_expression)
hidden_button_in_group = FactoryBot.create(:custom_button,
:applies_to_class => "ServiceTemplate",
:applies_to_id => service_template.id,
:visibility_expression => false_expression)
service_template.custom_button_sets << FactoryBot.create(:custom_button_set).tap do |group|
group.add_member(visible_button_in_group)
group.add_member(hidden_button_in_group)
end
expect(service_template.custom_action_buttons).to contain_exactly(visible_button, visible_button_in_group)
end
end
context "#type_display" do
before do
@st1 = FactoryBot.create(:service_template, :name => 'Service Template 1')
end
it "with default service_type" do
expect(@st1.service_type).to eq("atomic")
expect(@st1.type_display).to eq('Item')
end
it "with service_type of atomic" do
@st1.update_attributes(:service_type => described_class::SERVICE_TYPE_ATOMIC)
expect(@st1.type_display).to eq('Item')
end
it "with service_type of composite" do
@st1.update_attributes(:service_type => described_class::SERVICE_TYPE_COMPOSITE)
expect(@st1.type_display).to eq('Bundle')
end
it "with user service_type" do
@st1.update_attributes(:service_type => 'user')
expect(@st1.type_display).to eq('User')
end
it "with no service_type" do
@st1.update_attributes(:service_type => nil)
expect(@st1.type_display).to eq('Unknown')
end
end
context "#atomic?" do
before do
@st1 = FactoryBot.create(:service_template)
end
it "with service_type of unknown" do
@st1.update_attributes(:service_type => 'user')
expect(@st1.atomic?).to be_falsey
end
it "with service_type of atomic" do
@st1.update_attributes(:service_type => described_class::SERVICE_TYPE_ATOMIC)
expect(@st1.atomic?).to be_truthy
end
it "with service_type of composite" do
@st1.update_attributes(:service_type => described_class::SERVICE_TYPE_COMPOSITE)
expect(@st1.atomic?).to be_falsey
end
end
context "#template_copy" do
let(:service_template_ansible_tower) { FactoryBot.create(:service_template_ansible_tower, :name => "new_template") }
let(:service_template_orchestration) { FactoryBot.create(:service_template_orchestration, :name => "new_template2") }
let(:custom_button) { FactoryBot.create(:custom_button, :applies_to_class => "Service") }
let(:custom_button_set) { FactoryBot.create(:custom_button_set, :owner => @st1) }
before do
@st1 = FactoryBot.create(:service_template)
end
context "with given name" do
it "without resource " do
expect(ServiceTemplate.count).to eq(1)
@st1.template_copy("new_template")
expect(ServiceTemplate.count).to eq(2)
new_service_template = ServiceTemplate.find_by(:name => "new_template")
expect(new_service_template.display).to be(false)
expect(new_service_template.guid).not_to eq(@st1.guid)
end
it "with custom button" do
custom_button
expect(@st1.custom_buttons.count).to eq(1)
@st1.template_copy("new_template")
expect(ServiceTemplate.count).to eq(2)
new_service_template = ServiceTemplate.find_by(:name => "new_template")
expect(new_service_template.display).to be(false)
expect(new_service_template.guid).not_to eq(@st1.guid)
expect(new_service_template.custom_buttons.count).to eq(2)
end
it "with custom button set" do
custom_button_set.add_member(custom_button)
expect(@st1.custom_button_sets.count).to eq(1)
@st1.template_copy("new_template")
new_service_template = ServiceTemplate.find_by(:name => "new_template")
expect(ServiceTemplate.count).to eq(2)
expect(new_service_template.display).to be(false)
expect(new_service_template.guid).not_to eq(@st1.guid)
expect(new_service_template.custom_button_sets.count).to eq(1)
end
it "with non-copyable resource (configuration script base)" do
@st1.add_resource(FactoryBot.create(:configuration_script_base))
expect(ServiceTemplate.count).to eq(1)
@st1.template_copy("new_template")
new_service_template = ServiceTemplate.find_by(:name => "new_template")
expect(ServiceTemplate.count).to eq(2)
expect(@st1.service_resources.first.resource).not_to be(nil)
expect(new_service_template.service_resources.first.resource).to eq(@st1.service_resources.first.resource)
expect(ConfigurationScriptBase.count).to eq(1)
expect(new_service_template.display).to be(false)
expect(new_service_template.guid).not_to eq(@st1.guid)
end
it "with non-copyable resource (ext management system)" do
@st1.add_resource(FactoryBot.create(:ext_management_system))
expect(ServiceTemplate.count).to eq(1)
@st1.template_copy("new_template")
new_service_template = ServiceTemplate.find_by(:name => "new_template")
expect(ServiceTemplate.count).to eq(2)
expect(new_service_template.service_resources.first.resource_id).to eq(@st1.service_resources.first.resource_id)
expect(ExtManagementSystem.count).to eq(1)
expect(new_service_template.guid).not_to eq(@st1.guid)
expect(new_service_template.display).to be(false)
expect(@st1.service_resources.first.resource).not_to be(nil)
end
it "with non-copyable resource (orchestration template)" do
@st1.add_resource(FactoryBot.create(:orchestration_template))
expect(ServiceTemplate.count).to eq(1)
@st1.template_copy("new_template")
new_service_template = ServiceTemplate.find_by(:name => "new_template")
expect(ServiceTemplate.count).to eq(2)
expect(new_service_template.service_resources.first.resource_id).to eq(@st1.service_resources.first.resource_id)
expect(OrchestrationTemplate.count).to eq(1)
expect(new_service_template.guid).not_to eq(@st1.guid)
expect(new_service_template.display).to be(false)
expect(@st1.service_resources.first.resource).not_to be(nil)
end
it "with copyable resource" do
admin = FactoryBot.create(:user_admin)
vm_template = FactoryBot.create(:vm_openstack, :ext_management_system => FactoryBot.create(:ext_management_system))
ptr = FactoryBot.create(:miq_provision_request_template, :requester => admin, :src_vm_id => vm_template.id)
@st1.add_resource(ptr)
expect(ServiceTemplate.count).to eq(1)
@st1.template_copy("new_template")
new_service_template = ServiceTemplate.find_by(:name => "new_template")
expect(ServiceTemplate.count).to eq(2)
expect(MiqProvisionRequestTemplate.count).to eq(2)
expect(new_service_template.guid).not_to eq(@st1.guid)
expect(new_service_template.display).to be(false)
expect(new_service_template.service_resources).not_to be(nil)
expect(@st1.service_resources.first.resource).not_to be(nil)
end
it "with copyable resource copies sr options" do
admin = FactoryBot.create(:user_admin)
vm_template = FactoryBot.create(:vm_openstack, :ext_management_system => FactoryBot.create(:ext_management_system))
ptr = FactoryBot.create(:miq_provision_request_template, :requester => admin, :src_vm_id => vm_template.id)
@st1.add_resource(ptr)
@st1.service_resources.first.update_attributes(:scaling_min => 4)
expect(ServiceTemplate.count).to eq(1)
expect(@st1.service_resources.first.scaling_min).to eq(4)
@st1.template_copy("new_template")
new_service_template = ServiceTemplate.find_by(:name => "new_template")
expect(ServiceTemplate.count).to eq(2)
expect(MiqProvisionRequestTemplate.count).to eq(2)
expect(new_service_template.guid).not_to eq(@st1.guid)
expect(new_service_template.display).to be(false)
expect(new_service_template.service_resources.first.scaling_min).to eq(4)
expect(@st1.service_resources.first.resource).not_to be(nil)
end
it "service template ansible tower with copyable resource" do
admin = FactoryBot.create(:user_admin)
vm_template = FactoryBot.create(:vm_openstack, :ext_management_system => FactoryBot.create(:ext_management_system))
ptr = FactoryBot.create(:miq_provision_request_template, :requester => admin, :src_vm_id => vm_template.id)
service_template_ansible_tower.add_resource(ptr)
expect(ServiceTemplate.count).to eq(2)
service_template_ansible_tower.template_copy("new_template_copy")
new_service_template = ServiceTemplate.find_by(:name => "new_template_copy")
expect(ServiceTemplate.count).to eq(3)
expect(MiqProvisionRequestTemplate.count).to eq(2)
expect(new_service_template.guid).not_to eq(service_template_ansible_tower.guid)
expect(new_service_template.display).to be(false)
expect(new_service_template.service_resources).not_to be(nil)
expect(service_template_ansible_tower.service_resources.first.resource).not_to be(nil)
end
it "service template orchestration with copyable resource" do
admin = FactoryBot.create(:user_admin)
vm_template = FactoryBot.create(:vm_openstack, :ext_management_system => FactoryBot.create(:ext_management_system))
ptr = FactoryBot.create(:miq_provision_request_template, :requester => admin, :src_vm_id => vm_template.id)
service_template_orchestration.add_resource(ptr)
expect(ServiceTemplate.count).to eq(2)
service_template_orchestration.template_copy("new_template")
new_service_template = ServiceTemplate.find_by(:name => "new_template")
expect(ServiceTemplate.count).to eq(3)
expect(MiqProvisionRequestTemplate.count).to eq(2)
expect(new_service_template.guid).not_to eq(service_template_orchestration.guid)
expect(new_service_template.display).to be(false)
expect(new_service_template.service_resources).not_to be(nil)
expect(service_template_orchestration.service_resources.first.resource).not_to be(nil)
end
end
context "without given name" do
it "without resource" do
expect(ServiceTemplate.count).to eq(1)
@st1.template_copy
new_service_template = ServiceTemplate.find_by("name ILIKE ?", "Copy of service%")
expect(ServiceTemplate.count).to eq(2)
expect(new_service_template.guid).not_to eq(@st1.guid)
expect(new_service_template.display).to be(false)
expect(new_service_template.service_resources.count).to eq(0)
expect(@st1.service_resources.count).to eq(0)
end
it "with non-copyable resource (configuration_script_base)" do
@st1.add_resource(FactoryBot.create(:configuration_script_base))
expect(ServiceTemplate.count).to eq(1)
@st1.template_copy
new_service_template = ServiceTemplate.find_by("name ILIKE ?", "Copy of service%")
expect(ServiceTemplate.count).to eq(2)
expect(new_service_template.service_resources.first.resource_id).to eq(@st1.service_resources.first.resource_id)
expect(ConfigurationScriptBase.count).to eq(1)
expect(new_service_template.display).to be(false)
expect(new_service_template.guid).not_to eq(@st1.guid)
end
it "with non-copyable resource (ext management system)" do
@st1.add_resource(FactoryBot.create(:ext_management_system))
expect(ServiceTemplate.count).to eq(1)
@st1.template_copy
new_service_template = ServiceTemplate.find_by("name ILIKE ?", "Copy of service%")
expect(ServiceTemplate.count).to eq(2)
expect(ServiceTemplate.where("name ILIKE ?", "Copy of service%").first.service_resources.first.resource_id).to eq(@st1.service_resources.first.resource_id)
expect(ExtManagementSystem.count).to eq(1)
expect(new_service_template.guid).not_to eq(@st1.guid)
expect(new_service_template.display).to be(false)
expect(new_service_template.service_resources).not_to be(nil)
expect(@st1.service_resources.first.resource).not_to be(nil)
end
it "with non-copyable resource (orchestration template)" do
@st1.add_resource(FactoryBot.create(:orchestration_template))
expect(ServiceTemplate.count).to eq(1)
@st1.template_copy
new_service_template = ServiceTemplate.find_by("name ILIKE ?", "Copy of service%")
expect(ServiceTemplate.count).to eq(2)
expect(ServiceTemplate.where("name ILIKE ?", "Copy of service%").first.service_resources.first.resource_id).to eq(@st1.service_resources.first.resource_id)
expect(OrchestrationTemplate.count).to eq(1)
expect(new_service_template.guid).not_to eq(@st1.guid)
expect(new_service_template.display).to be(false)
expect(new_service_template.service_resources).not_to be(nil)
expect(@st1.service_resources.first.resource).not_to be(nil)
end
it "with copyable resource" do
admin = FactoryBot.create(:user_admin)
vm_template = FactoryBot.create(:vm_openstack, :ext_management_system => FactoryBot.create(:ext_management_system))
ptr = FactoryBot.create(:miq_provision_request_template, :requester => admin, :src_vm_id => vm_template.id)
@st1.add_resource(ptr)
expect(ServiceTemplate.count).to eq(1)
@st1.template_copy
new_service_template = ServiceTemplate.find_by("name ILIKE ?", "Copy of service%")
expect(ServiceTemplate.count).to eq(2)
expect(MiqProvisionRequestTemplate.count).to eq(2)
expect(new_service_template.guid).not_to eq(@st1.guid)
expect(new_service_template.display).to be(false)
expect(new_service_template.service_resources).not_to be(nil)
expect(@st1.service_resources.first.resource).not_to be(nil)
end
end
end
context "#composite?" do
before do
@st1 = FactoryBot.create(:service_template)
end
it "with service_type of unknown" do
@st1.update_attributes(:service_type => 'user')
expect(@st1.composite?).to be_falsey
end
it "with service_type of atomic" do
@st1.update_attributes(:service_type => described_class::SERVICE_TYPE_ATOMIC)
expect(@st1.composite?).to be_falsey
end
it "with service_type of composite" do
@st1.update_attributes(:service_type => described_class::SERVICE_TYPE_COMPOSITE)
expect(@st1.composite?).to be_truthy
end
end
context "initiator" do
shared_examples_for 'initiator example' do |initiator, match|
it 'test initiator' do
svc_template = FactoryBot.create(:service_template, :name => 'Svc A')
options = {:dialog => {}}
options[:initiator] = initiator if initiator
svc_task = instance_double("service_task", :options => options, :get_user => service_user)
svc = svc_template.create_service(svc_task, nil)
expect(svc.initiator).to eq(match)
end
end
context "initiator specified" do
it_behaves_like 'initiator example', 'fred', 'fred'
end
context "initiator not specified" do
it_behaves_like 'initiator example', nil, 'user'
end
end
context "with multiple services" do
before do
@svc_a = FactoryBot.create(:service_template, :name => 'Svc A')
@svc_b = FactoryBot.create(:service_template, :name => 'Svc B')
@svc_c = FactoryBot.create(:service_template, :name => 'Svc C')
@svc_d = FactoryBot.create(:service_template, :name => 'Svc D')
@svc_e = FactoryBot.create(:service_template, :name => 'Svc E')
end
it "should return level 1 sub-services" do
add_and_save_service(@svc_a, @svc_b)
add_and_save_service(@svc_b, @svc_c)
add_and_save_service(@svc_a, @svc_c)
add_and_save_service(@svc_c, @svc_d)
sub_svc = @svc_a.children
expect(sub_svc).not_to include(@svc_a)
expect(sub_svc.size).to eq(2)
expect(sub_svc).to include(@svc_b)
expect(sub_svc).to include(@svc_c)
expect(sub_svc).not_to include(@svc_d)
end
it "should return all sub-services" do
add_and_save_service(@svc_a, @svc_b)
add_and_save_service(@svc_b, @svc_c)
add_and_save_service(@svc_a, @svc_c)
add_and_save_service(@svc_c, @svc_d)
sub_svc = @svc_a.descendants
expect(sub_svc.size).to eq(5)
expect(sub_svc).not_to include(@svc_a)
expect(sub_svc).to include(@svc_b)
expect(sub_svc).to include(@svc_c)
expect(sub_svc).to include(@svc_d)
sub_svc.uniq!
expect(sub_svc.size).to eq(3)
expect(sub_svc).not_to include(@svc_a)
expect(sub_svc).to include(@svc_b)
expect(sub_svc).to include(@svc_c)
expect(sub_svc).to include(@svc_d)
end
describe "#create_service" do
let(:service_task) do
FactoryBot.create(:service_template_provision_task,
:miq_request => service_template_request,
:options => {:service_resource_id => service_resource.id})
end
let(:service_template_request) { FactoryBot.create(:service_template_provision_request, :requester => user) }
let(:service_resource) do
FactoryBot.create(:service_resource,
:resource_type => 'MiqRequest',
:resource_id => service_template_request.id)
end
let(:user) { FactoryBot.create(:user) }
let(:parent_service) { FactoryBot.create(:service) }
it "create service sets parent service resource resource id" do
@svc_a.create_service(service_task, parent_service)
parent_service.reload
expect(parent_service.service_resources.first.resource).to eq(parent_service.children.first)
end
it "should add_resource! only if a parent_svc exists" do
sub_svc = instance_double("service_task", :options => {:dialog => {}}, :get_user => service_user)
parent_svc = instance_double("service_task", :options => {:dialog => {}}, :service_resources => instance_double('service_resource'))
expect(parent_svc).to receive(:add_resource!).once
@svc_a.create_service(sub_svc, parent_svc)
end
it "should not call add_resource! if no parent_svc exists" do
sub_svc = instance_double("service_task", :options => {:dialog => {}}, :get_user => service_user)
expect(sub_svc).to receive(:add_resource!).never
@svc_a.create_service(sub_svc)
end
end
it "should pass display attribute to created top level service" do
@svc_a.display = true
expect(@svc_a.create_service(double(:options => {:dialog => {}}, :get_user => service_user)).display).to eq(true)
end
it "should set created child service's display to false" do
@svc_a.display = true
allow(@svc_b).to receive(:add_resource!)
expect(@svc_a.create_service(double(:options => {:dialog => {}}, :get_user => service_user), @svc_b).display).to eq(false)
end
it "should set created service's display to false by default" do
expect(@svc_a.create_service(double(:options => {:dialog => {}}, :get_user => service_user)).display).to eq(false)
end
it "should return all parent services for a service" do
add_and_save_service(@svc_a, @svc_b)
add_and_save_service(@svc_a, @svc_c)
add_and_save_service(@svc_a, @svc_d)
add_and_save_service(@svc_b, @svc_c)
expect(@svc_a.parent_services).to be_empty
parents = @svc_b.parent_services
expect(parents.size).to eq(1)
expect(parents.first.name).to eq(@svc_a.name)
parents = @svc_c.parent_services
expect(parents.size).to eq(2)
parent_names = parents.collect(&:name)
expect(parent_names).to include(@svc_a.name)
expect(parent_names).to include(@svc_b.name)
end
it "should not allow service templates to be connected to itself" do
expect { add_and_save_service(@svc_a, @svc_a) }.to raise_error(MiqException::MiqServiceCircularReferenceError)
end
it "should not allow service templates to be connected in a circular reference" do
expect { add_and_save_service(@svc_a, @svc_b) }.not_to raise_error
expect { add_and_save_service(@svc_b, @svc_c) }.not_to raise_error
expect { add_and_save_service(@svc_a, @svc_c) }.not_to raise_error
expect { add_and_save_service(@svc_c, @svc_d) }.not_to raise_error
expect { add_and_save_service(@svc_a, @svc_e) }.not_to raise_error
expect { add_and_save_service(@svc_c, @svc_a) }.to raise_error(MiqException::MiqServiceCircularReferenceError)
expect { add_and_save_service(@svc_d, @svc_a) }.to raise_error(MiqException::MiqServiceCircularReferenceError)
expect { add_and_save_service(@svc_c, @svc_b) }.to raise_error(MiqException::MiqServiceCircularReferenceError)
end
it "should not allow deeply nested service templates to be connected in a circular reference" do
expect { add_and_save_service(@svc_a, @svc_b) }.not_to raise_error
expect { add_and_save_service(@svc_b, @svc_c) }.not_to raise_error
expect { add_and_save_service(@svc_d, @svc_e) }.not_to raise_error
expect { add_and_save_service(@svc_e, @svc_a) }.not_to raise_error
expect { add_and_save_service(@svc_c, @svc_d) }.to raise_error(MiqException::MiqServiceCircularReferenceError)
end
it "should not allow service template to connect to self" do
expect { @svc_a << @svc_a }.to raise_error(MiqException::MiqServiceCircularReferenceError)
end
it "should allow service template to connect to a service with the same id" do
svc = FactoryBot.create(:service)
svc.id = @svc_a.id
expect { svc << @svc_a }.to_not raise_error
end
it "should not delete a service that has a parent service" do
add_and_save_service(@svc_a, @svc_b)
add_and_save_service(@svc_b, @svc_c)
expect { @svc_b.destroy }.to raise_error(MiqException::MiqServiceError, /Cannot delete.*child of another service/)
expect { @svc_c.destroy }.to raise_error(MiqException::MiqServiceError, /Cannot delete.*child of another service/)
expect { @svc_a.destroy }.not_to raise_error
expect { @svc_b.destroy }.not_to raise_error
expect { @svc_c.destroy }.not_to raise_error
end
end
context "with a small env" do
before do
@zone1 = FactoryBot.create(:small_environment)
allow(MiqServer).to receive(:my_server).and_return(@zone1.miq_servers.first)
@st1 = FactoryBot.create(:service_template, :name => 'Service Template 1')
end
it "should create a valid service template" do
expect(@st1.guid).not_to be_empty
expect(@st1.service_resources.size).to eq(0)
expect(@st1.service_type).to eq(described_class::SERVICE_TYPE_ATOMIC)
end
it "should not set the owner for the service template" do
@user = nil
@test_service = FactoryBot.create(:service, :name => 'test service')
expect(@test_service.evm_owner).to be_nil
@st1.set_ownership(@test_service, @user)
expect(@test_service.evm_owner).to be_nil
end
it "should set the owner and group for the service template" do
@user = FactoryBot.create(:user_with_group)
@test_service = FactoryBot.create(:service, :name => 'test service')
expect(@test_service.evm_owner).to be_nil
@st1.set_ownership(@test_service, @user)
expect(@test_service.evm_owner.name).to eq(@user.name)
expect(@test_service.evm_owner.current_group).not_to be_nil
expect(@test_service.evm_owner.current_group.description).to eq(@user.current_group.description)
end
it "should create a composite service template" do
st2 = FactoryBot.create(:service_template, :name => 'Service Template 2')
@st1.add_resource(st2)
expect(@st1.service_resources.size).to eq(1)
expect(@st1.composite?).to be_truthy
expect(@st1.atomic?).to be_falsey
end
it "should create an atomic service template" do
vm = Vm.first
@st1.add_resource(vm)
expect(@st1.service_resources.size).to eq(1)
expect(@st1.atomic?).to be_truthy
expect(@st1.composite?).to be_falsey
end
context "with a VM Provision Request Template" do
before do
admin = FactoryBot.create(:user_admin)
vm_template = Vm.first
ptr = FactoryBot.create(:miq_provision_request_template, :requester => admin, :src_vm_id => vm_template.id)
@st1.add_resource(ptr)
end
it "should allow VM Provision Request Template as a resource" do
expect(@st1.service_resources.size).to eq(1)
expect(@st1.atomic?).to be_truthy
expect(@st1.composite?).to be_falsey
end
it "should delete the VM Provision Request Template when the service template is deleted" do
expect(ServiceTemplate.count).to eq(1)
expect(MiqProvisionRequestTemplate.count).to eq(1)
@st1.destroy
expect(ServiceTemplate.count).to eq(0)
expect(MiqProvisionRequestTemplate.count).to eq(0)
end
end
end
context 'validate template' do
before do
@st1 = FactoryBot.create(:service_template, :name => 'Service Template 1')
user = FactoryBot.create(:user, :name => 'Fred Flintstone', :userid => 'fred')
@vm_template = FactoryBot.create(:template_vmware, :ext_management_system => FactoryBot.create(:ems_vmware_with_authentication))
@ptr = FactoryBot.create(:miq_provision_request_template, :requester => user, :src_vm_id => @vm_template.id)
end
context 'atomic' do
before { @st1.add_resource(@ptr) }
it 'valid template' do
expect(@st1.template_valid?).to be_truthy
expect(@st1.template_valid_error_message).to be_nil
end
it 'orphaned template' do
allow_any_instance_of(VmOrTemplate).to receive(:orphaned?).and_return(true)
expect(@st1.template_valid?).to be_falsey
expect(@st1.template_valid_error_message).to include("Id <#{@vm_template.id}> is orphaned")
end
it 'archived template' do
allow_any_instance_of(VmOrTemplate).to receive(:archived?).and_return(true)
expect(@st1.template_valid?).to be_falsey
expect(@st1.template_valid_error_message).to include("Id <#{@vm_template.id}> is archived")
end
it 'not existing template' do
@ptr.update_attributes(:src_vm_id => 999)
expect(@st1.template_valid?).to be_falsey
expect(@st1.template_valid_error_message).to include("Unable to find VM with Id [999]")
end
it 'generic' do
@st1.remove_resource(@ptr)
expect(@st1.template_valid?).to be_truthy
expect(@st1.template_valid_error_message).to be_nil
end
it 'not existing request' do
@st1.save!
@ptr.destroy
expect(@st1.reload.template_valid?).to be_falsey
msg = "Missing Service Resource(s): #{@ptr.class.base_model.name}:#{@ptr.id}"
expect(@st1.template_valid_error_message).to include(msg)
end
end
context 'composite' do
before do
@st1.add_resource(@ptr)
@st2 = FactoryBot.create(:service_template, :name => 'Service Template 2')
@st2.add_resource(@st1)
end
it 'valid template' do
expect(@st2.template_valid?).to be_truthy
expect(@st2.template_valid_error_message).to be_nil
end
it 'orphaned template' do
allow_any_instance_of(VmOrTemplate).to receive(:orphaned?).and_return(true)
expect(@st2.template_valid?).to be_falsey
expect(@st2.template_valid_error_message).to include("Id <#{@vm_template.id}> is orphaned")
end
it 'archived template' do
allow_any_instance_of(VmOrTemplate).to receive(:archived?).and_return(true)
expect(@st2.template_valid?).to be_falsey
expect(@st2.template_valid_error_message).to include("Id <#{@vm_template.id}> is archived")
end
it 'not existing template' do
@ptr.update_attributes(:src_vm_id => 999)
expect(@st2.template_valid?).to be_falsey
expect(@st2.template_valid_error_message).to include("Unable to find VM with Id [999]")
end
end
end
describe 'generic_subtype' do
context 'when prov_type = generic ' do
it 'sets a default value' do
st = ServiceTemplate.create(:prov_type => 'generic')
expect(st.generic_subtype).to eq('custom')
end
it 'sets specified value' do
st = ServiceTemplate.create(:prov_type => 'generic', :generic_subtype => 'vm')
expect(st.generic_subtype).to eq('vm')
end
end
context 'when prov_type != generic' do
it 'does not set default value' do
st = ServiceTemplate.create(:prov_type => 'vmware')
expect(st.generic_subtype).to be_nil
end
end
end
describe "#provision_action" do
it "returns the provision action" do
provision_action = FactoryBot.create(:resource_action, :action => "Provision")
service_template = FactoryBot.create(:service_template, :resource_actions => [provision_action])
expect(service_template.provision_action).to eq(provision_action)
end
end
describe '#config_info' do
before do
@user = FactoryBot.create(:user_with_group)
@ra = FactoryBot.create(:resource_action, :action => 'Provision', :fqname => '/a/b/c')
end
it 'returns the config_info passed to #create_catalog_item' do
options = {
:name => 'foo',
:config_info => {
:provision => {
:fqname => @ra.fqname
},
:retirement => {
:fqname => @ra.fqname
}
}
}
template = ServiceTemplate.create_catalog_item(options, @user)
expect(template.config_info).to eq(options[:config_info])
end
it 'will build the config_info if not created through #create_catalog_item' do
dialog = FactoryBot.create(:dialog)
template = FactoryBot.create(:service_template)
request = FactoryBot.create(:service_template_provision_request,
:requester => @user,
:options => {:foo => 'bar', :baz => nil })
template.create_resource_actions(:provision => { :fqname => @ra.fqname, :dialog_id => dialog.id })
add_and_save_service(template, request)
template.reload
expected_config_info = {
:foo => 'bar',
:provision => {
:fqname => '/a/b/c',
:dialog_id => dialog.id
}
}
expect(template.config_info).to eq(expected_config_info)
end
end
describe '.class_from_request_data' do
it 'returns the correct generic type' do
template_class = ServiceTemplate.class_from_request_data('prov_type' => 'generic_ansible_tower')
expect(template_class).to eq(ServiceTemplateAnsibleTower)
end
it 'returns the correct non generic type' do
template_class = ServiceTemplate.class_from_request_data('prov_type' => 'amazon')
expect(template_class).to eq(ServiceTemplate)
end
end
let(:user) { FactoryBot.create(:user_with_group) }
let(:ra1) { FactoryBot.create(:resource_action, :action => 'Provision') }
let(:ra2) { FactoryBot.create(:resource_action, :action => 'Retirement') }
let(:ems) { FactoryBot.create(:ems_amazon) }
let(:content) do
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAABGdBTUEAALGP"\
"C/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3Cc"\
"ulE8AAAACXBIWXMAAAsTAAALEwEAmpwYAAABWWlUWHRYTUw6Y29tLmFkb2Jl"\
"LnhtcAAAAAAAPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIg"\
"eDp4bXB0az0iWE1QIENvcmUgNS40LjAiPgogICA8cmRmOlJERiB4bWxuczpy"\
"ZGY9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvMDIvMjItcmRmLXN5bnRheC1u"\
"cyMiPgogICAgICA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIgogICAg"\
"ICAgICAgICB4bWxuczp0aWZmPSJodHRwOi8vbnMuYWRvYmUuY29tL3RpZmYv"\
"MS4wLyI+CiAgICAgICAgIDx0aWZmOk9yaWVudGF0aW9uPjE8L3RpZmY6T3Jp"\
"ZW50YXRpb24+CiAgICAgIDwvcmRmOkRlc2NyaXB0aW9uPgogICA8L3JkZjpS"\
"REY+CjwveDp4bXBtZXRhPgpMwidZAAAADUlEQVQIHWNgYGCwBQAAQgA+3N0+"\
"xQAAAABJRU5ErkJggg=="
end
let(:vm) { FactoryBot.create(:vm_amazon, :ext_management_system => ems) }
let(:flavor) { FactoryBot.create(:flavor_amazon) }
let(:request_dialog) { FactoryBot.create(:miq_dialog_provision) }
let(:service_dialog) { FactoryBot.create(:dialog) }
let(:catalog_item_options) do
{
:name => 'Atomic Service Template',
:service_type => 'atomic',
:prov_type => 'amazon',
:display => 'false',
:picture => { :content => content, :extension => 'jpg' },
:description => 'a description',
:config_info => {
:miq_request_dialog_name => request_dialog.name,
:placement_auto => [true, 1],
:number_of_vms => [1, '1'],
:src_vm_id => [vm.id, vm.name],
:vm_name => vm.name,
:schedule_type => ['immediately', 'Immediately on Approval'],
:instance_type => [flavor.id, flavor.name],
:src_ems_id => [ems.id, ems.name],
:provision => {
:fqname => ra1.fqname,
:dialog_id => service_dialog.id
},
:retirement => {
:fqname => ra2.fqname,
:dialog_id => service_dialog.id
}
}
}
end
describe '.create_catalog_item' do
it 'creates and returns a catalog item' do
service_template = ServiceTemplate.create_catalog_item(catalog_item_options, user)
expect(service_template.name).to eq('Atomic Service Template')
expect(service_template.service_resources.count).to eq(1)
expect(Base64.strict_encode64(service_template.picture.content)).to start_with('aVZCT1J3MEtHZ29BQUFBTlNVaEVVZ0FBQUFFQUFBQUJDQVlBQ')
expect(service_template.service_resources.first.resource_type).to eq('MiqRequest')
expect(service_template.dialogs.first).to eq(service_dialog)
expect(service_template.resource_actions.pluck(:action)).to include('Provision', 'Retirement')
expect(service_template.resource_actions.pluck(:ae_attributes)).to include({:service_action=>"Provision"}, {:service_action=>"Retirement"})