From 40472f5a21ded1befae34e794232ea13e5c47d10 Mon Sep 17 00:00:00 2001 From: Castulo Martinez Date: Fri, 5 Jun 2020 09:18:50 -0700 Subject: [PATCH] Updating "clean" to remove new and old cache dirs "Swupd clean" removes cache from certain pre-defined directories. This commit udpdates the locations where clean looks for cache so it includes the new locations where the cache is stored. Swupd will continue looking at old cache locations until the bump to format 31. Signed-off-by: Castulo Martinez --- src/3rd_party/3rd_party_clean.c | 17 +- src/cmds/clean.c | 315 +++++++++++++++--- src/cmds/update.c | 2 +- src/swupd.h | 9 +- .../functional/3rd-party/3rd-party-clean.bats | 235 +++++++++++++ test/functional/api/api-3rd-party-clean.bats | 48 ++- test/functional/api/api-clean.bats | 20 +- test/functional/clean/clean-basic.bats | 174 ++++++++++ .../clean/clean-grouped-manifests.bats | 152 +++++++++ test/functional/clean/clean-old-statedir.bats | 147 ++++++++ .../signature/permission-incorrect.bats | 5 + 11 files changed, 1049 insertions(+), 75 deletions(-) create mode 100755 test/functional/3rd-party/3rd-party-clean.bats create mode 100755 test/functional/clean/clean-basic.bats create mode 100755 test/functional/clean/clean-grouped-manifests.bats create mode 100755 test/functional/clean/clean-old-statedir.bats diff --git a/src/3rd_party/3rd_party_clean.c b/src/3rd_party/3rd_party_clean.c index b5e3459c..1b8ba901 100644 --- a/src/3rd_party/3rd_party_clean.c +++ b/src/3rd_party/3rd_party_clean.c @@ -95,14 +95,23 @@ static enum swupd_code clean_repos_state(UNUSED_PARAM char *unused) { enum swupd_code ret; int files_removed; + size_t bytes_removed; + char *bytes_removed_pretty = NULL; - ret = clean_statedir(cmdline_option_dry_run, cmdline_option_all); - files_removed = clean_get_stats(); + clean_reset_stats(); + ret = clean_cachedir(); + + files_removed = clean_get_file_stats(); + bytes_removed = clean_get_byte_stats(); + prettify_size(bytes_removed, &bytes_removed_pretty); if (cmdline_option_dry_run) { info("Would remove %d files\n", files_removed); + info("Aproximatelly %s would be freed\n", bytes_removed_pretty); } else { info("%d files removed\n", files_removed); + info("%s freed\n", bytes_removed_pretty); } + FREE(bytes_removed_pretty); return ret; } @@ -124,6 +133,10 @@ enum swupd_code third_party_clean_main(int argc, char **argv) return ret_code; } + /* set the command options */ + clean_set_option_all(cmdline_option_all); + clean_set_option_dry_run(cmdline_option_dry_run); + /* clean the cache */ ret_code = third_party_run_operation_multirepo(cmdline_option_repo, clean_repos_state, SWUPD_OK, "clean", steps_in_clean); diff --git a/src/cmds/clean.c b/src/cmds/clean.c index f060dcfd..bc1362c3 100644 --- a/src/cmds/clean.c +++ b/src/cmds/clean.c @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -29,6 +30,42 @@ #define FLAG_ALL 2000 #define FLAG_DRY_RUN 2001 +static struct { + int all; + int dry_run; +} options; + +static struct { + int files_removed; + size_t bytes_removed; +} stats; + +void clean_set_option_all(bool opt) +{ + options.all = opt; +} + +void clean_set_option_dry_run(bool opt) +{ + options.dry_run = opt; +} + +int clean_get_file_stats(void) +{ + return stats.files_removed; +} + +size_t clean_get_byte_stats(void) +{ + return stats.bytes_removed; +} + +void clean_reset_stats(void) +{ + stats.files_removed = 0; + stats.bytes_removed = 0; +} + static void print_help(void) { print("Remove cached content used for updates from state directory\n\n"); @@ -44,21 +81,6 @@ static void print_help(void) "\n"); } -static struct { - int all; - int dry_run; -} options; - -static struct { - int files_removed; - size_t bytes_removed; -} stats; - -int clean_get_stats(void) -{ - return stats.files_removed; -} - static const struct option prog_opts[] = { { "help", no_argument, 0, 'h' }, { "all", no_argument, 0, FLAG_ALL }, @@ -181,11 +203,6 @@ static enum swupd_code remove_if(const char *path, bool dry_run, remove_predicat return ret; } -static bool is_fullfile(const char UNUSED_PARAM *dir, const struct dirent *entry) -{ - return str_len(entry->d_name) == (SWUPD_HASH_LEN - 1); -} - static bool is_pack_indicator(const char UNUSED_PARAM *dir, const struct dirent *entry) { static const char prefix[] = "pack-"; @@ -361,7 +378,15 @@ static enum swupd_code clean_staged_manifests(const char *path, bool dry_run, bo if (size < 0) { size = 0; } + + // when doing a dry run, if --all was selcted we can assume the + // directory will also get deleted since it should be empty by now + if (dry_run && all) { + print("%s\n", version_dir); + } + if (!rmdir(version_dir) || (dry_run && all)) { + stats.files_removed++; stats.bytes_removed += long_to_ulong(size); } @@ -405,7 +430,7 @@ enum swupd_code clean_main(int argc, char **argv) * and keeping all the staged files of the current version. This helps recovering the * current version. Or do it for the previous version to allow a rollback. */ - ret = clean_statedir(options.dry_run, options.all); + ret = clean_cachedir(); /* TODO: Also print the bytes removed, need to take into account the hardlinks. */ prettify_size(stats.bytes_removed, &bytes_removed_pretty); @@ -424,49 +449,251 @@ enum swupd_code clean_main(int argc, char **argv) return ret; } -/* clean_statedir will clean the state directory used by swupd (default to - * /var/lib/swupd). It will remove all files except relevant manifests unless - * all is set to true. Setting dry_run to true will print the files that would - * be removed but will not actually remove them. */ -enum swupd_code clean_statedir(bool dry_run, bool all) +static int remove_irrelevant_file(const char *filename, const struct stat *st, int type __attribute__((unused)), struct FTW *ftw __attribute__((unused))) +{ + int ret; + char *root_cache_path = NULL; + char *relevant_path = NULL; + + // skip the starting directory of the walk + root_cache_path = statedir_get_cache_dir(); + ret = strncmp(filename, root_cache_path, str_len(filename)); + FREE(root_cache_path); + if (ret == 0) { + return 0; + } + + // if the file is from the current mirror, it + // is still relevant, skip it + relevant_path = statedir_get_cache_url_dir(); + ret = str_starts_with(filename, relevant_path); + FREE(relevant_path); + if (ret == 0){ + return 0; + } + + // if we got here, the file is irrelevant to the current mirror + if (options.dry_run) { + print("%s\n", filename); + } else { + ret = sys_rm(filename); + if (ret) { + return ret; + } + } + stats.files_removed++; + stats.bytes_removed+=long_to_ulong(st->st_size); + + return 0; +} + +static int count_file(const char *filename, const struct stat *st, int type __attribute__((unused)), struct FTW *ftw __attribute__((unused))) +{ + if (options.dry_run) { + print("%s\n", filename); + } + stats.files_removed++; + stats.bytes_removed+=long_to_ulong(st->st_size); + + return 0; +} + +static enum swupd_code count_dir_contents(const char *path) +{ + int ret; + + ret = nftw(path, &count_file, 0, FTW_DEPTH | FTW_PHYS | FTW_MOUNT); + if (ret) { + return SWUPD_COULDNT_REMOVE_FILE; + } + + return SWUPD_OK; +} + +static enum swupd_code clean_irrelevant_cache(void) +{ + int ret; + char *dir = NULL; + + dir = statedir_get_cache_dir(); + ret = nftw(dir, &remove_irrelevant_file, 0, FTW_DEPTH | FTW_PHYS | FTW_MOUNT); + FREE(dir); + if (ret) { + if (errno == ENOENT) { + return SWUPD_COULDNT_LIST_DIR; + } else { + return SWUPD_OUT_OF_MEMORY_ERROR; + } + } + + return SWUPD_OK; +} + +static enum swupd_code remove_cache_directory(const char *dir) +{ + enum swupd_code ret_code; + int ret; + + if (!sys_is_dir(dir)){ + return SWUPD_OK; + } + + ret_code = count_dir_contents(dir); + if (ret_code) { + return ret_code; + } + + if (!options.dry_run) { + ret = sys_rm_recursive(dir); + if (ret < 0 && ret != -ENOENT) { + return SWUPD_COULDNT_REMOVE_FILE; + } + } + + return SWUPD_OK; +} + +/* TODO: Remove after format bump to format 31 + * clean_statedir will clean the old statedir location used by swupd. + */ +static enum swupd_code clean_former_statedir(void) { enum swupd_code ret; char *path = NULL; - path = statedir_get_staged_dir(); - ret = remove_if(path, dry_run, is_fullfile); - FREE(path); - if (ret != SWUPD_OK) { - return ret; - } - /* Pack presence indicator files. */ - path = statedir_get_delta_pack_dir(); - ret = remove_if(path, dry_run, is_pack_indicator); - FREE(path); + ret = remove_if(globals.cache_dir, options.dry_run, is_pack_indicator); if (ret != SWUPD_OK) { return ret; } /* Manifest delta files. */ - path = statedir_get_manifest_delta_dir(); - ret = remove_if(path, dry_run, is_manifest_delta); + ret = remove_if(globals.cache_dir, options.dry_run, is_manifest_delta); + if (ret != SWUPD_OK) { + return ret; + } + + path = sys_path_join("%s/%s", globals.cache_dir, "staged"); + ret = remove_cache_directory(path); FREE(path); if (ret != SWUPD_OK) { return ret; } - /* NOTE: do not clean the state_dir/bundles directory */ - path = statedir_get_manifest_root_dir(); - ret = clean_staged_manifests(path, dry_run, all); + path = sys_path_join("%s/%s", globals.cache_dir, "manifest"); + ret = remove_cache_directory(path); FREE(path); if (ret != SWUPD_OK) { return ret; } - // TODO: Remove after format bump to format 31 - // Clean all manifests outside of the new manifests folder - ret = clean_staged_manifests(globals.cache_dir, dry_run, true); + path = sys_path_join("%s/%s", globals.cache_dir, "delta"); + ret = remove_cache_directory(path); + FREE(path); + if (ret != SWUPD_OK) { + return ret; + } + + path = sys_path_join("%s/%s", globals.cache_dir, "download"); + ret = remove_cache_directory(path); + FREE(path); + if (ret != SWUPD_OK) { + return ret; + } + + // Manifest files from old location. + ret = clean_staged_manifests(globals.cache_dir, options.dry_run, true); return ret; } + +/* clean_cachedir will clean the cache directory used by swupd (default to + * /var/lib/swupd/cache). It will remove all files except relevant manifests from + * the cache asociated to the current mirror being used unless all is set to true. + * Setting options.dry_run to true will print the files that would be removed but + * will not actually remove them. */ +enum swupd_code clean_cachedir(void) +{ + enum swupd_code ret_code = SWUPD_OK; + char *dir = NULL; + + if (options.all) { + // remove the whole cache directory, it will be regenerated + // from scratch the next time swupd initializes + dir = statedir_get_cache_dir(); + ret_code = remove_cache_directory(dir); + FREE(dir); + if (ret_code) { + return ret_code; + } + } else { + // remove everything from the cache directory except + // relevant manifests + dir = statedir_get_cache_url_dir(); + ret_code = remove_if(dir, options.dry_run, is_pack_indicator); + FREE(dir); + if (ret_code) { + return ret_code; + } + + dir = statedir_get_delta_dir(); + ret_code = remove_cache_directory(dir); + FREE(dir); + if (ret_code) { + return ret_code; + } + + dir = statedir_get_download_dir(); + ret_code = remove_cache_directory(dir); + FREE(dir); + if (ret_code) { + return ret_code; + } + + dir = statedir_get_staged_dir(); + ret_code = remove_cache_directory(dir); + FREE(dir); + if (ret_code) { + return ret_code; + } + + dir = statedir_get_temp_dir(); + ret_code = remove_cache_directory(dir); + FREE(dir); + if (ret_code) { + return ret_code; + } + + // clean manifests that are not relevant to the current version + // these include: + // - manifests from very old versions (versions no longer included in the current mom) + // - manifests from versions newer than the current version + // - manifests with hashes (e.g. Manifest.vim.) + dir = statedir_get_manifest_root_dir(); + /* Manifest delta files. */ + ret_code = remove_if(dir, options.dry_run, is_manifest_delta); + if (ret_code != SWUPD_OK) { + FREE(dir); + return ret_code; + } + ret_code = clean_staged_manifests(dir, options.dry_run, false); + FREE(dir); + if(ret_code) { + return ret_code; + } + + // remove all cache from other mirror URLs + ret_code = clean_irrelevant_cache(); + if(ret_code) { + return ret_code; + } + } + + // TODO: Remove after format bump to format 31 + // We need to clean everything in the old statedir locations + // regardless of if --all was used, manifests in old locations + // will no longer be used + ret_code = clean_former_statedir(); + + return ret_code; +} diff --git a/src/cmds/update.c b/src/cmds/update.c index ff90f4ab..b5d9c46e 100644 --- a/src/cmds/update.c +++ b/src/cmds/update.c @@ -504,7 +504,7 @@ clean_curl: /* clean up our cached content from the update. It is likely much more than * we need and the clean helps us prevent cache bloat. */ if (!keepcache) { - clean_statedir(false, false); + clean_cachedir(); } free_subscriptions(&latest_subs); diff --git a/src/swupd.h b/src/swupd.h index 49eb8ad5..57c76a58 100644 --- a/src/swupd.h +++ b/src/swupd.h @@ -369,7 +369,12 @@ extern void bundle_list_set_option_status(bool opt); extern void bundle_list_set_option_orphans(bool opt); /* clean.c */ -extern int clean_get_stats(void); +extern void clean_set_option_all(bool opt); +extern void clean_set_option_dry_run(bool opt); +extern int clean_get_file_stats(void); +extern size_t clean_get_byte_stats(void); +extern void clean_reset_stats(void); +extern enum swupd_code clean_cachedir(void); /* 3rd_party_check_update.c */ enum swupd_code third_party_execute_check_update(void); @@ -384,8 +389,6 @@ extern enum swupd_code print_update_conf_info(void); extern int handle_mirror_if_stale(void); -extern enum swupd_code clean_statedir(bool all, bool dry_run); - extern void warn_nosigcheck(const char *file); /* Parameter parsing in global.c */ diff --git a/test/functional/3rd-party/3rd-party-clean.bats b/test/functional/3rd-party/3rd-party-clean.bats new file mode 100755 index 00000000..ecbc0b72 --- /dev/null +++ b/test/functional/3rd-party/3rd-party-clean.bats @@ -0,0 +1,235 @@ +#!/usr/bin/env bats + +# Author: Castulo Martinez +# Email: castulo.martinez@intel.com + +load "../testlib" + +test_setup() { + + create_test_environment "$TEST_NAME" + set_current_version "$TEST_NAME" 33000 + + # ###################################################################### + # cache/data structure post swupd 4.2.1 (split cache/data) for 3rd-party + # ###################################################################### + # + # └── 3rd-party + # └── cache + #     ├── repo1 + #     │ ├── delta + #     │ ├── download + #     │ ├── manifest + #     │ │   ├── 10 + #     │ │   │   ├── Manifest.p11-kit + #     │ │   │   └── Manifest.p11-kit.dfd541b1ac9256982044848f2128f51e23ddbb72f68e9de43adaf8f7ead93657 + #     │ │   ├── 20 + #     │ │   │   ├── Manifest.MoM + #     │ │   │   ├── Manifest.MoM.sig + #     │ │   │   ├── Manifest.emacs + #     │ │   │   └── Manifest.emacs.7414710480607fa89c307ad11d145d8bb0cbdbdf3c0692f940d906b670f6fd5d + # │ │   ├── Manifest-emacs-delta-from-10-to-20 + #     │ │   └── Manifest-vim-delta-from-10-to-20 + #     │ ├── pack-blender-from-0-to-20.tar + #     │ ├── pack-vim-from-10-to-20.tar + #     │ ├── staged + #     │ │ ├── 00004ead4b4eb1cef1ebbe13849f6c47c21d325182f98cf8f5333e9a76d2e2df + # │ │ └── fffe891980af628e498b16a364b5b4d00377485370ccc4d09ac0fc072e85b31d + # │ └── temp + #    └── repo2 + #    ├── delta + #    ├── download + #    ├── manifest + #    │   ├── 50 + #    │   │   ├── Manifest.MoM + #    │   │   └── Manifest.MoM.sig + #     │   └── Manifest-qt-basic-delta-from-40-to-50 + #    ├── pack-lib-imageformat-from-30-to-50.tar + #    ├── staged + # │ └── ffaf66dab58a2074e5fe9dc6ab7bf8773c069817dcccf631148b6afc87166b4b + # └── temp + + # + # └── 3rd-party + # ├── bundles + # │   ├── editors + # │   ├── os-core + # │   └── vim + # ├── swupd_lock + # └── version + + # create the cache from "repo1" + create_third_party_repo -a "$TEST_NAME" 10 staging repo1 + set_current_version "$TEST_NAME" 20 repo1 + + sudo mkdir -p "$TPSTATEDIR_MANIFEST"/{10,20} + MOM=$(create_manifest "$TPSTATEDIR_MANIFEST"/20 MoM 1 10) + write_to_protected_file -a "$MOM" "M... dfd541b1ac9256982044848f2128f51e23ddbb72f68e9de43adaf8f7ead93657 10 p11-kit\n" + write_to_protected_file -a "$MOM" "M... 7414710480607fa89c307ad11d145d8bb0cbdbdf3c0692f940d906b670f6fd5d 20 emacs\n" + MOM_SIG="$MOM".sig ; sudo touch "$MOM_SIG" + FILE1="$TPSTATEDIR_MANIFEST"/10/Manifest.p11-kit ; sudo touch "$FILE1" + FILE2="$TPSTATEDIR_MANIFEST"/10/Manifest.p11-kit.dfd541b1ac9256982044848f2128f51e23ddbb72f68e9de43adaf8f7ead93657 ; sudo touch "$FILE2" + FILE3="$TPSTATEDIR_MANIFEST"/20/Manifest.emacs ; sudo touch "$FILE3" + FILE4="$TPSTATEDIR_MANIFEST"/20/Manifest.emacs.7414710480607fa89c307ad11d145d8bb0cbdbdf3c0692f940d906b670f6fd5d ; sudo touch "$FILE4" + FILE5="$TPSTATEDIR_MANIFEST"/Manifest-emacs-delta-from-10-to-20 ; sudo touch "$FILE5" + FILE6="$TPSTATEDIR_MANIFEST"/Manifest-vim-delta-from-10-to-20 ; sudo touch "$FILE6" + FILE7="$TPSTATEDIR_CACHE"/pack-blender-from-0-to-20.tar ; sudo touch "$FILE7" + FILE8="$TPSTATEDIR_CACHE"/pack-vim-from-10-to-20.tar ; sudo touch "$FILE8" + FILE9="$TPSTATEDIR_STAGED"/00004ead4b4eb1cef1ebbe13849f6c47c21d325182f98cf8f5333e9a76d2e2df ; sudo touch "$FILE9" + FILE10="$TPSTATEDIR_STAGED"/fffe891980af628e498b16a364b5b4d00377485370ccc4d09ac0fc072e85b31d ; sudo touch "$FILE10" + + # create the cache from "repo2" + create_third_party_repo -a "$TEST_NAME" 10 staging repo2 + set_current_version "$TEST_NAME" 50 repo2 + + sudo mkdir -p "$TPSTATEDIR_MANIFEST"/50 + MOM2=$(create_manifest "$TPSTATEDIR_MANIFEST"/50 MoM 1 40) + write_to_protected_file -a "$MOM2" "M... dfd541b1ac9256982044848f2128f51e23ddbb72f68e9de43adaf8f7ead12345 10 blender\n" + write_to_protected_file -a "$MOM2" "M... 1234510480607fa89c307ad11d145d8bb0cbdbdf3c0692f940d906b670f6fd5d 20 ansible\n" + MOM2_SIG="$MOM2".sig ; sudo touch "$MOM2_SIG" + FILE11="$TPSTATEDIR_MANIFEST"/Manifest-qt-basic-delta-from-40-to-50 ; sudo touch "$FILE11" + FILE12="$TPSTATEDIR_CACHE"/pack-lib-imageformat-from-30-to-50.tar ; sudo touch "$FILE12" + FILE13="$TPSTATEDIR_STAGED"/ffaf66dab58a2074e5fe9dc6ab7bf8773c069817dcccf631148b6afc87166b4b ; sudo touch "$FILE13" + +} + +@test "TPR098: Clean 3rd-party cached files except manifests" { + + run sudo sh -c "$SWUPD 3rd-party clean $SWUPD_OPTS" + + assert_status_is "$SWUPD_OK" + expected_output=$(cat <<-EOM + _____________________________ + 3rd-Party Repository: repo1 + _____________________________ + 12 files removed + .* KB freed + _____________________________ + 3rd-Party Repository: repo2 + _____________________________ + 7 files removed + .* KB freed + EOM + ) + assert_regex_is_output "$expected_output" + + # relevant manifests should still exist + assert_file_exists "$FILE1" + assert_file_exists "$FILE3" + assert_file_exists "$MOM" + assert_file_exists "$MOM_SIG" + + # the rest of the files should not exist + assert_file_not_exists "$FILE2" + assert_file_not_exists "$FILE4" + assert_file_not_exists "$FILE5" + assert_file_not_exists "$FILE6" + assert_file_not_exists "$FILE7" + assert_file_not_exists "$FILE8" + assert_file_not_exists "$FILE9" + assert_file_not_exists "$FILE10" + +} + +@test "TPR099: Clean all 3rd-party cached files including manifests" { + + run sudo sh -c "$SWUPD 3rd-party clean $SWUPD_OPTS --all" + + assert_status_is "$SWUPD_OK" + expected_output=$(cat <<-EOM + _____________________________ + 3rd-Party Repository: repo1 + _____________________________ + 21 files removed + .* KB freed + _____________________________ + 3rd-Party Repository: repo2 + _____________________________ + 13 files removed + .* KB freed + EOM + ) + assert_regex_is_output "$expected_output" + + # everything should have been cleaned + assert_file_not_exists "$FILE1" + assert_file_not_exists "$FILE2" + assert_file_not_exists "$FILE3" + assert_file_not_exists "$FILE4" + assert_file_not_exists "$FILE5" + assert_file_not_exists "$FILE6" + assert_file_not_exists "$FILE7" + assert_file_not_exists "$FILE8" + assert_file_not_exists "$FILE9" + assert_file_not_exists "$FILE10" + assert_file_not_exists "$FILE11" + assert_file_not_exists "$FILE12" + assert_file_not_exists "$FILE13" + assert_file_not_exists "$MOM" + assert_file_not_exists "$MOM_SIG" + assert_file_not_exists "$MOM2" + assert_file_not_exists "$MOM2_SIG" + +} + +@test "TPR100: Dry run clean to show the 3rd-party files that would be removed from the cache" { + + run sudo sh -c "$SWUPD 3rd-party clean $SWUPD_OPTS --dry-run" + + assert_status_is "$SWUPD_OK" + expected_output=$(cat <<-EOM + _____________________________ + 3rd-Party Repository: repo1 + _____________________________ + $STATEDIR_ABS/3rd-party/repo1/cache/.*repo1/pack-.*-from-.*-to-20.tar + $STATEDIR_ABS/3rd-party/repo1/cache/.*repo1/pack-.*-from-.*-to-20.tar + $STATEDIR_ABS/3rd-party/repo1/cache/.*repo1/delta + $STATEDIR_ABS/3rd-party/repo1/cache/.*repo1/download + $STATEDIR_ABS/3rd-party/repo1/cache/.*repo1/staged/.* + $STATEDIR_ABS/3rd-party/repo1/cache/.*repo1/staged/.* + $STATEDIR_ABS/3rd-party/repo1/cache/.*repo1/staged + $STATEDIR_ABS/3rd-party/repo1/cache/.*repo1/temp + $STATEDIR_ABS/3rd-party/repo1/cache/.*repo1/manifest/Manifest-.*-delta-from-10-to-20 + $STATEDIR_ABS/3rd-party/repo1/cache/.*repo1/manifest/Manifest-.*-delta-from-10-to-20 + $STATEDIR_ABS/3rd-party/repo1/cache/.*repo1/manifest/.0/Manifest\\..*\\..* + $STATEDIR_ABS/3rd-party/repo1/cache/.*repo1/manifest/.0/Manifest\\..*\\..* + Would remove 12 files + Aproximatelly .* KB would be freed + _____________________________ + 3rd-Party Repository: repo2 + _____________________________ + $STATEDIR_ABS/3rd-party/repo2/cache/.*repo2/pack-lib-imageformat-from-30-to-50.tar + $STATEDIR_ABS/3rd-party/repo2/cache/.*repo2/delta + $STATEDIR_ABS/3rd-party/repo2/cache/.*repo2/download + $STATEDIR_ABS/3rd-party/repo2/cache/.*repo2/staged/ffaf66dab58a2074e5fe9dc6ab7bf8773c069817dcccf631148b6afc87166b4b + $STATEDIR_ABS/3rd-party/repo2/cache/.*repo2/staged + $STATEDIR_ABS/3rd-party/repo2/cache/.*repo2/temp + $STATEDIR_ABS/3rd-party/repo2/cache/.*repo2/manifest/Manifest-qt-basic-delta-from-40-to-50 + Would remove 7 files + Aproximatelly .* KB would be freed + EOM + ) + assert_regex_in_output "$expected_output" + + # files should not be deleted, just printed + assert_file_exists "$FILE1" + assert_file_exists "$FILE2" + assert_file_exists "$FILE3" + assert_file_exists "$FILE4" + assert_file_exists "$FILE5" + assert_file_exists "$FILE6" + assert_file_exists "$FILE7" + assert_file_exists "$FILE8" + assert_file_exists "$FILE9" + assert_file_exists "$FILE10" + assert_file_exists "$FILE11" + assert_file_exists "$FILE12" + assert_file_exists "$FILE13" + assert_file_exists "$MOM" + assert_file_exists "$MOM_SIG" + assert_file_exists "$MOM" + assert_file_exists "$MOM_SIG" + assert_file_exists "$MOM2" + assert_file_exists "$MOM2_SIG" + +} diff --git a/test/functional/api/api-3rd-party-clean.bats b/test/functional/api/api-3rd-party-clean.bats index ca7f832c..957ec0c0 100755 --- a/test/functional/api/api-3rd-party-clean.bats +++ b/test/functional/api/api-3rd-party-clean.bats @@ -8,14 +8,16 @@ load "../testlib" test_setup() { create_test_environment "$TEST_NAME" 10 1 + create_third_party_repo -a "$TEST_NAME" 10 1 repo1 - STATE1="$TPSTATEDIR" - sudo mkdir -p "$STATE1"/manifest/10 - sudo touch "$STATE1"/manifest/10/Manifest.test{1..3} - sudo touch "$STATE1"/pack-test{1..2}-from-0.tar + CACHE1="$TPSTATEDIR_CACHE" + sudo mkdir -p "$TPSTATEDIR_MANIFEST"/10 + sudo touch "$TPSTATEDIR_MANIFEST"/10/Manifest.test{1..3} + sudo touch "$CACHE1"/pack-test{1..2}-from-0.tar + create_third_party_repo -a "$TEST_NAME" 10 1 repo2 - STATE2="$TPSTATEDIR" - sudo touch "$STATE2"/pack-test3-from-0.tar + CACHE2="$TPSTATEDIR_CACHE" + sudo touch "$CACHE2"/pack-test3-from-0.tar } @@ -41,13 +43,21 @@ test_setup() { assert_status_is "$SWUPD_OK" expected_output=$(cat <<-EOM \\[repo1\\] - $TEST_ROOT_DIR/$STATE1/pack-test.-from-0.tar - $TEST_ROOT_DIR/$STATE1/pack-test.-from-0.tar - $TEST_ROOT_DIR/$STATE1/manifest/10/Manifest.test. - $TEST_ROOT_DIR/$STATE1/manifest/10/Manifest.test. - $TEST_ROOT_DIR/$STATE1/manifest/10/Manifest.test. + $CACHE1/pack-test.-from-0.tar + $CACHE1/pack-test.-from-0.tar + $CACHE1/delta + $CACHE1/download + $CACHE1/staged + $CACHE1/temp + $CACHE1/manifest/10/Manifest.test. + $CACHE1/manifest/10/Manifest.test. + $CACHE1/manifest/10/Manifest.test. \\[repo2\\] - $TEST_ROOT_DIR/$STATE2/pack-test3-from-0.tar + $CACHE2/pack-test3-from-0.tar + $CACHE2/delta + $CACHE2/download + $CACHE2/staged + $CACHE2/temp EOM ) assert_regex_is_output "$expected_output" @@ -60,11 +70,15 @@ test_setup() { assert_status_is "$SWUPD_OK" expected_output=$(cat <<-EOM - $TEST_ROOT_DIR/$STATE1/pack-test.-from-0.tar - $TEST_ROOT_DIR/$STATE1/pack-test.-from-0.tar - $TEST_ROOT_DIR/$STATE1/manifest/10/Manifest.test. - $TEST_ROOT_DIR/$STATE1/manifest/10/Manifest.test. - $TEST_ROOT_DIR/$STATE1/manifest/10/Manifest.test. + $CACHE1/pack-test.-from-0.tar + $CACHE1/pack-test.-from-0.tar + $CACHE1/delta + $CACHE1/download + $CACHE1/staged + $CACHE1/temp + $CACHE1/manifest/10/Manifest.test. + $CACHE1/manifest/10/Manifest.test. + $CACHE1/manifest/10/Manifest.test. EOM ) assert_regex_is_output "$expected_output" diff --git a/test/functional/api/api-clean.bats b/test/functional/api/api-clean.bats index aacd8c67..b25decc3 100755 --- a/test/functional/api/api-clean.bats +++ b/test/functional/api/api-clean.bats @@ -8,9 +8,9 @@ load "../testlib" test_setup() { create_test_environment "$TEST_NAME" - sudo mkdir "$STATEDIR"/manifest/10 - sudo touch "$STATEDIR"/manifest/10/Manifest.test{1..3} - sudo touch "$STATEDIR"/pack-test{1..2}-from-0.tar + sudo mkdir "$STATEDIR_MANIFEST"/10 + sudo touch "$STATEDIR_MANIFEST"/10/Manifest.test{1..3} + sudo touch "$STATEDIR_CACHE"/pack-test{1..2}-from-0.tar } @@ -29,11 +29,15 @@ test_setup() { assert_status_is "$SWUPD_OK" expected_output=$(cat <<-EOM - $TEST_ROOT_DIR/$STATEDIR/pack-test.-from-0.tar - $TEST_ROOT_DIR/$STATEDIR/pack-test.-from-0.tar - $TEST_ROOT_DIR/$STATEDIR/manifest/10/Manifest.test. - $TEST_ROOT_DIR/$STATEDIR/manifest/10/Manifest.test. - $TEST_ROOT_DIR/$STATEDIR/manifest/10/Manifest.test. + $STATEDIR_CACHE/pack-test.-from-0.tar + $STATEDIR_CACHE/pack-test.-from-0.tar + $STATEDIR_DELTA + $STATEDIR_DOWNLOAD + $STATEDIR_STAGED + $STATEDIR_TEMP + $STATEDIR_MANIFEST/10/Manifest.test. + $STATEDIR_MANIFEST/10/Manifest.test. + $STATEDIR_MANIFEST/10/Manifest.test. EOM ) assert_regex_is_output "$expected_output" diff --git a/test/functional/clean/clean-basic.bats b/test/functional/clean/clean-basic.bats new file mode 100755 index 00000000..58481ce8 --- /dev/null +++ b/test/functional/clean/clean-basic.bats @@ -0,0 +1,174 @@ +#!/usr/bin/env bats + +# Author: Castulo Martinez +# Email: castulo.martinez@intel.com + +load "../testlib" + +test_setup() { + + create_test_environment "$TEST_NAME" + set_current_version "$TEST_NAME" 33000 + + # ######################################################## + # cache/data structure post swupd 4.2.1 (split cache/data) + # ######################################################## + # + # └── cache + #    ├── https_cdn.download.clearlinux.org_update + #    │ ├── delta + #    │ ├── download + #    │ ├── manifest + #    │ │   ├── 32900 + #    │ │   │   ├── Manifest.p11-kit + #    │ │   │   └── Manifest.p11-kit.dfd541b1ac9256982044848f2128f51e23ddbb72f68e9de43adaf8f7ead93657 + #    │ │   ├── 33000 + #    │ │   │   ├── Manifest.MoM + #    │ │   │   ├── Manifest.MoM.sig + #    │ │   │   ├── Manifest.emacs + #    │ │   │   └── Manifest.emacs.7414710480607fa89c307ad11d145d8bb0cbdbdf3c0692f940d906b670f6fd5d + # │ │   ├── Manifest-emacs-delta-from-32900-to-33000 + #    │ │   └── Manifest-vim-delta-from-32900-to-33000 + #    │ ├── pack-blender-from-0-to-33000.tar + #    │ ├── pack-vim-from-32800-to-32900.tar + #    │ ├── staged + #    │ │ ├── 00004ead4b4eb1cef1ebbe13849f6c47c21d325182f98cf8f5333e9a76d2e2df + #    │ │ └── fffe891980af628e498b16a364b5b4d00377485370ccc4d09ac0fc072e85b31d + # │ └── temp + #    └── https_other.clearlinux.update_site + #    ├── delta + #    ├── download + #    ├── manifest + #    │   ├── 32900 + #    │   │   ├── Manifest.MoM + #    │   │   └── Manifest.MoM.sig + #    │   └── Manifest-qt-basic-delta-from-33010-to-33140 + #    ├── pack-lib-imageformat-from-33090-to-33170.tar + #    ├── staged + #    │ └── ffaf66dab58a2074e5fe9dc6ab7bf8773c069817dcccf631148b6afc87166b4b + # └── temp + + # + # │ + # ├── bundles + # │   ├── editors + # │   ├── os-core + # │   └── vim + # ├── telemetry + # │   ├── 2.packmissing.1.Zh11gI + # │   └── 2.verify.1.4hR0jx + # ├── swupd_lock + # └── version + + sudo mkdir -p "$STATEDIR_CACHE"/{delta,download,manifest,staged} + sudo chmod 0700 "$STATEDIR_CACHE"/staged + sudo mkdir -p "$STATEDIR_MANIFEST"/{32900,33000} + + MOM=$(create_manifest "$STATEDIR_MANIFEST"/33000 MoM 1 32900) + write_to_protected_file -a "$MOM" "M... dfd541b1ac9256982044848f2128f51e23ddbb72f68e9de43adaf8f7ead93657 32900 p11-kit\n" + write_to_protected_file -a "$MOM" "M... 7414710480607fa89c307ad11d145d8bb0cbdbdf3c0692f940d906b670f6fd5d 33000 emacs\n" + MOM_SIG="$MOM".sig ; sudo touch "$MOM_SIG" + + FILE1="$STATEDIR_MANIFEST"/32900/Manifest.p11-kit ; sudo touch "$FILE1" + FILE2="$STATEDIR_MANIFEST"/32900/Manifest.p11-kit.dfd541b1ac9256982044848f2128f51e23ddbb72f68e9de43adaf8f7ead93657 ; sudo touch "$FILE2" + FILE3="$STATEDIR_MANIFEST"/33000/Manifest.emacs ; sudo touch "$FILE3" + FILE4="$STATEDIR_MANIFEST"/33000/Manifest.emacs.7414710480607fa89c307ad11d145d8bb0cbdbdf3c0692f940d906b670f6fd5d ; sudo touch "$FILE4" + FILE5="$STATEDIR_MANIFEST"/Manifest-emacs-delta-from-32900-to-33000 ; sudo touch "$FILE5" + FILE6="$STATEDIR_MANIFEST"/Manifest-vim-delta-from-32900-to-33000 ; sudo touch "$FILE6" + FILE7="$STATEDIR_CACHE"/pack-blender-from-0-to-33000.tar ; sudo touch "$FILE7" + FILE8="$STATEDIR_CACHE"/pack-vim-from-32800-to-32900.tar ; sudo touch "$FILE8" + FILE9="$STATEDIR_STAGED"/00004ead4b4eb1cef1ebbe13849f6c47c21d325182f98cf8f5333e9a76d2e2df ; sudo touch "$FILE9" + FILE10="$STATEDIR_STAGED"/fffe891980af628e498b16a364b5b4d00377485370ccc4d09ac0fc072e85b31d ; sudo touch "$FILE10" + # second url cache + sudo mkdir -p "$STATEDIR_ABS"/cache/https_other.clearlinux.update_site/{delta,download,manifest,staged,temp} + sudo mkdir -p "$STATEDIR_ABS"/cache/https_other.clearlinux.update_site/manifest/32900 + FILE11="$STATEDIR_ABS"/cache/https_other.clearlinux.update_site/pack-lib-imageformat-from-33090-to-33170.tar ; sudo touch "$FILE11" + FILE12="$STATEDIR_ABS"/cache/https_other.clearlinux.update_site/manifest/32900/Manifest.MoM ; sudo touch "$FILE12" + FILE13="$STATEDIR_ABS"/cache/https_other.clearlinux.update_site/manifest/32900/Manifest.MoM.sig ; sudo touch "$FILE13" + FILE14="$STATEDIR_ABS"/cache/https_other.clearlinux.update_site/manifest/Manifest-qt-basic-delta-from-33010-to-33140 ; sudo touch "$FILE14" + FILE15="$STATEDIR_ABS"/cache/https_other.clearlinux.update_site/staged/ffaf66dab58a2074e5fe9dc6ab7bf8773c069817dcccf631148b6afc87166b4b ; sudo touch "$FILE15" + +} + +@test "CLN001: Clean cached files except manifests" { + + run sudo sh -c "$SWUPD clean $SWUPD_OPTS" + + assert_status_is "$SWUPD_OK" + expected_output=$(cat <<-EOM + 24 files removed + .* KB freed + EOM + ) + assert_regex_is_output "$expected_output" + + # relevant manifests should still exist + assert_file_exists "$STATEDIR_MANIFEST"/32900/Manifest.p11-kit + assert_file_exists "$STATEDIR_MANIFEST"/33000/Manifest.emacs + assert_file_exists "$STATEDIR_MANIFEST"/33000/Manifest.MoM + assert_file_exists "$STATEDIR_MANIFEST"/33000/Manifest.MoM.sig + + # the rest of the files should not exist + assert_file_not_exists "$FILE2" + assert_file_not_exists "$FILE4" + assert_file_not_exists "$FILE5" + assert_file_not_exists "$FILE6" + assert_file_not_exists "$FILE7" + assert_file_not_exists "$FILE8" + assert_dir_not_exists "$STATEDIR_STAGED" + assert_dir_not_exists "$STATEDIR_DOWNLOAD" + assert_dir_not_exists "$STATEDIR_DELTA" + assert_dir_not_exists "$STATEDIR_ABS"/cache/https_other.clearlinux.update_site + +} + +@test "CLN002: Clean all cached files including manifests" { + + run sudo sh -c "$SWUPD clean $SWUPD_OPTS --all" + + assert_status_is "$SWUPD_OK" + expected_output=$(cat <<-EOM + 33 files removed + .* KB freed + EOM + ) + assert_regex_is_output "$expected_output" + + # everything should have been cleaned + assert_dir_not_exists "$STATEDIR_ABS"/cache + +} + +@test "CLN003: Dry run clean to show the files that would be removed from the cache" { + + run sudo sh -c "$SWUPD clean $SWUPD_OPTS --dry-run" + + assert_status_is "$SWUPD_OK" + expected_output=$(cat <<-EOM + $STATEDIR_ABS/cache/https_other.clearlinux.update_site + Would remove 24 files + Aproximatelly .* KB would be freed + EOM + ) + assert_regex_in_output "$expected_output" + + # files should not be deleted, just printed + assert_file_exists "$FILE1" + assert_file_exists "$FILE2" + assert_file_exists "$FILE3" + assert_file_exists "$FILE4" + assert_file_exists "$FILE5" + assert_file_exists "$FILE6" + assert_file_exists "$FILE7" + assert_file_exists "$FILE8" + assert_file_exists "$FILE9" + assert_file_exists "$FILE10" + assert_file_exists "$FILE11" + assert_file_exists "$FILE12" + assert_file_exists "$FILE13" + assert_file_exists "$FILE14" + assert_file_exists "$FILE15" + assert_file_exists "$MOM" + assert_file_exists "$MOM_SIG" + +} diff --git a/test/functional/clean/clean-grouped-manifests.bats b/test/functional/clean/clean-grouped-manifests.bats new file mode 100755 index 00000000..18eff501 --- /dev/null +++ b/test/functional/clean/clean-grouped-manifests.bats @@ -0,0 +1,152 @@ +#!/usr/bin/env bats + +# Author: Castulo Martinez +# Email: castulo.martinez@intel.com + +load "../testlib" + +test_setup() { + + create_test_environment "$TEST_NAME" + set_current_version "$TEST_NAME" 33000 + + # ################################################# + # statedir structure 4.2.1 (with grouped manifests) + # ################################################# + # + # ├── manifest + # │   ├── 32900 + # │   │   ├── Manifest.editors + # │   │   └── Manifest.editors.8f7aca1643c1cd8b109774b65c83c0dad5a2d259348343a9395fd63f71640c63 + # │   ├── 33000 + # │ │ ├── Manifest.MoM + # │ │ ├── Manifest.MoM.sig + # │   │   ├── Manifest.emacs + # │   │   └── Manifest.emacs.7414710480607fa89c307ad11d145d8bb0cbdbdf3c0692f940d906b670f6fd5d + # │   └── Manifest-emacs-delta-from-32900-to-33000 + # ├── delta + # ├── download + # ├── pack-vim-from-32800-to-32900.tar + # ├── staged + # │   ├── 00004ead4b4eb1cef1ebbe13849f6c47c21d325182f98cf8f5333e9a76d2e2df + # │   └── fffe891980af628e498b16a364b5b4d00377485370ccc4d09ac0fc072e85b31d + # │ + + # 3rd-party has both, data and cache + + # │ + # ├── 3rd-party + # │ + + # From here on it is just data + + # │ + # ├── bundles + # │   ├── editors + # │   ├── emacs + # │   ├── os-core + # │   └── vim + # ├── telemetry + # │   ├── 2.update.1.Dpjz7G + # │   └── 2.verify.1.4hR0jx + # ├── swupd_lock + # └── version + + sudo mkdir -p "$STATEDIR_ABS"/manifest/{32900,33000} + sudo mkdir -p "$STATEDIR_ABS"/{staged,delta,download} + + MOM="$STATEDIR_ABS"/manifest/33000/Manifest.MoM ; sudo touch "$MOM" + MOM_SIG="$STATEDIR_ABS"/manifest/33000/Manifest.MoM.sig ; sudo touch "$MOM_SIG" + FILE1="$STATEDIR_ABS"/manifest/32900/Manifest.editors ; sudo touch "$FILE1" + FILE2="$STATEDIR_ABS"/manifest/32900/Manifest.editors.8f7aca1643c1cd8b109774b65c83c0dad5a2d259348343a9395fd63f71640c63 ; sudo touch "$FILE2" + FILE3="$STATEDIR_ABS"/manifest/33000/Manifest.emacs ; sudo touch "$FILE3" + FILE4="$STATEDIR_ABS"/manifest/33000/Manifest.emacs.7414710480607fa89c307ad11d145d8bb0cbdbdf3c0692f940d906b670f6fd5d ; sudo touch "$FILE4" + FILE5="$STATEDIR_ABS"/manifest/Manifest-emacs-delta-from-32900-to-33000 ; sudo touch "$FILE5" + FILE6="$STATEDIR_ABS"/pack-vim-from-32800-to-32900.tar ; sudo touch "$FILE6" + FILE7="$STATEDIR_ABS"/staged/00004ead4b4eb1cef1ebbe13849f6c47c21d325182f98cf8f5333e9a76d2e2df ; sudo touch "$FILE7" + FILE8="$STATEDIR_ABS"/staged/fffe891980af628e498b16a364b5b4d00377485370ccc4d09ac0fc072e85b31d ; sudo touch "$FILE8" + + sudo chmod -R 0700 "$STATEDIR_ABS" + +} + +@test "CLN006: Clean cached files in the 4.2.1 statedir structure" { + + # during release 4.2.1 swupd used a statedir that contained both, cache + # and data, but all manifest related artifcts were grouped within the + # "manifest" directory. We need to make sure we are still cleaning those + # files regardless of if the user used the --all flag or not since that + # cache is not going to be used anymore anyway. + + run sudo sh -c "$SWUPD clean $SWUPD_OPTS" + + assert_status_is "$SWUPD_OK" + expected_output=$(cat <<-EOM + 20 files removed + .* KB freed + EOM + ) + assert_regex_is_output "$expected_output" + + # these old directories should be removed since are no longer used + assert_dir_not_exists "$STATEDIR_ABS"/delta + assert_dir_not_exists "$STATEDIR_ABS"/download + assert_dir_not_exists "$STATEDIR_ABS"/staged + assert_dir_not_exists "$STATEDIR_ABS"/manifest + + # files in the old structure should no longer be there either + assert_file_not_exists "$STATEDIR_ABS"/Manifest-emacs-delta-from-32900-to-33000 + assert_file_not_exists "$STATEDIR_ABS"/pack-vim-from-32800-to-32900.tar + +} + +@test "CLN007: Dry run includes files in the 4.2.1 statedir structure" { + + # during release 4.2.1 swupd used a statedir that contained both, cache + # and data, but all manifest related artifcts were grouped within the + # "manifest" directory. If the user runs a "clean --dry-run" we should + # include those files in the list of things that will be removed. + + run sudo sh -c "$SWUPD clean $SWUPD_OPTS --dry-run" + + assert_status_is "$SWUPD_OK" + expected_output=$(cat <<-EOM + $STATEDIR_DELTA + $STATEDIR_DOWNLOAD + $STATEDIR_STAGED + $STATEDIR_TEMP + $STATEDIR_ABS/pack-vim-from-32800-to-32900.tar + $STATEDIR_ABS/staged/.* + $STATEDIR_ABS/staged/.* + $STATEDIR_ABS/staged + $STATEDIR_ABS/manifest/Manifest-emacs-delta-from-32900-to-33000 + $STATEDIR_ABS/manifest/32900/Manifest\\..* + $STATEDIR_ABS/manifest/32900/Manifest\\..* + $STATEDIR_ABS/manifest/32900 + $STATEDIR_ABS/manifest/33000/Manifest\\..* + $STATEDIR_ABS/manifest/33000/Manifest\\..* + $STATEDIR_ABS/manifest/33000/Manifest\\..* + $STATEDIR_ABS/manifest/33000/Manifest\\..* + $STATEDIR_ABS/manifest/33000 + $STATEDIR_ABS/manifest + $STATEDIR_ABS/delta + $STATEDIR_ABS/download + Would remove 20 files + Aproximatelly .* KB would be freed + EOM + ) + assert_regex_is_output "$expected_output" + + # non of the files should be removed since it was just a dry run + assert_file_exists "$MOM" + assert_file_exists "$MOM_SIG" + assert_file_exists "$FILE1" + assert_file_exists "$FILE2" + assert_file_exists "$FILE3" + assert_file_exists "$FILE4" + assert_file_exists "$FILE5" + assert_file_exists "$FILE6" + assert_file_exists "$FILE7" + assert_file_exists "$FILE8" + +} diff --git a/test/functional/clean/clean-old-statedir.bats b/test/functional/clean/clean-old-statedir.bats new file mode 100755 index 00000000..112094bb --- /dev/null +++ b/test/functional/clean/clean-old-statedir.bats @@ -0,0 +1,147 @@ +#!/usr/bin/env bats + +# Author: Castulo Martinez +# Email: castulo.martinez@intel.com + +load "../testlib" + +test_setup() { + + create_test_environment "$TEST_NAME" + set_current_version "$TEST_NAME" 33000 + + # ########################### + # statedir structure <= 4.2.0 + # ########################### + # + # ├── 32900 + # │   ├── Manifest.p11-kit + # │   └── Manifest.p11-kit.dfd541b1ac9256982044848f2128f51e23ddbb72f68e9de43adaf8f7ead93657 + # ├── 33000 + # │ ├── Manifest.MoM + # │ ├── Manifest.MoM.sig + # │   ├── Manifest.emacs + # │   └── Manifest.emacs.7414710480607fa89c307ad11d145d8bb0cbdbdf3c0692f940d906b670f6fd5d + # ├── delta + # ├── download + # ├── Manifest-emacs-delta-from-32900-to-33000 + # ├── pack-vim-from-32800-to-32900.tar + # ├── staged + # │   ├── 00004ead4b4eb1cef1ebbe13849f6c47c21d325182f98cf8f5333e9a76d2e2df + # │   └── fffe891980af628e498b16a364b5b4d00377485370ccc4d09ac0fc072e85b31d + + # 3rd-party has both, data and cache + + # │ + # ├── 3rd-party + # │ + + # From here on it is just data + + # │ + # ├── bundles + # │   ├── editors + # │   ├── os-core + # │   └── vim + # ├── telemetry + # │   ├── 2.packmissing.1.Zh11gI + # │   └── 2.verify.1.4hR0jx + # ├── swupd_lock + # └── version + + sudo mkdir -p "$STATEDIR_ABS"/{32900,33000,delta,download,staged} + + MOM="$STATEDIR_ABS"/33000/Manifest.MoM ; sudo touch "$MOM" + MOM_SIG="$STATEDIR_ABS"/33000/Manifest.MoM.sig ; sudo touch "$MOM_SIG" + FILE1="$STATEDIR_ABS"/32900/Manifest.p11-kit ; sudo touch "$FILE1" + FILE2="$STATEDIR_ABS"/32900/Manifest.p11-kit.dfd541b1ac9256982044848f2128f51e23ddbb72f68e9de43adaf8f7ead93657 ; sudo touch "$FILE2" + FILE3="$STATEDIR_ABS"/33000/Manifest.emacs ; sudo touch "$FILE3" + FILE4="$STATEDIR_ABS"/33000/Manifest.emacs.7414710480607fa89c307ad11d145d8bb0cbdbdf3c0692f940d906b670f6fd5d ; sudo touch "$FILE4" + FILE5="$STATEDIR_ABS"/Manifest-emacs-delta-from-32900-to-33000 ; sudo touch "$FILE5" + FILE6="$STATEDIR_ABS"/pack-vim-from-32800-to-32900.tar ; sudo touch "$FILE6" + FILE7="$STATEDIR_ABS"/staged/00004ead4b4eb1cef1ebbe13849f6c47c21d325182f98cf8f5333e9a76d2e2df ; sudo touch "$FILE7" + FILE8="$STATEDIR_ABS"/staged/fffe891980af628e498b16a364b5b4d00377485370ccc4d09ac0fc072e85b31d ; sudo touch "$FILE8" + + sudo chmod -R 0700 "$STATEDIR_ABS" + +} + +@test "CLN004: Clean cached files in the pre 4.2.1 statedir structure" { + + # before release 4.2.0 swupd used a statedir that contained both, cache + # and data. We need to make sure we are still cleaning that in case users + # have the old statedir structure still in their system. We should clean + # everything in the old structure regardless of if the user used the + # --all flag or not since that cache is not going to be used anymore anyway. + + run sudo sh -c "$SWUPD clean $SWUPD_OPTS" + + assert_status_is "$SWUPD_OK" + expected_output=$(cat <<-EOM + 19 files removed + .* KB freed + EOM + ) + assert_regex_is_output "$expected_output" + + # these old directories should be removed since are no longer used + assert_dir_not_exists "$STATEDIR_ABS"/delta + assert_dir_not_exists "$STATEDIR_ABS"/download + assert_dir_not_exists "$STATEDIR_ABS"/staged + assert_dir_not_exists "$STATEDIR_ABS"/32900 + assert_dir_not_exists "$STATEDIR_ABS"/33000 + + # files in the old structure should no longer be there either + assert_file_not_exists "$STATEDIR_ABS"/Manifest-emacs-delta-from-32900-to-33000 + assert_file_not_exists "$STATEDIR_ABS"/pack-vim-from-32800-to-32900.tar + +} + +@test "CLN005: Dry run includes files in the pre 4.2.1 statedir structure" { + + # before release 4.2.0 swupd used a statedir that contained both, cache + # and data. If the user runs a "clean --dry-run" we should include those + # files in the list of things that will be removed. + + run sudo sh -c "$SWUPD clean $SWUPD_OPTS --dry-run" + + assert_status_is "$SWUPD_OK" + expected_output=$(cat <<-EOM + $STATEDIR_DELTA + $STATEDIR_DOWNLOAD + $STATEDIR_STAGED + $STATEDIR_TEMP + $STATEDIR_ABS/pack-vim-from-32800-to-32900.tar + $STATEDIR_ABS/Manifest-emacs-delta-from-32900-to-33000 + $STATEDIR_ABS/staged/.* + $STATEDIR_ABS/staged/.* + $STATEDIR_ABS/staged + $STATEDIR_ABS/delta + $STATEDIR_ABS/download + $STATEDIR_ABS/32900/Manifest\\..* + $STATEDIR_ABS/32900/Manifest\\..* + $STATEDIR_ABS/32900 + $STATEDIR_ABS/33000/Manifest\\..* + $STATEDIR_ABS/33000/Manifest\\..* + $STATEDIR_ABS/33000/Manifest\\..* + $STATEDIR_ABS/33000/Manifest\\..* + $STATEDIR_ABS/33000 + Would remove 19 files + Aproximatelly .* KB would be freed + EOM + ) + assert_regex_is_output "$expected_output" + + # non of the files should be removed since it was just a dry run + assert_file_exists "$MOM" + assert_file_exists "$MOM_SIG" + assert_file_exists "$FILE1" + assert_file_exists "$FILE2" + assert_file_exists "$FILE3" + assert_file_exists "$FILE4" + assert_file_exists "$FILE5" + assert_file_exists "$FILE6" + assert_file_exists "$FILE7" + assert_file_exists "$FILE8" + +} diff --git a/test/functional/signature/permission-incorrect.bats b/test/functional/signature/permission-incorrect.bats index 28795233..dbedf1cb 100755 --- a/test/functional/signature/permission-incorrect.bats +++ b/test/functional/signature/permission-incorrect.bats @@ -71,6 +71,11 @@ test_setup() { assert_status_is 0 assert_in_output "700" + # staged directory should only be accesible by root + run sudo sh -c "stat -c '%a' $STATEDIR_STAGED" + assert_status_is 0 + assert_in_output "700" + } @test "SIG028: Check if state dir permissions are correct after execution even if they are wrong before" {