From 7909fe93d374cd6ad6533918cbfb9cb7aa96d55b Mon Sep 17 00:00:00 2001 From: Ewoud Kohl van Wijngaarden Date: Tue, 10 Dec 2024 23:39:33 +0100 Subject: [PATCH] Rails 7 compatible Zeitwerk loading Fixes: 61433f5d07da01fdfe62b45682c9fc5dc17dbb60 ("Fixes #37625 - Fix the plugin to support zeitwerk") --- .../lib}/azure_sdk_adapter.rb | 0 lib/foreman_azure_rm/engine.rb | 32 +-- test/unit/azure_rm_test.rb | 254 +++++++++--------- test/unit/azure_sdk_adapter_test.rb | 36 ++- 4 files changed, 156 insertions(+), 166 deletions(-) rename {lib/foreman_azure_rm => app/lib}/azure_sdk_adapter.rb (100%) diff --git a/lib/foreman_azure_rm/azure_sdk_adapter.rb b/app/lib/azure_sdk_adapter.rb similarity index 100% rename from lib/foreman_azure_rm/azure_sdk_adapter.rb rename to app/lib/azure_sdk_adapter.rb diff --git a/lib/foreman_azure_rm/engine.rb b/lib/foreman_azure_rm/engine.rb index 3792fcc..5c70af2 100644 --- a/lib/foreman_azure_rm/engine.rb +++ b/lib/foreman_azure_rm/engine.rb @@ -2,17 +2,19 @@ module ForemanAzureRm class Engine < ::Rails::Engine engine_name 'foreman_azure_rm' - #autoloading all files inside lib dir - config.eager_load_paths += Dir["#{config.root}/lib"] - config.eager_load_paths += Dir["#{config.root}/app/models/concerns/"] - config.eager_load_paths += Dir["#{config.root}/app/helpers/"] - - initializer 'foreman_azure_rm.register_plugin', :before => :finisher_hook do - Foreman::Plugin.register :foreman_azure_rm do - requires_foreman '>= 3.13' - register_gettext - compute_resource ForemanAzureRm::AzureRm - parameter_filter ComputeResource, :azure_vm, :tenant, :app_ident, :secret_key, :sub_id, :region, :cloud + lib = config.root.join('app', 'lib') + + config.autoload_paths << lib + config.eager_load_paths << lib + + initializer 'foreman_azure_rm.register_plugin', :before => :finisher_hook do |app| + app.reloader.to_prepare do + Foreman::Plugin.register :foreman_azure_rm do + requires_foreman '>= 3.13' + register_gettext + compute_resource ForemanAzureRm::AzureRm + parameter_filter ComputeResource, :azure_vm, :tenant, :app_ident, :secret_key, :sub_id, :region, :cloud + end end end @@ -29,14 +31,6 @@ class Engine < ::Rails::Engine end end - initializer "foreman_azure_rm.zeitwerk" do - Rails.autoloaders.each do |loader| - loader.ignore( - ForemanAzureRm::Engine.root.join('lib/foreman_azure_rm/version.rb') - ) - end - end - config.to_prepare do require 'azure_mgmt_resources' require 'azure_mgmt_network' diff --git a/test/unit/azure_rm_test.rb b/test/unit/azure_rm_test.rb index 3440e6b..5f897fb 100644 --- a/test/unit/azure_rm_test.rb +++ b/test/unit/azure_rm_test.rb @@ -1,143 +1,141 @@ require_relative '../test_plugin_helper' require_relative '../azure_rm_test_helper' -module ForemanAzureRm - class ForemanAzureRmTest < ActiveSupport::TestCase - include AzureRmTestHelper +class ForemanAzureRmTest < ActiveSupport::TestCase + include AzureRmTestHelper + setup do + @mock_sdk = mock('mock_sdk') + ForemanAzureRm::AzureRm.any_instance.stubs(:sdk).returns(@mock_sdk) + mock_region_sdk = mock('mock_region_sdk') + @mock_sdk.stubs(:list_regions).with('11111111-1111-1111-1111-111111111111').returns(mock_region_sdk) + eastus_region = mock('eastus_region') + westus_region = mock('westus_region') + mock_region_sdk.stubs(:value).returns([eastus_region, westus_region]) + eastus_region.stubs(:display_name => 'East US', :name => 'eastus') + westus_region.stubs(:display_name => 'West US', :name => 'westus') + + @azure_cr = FactoryBot.create(:azure_rm) + @azure_cr.stubs(:sdk).returns(@mock_sdk) + end + + test "list all valid regions" do + assert_equal [['East US', 'eastus'], ['West US', 'westus']], @azure_cr.regions + end + + test "list valid standard cloud name" do + cloud = %w[azure azureusgovernment azurechina azuregermancloud].sample + ForemanAzureRm::AzureRm.any_instance.stubs(:validate_cloud?).returns(true) + @azure_cr.cloud=(cloud) + assert @azure_cr.validate_cloud? + end + + test "list all resource groups" do + mock_resource_client = mock('mock_resource_client') + @mock_sdk.stubs(:resource_client).returns(mock_resource_client) + @mock_sdk.stubs(:rgs).returns(['rg1', 'rg2', 'rg3']) + assert ['rg1', 'rg2', 'rg3'], @azure_cr.resource_groups + end + + context 'sdk access' do setup do - @mock_sdk = mock('mock_sdk') - ForemanAzureRm::AzureRm.any_instance.stubs(:sdk).returns(@mock_sdk) - mock_region_sdk = mock('mock_region_sdk') - @mock_sdk.stubs(:list_regions).with('11111111-1111-1111-1111-111111111111').returns(mock_region_sdk) - eastus_region = mock('eastus_region') - westus_region = mock('westus_region') - mock_region_sdk.stubs(:value).returns([eastus_region, westus_region]) - eastus_region.stubs(:display_name => 'East US', :name => 'eastus') - westus_region.stubs(:display_name => 'West US', :name => 'westus') - - @azure_cr = FactoryBot.create(:azure_rm) - @azure_cr.stubs(:sdk).returns(@mock_sdk) + mock_sdk_results + @azure_cr.key_pair = @azure_cr.setup_key_pair + end + + test "create vm with password and without custom data" do + @mock_vm.stubs(:disable_password_authentication).returns(false) + @mock_vm.os_profile.stubs(:custom_data) + mock_create_or_update_vm_with_password + vm_args = base_vm_args.merge(with_password_auth).merge(with_marketplace_image) + actual_server = @azure_cr.create_vm(vm_args) + + assert_equal "ervin-golomb", actual_server.name + assert_equal "rg1", actual_server.resource_group + assert actual_server.password.present? + assert_equal 1, actual_server.interfaces.count + refute actual_server.azure_vm.disable_password_authentication + refute actual_server.azure_vm.os_profile.custom_data.present? end - test "list all valid regions" do - assert_equal [['East US', 'eastus'], ['West US', 'westus']], @azure_cr.regions + test "create vm with password, custom data and vm extension" do + @mock_vm.stubs(:disable_password_authentication).returns(false) + @mock_vm.os_profile.stubs(:custom_data).returns('ZWNobyBjdXN0b21EYXRh') + mock_create_or_update_vm_with_password + mock_vm_extension = mock('mock_vm_extension') + @mock_sdk.expects(:create_or_update_vm_extensions).with() do |actual_rg, vm_name, script_name, actual_ext| + actual_rg == "rg1" && + actual_ext.settings["commandToExecute"] == "su - \"testuser\" -c \"sudo sh /var/lib/waagent/custom-script/download/0/myscript.sh\"" && + actual_ext.settings["fileUris"] == ["https://gist.githubusercontent.com/apuntamb/f4e9ff4e2daf62bc847313b0c64e59f9/raw/73578e1bb7b03237ae1ac6b787b08d00d126adf0/myscript.sh"] + end.returns(mock_vm_extension) + vm_args = base_vm_args.merge(with_password_auth).merge(with_custom_data).merge(with_marketplace_image).merge(with_vm_extensions) + actual_server = @azure_cr.create_vm(vm_args) + + assert_equal "testpswd123", actual_server.password + assert actual_server.azure_vm.os_profile.custom_data.present? + refute actual_server.azure_vm.disable_password_authentication end - test "list valid standard cloud name" do - cloud = %w[azure azureusgovernment azurechina azuregermancloud].sample - ForemanAzureRm::AzureRm.any_instance.stubs(:validate_cloud?).returns(true) - @azure_cr.cloud=(cloud) - assert @azure_cr.validate_cloud? + test "create vm with sshkey and without custom data" do + @mock_vm.stubs(:disable_password_authentication).returns(true) + @mock_vm.os_profile.stubs(:custom_data) + @mock_sdk.expects(:create_or_update_vm_extensions).never + mock_create_or_update_vm_with_sshkey + vm_args = base_vm_args.merge(with_ssh_key_auth).merge(with_marketplace_image) + actual_server = @azure_cr.create_vm(vm_args) + + assert actual_server.azure_vm.disable_password_authentication + refute actual_server.azure_vm.os_profile.custom_data.present? end - test "list all resource groups" do - mock_resource_client = mock('mock_resource_client') - @mock_sdk.stubs(:resource_client).returns(mock_resource_client) - @mock_sdk.stubs(:rgs).returns(['rg1', 'rg2', 'rg3']) - assert ['rg1', 'rg2', 'rg3'], @azure_cr.resource_groups + test "create vm with sshkey, custom data and vm extension" do + @mock_vm.stubs(:disable_password_authentication).returns(true) + @mock_vm.os_profile.stubs(:custom_data).returns('ZWNobyBjdXN0b21EYXRh') + mock_create_or_update_vm_with_sshkey + mock_vm_extension = mock('mock_vm_extension') + @mock_sdk.expects(:create_or_update_vm_extensions).with() do |actual_rg, vm_name, script_name, actual_ext| + actual_rg == "rg1" && + actual_ext.settings["commandToExecute"] == "sudo sh /var/lib/waagent/custom-script/download/0/myscript.sh" && + actual_ext.settings["fileUris"] == ["https://gist.githubusercontent.com/apuntamb/f4e9ff4e2daf62bc847313b0c64e59f9/raw/73578e1bb7b03237ae1ac6b787b08d00d126adf0/myscript.sh"] + end.returns(mock_vm_extension) + vm_args = base_vm_args.merge(with_ssh_key_auth).merge(with_marketplace_image).merge(with_custom_data).merge(with_vm_extensions) + actual_server = @azure_cr.create_vm(vm_args) + + assert actual_server.azure_vm.disable_password_authentication + assert actual_server.azure_vm.os_profile.custom_data.present? end - context 'sdk access' do - setup do - mock_sdk_results - @azure_cr.key_pair = @azure_cr.setup_key_pair - end - - test "create vm with password and without custom data" do - @mock_vm.stubs(:disable_password_authentication).returns(false) - @mock_vm.os_profile.stubs(:custom_data) - mock_create_or_update_vm_with_password - vm_args = base_vm_args.merge(with_password_auth).merge(with_marketplace_image) - actual_server = @azure_cr.create_vm(vm_args) - - assert_equal "ervin-golomb", actual_server.name - assert_equal "rg1", actual_server.resource_group - assert actual_server.password.present? - assert_equal 1, actual_server.interfaces.count - refute actual_server.azure_vm.disable_password_authentication - refute actual_server.azure_vm.os_profile.custom_data.present? - end - - test "create vm with password, custom data and vm extension" do - @mock_vm.stubs(:disable_password_authentication).returns(false) - @mock_vm.os_profile.stubs(:custom_data).returns('ZWNobyBjdXN0b21EYXRh') - mock_create_or_update_vm_with_password - mock_vm_extension = mock('mock_vm_extension') - @mock_sdk.expects(:create_or_update_vm_extensions).with() do |actual_rg, vm_name, script_name, actual_ext| - actual_rg == "rg1" && - actual_ext.settings["commandToExecute"] == "su - \"testuser\" -c \"sudo sh /var/lib/waagent/custom-script/download/0/myscript.sh\"" && - actual_ext.settings["fileUris"] == ["https://gist.githubusercontent.com/apuntamb/f4e9ff4e2daf62bc847313b0c64e59f9/raw/73578e1bb7b03237ae1ac6b787b08d00d126adf0/myscript.sh"] - end.returns(mock_vm_extension) - vm_args = base_vm_args.merge(with_password_auth).merge(with_custom_data).merge(with_marketplace_image).merge(with_vm_extensions) - actual_server = @azure_cr.create_vm(vm_args) - - assert_equal "testpswd123", actual_server.password - assert actual_server.azure_vm.os_profile.custom_data.present? - refute actual_server.azure_vm.disable_password_authentication - end - - test "create vm with sshkey and without custom data" do - @mock_vm.stubs(:disable_password_authentication).returns(true) - @mock_vm.os_profile.stubs(:custom_data) - @mock_sdk.expects(:create_or_update_vm_extensions).never - mock_create_or_update_vm_with_sshkey - vm_args = base_vm_args.merge(with_ssh_key_auth).merge(with_marketplace_image) - actual_server = @azure_cr.create_vm(vm_args) - - assert actual_server.azure_vm.disable_password_authentication - refute actual_server.azure_vm.os_profile.custom_data.present? - end - - test "create vm with sshkey, custom data and vm extension" do - @mock_vm.stubs(:disable_password_authentication).returns(true) - @mock_vm.os_profile.stubs(:custom_data).returns('ZWNobyBjdXN0b21EYXRh') - mock_create_or_update_vm_with_sshkey - mock_vm_extension = mock('mock_vm_extension') - @mock_sdk.expects(:create_or_update_vm_extensions).with() do |actual_rg, vm_name, script_name, actual_ext| - actual_rg == "rg1" && - actual_ext.settings["commandToExecute"] == "sudo sh /var/lib/waagent/custom-script/download/0/myscript.sh" && - actual_ext.settings["fileUris"] == ["https://gist.githubusercontent.com/apuntamb/f4e9ff4e2daf62bc847313b0c64e59f9/raw/73578e1bb7b03237ae1ac6b787b08d00d126adf0/myscript.sh"] - end.returns(mock_vm_extension) - vm_args = base_vm_args.merge(with_ssh_key_auth).merge(with_marketplace_image).merge(with_custom_data).merge(with_vm_extensions) - actual_server = @azure_cr.create_vm(vm_args) - - assert actual_server.azure_vm.disable_password_authentication - assert actual_server.azure_vm.os_profile.custom_data.present? - end - - test "create vm with custom image and sshkey" do - @mock_vm.stubs(:disable_password_authentication).returns(true) - mock_custom_img = mock('mock_custom_img') - @mock_sdk.expects(:get_custom_image).with("rg1", "first_custom_img").returns(mock_custom_img) - mock_custom_img.stubs(:name).returns('first_custom_img') - mock_custom_img.stubs(:id).returns('/subscriptions/11111111-1111-1111-1111-111111111111/resourceGroups/rg1/providers/Microsoft.Compute/images/first_custom_img') - @mock_vm.storage_profile.image_reference.stubs(:id).returns('/subscriptions/11111111-1111-1111-1111-111111111111/resourceGroups/rg1/providers/Microsoft.Compute/images/first_custom_img') - @mock_sdk.stubs(:list_custom_images).returns([mock_custom_img]) - mock_create_or_update_vm_with_sshkey - @mock_sdk.expects(:create_or_update_vm_extensions).never - vm_args = base_vm_args.merge(with_ssh_key_auth).merge(with_custom_image) - actual_server = @azure_cr.create_vm(vm_args) - - assert actual_server.azure_vm.disable_password_authentication - assert_equal "custom://first_custom_img", actual_server.image_id - end - - - test "create vm with shared image gallery and password" do - @mock_vm.stubs(:disable_password_authentication).returns(false) - @mock_sdk.expects(:fetch_gallery_image_id).with("rg1", "first_gallery_img").returns("/subscriptions/11111111-1111-1111-1111-111111111111/resourceGroups/rg1/providers/Microsoft.Compute/galleries/first_gallery/images/first_gallery_img").times(2) - mock_gallery_image = mock('mock_gallery_image') - mock_gallery_image.stubs(:id).returns('/subscriptions/11111111-1111-1111-1111-111111111111/resourceGroups/rg1/providers/Microsoft.Compute/galleries/first_gallery/images/first_gallery_img') - @mock_vm.storage_profile.image_reference.stubs(:id).returns('/subscriptions/11111111-1111-1111-1111-111111111111/resourceGroups/rg1/providers/Microsoft.Compute/galleries/first_gallery/images/first_gallery_img') - @mock_sdk.stubs(:list_custom_images).returns([]) - mock_create_or_update_vm_with_password - vm_args = base_vm_args.merge(with_password_auth).merge(with_gallery_image) - actual_server = @azure_cr.create_vm(vm_args) - - refute actual_server.azure_vm.disable_password_authentication - assert_equal "testpswd123", actual_server.password - assert_equal "gallery://first_gallery_img", actual_server.image_id - end + test "create vm with custom image and sshkey" do + @mock_vm.stubs(:disable_password_authentication).returns(true) + mock_custom_img = mock('mock_custom_img') + @mock_sdk.expects(:get_custom_image).with("rg1", "first_custom_img").returns(mock_custom_img) + mock_custom_img.stubs(:name).returns('first_custom_img') + mock_custom_img.stubs(:id).returns('/subscriptions/11111111-1111-1111-1111-111111111111/resourceGroups/rg1/providers/Microsoft.Compute/images/first_custom_img') + @mock_vm.storage_profile.image_reference.stubs(:id).returns('/subscriptions/11111111-1111-1111-1111-111111111111/resourceGroups/rg1/providers/Microsoft.Compute/images/first_custom_img') + @mock_sdk.stubs(:list_custom_images).returns([mock_custom_img]) + mock_create_or_update_vm_with_sshkey + @mock_sdk.expects(:create_or_update_vm_extensions).never + vm_args = base_vm_args.merge(with_ssh_key_auth).merge(with_custom_image) + actual_server = @azure_cr.create_vm(vm_args) + + assert actual_server.azure_vm.disable_password_authentication + assert_equal "custom://first_custom_img", actual_server.image_id + end + + + test "create vm with shared image gallery and password" do + @mock_vm.stubs(:disable_password_authentication).returns(false) + @mock_sdk.expects(:fetch_gallery_image_id).with("rg1", "first_gallery_img").returns("/subscriptions/11111111-1111-1111-1111-111111111111/resourceGroups/rg1/providers/Microsoft.Compute/galleries/first_gallery/images/first_gallery_img").times(2) + mock_gallery_image = mock('mock_gallery_image') + mock_gallery_image.stubs(:id).returns('/subscriptions/11111111-1111-1111-1111-111111111111/resourceGroups/rg1/providers/Microsoft.Compute/galleries/first_gallery/images/first_gallery_img') + @mock_vm.storage_profile.image_reference.stubs(:id).returns('/subscriptions/11111111-1111-1111-1111-111111111111/resourceGroups/rg1/providers/Microsoft.Compute/galleries/first_gallery/images/first_gallery_img') + @mock_sdk.stubs(:list_custom_images).returns([]) + mock_create_or_update_vm_with_password + vm_args = base_vm_args.merge(with_password_auth).merge(with_gallery_image) + actual_server = @azure_cr.create_vm(vm_args) + + refute actual_server.azure_vm.disable_password_authentication + assert_equal "testpswd123", actual_server.password + assert_equal "gallery://first_gallery_img", actual_server.image_id end end end diff --git a/test/unit/azure_sdk_adapter_test.rb b/test/unit/azure_sdk_adapter_test.rb index 2539a81..4800c46 100644 --- a/test/unit/azure_sdk_adapter_test.rb +++ b/test/unit/azure_sdk_adapter_test.rb @@ -1,25 +1,23 @@ require_relative '../test_plugin_helper' -module ForemanAzureRm - class AzureSdkAdapterTest < ActiveSupport::TestCase - setup do - compute_resource = FactoryBot.build(:azure_rm) - tenant = compute_resource.uuid - app_ident = compute_resource.app_ident - secret_key = compute_resource.password - sub_id = compute_resource.user - cloud = compute_resource.cloud - @test_adapter = AzureSdkAdapter.new(tenant, app_ident, secret_key, sub_id, cloud) - AzureSdkAdapter.stubs(:gallery_caching).with('test_rg').returns({}) - end +class AzureSdkAdapterTest < ActiveSupport::TestCase + setup do + compute_resource = FactoryBot.build(:azure_rm) + tenant = compute_resource.uuid + app_ident = compute_resource.app_ident + secret_key = compute_resource.password + sub_id = compute_resource.user + cloud = compute_resource.cloud + @test_adapter = ForemanAzureRm::AzureSdkAdapter.new(tenant, app_ident, secret_key, sub_id, cloud) + ForemanAzureRm::AzureSdkAdapter.stubs(:gallery_caching).with('test_rg').returns({}) + end - test "should call #actual_gallery_image_id when #gallery_caching is {} otherwise return #gallery_caching" do - @test_adapter.expects(:actual_gallery_image_id).with('test_rg', 'test_gallery_image_name').once.returns('test_gallery_img_id') - actual1 = @test_adapter.fetch_gallery_image_id('test_rg', 'test_gallery_image_name') - actual2 = @test_adapter.fetch_gallery_image_id('test_rg', 'test_gallery_image_name') + test "should call #actual_gallery_image_id when #gallery_caching is {} otherwise return #gallery_caching" do + @test_adapter.expects(:actual_gallery_image_id).with('test_rg', 'test_gallery_image_name').once.returns('test_gallery_img_id') + actual1 = @test_adapter.fetch_gallery_image_id('test_rg', 'test_gallery_image_name') + actual2 = @test_adapter.fetch_gallery_image_id('test_rg', 'test_gallery_image_name') - assert_equal actual1, actual2 - assert_equal 'test_gallery_img_id', actual1 - end + assert_equal actual1, actual2 + assert_equal 'test_gallery_img_id', actual1 end end