From dbd3b0d130bcf5f70e253b894c552ca83780ad59 Mon Sep 17 00:00:00 2001 From: Adam Grare Date: Tue, 19 May 2020 13:56:41 -0400 Subject: [PATCH 1/3] Fix constantizing CloudVolume for disable? check In some environments Module.const_defined?("Namespace::CloudVolume") returns ::CloudVolume so this check doesn't work. Module.const_defined?("ManageIQ::Providers::Amazon::CloudManager::CloudVolume") returns true then invoking #supports?() on that class raises a NameError. String#safe_constantize is a more reliable way of determining if a class exists. --- .../application_helper/button/cloud_volume_backup_create.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/helpers/application_helper/button/cloud_volume_backup_create.rb b/app/helpers/application_helper/button/cloud_volume_backup_create.rb index 2115c8401b0..bcdafb147f9 100644 --- a/app/helpers/application_helper/button/cloud_volume_backup_create.rb +++ b/app/helpers/application_helper/button/cloud_volume_backup_create.rb @@ -1,8 +1,7 @@ class ApplicationHelper::Button::CloudVolumeBackupCreate < ApplicationHelper::Button::ButtonNewDiscover def disabled? ManageIQ::Providers::CloudManager.none? do |ems| - Module.const_defined?("#{ems.class}::CloudVolume") && - ems.class::CloudVolume.supports?(:backup_create) + "#{ems.class}::CloudVolume".safe_constantize&.supports?(:backup_create) end end end From 5023bf5ef9c5f3a39064970fe8e979b93272fc6d Mon Sep 17 00:00:00 2001 From: Adam Grare Date: Tue, 19 May 2020 14:29:02 -0400 Subject: [PATCH 2/3] Add spec test for CloudVolumeBackupCreate#disabled? --- .../cloud_volume_backup_create_spec.rb | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 spec/helpers/application_helper/buttons/cloud_volume_backup_create_spec.rb diff --git a/spec/helpers/application_helper/buttons/cloud_volume_backup_create_spec.rb b/spec/helpers/application_helper/buttons/cloud_volume_backup_create_spec.rb new file mode 100644 index 00000000000..2e65a340790 --- /dev/null +++ b/spec/helpers/application_helper/buttons/cloud_volume_backup_create_spec.rb @@ -0,0 +1,28 @@ +describe ApplicationHelper::Button::CloudVolumeBackupCreate do + describe "#disabled?" do + let(:view_context) { setup_view_context_with_sandbox({}) } + let(:button) { described_class.new(view_context, {}, {}, {}) } + + context "with no providers" do + it "the button is disabled" do + expect(button.disabled?).to be true + end + end + + context "with an OpenStack CloudManager supporting backup_create" do + before { FactoryBot.create(:ems_openstack) } + + it "the button is enabled" do + expect(button.disabled?).to be false + end + end + + context "with an Amazon CloudManager not supporting backup_create" do + before { FactoryBot.create(:ems_amazon) } + + it "the button is disabled" do + expect(button.disabled?).to be true + end + end + end +end From b74c99cb9ec5f56427dbdd37a492118f4d788628 Mon Sep 17 00:00:00 2001 From: Adam Grare Date: Tue, 19 May 2020 14:34:55 -0400 Subject: [PATCH 3/3] Fix cloud_volume_backup_restore --- .../button/cloud_volume_backup_restore.rb | 3 +- .../cloud_volume_backup_restore_spec.rb | 28 +++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 spec/helpers/application_helper/buttons/cloud_volume_backup_restore_spec.rb diff --git a/app/helpers/application_helper/button/cloud_volume_backup_restore.rb b/app/helpers/application_helper/button/cloud_volume_backup_restore.rb index 676481a081d..14ddc01fb74 100644 --- a/app/helpers/application_helper/button/cloud_volume_backup_restore.rb +++ b/app/helpers/application_helper/button/cloud_volume_backup_restore.rb @@ -1,8 +1,7 @@ class ApplicationHelper::Button::CloudVolumeBackupRestore < ApplicationHelper::Button::ButtonNewDiscover def disabled? ManageIQ::Providers::CloudManager.none? do |ems| - Module.const_defined?("#{ems.class}::CloudVolume") && - ems.class::CloudVolume.supports?(:backup_restore) + "#{ems.class}::CloudVolume".safe_constantize&.supports?(:backup_restore) end end end diff --git a/spec/helpers/application_helper/buttons/cloud_volume_backup_restore_spec.rb b/spec/helpers/application_helper/buttons/cloud_volume_backup_restore_spec.rb new file mode 100644 index 00000000000..b48358ecad1 --- /dev/null +++ b/spec/helpers/application_helper/buttons/cloud_volume_backup_restore_spec.rb @@ -0,0 +1,28 @@ +describe ApplicationHelper::Button::CloudVolumeBackupRestore do + describe "#disabled?" do + let(:view_context) { setup_view_context_with_sandbox({}) } + let(:button) { described_class.new(view_context, {}, {}, {}) } + + context "with no providers" do + it "the button is disabled" do + expect(button.disabled?).to be true + end + end + + context "with an OpenStack CloudManager supporting backup_restore" do + before { FactoryBot.create(:ems_openstack) } + + it "the button is enabled" do + expect(button.disabled?).to be false + end + end + + context "with an Amazon CloudManager not supporting backup_restore" do + before { FactoryBot.create(:ems_amazon) } + + it "the button is disabled" do + expect(button.disabled?).to be true + end + end + end +end