9
9
import uuid
10
10
from io import BytesIO
11
11
12
+ import docker .errors
12
13
import mock
13
14
import pytest
14
15
from packaging import version as packaging_version
@@ -59,11 +60,11 @@ def cleanup_image(cls):
59
60
IntegSquash .image .__exit__ (None , None , None )
60
61
61
62
class Image (object ):
62
- def __init__ (self , dockerfile ):
63
+ def __init__ (self , dockerfile , tag = "latest" ):
63
64
self .dockerfile = dockerfile
64
65
self .docker = TestIntegSquash .docker
65
66
self .name = "integ-%s" % uuid .uuid1 ()
66
- self .tag = "%s:latest" % self .name
67
+ self .tag = f" { self .name } : { tag } "
67
68
68
69
def __enter__ (self ):
69
70
f = BytesIO (self .dockerfile .encode ("utf-8" ))
@@ -85,7 +86,11 @@ def __enter__(self):
85
86
86
87
def __exit__ (self , exc_type , exc_val , exc_tb ):
87
88
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
89
94
90
95
# Duplicated, I know...
91
96
def _save_image (self ):
@@ -129,6 +134,7 @@ def __init__(
129
134
tmp_dir = None ,
130
135
log = None ,
131
136
tag = True ,
137
+ cleanup = False ,
132
138
):
133
139
self .image = image
134
140
self .number_of_layers = number_of_layers
@@ -142,6 +148,7 @@ def __init__(
142
148
self .load_image = load_image
143
149
self .numeric = numeric
144
150
self .tmp_dir = tmp_dir
151
+ self .cleanup = cleanup
145
152
146
153
def __enter__ (self ):
147
154
from_layer = self .number_of_layers
@@ -160,6 +167,7 @@ def __enter__(self):
160
167
output_path = self .output_path ,
161
168
load_image = self .load_image ,
162
169
tmp_dir = self .tmp_dir ,
170
+ cleanup = self .cleanup ,
163
171
)
164
172
165
173
self .image_id = squash .run ()
@@ -242,6 +250,11 @@ def assertFileIsNotHardLink(self, name):
242
250
"File '%s' should not be a hard link, but it is" % name
243
251
)
244
252
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
+
245
258
class Container (object ):
246
259
def __init__ (self , image ):
247
260
self .image = image
@@ -289,6 +302,22 @@ class TestIntegSquash(IntegSquash):
289
302
def inject_fixtures (self , caplog ):
290
303
self .caplog = caplog
291
304
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
+
292
321
def test_all_files_should_be_in_squashed_layer (self ):
293
322
"""
294
323
We squash all layers in RUN, all files should be in the resulting squashed layer.
0 commit comments