diff --git a/lib/instance_agent/plugins/codedeploy/install_instruction.rb b/lib/instance_agent/plugins/codedeploy/install_instruction.rb index b33cc81c..314a6468 100644 --- a/lib/instance_agent/plugins/codedeploy/install_instruction.rb +++ b/lib/instance_agent/plugins/codedeploy/install_instruction.rb @@ -232,7 +232,7 @@ def execute(cleanup_file) # the CopyCommand entry should not even be created by Installer cleanup_file.puts(@destination) if File.symlink?(@source) - FileUtils.symlink(File.readlink(@source), @destination) + FileUtils.symlink(File.readlink(@source), @destination, :force => true) else FileUtils.copy(@source, @destination, :preserve => true) end diff --git a/lib/instance_agent/plugins/codedeploy/installer.rb b/lib/instance_agent/plugins/codedeploy/installer.rb index 0ed61073..9e24b91c 100644 --- a/lib/instance_agent/plugins/codedeploy/installer.rb +++ b/lib/instance_agent/plugins/codedeploy/installer.rb @@ -123,6 +123,9 @@ def generate_normal_copy(i, absolute_source_path, destination) when "DISALLOW" raise "The deployment failed because a specified file already exists at this location: #{destination}" when "OVERWRITE" + if File.directory?(destination) + raise "The deployment failed because a directory already exists at this location: #{destination}" + end i.copy(absolute_source_path, destination) when "RETAIN" # neither generate copy command or fail the deployment diff --git a/test/instance_agent/plugins/codedeploy/installer_test.rb b/test/instance_agent/plugins/codedeploy/installer_test.rb index 22ddf2e1..12d1347f 100644 --- a/test/instance_agent/plugins/codedeploy/installer_test.rb +++ b/test/instance_agent/plugins/codedeploy/installer_test.rb @@ -157,17 +157,31 @@ class CodeDeployPluginInstallerTest < InstanceAgentTestCase end end - should "generate a copy command if the file already exists and @file_exists_behavior is set to 'OVERWRITE'" do - @app_spec - .stubs(:files) - .returns([stub(:source => "src1", - :destination => "dst1")]) - File.stubs(:exists?).with("dst1/src1").returns(true) - @instruction_builder - .expects(:copy) - .with("deploy-archive-dir/src1", "dst1/src1") - @installer.file_exists_behavior = "OVERWRITE" - @installer.install(@deployment_group_id, @app_spec) + context "if the file already exists and @file_exists_behavior is set to 'OVERWRITE'" do + setup do + @app_spec + .stubs(:files) + .returns([stub(:source => "src1", + :destination => "dst1")]) + File.stubs(:exists?).with("dst1/src1").returns(true) + @installer.file_exists_behavior = "OVERWRITE" + end + + should "generate a copy command if destination is a regular file" do + File.stubs(:directory?).with("dst1/src1").returns(false) + @instruction_builder + .expects(:copy) + .with("deploy-archive-dir/src1", "dst1/src1") + @installer.install(@deployment_group_id, @app_spec) + end + + should "raise an error if destination is directory" do + File.stubs(:directory?).with("dst1/src1").returns(true) + File.stubs(:symlink?).with("dst1/src1").returns(false) + assert_raised_with_message("The deployment failed because a directory already exists at this location: dst1/src1") do + @installer.install(@deployment_group_id, @app_spec) + end + end end should "neither generate a copy command nor raise an error if the file already exists and @file_exists_behavior is set to 'RETAIN'" do