From 944745c21c7df19ada39e1d347ca732c1f424070 Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Thu, 4 Sep 2014 01:36:15 +0000 Subject: Define setup and teardown functions and macros, and use them. Merged back changes from the test refactor branch to reduce diffs. --- lib/backupstore/StoreTestUtils.h | 3 -- test/backupstore/testbackupstore.cpp | 89 +++++++++++++++++++----------------- test/bbackupd/testbbackupd.cpp | 58 +++++++++++++++++++---- 3 files changed, 95 insertions(+), 55 deletions(-) diff --git a/lib/backupstore/StoreTestUtils.h b/lib/backupstore/StoreTestUtils.h index 2fc7ca64..5fa1ccc6 100644 --- a/lib/backupstore/StoreTestUtils.h +++ b/lib/backupstore/StoreTestUtils.h @@ -17,9 +17,6 @@ class BackupProtocolClient; class SocketStreamTLS; class TLSContext; -//! Simplifies calling setUp() with the current function name in each test. -#define SETUP() if (!setUp(__FUNCTION__)) return true; // skip this test - //! Holds the expected reference counts of each object. extern std::vector ExpectedRefCounts; diff --git a/test/backupstore/testbackupstore.cpp b/test/backupstore/testbackupstore.cpp index 5792fec7..5781d111 100644 --- a/test/backupstore/testbackupstore.cpp +++ b/test/backupstore/testbackupstore.cpp @@ -239,6 +239,23 @@ void CheckEntries(BackupStoreDirectory &rDir, int16_t FlagsMustBeSet, int16_t Fl TEST_THAT(DIR_NUM == SkipEntries(e, FlagsMustBeSet, FlagsNotToBeSet)); } +//! Simplifies calling setUp() with the current function name in each test. +#define SETUP() if (!setUp(__FUNCTION__)) return true; // skip this test + +//! Checks account for errors and shuts down daemons at end of every test. +bool teardown_test_backupstore() +{ + bool status = tearDown(); + + if (FileExists("testfiles/0_0/backup/01234567/info.rf")) + { + TEST_THAT_OR(check_reference_counts(), status = false); + TEST_THAT_OR(check_account(), status = false); + } + + return status; +} + bool test_filename_encoding() { SETUP(); @@ -322,8 +339,7 @@ bool test_filename_encoding() } } - tearDown(); - return true; + return teardown_test_backupstore(); } bool test_backupstore_directory() @@ -409,7 +425,7 @@ bool test_backupstore_directory() } } - return tearDown(); + return teardown_test_backupstore(); } void write_test_file(int t) @@ -656,6 +672,8 @@ void recursive_count_objects_r(BackupProtocolCallable &protocol, int64_t id, } } +TLSContext context; + void recursive_count_objects(const char *hostname, int64_t id, recursive_count_objects_results &results) { // Context @@ -1096,9 +1114,8 @@ bool test_server_housekeeping() protocol.QueryFinished(); TEST_THAT(run_housekeeping_and_check_account()); - ExpectedRefCounts.resize(3); // stop test failure in tearDown() - tearDown(); - return true; + ExpectedRefCounts.resize(3); // stop test failure in teardown_test_backupstore() + return teardown_test_backupstore(); } int64_t create_directory(BackupProtocolCallable& protocol, int64_t parent_dir_id) @@ -1174,7 +1191,6 @@ int64_t assert_readonly_connection_succeeds(BackupProtocolCallable& protocol) bool test_multiple_uploads() { - TLSContext context; SETUP(); TEST_THAT_THROWONFAIL(StartServer()); @@ -1806,8 +1822,7 @@ bool test_multiple_uploads() apProtocol->QueryFinished(); } - tearDown(); - return true; + return teardown_test_backupstore(); } int get_object_size(BackupProtocolCallable& protocol, int64_t ObjectID, @@ -1988,8 +2003,8 @@ bool test_directory_parent_entry_tracks_directory_size() BACKUPSTORE_ROOT_DIRECTORY_ID)); protocolReadOnly.QueryFinished(); - tearDown(); - return true; + + return teardown_test_backupstore(); } bool test_cannot_open_multiple_writable_connections() @@ -2024,8 +2039,8 @@ bool test_cannot_open_multiple_writable_connections() TEST_EQUAL(0x8732523ab23aLL, assert_readonly_connection_succeeds(protocolReadOnly2)); - tearDown(); - return true; + protocolWritable.QueryFinished(); + return teardown_test_backupstore(); } bool test_encoding() @@ -2185,8 +2200,7 @@ bool test_encoding() } } - tearDown(); - return true; + return teardown_test_backupstore(); } bool test_symlinks() @@ -2213,7 +2227,7 @@ bool test_symlinks() UNLINK_IF_EXISTS("testfiles/testsymlink_2"); BackupStoreFile::DecodeFile(b, "testfiles/testsymlink_2", IOStream::TimeOutInfinite); } - tearDown(); + teardown_test_backupstore(); #endif return true; @@ -2270,12 +2284,9 @@ bool test_store_info() TEST_THAT(delfiles[1] == 4); } - tearDown(); - return true; + return teardown_test_backupstore(); } -TLSContext context; - bool test_login_without_account() { // First, try logging in without an account having been created... just make sure login fails. @@ -2301,11 +2312,10 @@ bool test_login_without_account() protocol.QueryFinished(); } - // Recreate the account so that tearDown() doesn't freak out + // Recreate the account so that teardown_test_backupstore() doesn't freak out // TEST_THAT_THROWONFAIL(create_account(10000, 20000)); - tearDown(); - return true; + return teardown_test_backupstore(); } bool test_bbstoreaccounts_create() @@ -2320,8 +2330,7 @@ bool test_bbstoreaccounts_create() "10000B 20000B") == 0); TestRemoteProcessMemLeaks("bbstoreaccounts.memleaks"); - tearDown(); - return true; + return teardown_test_backupstore(); } bool test_bbstoreaccounts_delete() @@ -2331,11 +2340,10 @@ bool test_bbstoreaccounts_delete() " -c testfiles/bbstored.conf -Wwarning delete 01234567 yes") == 0); TestRemoteProcessMemLeaks("bbstoreaccounts.memleaks"); - // Recreate the account so that tearDown() doesn't freak out + // Recreate the account so that teardown_test_backupstore() doesn't freak out TEST_THAT_THROWONFAIL(create_account(10000, 20000)); - tearDown(); - return true; + return teardown_test_backupstore(); } // Test that login fails on a disabled account @@ -2383,8 +2391,7 @@ bool test_login_with_disabled_account() protocol.QueryFinished(); } - tearDown(); - return true; + return teardown_test_backupstore(); } bool test_login_with_no_refcount_db() @@ -2448,8 +2455,7 @@ bool test_login_with_no_refcount_db() run_housekeeping(account); apReferences = BackupStoreRefCountDatabase::Load(account, true); - tearDown(); - return true; + return teardown_test_backupstore(); } bool test_housekeeping_deletes_files() @@ -2517,16 +2523,15 @@ bool test_housekeeping_deletes_files() TEST_EQUAL(0, after.old); // Adjust reference counts on deleted files, so that the final checks in - // tearDown() don't fail. + // teardown_test_backupstore() don't fail. ExpectedRefCounts.resize(2); - // Delete the account to stop tearDown from checking it. TODO FIXME - // I'm aware of the block count mismatch that tearDown catches and - // will investigate. + // Delete the account to stop teardown_test_backupstore from checking it. + // TODO FIXME investigate the block count mismatch that teardown_test_backupstore + // catches if we don't delete the account. delete_account(); - tearDown(); - return true; + return teardown_test_backupstore(); } bool test_account_limits_respected() @@ -2574,8 +2579,7 @@ bool test_account_limits_respected() protocol.QueryFinished(); } - tearDown(); - return true; + return teardown_test_backupstore(); } int multi_server() @@ -2914,14 +2918,13 @@ bool test_read_old_backupstoreinfo_files() "BackupStoreInfo::CreateForRegeneration and reloaded", info_v1, *apInfo, "spurtle", false /* AccountEnabled */, extra_data); - // Delete the account to stop tearDown checking it for errors. + // Delete the account to stop teardown_test_backupstore checking it for errors. apInfo.reset(); TEST_THAT_ABORTONFAIL(::system(BBSTOREACCOUNTS " -c testfiles/bbstored.conf delete 01234567 yes") == 0); TestRemoteProcessMemLeaks("bbstoreaccounts.memleaks"); - tearDown(); - return true; + return teardown_test_backupstore(); } int test(int argc, const char *argv[]) diff --git a/test/bbackupd/testbbackupd.cpp b/test/bbackupd/testbbackupd.cpp index 304e5fec..a6d0da75 100644 --- a/test/bbackupd/testbbackupd.cpp +++ b/test/bbackupd/testbbackupd.cpp @@ -437,8 +437,48 @@ bool kill_running_daemons() return true; } +bool setup_test_bbackupd(BackupDaemon& bbackupd, bool do_unpack_files = true, + bool do_start_bbstored = true) +{ + Timers::Cleanup(false); // don't throw exception if not initialised + Timers::Init(); + + if (do_start_bbstored) + { + TEST_THAT_OR(StartServer(), FAIL); + } + + if (do_unpack_files) + { + TEST_THAT_OR(unpack_files("test_base"), FAIL); + } + + TEST_THAT_OR(configure_bbackupd(bbackupd, "testfiles/bbackupd.conf"), + FAIL); + spDaemon = &bbackupd; + return true; +} + int num_tests_selected = 0; +//! Simplifies calling setUp() with the current function name in each test. +#define SETUP() \ + TEST_THAT(kill_running_daemons()); \ + if (!setUp(__FUNCTION__)) return true; \ + num_tests_selected++; \ + int old_failure_count = failures; + +#define SETUP_WITHOUT_FILES() \ + SETUP() \ + BackupDaemon bbackupd; \ + TEST_THAT_OR(setup_test_bbackupd(bbackupd, false), FAIL); \ + TEST_THAT_OR(::mkdir("testfiles/TestDir1", 0755) == 0, FAIL); + +#define SETUP_WITH_BBSTORED() \ + SETUP() \ + BackupDaemon bbackupd; \ + TEST_THAT_OR(setup_test_bbackupd(bbackupd), FAIL); + //! Checks account for errors and shuts down daemons at end of every test. bool teardown_test_bbackupd(std::string test_name, int old_failure_count) { @@ -461,8 +501,13 @@ bool teardown_test_bbackupd(std::string test_name, int old_failure_count) return tearDown(); } +#define TEARDOWN() \ + return teardown_test_bbackupd(__FUNCTION__, old_failure_count); + bool test_basics() { + SETUP(); + // Read attributes from files BackupClientFileAttributes t1; t1.ReadAttributes("testfiles/test1"); @@ -578,7 +623,7 @@ bool test_basics() finish_with_write_xattr_test(); #endif // HAVE_SYS_XATTR_H - return false; + TEARDOWN(); } int64_t GetDirID(BackupProtocolCallable &protocol, const char *name, int64_t InDirectory) @@ -3941,11 +3986,6 @@ int test(int argc, const char *argv[]) "| ( cd testfiles && tar xf - )") == 0); #endif - // Do the tests - - int r = test_basics(); - if(r != 0) return r; - { std::string errs; std::auto_ptr config( @@ -3959,10 +3999,10 @@ int test(int argc, const char *argv[]) rcontroller.Initialise(config->GetKeyValue("RaidFileConf").c_str()); } - r = (create_account(1000, 2000) ? 0 : 1); - if(r != 0) return r; + // Do the tests + TEST_THAT(test_basics()); - r = (StartServer() ? 0 : 1); + int r = (StartServer() ? 0 : 1); TEST_THAT(r == 0); if(r != 0) return r; -- cgit v1.2.3