Skip to content

Commit 166b7c5

Browse files
authored
Merge pull request #240 from rnc/ISSUE169
Don't clean up if tmp_dir (aka development mode) is used
2 parents c160939 + 0cfb1bf commit 166b7c5

File tree

4 files changed

+24
-32
lines changed

4 files changed

+24
-32
lines changed

README.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ Usage
6262
-h, --help show this help message and exit
6363
-v, --verbose Verbose output
6464
--version Show version and exit
65-
-d, --development Does not clean up after failure for easier debugging
6665
-f FROM_LAYER, --from-layer FROM_LAYER
6766
Number of layers to squash or ID of the layer (or image ID or image name) to squash from.
6867
In case the provided value is an integer, specified number of layers will be squashed.
@@ -71,7 +70,8 @@ Usage
7170
-m MESSAGE, --message MESSAGE
7271
Specify a commit message (comment) for the new image.
7372
-c, --cleanup Remove source image from Docker after squashing
74-
--tmp-dir TMP_DIR Temporary directory to be created and used
73+
--tmp-dir TMP_DIR Temporary directory to be created and used. This will NOT be deleted afterwards for
74+
easier debugging.
7575
--output-path OUTPUT_PATH
7676
Path where the image may be stored after squashing.
7777
--load-image [LOAD_IMAGE]

docker_squash/cli.py

+3-9
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,6 @@ def run(self):
7171
)
7272

7373
parser.add_argument("image", help="Image to be squashed")
74-
parser.add_argument(
75-
"-d",
76-
"--development",
77-
action="store_true",
78-
help="Does not clean up after failure for easier debugging",
79-
)
8074
parser.add_argument(
8175
"-f",
8276
"--from-layer",
@@ -100,7 +94,8 @@ def run(self):
10094
help="Remove source image from Docker after squashing",
10195
)
10296
parser.add_argument(
103-
"--tmp-dir", help="Temporary directory to be created and used"
97+
"--tmp-dir",
98+
help="Temporary directory to be created and used. This will NOT be deleted afterwards for easier debugging.",
10499
)
105100
parser.add_argument(
106101
"--output-path",
@@ -133,7 +128,6 @@ def run(self):
133128
output_path=args.output_path,
134129
load_image=args.load_image,
135130
tmp_dir=args.tmp_dir,
136-
development=args.development,
137131
cleanup=args.cleanup,
138132
).run()
139133
except KeyboardInterrupt:
@@ -142,7 +136,7 @@ def run(self):
142136
except Exception:
143137
e = sys.exc_info()[1]
144138

145-
if args.development or args.verbose:
139+
if args.verbose:
146140
self.log.exception(e)
147141
else:
148142
self.log.error(str(e))

docker_squash/squash.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ def __init__(
2424
tmp_dir=None,
2525
output_path=None,
2626
load_image=True,
27-
development=False,
2827
cleanup=False,
2928
):
3029
self.log = log
@@ -36,9 +35,11 @@ def __init__(
3635
self.tmp_dir = tmp_dir
3736
self.output_path = output_path
3837
self.load_image = load_image
39-
self.development = development
4038
self.cleanup = cleanup
39+
self.development = False
4140

41+
if tmp_dir:
42+
self.development = True
4243
if not docker:
4344
self.docker = common.docker_client(self.log)
4445

tests/test_integ_squash.py

+16-19
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
import json
33
import logging
44
import os
5+
import re
56
import shutil
67
import tarfile
78
import unittest
89
import uuid
910
from io import BytesIO
1011

1112
import mock
13+
import pytest
1214

1315
from docker_squash.errors import SquashError, SquashUnnecessaryError
1416
from docker_squash.lib import common
@@ -125,7 +127,6 @@ def __init__(
125127
numeric=False,
126128
tmp_dir=None,
127129
log=None,
128-
development=False,
129130
tag=True,
130131
):
131132
self.image = image
@@ -140,7 +141,6 @@ def __init__(
140141
self.load_image = load_image
141142
self.numeric = numeric
142143
self.tmp_dir = tmp_dir
143-
self.development = development
144144

145145
def __enter__(self):
146146
from_layer = self.number_of_layers
@@ -159,7 +159,6 @@ def __enter__(self):
159159
output_path=self.output_path,
160160
load_image=self.load_image,
161161
tmp_dir=self.tmp_dir,
162-
development=self.development,
163162
)
164163

165164
self.image_id = squash.run()
@@ -278,6 +277,10 @@ def assertFileDoesNotExist(self, name):
278277

279278

280279
class TestIntegSquash(IntegSquash):
280+
@pytest.fixture(autouse=True)
281+
def inject_fixtures(self, caplog):
282+
self.caplog = caplog
283+
281284
def test_all_files_should_be_in_squashed_layer(self):
282285
"""
283286
We squash all layers in RUN, all files should be in the resulting squashed layer.
@@ -754,35 +757,29 @@ def test_should_squash_every_layer(self):
754757

755758
# https://github.com/goldmann/docker-scripts/issues/44
756759
def test_remove_tmp_dir_after_failure(self):
760+
self.caplog.set_level(logging.DEBUG, logger="cekit")
757761
dockerfile = """
758762
FROM busybox:1.24.0
759763
LABEL foo bar
760764
"""
761765

762-
tmp_dir = "/tmp/docker-squash-integ-tmp-dir"
763-
log = mock.Mock()
764-
shutil.rmtree(tmp_dir, ignore_errors=True)
765-
766-
self.assertFalse(os.path.exists(tmp_dir))
767-
768766
with self.Image(dockerfile) as image:
769767
with self.assertRaisesRegex(
770768
SquashError,
771769
r"Cannot squash 20 layers, the .* image contains only \d " r"layers",
772770
):
773-
with self.SquashedImage(
774-
image, 20, numeric=True, tmp_dir=tmp_dir, log=log
775-
):
771+
with self.SquashedImage(image, 20, numeric=True):
776772
pass
777-
778-
log.debug.assert_any_call(
779-
"Using /tmp/docker-squash-integ-tmp-dir as the temporary directory"
773+
tmp_location = re.search(
774+
".*Using (.*)as the temporary directory.*", self.caplog.text
780775
)
781-
log.debug.assert_any_call(
782-
"Cleaning up /tmp/docker-squash-integ-tmp-dir temporary directory"
776+
777+
assert tmp_location
778+
assert "Cleaning up %s temporary directory", (
779+
tmp_location.group(1) in self.caplog.text
783780
)
784781

785-
self.assertFalse(os.path.exists(tmp_dir))
782+
self.assertFalse(os.path.exists(tmp_location.group(1)))
786783

787784
def test_should_not_remove_tmp_dir_after_failure_if_development_mode_is_on(self):
788785
dockerfile = """
@@ -802,7 +799,7 @@ def test_should_not_remove_tmp_dir_after_failure_if_development_mode_is_on(self)
802799
r"Cannot squash 20 layers, the .* image contains only \d " r"layers",
803800
):
804801
with self.SquashedImage(
805-
image, 20, numeric=True, tmp_dir=tmp_dir, log=log, development=True
802+
image, 20, numeric=True, tmp_dir=tmp_dir, log=log
806803
):
807804
pass
808805

0 commit comments

Comments
 (0)