Skip to content

Commit f32aabe

Browse files
Add comprehensive error handling to image lifecycle
Improvements to create_image: - Catch and report Docker build failures with clear error messages - Re-raise exception to fail tests appropriately Improvements to delete_image: - Guard against nil @image (if build failed) - Wrap container cleanup in error handling to continue if issues occur - Catch individual container cleanup failures and continue - Handle NotFoundError if image already removed - Always attempt image removal even if container cleanup fails - Add informative status messages for debugging Benefits: - Tests fail gracefully with clear error messages - Cleanup attempts even after failures (prevents resource leaks) - Better visibility into what's happening during cleanup - Prevents cascading failures from cleanup issues Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
1 parent 096ce0f commit f32aabe

File tree

1 file changed

+37
-14
lines changed

1 file changed

+37
-14
lines changed

spec/spec_helper.rb

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
module Helpers
22
def create_image(version)
33
puts "Building image..."
4-
@image = Docker::Image.build_from_dir("#{version}/")
4+
5+
begin
6+
@image = Docker::Image.build_from_dir("#{version}/")
7+
rescue Docker::Error::DockerError => e
8+
puts "Failed to build Docker image: #{e.message}"
9+
raise
10+
end
511

612
set :os, :family => 'debian'
713
set :backend, :docker
@@ -11,25 +17,42 @@ def create_image(version)
1117
end
1218

1319
def delete_image
20+
return unless @image
21+
1422
puts "Deleting image..."
23+
1524
# Stop and remove only containers created from this image
16-
Docker::Container.all(:all => true).each do |container|
17-
container_image = container.info['Image']
18-
container_image_id = container.info['ImageID']
19-
20-
# Match image IDs - container IDs may have sha256: prefix and be full hashes
21-
# while @image.id is the short ID
22-
if container_image&.include?(@image.id) || container_image_id&.include?(@image.id)
23-
begin
24-
container.stop unless container.info['State'] == 'exited'
25-
container.delete(:force => true)
26-
rescue Docker::Error::NotModifiedError
27-
# Container already stopped, ignore
25+
begin
26+
Docker::Container.all(:all => true).each do |container|
27+
container_image = container.info['Image']
28+
container_image_id = container.info['ImageID']
29+
30+
# Match image IDs - container IDs may have sha256: prefix and be full hashes
31+
# while @image.id is the short ID
32+
if container_image&.include?(@image.id) || container_image_id&.include?(@image.id)
33+
begin
34+
container.stop unless container.info['State'] == 'exited'
35+
container.delete(:force => true)
36+
rescue Docker::Error::NotModifiedError
37+
# Container already stopped, ignore
38+
rescue Docker::Error::DockerError => e
39+
puts "Warning: Failed to cleanup container #{container.id[0..11]}: #{e.message}"
40+
end
2841
end
2942
end
43+
rescue Docker::Error::DockerError => e
44+
puts "Warning: Error during container cleanup: #{e.message}"
3045
end
3146

32-
@image.remove(:force => true)
47+
# Always attempt to remove the image, even if container cleanup failed
48+
begin
49+
@image.remove(:force => true)
50+
puts "Image removed successfully"
51+
rescue Docker::Error::NotFoundError
52+
puts "Image already removed"
53+
rescue Docker::Error::DockerError => e
54+
puts "Warning: Failed to remove image: #{e.message}"
55+
end
3356
end
3457
end
3558

0 commit comments

Comments
 (0)