From 330a232445b9db00f63f3975a1338e7116bd570c Mon Sep 17 00:00:00 2001 From: Damian Pietruchowski Date: Fri, 7 Apr 2017 13:02:40 +0200 Subject: [PATCH] [SmokeTest] Fix restore backup When something goes wrong (for example unmount failed) then backup is not restored. After that fix, directory, which could not be removed, will be renamed to path with "trash" extension. User have to remove this directory manually.. Change-Id: Ie616f27f1bccbe2d5efbc31316f7b99cf6fec87b Signed-off-by: Damian Pietruchowski --- src/unit_tests/smoke_test.cc | 4 ++-- src/unit_tests/smoke_utils.cc | 42 +++++++++++++++++++++++++++++++++++------- src/unit_tests/smoke_utils.h | 4 ++-- 3 files changed, 39 insertions(+), 11 deletions(-) diff --git a/src/unit_tests/smoke_test.cc b/src/unit_tests/smoke_test.cc index 9b90c2a..35bdfbc 100644 --- a/src/unit_tests/smoke_test.cc +++ b/src/unit_tests/smoke_test.cc @@ -27,7 +27,7 @@ class SmokeEnvironment : public testing::Environment { } backups_ = SetupBackupDirectories(); for (auto& path : backups_) - BackupPath(path); + ASSERT_TRUE(BackupPath(path)); } void TearDown() override { ASSERT_TRUE(request_mode_ == ci::RequestMode::GLOBAL || @@ -35,7 +35,7 @@ class SmokeEnvironment : public testing::Environment { kGlobalUserUid != kTestUserId)); UninstallAllSmokeApps(request_mode_); for (auto& path : backups_) - RestorePath(path); + ASSERT_TRUE(RestorePath(path)); if (request_mode_ == ci::RequestMode::USER) ASSERT_TRUE(DeleteTestUser(kNormalUserName)); } diff --git a/src/unit_tests/smoke_utils.cc b/src/unit_tests/smoke_utils.cc index 2ef8402..ec50e82 100644 --- a/src/unit_tests/smoke_utils.cc +++ b/src/unit_tests/smoke_utils.cc @@ -729,7 +729,21 @@ ci::AppInstaller::Result Recover(const bf::path& recovery_file, return CallBackend(SIZEOFARRAY(argv), argv, type, mode); } -void BackupPath(const bf::path& path) { +namespace { + +boost::filesystem::path GetTrashPath(const boost::filesystem::path& path) { + return path.string() + ".trash"; +} + +} + +bool BackupPath(const bf::path& path) { + bf::path trash_path = GetTrashPath(path); + if (bf::exists(trash_path)) { + LOG(ERROR) << trash_path << " exists. Please remove " + << trash_path << " manually!"; + return false; + } bf::path backup_path = path.string() + ".bck"; std::cout << "Backup path: " << path << " to " << backup_path << std::endl; bs::error_code error; @@ -739,28 +753,42 @@ void BackupPath(const bf::path& path) { << " (" << error.message() << ")"; if (bf::exists(path)) { bf::rename(path, backup_path, error); - if (error) + if (error) { LOG(ERROR) << "Failed to setup test environment. Does some previous" << " test crashed? Path: " << backup_path << " should not exist."; + return false; + } assert(!error); } + return true; } -void RestorePath(const bf::path& path) { +bool RestorePath(const bf::path& path) { bf::path backup_path = path.string() + ".bck"; std::cout << "Restore path: " << path << " from " << backup_path << std::endl; bs::error_code error; bf::remove_all(path, error); - if (error) - LOG(ERROR) << "Remove failed: " << path - << " (" << error.message() << ")"; + if (error) { + bf::path trash_path = GetTrashPath(path); + LOG(ERROR) << "Remove failed: " << path << " (" << error.message() << ")"; + std::cout << "Moving " << path << " to " << trash_path << std::endl; + bf::rename(path, trash_path, error); + if (error) + LOG(ERROR) << "Failed to move " << path << " to " << trash_path + << " (" << error.message() << ")"; + else + LOG(ERROR) << trash_path << " should be removed manually!"; + } if (bf::exists(backup_path)) { bf::rename(backup_path, path, error); - if (error) + if (error) { LOG(ERROR) << "Failed to restore backup path: " << backup_path << " (" << error.message() << ")"; + return false; + } } + return true; } std::vector SetupBackupDirectories() { diff --git a/src/unit_tests/smoke_utils.h b/src/unit_tests/smoke_utils.h index 448b71f..808e19b 100644 --- a/src/unit_tests/smoke_utils.h +++ b/src/unit_tests/smoke_utils.h @@ -263,9 +263,9 @@ ci::AppInstaller::Result Recover(const bf::path& recovery_file, PackageType type, RequestResult mode = RequestResult::NORMAL); -void BackupPath(const bf::path& path); +bool BackupPath(const bf::path& path); -void RestorePath(const bf::path& path); +bool RestorePath(const bf::path& path); std::vector SetupBackupDirectories(); -- 2.7.4