From 74b2cf8b26ae7dfcc8a0d849de23f9a518976cc2 Mon Sep 17 00:00:00 2001 From: Hilda Stastna Date: Mon, 7 Jan 2019 13:52:04 +0100 Subject: [PATCH 1/3] Fix enabling Power operations for a template Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1634713 --- app/controllers/application_controller/ci_processing.rb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/app/controllers/application_controller/ci_processing.rb b/app/controllers/application_controller/ci_processing.rb index 98dffd7656d..e6f7dfade21 100644 --- a/app/controllers/application_controller/ci_processing.rb +++ b/app/controllers/application_controller/ci_processing.rb @@ -636,10 +636,7 @@ def generic_button_operation(action, action_name, operation, options = {}) # - false otherwise def testable_action(action) controller = params[:controller] - vm_infra_untestable_actions = %w( - reboot_guest stop start check_compliance_queue destroy - refresh_ems vm_miq_request_new suspend reset shutdown_guest - ) + vm_infra_untestable_actions = %w(check_compliance_queue destroy refresh_ems vm_miq_request_new) ems_cluster_untestable_actions = %w(scan) if controller == "vm_infra" return vm_infra_untestable_actions.exclude?(action) From 2f3d3f352c130d16690af2bb1c0d4179b8ee43ec Mon Sep 17 00:00:00 2001 From: Hilda Stastna Date: Tue, 22 Jan 2019 15:32:00 +0100 Subject: [PATCH 2/3] Add specs for power operations regarding testable_action method --- .../ci_processing_spec.rb | 42 +++++++++++++------ 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/spec/controllers/application_controller/ci_processing_spec.rb b/spec/controllers/application_controller/ci_processing_spec.rb index a2048a73636..a7f45ee5583 100644 --- a/spec/controllers/application_controller/ci_processing_spec.rb +++ b/spec/controllers/application_controller/ci_processing_spec.rb @@ -34,7 +34,7 @@ end end - describe "action_to_feature" do + describe "#action_to_feature" do let(:record) { FactoryBot.create(:vm_redhat) } context 'the UI action is also a queryable feature' do @@ -681,7 +681,7 @@ end end - context "#discover" do + describe "#discover" do it "checks that keys in @to remain set if there is an error after submit is pressed" do from_first = "1" from_second = "1" @@ -720,7 +720,7 @@ end end - context "#process_elements" do + describe "#process_elements" do it "shows passed in display name in flash message" do pxe = FactoryBot.create(:pxe_server) controller.send(:process_elements, [pxe.id], PxeServer, 'synchronize_advertised_images_queue', 'Refresh Relationships') @@ -735,7 +735,7 @@ end end - context "#identify_record" do + describe "#identify_record" do it "Verify flash error message when passed in ID no longer exists in database" do record = controller.send(:identify_record, "1", ExtManagementSystem) expect(record).to be_nil @@ -749,7 +749,7 @@ end end - context "#get_record" do + describe "#get_record" do it "use passed in db to set class for identify_record call" do host = FactoryBot.create(:host) controller.instance_variable_set(:@_params, :id => host.id) @@ -800,7 +800,7 @@ let!(:server) { EvmSpecHelper.local_miq_server(:zone => zone) } let(:zone) { FactoryBot.create(:zone) } - context "#show_association" do + describe "#show_association" do before do stub_user(:features => :all) EvmSpecHelper.create_guid_miq_server_zone @@ -846,7 +846,7 @@ end end - context "#process_objects" do + describe "#process_objects" do it "returns array of object ids " do vm1 = FactoryBot.create(:vm_vmware) vm2 = FactoryBot.create(:vm_vmware) @@ -858,7 +858,7 @@ end end - context "#process_hosts" do + describe "#process_hosts" do before do @host1 = FactoryBot.create(:host) @host2 = FactoryBot.create(:host) @@ -881,7 +881,7 @@ end end - context "#generic_button_operation" do + describe "#generic_button_operation" do before do allow(subject).to receive(:vm_button_action).and_return(subject.method(:process_objects)) allow(controller).to receive(:render) @@ -916,7 +916,7 @@ end describe ServiceController do - context "#vm_button_operation" do + describe "#vm_button_operation" do let(:user) { FactoryBot.create(:user_admin) } before do @@ -947,7 +947,7 @@ end describe MiqTemplateController do - context "#vm_button_operation" do + describe "#vm_button_operation" do before do _guid, @miq_server, @zone = EvmSpecHelper.remote_guid_miq_server_zone allow(MiqServer).to receive(:my_zone).and_return("default") @@ -973,7 +973,7 @@ end describe VmOrTemplateController do - context "#vm_button_operation" do + describe "#vm_button_operation" do let(:user) { FactoryBot.create(:user_admin) } before do @@ -1029,7 +1029,7 @@ end describe OrchestrationStackController do - context "#orchestration_stack_delete" do + describe "#orchestration_stack_delete" do let(:orchestration_stack) { FactoryBot.create(:orchestration_stack_cloud) } let(:orchestration_stack_deleted) { FactoryBot.create(:orchestration_stack_cloud) } @@ -1082,3 +1082,19 @@ end end end + +describe VmInfraController do + describe '#testable_action' do + before do + controller.instance_variable_set(:@_params, :controller => 'vm_infra') + end + + context 'power operations and vm infra controller' do + %w(reboot_guest reset shutdown_guest start stop suspend).each do |op| + it "returns true for #{op} operation on a VM" do + expect(controller.send(:testable_action, op)).to be(true) + end + end + end + end +end From 0643df55a22c3e3c099d65c1deb6d4bddc8c7589 Mon Sep 17 00:00:00 2001 From: Hilda Stastna Date: Wed, 23 Jan 2019 13:35:59 +0100 Subject: [PATCH 3/3] Fix specs for Power operations on VMs, add specs for Templates --- spec/controllers/vm_infra_controller_spec.rb | 79 ++++++++++++-------- 1 file changed, 49 insertions(+), 30 deletions(-) diff --git a/spec/controllers/vm_infra_controller_spec.rb b/spec/controllers/vm_infra_controller_spec.rb index 54dc2fc65ab..d26ad8d2ca0 100644 --- a/spec/controllers/vm_infra_controller_spec.rb +++ b/spec/controllers/vm_infra_controller_spec.rb @@ -2,6 +2,7 @@ let(:host_1x1) { FactoryBot.create(:host_vmware_esx, :hardware => FactoryBot.create(:hardware, :cpu1x1, :ram1GB)) } let(:host_2x2) { FactoryBot.create(:host_vmware_esx, :hardware => FactoryBot.create(:hardware, :cpu2x2, :ram1GB)) } let(:vm_vmware) { FactoryBot.create(:vm_vmware) } + before do stub_user(:features => :all) @@ -434,46 +435,64 @@ end end - it 'can Shutdown Guest' do - post :x_button, :params => {:pressed => 'vm_guest_shutdown', :id => vm_vmware.id} - expect(response.status).to eq(200) + context 'power operations' do + let(:ems) { FactoryBot.create(:ems_vmware, :hostname => 'foo.com') } - expect(response.body).to include('Shutdown Guest initiated for 1 VM and Instance from the %{product} Database' % {:product => Vmdb::Appliance.PRODUCT_NAME}) - end + context 'operations on VMs' do + let(:vm) { FactoryBot.create(:vm_vmware, :host => host_1x1, :raw_power_state => 'poweredOn', :ext_management_system => ems) } - it 'can Restart Guest' do - post :x_button, :params => {:pressed => 'vm_guest_restart', :id => vm_vmware.id} - expect(response.status).to eq(200) - - expect(response.body).to include('Restart Guest initiated for 1 VM and Instance from the %{product} Database' % {:product => Vmdb::Appliance.PRODUCT_NAME}) - end + it 'can Shutdown Guest' do + post :x_button, :params => {:pressed => 'vm_guest_shutdown', :id => vm.id} + expect(response.status).to eq(200) + expect(response.body).to include('Shutdown Guest initiated for 1 VM and Instance from the %{product} Database' % {:product => Vmdb::Appliance.PRODUCT_NAME}) + end - it 'can Power On VM' do - post :x_button, :params => {:pressed => 'vm_start', :id => vm_vmware.id} - expect(response.status).to eq(200) + it 'can Restart Guest' do + post :x_button, :params => {:pressed => 'vm_guest_restart', :id => vm.id} + expect(response.status).to eq(200) + expect(response.body).to include('Restart Guest initiated for 1 VM and Instance from the %{product} Database' % {:product => Vmdb::Appliance.PRODUCT_NAME}) + end - expect(response.body).to include('Start initiated for 1 VM and Instance from the %{product} Database' % {:product => Vmdb::Appliance.PRODUCT_NAME}) - end + context 'powering on VM' do + let(:vm) { FactoryBot.create(:vm_vmware, :host => host_1x1, :raw_power_state => 'poweredOff', :ext_management_system => ems) } - it 'can Power Off VM' do - post :x_button, :params => {:pressed => 'vm_stop', :id => vm_vmware.id} - expect(response.status).to eq(200) + it 'can Power On VM' do + post :x_button, :params => {:pressed => 'vm_start', :id => vm.id} + expect(response.status).to eq(200) + expect(response.body).to include('Start initiated for 1 VM and Instance from the %{product} Database' % {:product => Vmdb::Appliance.PRODUCT_NAME}) + end + end - expect(response.body).to include('Stop initiated for 1 VM and Instance from the %{product} Database' % {:product => Vmdb::Appliance.PRODUCT_NAME}) - end + it 'can Power Off VM' do + post :x_button, :params => {:pressed => 'vm_stop', :id => vm.id} + expect(response.status).to eq(200) + expect(response.body).to include('Stop initiated for 1 VM and Instance from the %{product} Database' % {:product => Vmdb::Appliance.PRODUCT_NAME}) + end - it 'can Suspend VM' do - post :x_button, :params => {:pressed => 'vm_suspend', :id => vm_vmware.id} - expect(response.status).to eq(200) + it 'can Suspend VM' do + post :x_button, :params => {:pressed => 'vm_suspend', :id => vm.id} + expect(response.status).to eq(200) + expect(response.body).to include('Suspend initiated for 1 VM and Instance from the %{product} Database' % {:product => Vmdb::Appliance.PRODUCT_NAME}) + end - expect(response.body).to include('Suspend initiated for 1 VM and Instance from the %{product} Database' % {:product => Vmdb::Appliance.PRODUCT_NAME}) - end + it 'can Reset VM' do + post :x_button, :params => {:pressed => 'vm_reset', :id => vm.id} + expect(response.status).to eq(200) + expect(response.body).to include('Reset initiated for 1 VM and Instance from the %{product} Database' % {:product => Vmdb::Appliance.PRODUCT_NAME}) + end + end - it 'can Reset VM' do - post :x_button, :params => {:pressed => 'vm_reset', :id => vm_vmware.id} - expect(response.status).to eq(200) + context 'operations on Templates' do + let(:template) { FactoryBot.create(:template, :ext_management_system => ems) } - expect(response.body).to include('Reset initiated for 1 VM and Instance from the %{product} Database' % {:product => Vmdb::Appliance.PRODUCT_NAME}) + %w(vm_guest_shutdown vm_guest_restart vm_start vm_stop vm_suspend vm_reset).zip(['Shutdown Guest', 'Restart Guest', 'Start', 'Stop', 'Suspend', 'Reset']).each do |action, action_name| + it "cannot #{action_name}" do + post :x_button, :params => {:pressed => action, :id => template.id} + expect(response.status).to eq(200) + expect(response.body).to include('%{action_name} action does not apply to selected items' % {:action_name => action_name}) + end + end + end end it 'can run Utilization' do