Skip to content

Commit b208c72

Browse files
Merge pull request #6426 from hstastna/Check_Compliance_Last_Known_Config_VM
Enable Check Compliance toolbar button for nested list of VMs and make it work
2 parents 7b8d0c6 + eb1c87d commit b208c72

19 files changed

+488
-59
lines changed

app/controllers/application_controller/ci_processing.rb

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,18 @@ def retirevms_now
388388

389389
def check_compliance_vms
390390
assert_privileges(params[:pressed])
391+
392+
records = find_records_with_rbac(record_class, checked_or_params)
393+
# Check each record if there is any compliance policy assigned to it
394+
if records.any? { |record| !record.has_compliance_policies? }
395+
javascript_flash(
396+
:text => _('No Compliance Policies assigned to one or more of the selected items'),
397+
:severity => :error,
398+
:scroll_top => true
399+
)
400+
return
401+
end
402+
391403
generic_button_operation('check_compliance_queue', _('Check Compliance'), vm_button_action)
392404
end
393405
alias image_check_compliance check_compliance_vms
@@ -616,15 +628,21 @@ def generic_button_operation(action, action_name, operation, options = {})
616628
def testable_action(action)
617629
controller = params[:controller]
618630
vm_infra_untestable_actions = %w[check_compliance_queue destroy refresh_ems vm_miq_request_new]
619-
ems_cluster_untestable_actions = %w[scan]
620-
if controller == "vm_infra"
621-
return vm_infra_untestable_actions.exclude?(action)
622-
end
623-
if controller == "ems_cluster" || @display == 'ems_clusters'
624-
return ems_cluster_untestable_actions.exclude?(action)
631+
ems_cluster_untestable_actions = %w[check_compliance_queue scan]
632+
other_untestable_actions = %w[check_compliance_queue]
633+
634+
return false if @display == 'ems_clusters' && action == 'scan'
635+
636+
case controller
637+
when 'ems_cluster'
638+
ems_cluster_untestable_actions.exclude?(action)
639+
when 'auth_key_pair_cloud', 'availability_zone', 'cloud_network', 'cloud_tenant', 'ems_cloud', 'ems_infra', 'flavor', 'host', 'host_aggregate', 'resource_pool', 'storage', 'vm_cloud'
640+
other_untestable_actions.exclude?(action)
641+
when 'vm_infra'
642+
vm_infra_untestable_actions.exclude?(action)
643+
else
644+
true
625645
end
626-
627-
true
628646
end
629647

630648
# Maps UI actions to queryable feature in case it is not possible

app/controllers/ems_cluster_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def button
8989
elsif @refresh_div == "main_div" && @lastaction == "show_list"
9090
replace_gtl_main_div
9191
else
92-
render_flash
92+
render_flash unless performed?
9393
end
9494
end
9595

app/controllers/host_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ def button
349349
elsif @refresh_div == "main_div" && @lastaction == "show_list"
350350
replace_gtl_main_div
351351
else
352-
render_flash
352+
render_flash unless performed?
353353
end
354354
end
355355

app/controllers/resource_pool_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def button
5858
elsif @refresh_div == "main_div" && @lastaction == "show_list"
5959
replace_gtl_main_div
6060
else
61-
render_flash
61+
render_flash unless performed?
6262
end
6363
end
6464

app/helpers/application_helper/toolbar/vm_clouds_center.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class ApplicationHelper::Toolbar::VmCloudsCenter < ApplicationHelper::Toolbar::B
122122
:url_parms => "main_div",
123123
:send_checked => true,
124124
:confirm => N_("Initiate Check Compliance of the last known configuration for the selected items?"),
125-
:enabled => false,
125+
:enabled => true,
126126
:onwhen => "1+",
127127
:klass => ApplicationHelper::Button::InstanceCheckCompare),
128128
]

app/helpers/application_helper/toolbar/vm_infras_center.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ class ApplicationHelper::Toolbar::VmInfrasCenter < ApplicationHelper::Toolbar::B
151151
:url_parms => "main_div",
152152
:send_checked => true,
153153
:confirm => N_("Initiate Check Compliance of the last known configuration for the selected items?"),
154-
:enabled => false,
154+
:enabled => true,
155155
:onwhen => "1+",
156156
:klass => ApplicationHelper::Button::VmCheckCompliance),
157157
]

spec/controllers/auth_key_pair_cloud_controller_spec.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,38 @@
9999
end
100100
end
101101
end
102+
103+
describe '#button' do
104+
context 'Check Compliance of Last Known Configuration on Instances' do
105+
let(:vm_instance) { FactoryBot.create(:vm_or_template) }
106+
107+
before do
108+
allow(controller).to receive(:assert_privileges)
109+
allow(controller).to receive(:drop_breadcrumb)
110+
allow(controller).to receive(:performed?).and_return(true)
111+
allow(controller).to receive(:render)
112+
controller.instance_variable_set(:@display, 'instances')
113+
controller.params = {:miq_grid_checks => vm_instance.id.to_s, :pressed => 'instance_check_compliance', :id => kp.id.to_s, :controller => 'auth_key_pair_cloud'}
114+
end
115+
116+
it 'does not initiate Check Compliance because of missing Compliance policies' do
117+
controller.send(:button)
118+
expect(controller.instance_variable_get(:@flash_array)).to eq([{:message => 'No Compliance Policies assigned to one or more of the selected items', :level => :error}])
119+
end
120+
121+
context 'VM Compliance policy set' do
122+
let(:policy) { FactoryBot.create(:miq_policy, :mode => 'compliance', :towhat => 'Vm', :active => true) }
123+
124+
before do
125+
vm_instance.add_policy(policy)
126+
allow(MiqPolicy).to receive(:policy_for_event?).and_return(true)
127+
end
128+
129+
it 'initiates Check Compliance action' do
130+
controller.send(:button)
131+
expect(controller.instance_variable_get(:@flash_array)).to eq([{:message => 'Check Compliance initiated for 1 VM and Instance from the ManageIQ Database', :level => :success}])
132+
end
133+
end
134+
end
135+
end
102136
end

spec/controllers/availability_zone_controller_spec.rb

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,64 @@
11
describe AvailabilityZoneController do
22
describe "#show" do
3+
let(:params) { {:id => zone.id} }
4+
let(:zone) { FactoryBot.create(:availability_zone) }
5+
36
before do
47
EvmSpecHelper.create_guid_miq_server_zone
5-
@zone = FactoryBot.create(:availability_zone)
68
login_as FactoryBot.create(:user_admin)
79
end
810

9-
subject do
10-
get :show, :params => {:id => @zone.id}
11+
subject { get :show, :params => params }
12+
13+
render_views
14+
15+
it 'renders listnav partial' do
16+
is_expected.to have_http_status 200
17+
is_expected.to render_template(:partial => "layouts/listnav/_availability_zone")
1118
end
1219

13-
context "render listnav partial" do
14-
render_views
20+
context 'display timeline' do
21+
let(:params) { {:id => zone.id, :display => 'timeline'} }
1522

16-
it do
23+
it 'renders listnav partial' do
1724
is_expected.to have_http_status 200
1825
is_expected.to render_template(:partial => "layouts/listnav/_availability_zone")
1926
end
2027
end
2128
end
2229

23-
describe "#show display=timeline" do
24-
before do
25-
EvmSpecHelper.create_guid_miq_server_zone
26-
@zone = FactoryBot.create(:availability_zone)
27-
login_as FactoryBot.create(:user_admin)
28-
end
30+
describe '#button' do
31+
context 'Check Compliance of Last Known Configuration on Instances' do
32+
let(:vm_instance) { FactoryBot.create(:vm_or_template) }
33+
let(:av_zone) { FactoryBot.create(:availability_zone) }
2934

30-
subject do
31-
get :show, :params => {:id => @zone.id, :display => 'timeline'}
32-
end
35+
before do
36+
allow(controller).to receive(:assert_privileges)
37+
allow(controller).to receive(:drop_breadcrumb)
38+
allow(controller).to receive(:performed?).and_return(true)
39+
allow(controller).to receive(:render)
40+
controller.instance_variable_set(:@display, 'instances')
41+
controller.params = {:miq_grid_checks => vm_instance.id.to_s, :pressed => 'instance_check_compliance', :id => av_zone.id.to_s, :controller => 'availability_zone'}
42+
end
3343

34-
context "render listnav partial" do
35-
render_views
44+
it 'does not initiate Check Compliance because of missing Compliance policies' do
45+
controller.send(:button)
46+
expect(controller.instance_variable_get(:@flash_array)).to eq([{:message => 'No Compliance Policies assigned to one or more of the selected items', :level => :error}])
47+
end
3648

37-
it do
38-
is_expected.to have_http_status 200
39-
is_expected.to render_template(:partial => "layouts/listnav/_availability_zone")
49+
context 'VM Compliance policy set' do
50+
let(:policy) { FactoryBot.create(:miq_policy, :mode => 'compliance', :towhat => 'Vm', :active => true) }
51+
52+
before do
53+
EvmSpecHelper.create_guid_miq_server_zone
54+
vm_instance.add_policy(policy)
55+
allow(MiqPolicy).to receive(:policy_for_event?).and_return(true)
56+
end
57+
58+
it 'initiates Check Compliance action' do
59+
controller.send(:button)
60+
expect(controller.instance_variable_get(:@flash_array)).to eq([{:message => 'Check Compliance initiated for 1 VM and Instance from the ManageIQ Database', :level => :success}])
61+
end
4062
end
4163
end
4264
end

spec/controllers/cloud_tenant_controller_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,38 @@
4040
controller.send(:button)
4141
end
4242
end
43+
44+
context 'Check Compliance of Last Known Configuration on Instances' do
45+
let(:vm_instance) { FactoryBot.create(:vm_or_template) }
46+
47+
before do
48+
allow(controller).to receive(:assert_privileges)
49+
allow(controller).to receive(:drop_breadcrumb)
50+
allow(controller).to receive(:performed?).and_return(true)
51+
allow(controller).to receive(:render)
52+
controller.instance_variable_set(:@display, 'instances')
53+
controller.params = {:miq_grid_checks => vm_instance.id.to_s, :pressed => 'instance_check_compliance', :id => tenant.id.to_s, :controller => 'cloud_tenant'}
54+
end
55+
56+
it 'does not initiate Check Compliance because of missing Compliance policies' do
57+
controller.send(:button)
58+
expect(controller.instance_variable_get(:@flash_array)).to eq([{:message => 'No Compliance Policies assigned to one or more of the selected items', :level => :error}])
59+
end
60+
61+
context 'VM Compliance policy set' do
62+
let(:policy) { FactoryBot.create(:miq_policy, :mode => 'compliance', :towhat => 'Vm', :active => true) }
63+
64+
before do
65+
vm_instance.add_policy(policy)
66+
allow(MiqPolicy).to receive(:policy_for_event?).and_return(true)
67+
end
68+
69+
it 'initiates Check Compliance action' do
70+
controller.send(:button)
71+
expect(controller.instance_variable_get(:@flash_array)).to eq([{:message => 'Check Compliance initiated for 1 VM and Instance from the ManageIQ Database', :level => :success}])
72+
end
73+
end
74+
end
4375
end
4476

4577
describe "#tags_edit" do

spec/controllers/ems_cloud_controller_spec.rb

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
describe EmsCloudController do
22
let!(:server) { EvmSpecHelper.local_miq_server(:zone => zone) }
33
let(:zone) { FactoryBot.build(:zone) }
4+
45
describe "#create" do
56
before do
67
allow(controller).to receive(:check_privileges).and_return(true)
@@ -264,7 +265,7 @@
264265
end
265266
end
266267

267-
context "#build_credentials only contains credentials that it supports and has a username for in params" do
268+
describe "#build_credentials only contains credentials that it supports and has a username for in params" do
268269
let(:mocked_ems) { double(ManageIQ::Providers::Openstack::CloudManager) }
269270
let(:default_creds) { {:userid => "default_userid", :password => "default_password"} }
270271
let(:amqp_creds) { {:userid => "amqp_userid", :password => "amqp_password"} }
@@ -294,7 +295,7 @@
294295
end
295296
end
296297

297-
context "#update_ems_button_validate" do
298+
describe "#update_ems_button_validate" do
298299
let(:mocked_ems) { FactoryBot.create(:ems_openstack) }
299300

300301
it "calls authentication_check with save = false" do
@@ -308,8 +309,9 @@
308309
end
309310
end
310311

311-
context "#create_ems_button_validate" do
312+
describe "#create_ems_button_validate" do
312313
let(:mocked_params) { {:controller => mocked_class_controller, :cred_type => "default"} }
314+
313315
before do
314316
allow(controller).to receive(:render)
315317
controller.params = mocked_params
@@ -668,9 +670,8 @@
668670

669671
describe "#sync_users" do
670672
let(:ems) { FactoryBot.create(:ems_openstack_with_authentication) }
671-
before do
672-
stub_user(:features => :all)
673-
end
673+
674+
before { stub_user(:features => :all) }
674675

675676
it "redirects when request is successful" do
676677
expect(controller).to receive(:find_record_with_rbac).and_return(ems)
@@ -717,6 +718,7 @@ def verify_password_and_confirm(password, verify)
717718

718719
context "'Set Default' button rendering in listnav" do
719720
render_views
721+
720722
before do
721723
stub_user(:features => :all)
722724
EvmSpecHelper.create_guid_miq_server_zone
@@ -783,4 +785,38 @@ def verify_password_and_confirm(password, verify)
783785

784786
include_examples 'hiding tenant column for non admin user', :name => "Name", :emstype_description => "Type"
785787
end
788+
789+
describe '#button' do
790+
context 'Check Compliance of Last Known Configuration on Instances' do
791+
let(:vm_instance) { FactoryBot.create(:vm_or_template) }
792+
let(:ems) { FactoryBot.create(:ems_openstack) }
793+
794+
before do
795+
allow(controller).to receive(:assert_privileges)
796+
allow(controller).to receive(:performed?).and_return(true)
797+
allow(controller).to receive(:render)
798+
controller.instance_variable_set(:@display, 'instances')
799+
controller.params = {:miq_grid_checks => vm_instance.id.to_s, :pressed => 'instance_check_compliance', :id => ems.id.to_s, :controller => 'ems_cloud'}
800+
end
801+
802+
it 'does not initiate Check Compliance because of missing Compliance policies' do
803+
controller.send(:button)
804+
expect(controller.instance_variable_get(:@flash_array)).to eq([{:message => 'No Compliance Policies assigned to one or more of the selected items', :level => :error}])
805+
end
806+
807+
context 'VM Compliance policy set' do
808+
let(:policy) { FactoryBot.create(:miq_policy, :mode => 'compliance', :towhat => 'Vm', :active => true) }
809+
810+
before do
811+
vm_instance.add_policy(policy)
812+
allow(MiqPolicy).to receive(:policy_for_event?).and_return(true)
813+
end
814+
815+
it 'initiates Check Compliance action' do
816+
controller.send(:button)
817+
expect(controller.instance_variable_get(:@flash_array)).to eq([{:message => 'Check Compliance initiated for 1 VM and Instance from the ManageIQ Database', :level => :success}])
818+
end
819+
end
820+
end
821+
end
786822
end

0 commit comments

Comments
 (0)