diff --git a/app/models/manageiq/providers/embedded_automation_manager/configuration_script_source.rb b/app/models/manageiq/providers/embedded_automation_manager/configuration_script_source.rb index 19e041b5985..1b91afd502d 100644 --- a/app/models/manageiq/providers/embedded_automation_manager/configuration_script_source.rb +++ b/app/models/manageiq/providers/embedded_automation_manager/configuration_script_source.rb @@ -85,11 +85,22 @@ def sync raise NotImplementedError, N_("sync must be implemented in a subclass") end - def checkout_git_repository(target_directory) + def checkout_git_repository(target_directory = nil) return if git_repository.nil? + target_directory_given = !!target_directory + target_directory ||= Pathname.new(Dir.mktmpdir) + git_repository.update_repo git_repository.checkout(scm_branch, target_directory) + + return target_directory unless block_given? + + begin + yield target_directory + ensure + FileUtils.rm_rf(target_directory.to_s) unless target_directory_given + end end private diff --git a/spec/factories/configuration_script_source.rb b/spec/factories/configuration_script_source.rb index d12d5276fa9..ed2e78ed4e7 100644 --- a/spec/factories/configuration_script_source.rb +++ b/spec/factories/configuration_script_source.rb @@ -3,6 +3,9 @@ sequence(:name) { |n| "configuration_script_source#{seq_padded_for_sorting(n)}" } end + factory :embedded_automation_configuration_script_source, :parent => :configuration_script_source, + :class => "ManageIQ::Providers::EmbeddedAutomationManager::ConfigurationScriptSource" + factory :ansible_configuration_script_source, :parent => :configuration_script_source, :class => "ManageIQ::Providers::AnsibleTower::AutomationManager::ConfigurationScriptSource" @@ -12,13 +15,13 @@ :class => "ManageIQ::Providers::Awx::AutomationManager::ConfigurationScriptSource" factory :embedded_ansible_configuration_script_source, - :parent => :configuration_script_source, + :parent => :embedded_automation_configuration_script_source, :class => "ManageIQ::Providers::EmbeddedAnsible::AutomationManager::ConfigurationScriptSource" do scm_url { "https://example.com/foo.git" } end factory :embedded_workflow_configuration_script_source, - :parent => :configuration_script_source, + :parent => :embedded_automation_configuration_script_source, :class => "ManageIQ::Providers::Workflows::AutomationManager::ConfigurationScriptSource" do scm_url { "https://example.com/foo.git" } end diff --git a/spec/models/manageiq/providers/embedded_automation_manager/configuration_script_source_spec.rb b/spec/models/manageiq/providers/embedded_automation_manager/configuration_script_source_spec.rb new file mode 100644 index 00000000000..075180f3c05 --- /dev/null +++ b/spec/models/manageiq/providers/embedded_automation_manager/configuration_script_source_spec.rb @@ -0,0 +1,55 @@ +RSpec.describe ManageIQ::Providers::EmbeddedAutomationManager::ConfigurationScriptSource do + let(:subject) { FactoryBot.create(:embedded_automation_configuration_script_source, :scm_url => "https://example.com/foo.git") } + let(:git_repository) { FactoryBot.create(:git_repository) } + + describe "#checkout_git_repository" do + before do + expect(subject.git_repository).to receive(:update_repo) + expect(subject.git_repository).to receive(:checkout) + end + + context "without a block" do + it "creates a temporary directory" do + expect(Dir).to receive(:mktmpdir).and_return("/tmp/mydir") + + subject.checkout_git_repository + end + + it "doesn't delete the temporary directory" do + expect(Dir).to receive(:mktmpdir).and_return("/tmp/mydir") + expect(FileUtils).not_to receive(:rm_rf) + + subject.checkout_git_repository + end + + it "doesn't create a new temp dir when passing in a target directory" do + expect(Dir).not_to receive(:mktmpdir) + + subject.checkout_git_repository("/tmp/mydir") + end + end + + context "with a block" do + it "creates a temporary directory and deletes it" do + expect(Dir).to receive(:mktmpdir).and_return("/tmp/mydir") + expect(FileUtils).to receive(:rm_rf).with("/tmp/mydir") + + subject.checkout_git_repository { |target_directory| target_directory } + end + + it "deletes the temporary directory if the block raises an exception" do + expect(Dir).to receive(:mktmpdir).and_return("/tmp/mydir") + expect(FileUtils).to receive(:rm_rf).with("/tmp/mydir") + + expect { subject.checkout_git_repository { |_| raise "Exception" } }.to raise_error(RuntimeError, "Exception") + end + + it "doesn't create a new temp dir when passing in a target directory" do + expect(Dir).not_to receive(:mktmpdir) + expect(FileUtils).not_to receive(:rm_rf).with("/tmp/mydir") + + subject.checkout_git_repository("/tmp/mydir") { |target_directory| target_directory } + end + end + end +end