From 31b5abccd5b22829e691dfa5779b347b1a702ce9 Mon Sep 17 00:00:00 2001 From: Pascal Arlt Date: Mon, 11 Jun 2018 16:42:45 +0200 Subject: [PATCH] Use custom cache dir for zypper up/patch commands Previously the default zypper cache directory was used for update/patch commands. This cache dir was emptied afterwards. If a user already had data from previous zypper commands in the cache, the data would be lost after a zypper-docker up/patch. Now a custom directory is used for the zypper-docker commands that will be deleted before the updated image is created. This way the default zypper cache dir will be preserved. Signed-off-by: Pascal Arlt --- helpers.go | 8 +++++--- patches_test.go | 2 +- test/helpers.bash | 15 +++++++++++++++ test/updates.bats | 8 ++++++++ updates_test.go | 2 +- 5 files changed, 30 insertions(+), 5 deletions(-) diff --git a/helpers.go b/helpers.go index b375c6da..2a584f6c 100644 --- a/helpers.go +++ b/helpers.go @@ -315,10 +315,12 @@ func updatePatchCmd(zypperCmd string, ctx *cli.Context) { "replacefiles"} toIgnore := []string{"author", "message"} - cmd := formatZypperCommand("ref", fmt.Sprintf("-n %v", zypperCmd)) - clean := formatZypperCommand("clean -a") + makeTemp := "temp=$(mktemp -d)" + rmTemp := "rm -rf $temp" + cacheDir := "--cache-dir=$temp" + cmd := formatZypperCommand(fmt.Sprintf("%s ref", cacheDir), fmt.Sprintf("%s -n %v", cacheDir, zypperCmd)) cmd = cmdWithFlags(cmd, ctx, boolFlags, toIgnore) - cmd += " && " + clean + cmd = makeTemp + " && " + cmd + " && " + rmTemp newImgID, err := runCommandAndCommitToImage( img, diff --git a/patches_test.go b/patches_test.go index 15fed79c..eebc7efe 100644 --- a/patches_test.go +++ b/patches_test.go @@ -27,7 +27,7 @@ func TestPatchCommand(t *testing.T) { {"Cannot inspect", &mockClient{inspectFail: true}, 1, []string{"opensuse:13.2", "new:1.0.0"}, true, "could not inspect image 'opensuse:13.2': inspect fail", ""}, {"Patch success", &mockClient{listReturnOneImage: true}, 0, []string{"opensuse:13.2", "new:1.0.0"}, true, "new:1.0.0 successfully created", ""}, } - cases.run(t, patchCmd, "zypper -n patch", "") + cases.run(t, patchCmd, "zypper --cache-dir=$temp ref", "") } // LIST PATCHES diff --git a/test/helpers.bash b/test/helpers.bash index 8c161193..f946d12d 100755 --- a/test/helpers.bash +++ b/test/helpers.bash @@ -59,3 +59,18 @@ function remove_container() { docker rm -f $CONTAINER_ID > /dev/null CONTAINER_ID="" } + +function check_cache() { + container_id=$(docker run -t -d $TESTIMAGE:$TAG) + docker exec $container_id zypper ref + cache_before=$(docker exec $container_id find /var/cache/zypp/ -type f -exec md5sum {} \; | md5sum) + docker exec $container_id zypper --non-interactive --cache-dir=$(mktemp -d) in tar + cache_after=$(docker exec $container_id find /var/cache/zypp/ -type f -exec md5sum {} \; | md5sum) + + if [[ $cache_before == $cache_after ]]; then + echo "Cache check successful" + else + echo "Cache check failed" + fi + docker rm -f $container_id > /dev/null +} diff --git a/test/updates.bats b/test/updates.bats index a85f173b..bf2fa58d 100644 --- a/test/updates.bats +++ b/test/updates.bats @@ -33,6 +33,14 @@ load helpers docker rmi -f $TESTIMAGE:updated } +# This test is currently disabled due to a bug with zypper: +# https://github.com/openSUSE/zypper/issues/180 +# It will be reactivated once this is fixed +# @test "Check zypper cache" { +# sane_run check_cache +# [[ "$output" =~ "Cache check successful"+ ]] +# } + @test "zypper-docker list-updates" { zypperdocker lu $TESTIMAGE:$TAG [ "$status" -eq 0 ] diff --git a/updates_test.go b/updates_test.go index c8307a65..dcb01a92 100644 --- a/updates_test.go +++ b/updates_test.go @@ -27,7 +27,7 @@ func TestUpdateCommand(t *testing.T) { {"Start fail on commit", &mockClient{startFail: true}, 1, []string{"ori", "new:1.0.0"}, true, "Could not commit to the new image: Start failed", ""}, {"Update success", &mockClient{listReturnOneImage: true}, 0, []string{"opensuse:13.2", "new:1.0.0"}, true, "new:1.0.0 successfully created", ""}, } - cases.run(t, updateCmd, "zypper -n up", "") + cases.run(t, updateCmd, "zypper --cache-dir=$temp ref", "") } // LIST UPDATES