Skip to content

Commit 0f5cb2a

Browse files
committed
Fix OVERWRITE option bug when source file is symlink and destination file exists
1 parent dc22129 commit 0f5cb2a

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

lib/instance_agent/plugins/codedeploy/install_instruction.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ def execute(cleanup_file)
232232
# the CopyCommand entry should not even be created by Installer
233233
cleanup_file.puts(@destination)
234234
if File.symlink?(@source)
235-
FileUtils.symlink(File.readlink(@source), @destination)
235+
FileUtils.symlink(File.readlink(@source), @destination, :force => true)
236236
else
237237
FileUtils.copy(@source, @destination, :preserve => true)
238238
end

lib/instance_agent/plugins/codedeploy/installer.rb

+3
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ def generate_normal_copy(i, absolute_source_path, destination)
123123
when "DISALLOW"
124124
raise "The deployment failed because a specified file already exists at this location: #{destination}"
125125
when "OVERWRITE"
126+
if File.directory?(destination)
127+
raise "The deployment failed because a directory already exists at this location: #{destination}"
128+
end
126129
i.copy(absolute_source_path, destination)
127130
when "RETAIN"
128131
# neither generate copy command or fail the deployment

test/instance_agent/plugins/codedeploy/installer_test.rb

+25-11
Original file line numberDiff line numberDiff line change
@@ -157,17 +157,31 @@ class CodeDeployPluginInstallerTest < InstanceAgentTestCase
157157
end
158158
end
159159

160-
should "generate a copy command if the file already exists and @file_exists_behavior is set to 'OVERWRITE'" do
161-
@app_spec
162-
.stubs(:files)
163-
.returns([stub(:source => "src1",
164-
:destination => "dst1")])
165-
File.stubs(:exists?).with("dst1/src1").returns(true)
166-
@instruction_builder
167-
.expects(:copy)
168-
.with("deploy-archive-dir/src1", "dst1/src1")
169-
@installer.file_exists_behavior = "OVERWRITE"
170-
@installer.install(@deployment_group_id, @app_spec)
160+
context "if the file already exists and @file_exists_behavior is set to 'OVERWRITE'" do
161+
setup do
162+
@app_spec
163+
.stubs(:files)
164+
.returns([stub(:source => "src1",
165+
:destination => "dst1")])
166+
File.stubs(:exists?).with("dst1/src1").returns(true)
167+
@installer.file_exists_behavior = "OVERWRITE"
168+
end
169+
170+
should "generate a copy command if destination is regular file" do
171+
File.stubs(:directory?).with("dst1/src1").returns(false)
172+
@instruction_builder
173+
.expects(:copy)
174+
.with("deploy-archive-dir/src1", "dst1/src1")
175+
@installer.install(@deployment_group_id, @app_spec)
176+
end
177+
178+
should "raise an error if destination is directory" do
179+
File.stubs(:directory?).with("dst1/src1").returns(true)
180+
File.stubs(:symlink?).with("dst1/src1").returns(false)
181+
assert_raised_with_message("The deployment failed because a directory already exists at this location: dst1/src1") do
182+
@installer.install(@deployment_group_id, @app_spec)
183+
end
184+
end
171185
end
172186

173187
should "neither generate a copy command nor raise an error if the file already exists and @file_exists_behavior is set to 'RETAIN'" do

0 commit comments

Comments
 (0)