Skip to content

Commit 4591dff

Browse files
authored
Merge pull request #242 from rnc/ISSUE172
Workaround cleanup removing tagged image
2 parents c87af4f + 4f66b19 commit 4591dff

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

docker_squash/squash.py

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ def __init__(
4141
self.cleanup: bool = cleanup
4242
self.development = False
4343

44+
if tag == image and cleanup:
45+
log.warning("Tag is the same as image; preventing cleanup")
46+
self.cleanup = False
4447
if tmp_dir:
4548
self.development = True
4649
if not docker:

tests/test_integ_squash.py

+32-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import uuid
1010
from io import BytesIO
1111

12+
import docker.errors
1213
import mock
1314
import pytest
1415
from packaging import version as packaging_version
@@ -59,11 +60,11 @@ def cleanup_image(cls):
5960
IntegSquash.image.__exit__(None, None, None)
6061

6162
class Image(object):
62-
def __init__(self, dockerfile):
63+
def __init__(self, dockerfile, tag="latest"):
6364
self.dockerfile = dockerfile
6465
self.docker = TestIntegSquash.docker
6566
self.name = "integ-%s" % uuid.uuid1()
66-
self.tag = "%s:latest" % self.name
67+
self.tag = f"{self.name}:{tag}"
6768

6869
def __enter__(self):
6970
f = BytesIO(self.dockerfile.encode("utf-8"))
@@ -85,7 +86,11 @@ def __enter__(self):
8586

8687
def __exit__(self, exc_type, exc_val, exc_tb):
8788
if not os.getenv("CI"):
88-
self.docker.remove_image(image=self.tag, force=True)
89+
try:
90+
self.docker.remove_image(image=self.tag, force=True)
91+
except docker.errors.DockerException:
92+
# Can throw if cleanup results in the same image being deleted.
93+
pass
8994

9095
# Duplicated, I know...
9196
def _save_image(self):
@@ -129,6 +134,7 @@ def __init__(
129134
tmp_dir=None,
130135
log=None,
131136
tag=True,
137+
cleanup=False,
132138
):
133139
self.image = image
134140
self.number_of_layers = number_of_layers
@@ -142,6 +148,7 @@ def __init__(
142148
self.load_image = load_image
143149
self.numeric = numeric
144150
self.tmp_dir = tmp_dir
151+
self.cleanup = cleanup
145152

146153
def __enter__(self):
147154
from_layer = self.number_of_layers
@@ -160,6 +167,7 @@ def __enter__(self):
160167
output_path=self.output_path,
161168
load_image=self.load_image,
162169
tmp_dir=self.tmp_dir,
170+
cleanup=self.cleanup,
163171
)
164172

165173
self.image_id = squash.run()
@@ -242,6 +250,11 @@ def assertFileIsNotHardLink(self, name):
242250
"File '%s' should not be a hard link, but it is" % name
243251
)
244252

253+
def assert_target_tag_exists(self):
254+
if self.tag:
255+
# Raise exception if it doesn't exist
256+
self.docker.inspect_image(self.tag)
257+
245258
class Container(object):
246259
def __init__(self, image):
247260
self.image = image
@@ -289,6 +302,22 @@ class TestIntegSquash(IntegSquash):
289302
def inject_fixtures(self, caplog):
290303
self.caplog = caplog
291304

305+
def test_same_source_and_target_image_tag_should_not_be_deleted(self):
306+
self.caplog.set_level(logging.DEBUG, logger="cekit")
307+
dockerfile = (
308+
"""
309+
FROM %s
310+
RUN touch /somefile_layer1
311+
RUN touch /somefile_layer2
312+
"""
313+
% TestIntegSquash.BUSYBOX_IMAGE
314+
)
315+
316+
with self.Image(dockerfile, tag="squashed") as image:
317+
with self.SquashedImage(image, 2, cleanup=True) as squashed_image:
318+
squashed_image.assert_target_tag_exists()
319+
assert "Tag is the same as image; preventing cleanup" in self.caplog.text
320+
292321
def test_all_files_should_be_in_squashed_layer(self):
293322
"""
294323
We squash all layers in RUN, all files should be in the resulting squashed layer.

0 commit comments

Comments
 (0)