Skip to content

Commit

Permalink
Don't clean up if tmp_dir (aka development mode) is used
Browse files Browse the repository at this point in the history
  • Loading branch information
rnc committed Feb 6, 2024
1 parent c160939 commit 5ac1f52
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 32 deletions.
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ Usage
-h, --help show this help message and exit
-v, --verbose Verbose output
--version Show version and exit
-d, --development Does not clean up after failure for easier debugging
-f FROM_LAYER, --from-layer FROM_LAYER
Number of layers to squash or ID of the layer (or image ID or image name) to squash from.
In case the provided value is an integer, specified number of layers will be squashed.
Expand All @@ -71,7 +70,8 @@ Usage
-m MESSAGE, --message MESSAGE
Specify a commit message (comment) for the new image.
-c, --cleanup Remove source image from Docker after squashing
--tmp-dir TMP_DIR Temporary directory to be created and used
--tmp-dir TMP_DIR Temporary directory to be created and used. This will NOT be deleted afterwards for
easier debugging.
--output-path OUTPUT_PATH
Path where the image may be stored after squashing.
--load-image [LOAD_IMAGE]
Expand Down
11 changes: 2 additions & 9 deletions docker_squash/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,6 @@ def run(self):
)

parser.add_argument("image", help="Image to be squashed")
parser.add_argument(
"-d",
"--development",
action="store_true",
help="Does not clean up after failure for easier debugging",
)
parser.add_argument(
"-f",
"--from-layer",
Expand All @@ -100,7 +94,7 @@ def run(self):
help="Remove source image from Docker after squashing",
)
parser.add_argument(
"--tmp-dir", help="Temporary directory to be created and used"
"--tmp-dir", help="Temporary directory to be created and used. This will NOT be deleted afterwards for easier debugging."
)
parser.add_argument(
"--output-path",
Expand Down Expand Up @@ -133,7 +127,6 @@ def run(self):
output_path=args.output_path,
load_image=args.load_image,
tmp_dir=args.tmp_dir,
development=args.development,
cleanup=args.cleanup,
).run()
except KeyboardInterrupt:
Expand All @@ -142,7 +135,7 @@ def run(self):
except Exception:
e = sys.exc_info()[1]

if args.development or args.verbose:
if args.verbose:
self.log.exception(e)
else:
self.log.error(str(e))
Expand Down
5 changes: 3 additions & 2 deletions docker_squash/squash.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ def __init__(
tmp_dir=None,
output_path=None,
load_image=True,
development=False,
cleanup=False,
):
self.log = log
Expand All @@ -36,9 +35,11 @@ def __init__(
self.tmp_dir = tmp_dir
self.output_path = output_path
self.load_image = load_image
self.development = development
self.cleanup = cleanup
self.development = False

if tmp_dir:
self.development = True
if not docker:
self.docker = common.docker_client(self.log)

Expand Down
35 changes: 16 additions & 19 deletions tests/test_integ_squash.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
import json
import logging
import os
import re
import shutil
import tarfile
import unittest
import uuid
from io import BytesIO

import mock
import pytest

from docker_squash.errors import SquashError, SquashUnnecessaryError
from docker_squash.lib import common
Expand Down Expand Up @@ -125,7 +127,6 @@ def __init__(
numeric=False,
tmp_dir=None,
log=None,
development=False,
tag=True,
):
self.image = image
Expand All @@ -140,7 +141,6 @@ def __init__(
self.load_image = load_image
self.numeric = numeric
self.tmp_dir = tmp_dir
self.development = development

def __enter__(self):
from_layer = self.number_of_layers
Expand All @@ -159,7 +159,6 @@ def __enter__(self):
output_path=self.output_path,
load_image=self.load_image,
tmp_dir=self.tmp_dir,
development=self.development,
)

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


class TestIntegSquash(IntegSquash):
@pytest.fixture(autouse=True)
def inject_fixtures(self, caplog):
self.caplog = caplog

def test_all_files_should_be_in_squashed_layer(self):
"""
We squash all layers in RUN, all files should be in the resulting squashed layer.
Expand Down Expand Up @@ -754,35 +757,29 @@ def test_should_squash_every_layer(self):

# https://github.com/goldmann/docker-scripts/issues/44
def test_remove_tmp_dir_after_failure(self):
self.caplog.set_level(logging.DEBUG, logger="cekit")
dockerfile = """
FROM busybox:1.24.0
LABEL foo bar
"""

tmp_dir = "/tmp/docker-squash-integ-tmp-dir"
log = mock.Mock()
shutil.rmtree(tmp_dir, ignore_errors=True)

self.assertFalse(os.path.exists(tmp_dir))

with self.Image(dockerfile) as image:
with self.assertRaisesRegex(
SquashError,
r"Cannot squash 20 layers, the .* image contains only \d " r"layers",
):
with self.SquashedImage(
image, 20, numeric=True, tmp_dir=tmp_dir, log=log
):
with self.SquashedImage(image, 20, numeric=True):
pass

log.debug.assert_any_call(
"Using /tmp/docker-squash-integ-tmp-dir as the temporary directory"
tmp_location = re.search(
".*Using (.*)as the temporary directory.*", self.caplog.text
)
log.debug.assert_any_call(
"Cleaning up /tmp/docker-squash-integ-tmp-dir temporary directory"

assert tmp_location
assert "Cleaning up %s temporary directory", (
tmp_location.group(1) in self.caplog.text
)

self.assertFalse(os.path.exists(tmp_dir))
self.assertFalse(os.path.exists(tmp_location.group(1)))

def test_should_not_remove_tmp_dir_after_failure_if_development_mode_is_on(self):
dockerfile = """
Expand All @@ -802,7 +799,7 @@ def test_should_not_remove_tmp_dir_after_failure_if_development_mode_is_on(self)
r"Cannot squash 20 layers, the .* image contains only \d " r"layers",
):
with self.SquashedImage(
image, 20, numeric=True, tmp_dir=tmp_dir, log=log, development=True
image, 20, numeric=True, tmp_dir=tmp_dir, log=log
):
pass

Expand Down

0 comments on commit 5ac1f52

Please sign in to comment.