Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor test specialisations into RaidAndS3TestSpecs
This moves most of the code required to setup a specialised test suite into a reusable class, and makes it easier to extend specialisations to other object attributes/accessors in future.
- Loading branch information
Showing
5 changed files
with
298 additions
and
236 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// -------------------------------------------------------------------------- | ||
// | ||
// File | ||
// Name: ClientTestUtils.cpp | ||
// Purpose: Utilities for specialised client tests | ||
// Created: 2018-05-29 | ||
// | ||
// -------------------------------------------------------------------------- | ||
|
||
#include "Box.h" | ||
|
||
#include <cstdio> | ||
#include <string> | ||
#include <vector> | ||
|
||
#include "autogen_BackupProtocol.h" | ||
#include "BackupAccountControl.h" | ||
#include "BackupDaemonConfigVerify.h" | ||
#include "BackupStoreConfigVerify.h" | ||
#include "BackupStoreConstants.h" | ||
#include "BackupStoreInfo.h" | ||
#include "BoxPortsAndFiles.h" | ||
#include "ClientTestUtils.h" | ||
#include "Logging.h" | ||
#include "StoreTestUtils.h" | ||
#include "Test.h" | ||
|
||
bool setup_test_specialised(const std::string& spec_name, | ||
BackupAccountControl& control) | ||
{ | ||
if (ServerIsAlive(bbstored_pid)) | ||
{ | ||
TEST_THAT_OR(StopServer(), FAIL); | ||
} | ||
|
||
ExpectedRefCounts.resize(BACKUPSTORE_ROOT_DIRECTORY_ID + 1); | ||
set_refcount(BACKUPSTORE_ROOT_DIRECTORY_ID, 1); | ||
|
||
TEST_THAT_OR(create_test_account_specialised(spec_name, control), FAIL); | ||
|
||
return true; | ||
} | ||
|
||
//! Checks account for errors and shuts down daemons at end of every test. | ||
bool teardown_test_specialised(const std::string& spec_name, | ||
BackupAccountControl& control, bool check_for_errors) | ||
{ | ||
bool status = true; | ||
|
||
if (ServerIsAlive(bbstored_pid)) | ||
{ | ||
TEST_THAT_OR(StopServer(), status = false); | ||
} | ||
|
||
BackupFileSystem& fs(control.GetFileSystem()); | ||
if(check_for_errors) | ||
{ | ||
TEST_THAT_OR(check_reference_counts( | ||
fs.GetPermanentRefCountDatabase(true)), // ReadOnly | ||
status = false); | ||
TEST_EQUAL_OR(0, check_account_and_fix_errors(fs), status = false); | ||
} | ||
|
||
// Release the lock (acquired by check_account_and_fix_errors if it wasn't already held) | ||
// before the next test's setUp deletes all files in the store, including the lockfile: | ||
fs.ReleaseLock(); | ||
|
||
return status; | ||
} | ||
|
||
RaidAndS3TestSpecs::RaidAndS3TestSpecs() | ||
{ | ||
auto ap_s3_config = load_config_file(DEFAULT_BBACKUPD_CONFIG_FILE, BackupDaemonConfigVerify); | ||
std::auto_ptr<BackupAccountControl> ap_s3_control(new S3BackupAccountControl(*ap_s3_config)); | ||
|
||
auto ap_store_config = load_config_file(DEFAULT_BBSTORED_CONFIG_FILE, BackupConfigFileVerify); | ||
std::auto_ptr<BackupAccountControl> ap_store_control( | ||
new BackupStoreAccountControl(*ap_store_config, 0x01234567)); | ||
|
||
mSpecialisations.push_back(Specialisation("s3", ap_s3_config, ap_s3_control)); | ||
mpS3 = &(*mSpecialisations.rbegin()); | ||
mSpecialisations.push_back(Specialisation("store", ap_store_config, ap_store_control)); | ||
mpStore = &(*mSpecialisations.rbegin()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// -------------------------------------------------------------------------- | ||
// | ||
// File | ||
// Name: ClientTestUtils.h | ||
// Purpose: Utilities for specialised client tests | ||
// Created: 2018-05-29 | ||
// | ||
// -------------------------------------------------------------------------- | ||
|
||
#ifndef CLIENTTESTUTILS__H | ||
#define CLIENTTESTUTILS__H | ||
|
||
#include <string> | ||
|
||
#include "Test.h" | ||
|
||
#define SETUP_TEST_SPECIALISED(spec) \ | ||
SETUP_SPECIALISED(spec.name()); \ | ||
TEST_THAT_OR(setup_test_specialised(spec.name(), spec.control()), FAIL); \ | ||
try \ | ||
{ // left open for TEARDOWN_TEST_SPECIALISED() | ||
|
||
#define _TEARDOWN_TEST_SPECIALISED(spec, check_for_errors) \ | ||
TEST_THAT_OR(teardown_test_specialised(spec.name(), spec.control(), \ | ||
check_for_errors), FAIL); \ | ||
} \ | ||
catch (BoxException &e) \ | ||
{ \ | ||
BOX_WARNING("Specialised test failed with exception, cleaning up: " << \ | ||
spec.name() << ": " << e.what()); \ | ||
TEST_THAT_OR(teardown_test_specialised(spec.name(), spec.control(), \ | ||
check_for_errors), FAIL); \ | ||
throw; \ | ||
} \ | ||
TEARDOWN(); | ||
|
||
#define TEARDOWN_TEST_SPECIALISED(spec) \ | ||
_TEARDOWN_TEST_SPECIALISED(spec, true) | ||
|
||
#define TEARDOWN_TEST_SPECIALISED_NO_CHECK(spec) \ | ||
_TEARDOWN_TEST_SPECIALISED(spec, false) | ||
|
||
class RaidAndS3TestSpecs | ||
{ | ||
public: | ||
class Specialisation | ||
{ | ||
private: | ||
std::string mName; | ||
std::auto_ptr<Configuration> mapConfig; | ||
std::auto_ptr<BackupAccountControl> mapControl; | ||
public: | ||
Specialisation(const std::string& name, std::auto_ptr<Configuration> config, | ||
std::auto_ptr<BackupAccountControl> control) | ||
: mName(name), | ||
mapConfig(config), | ||
mapControl(control) | ||
{ } | ||
Specialisation(Specialisation &&other) // move constructor | ||
: mName(other.mName), | ||
mapConfig(other.mapConfig), | ||
mapControl(other.mapControl) | ||
{ } | ||
const std::string& name() const { return mName; } | ||
Configuration& config() const { return *mapConfig; } | ||
BackupAccountControl& control() const { return *mapControl; } | ||
S3BackupAccountControl& s3_control() const | ||
{ | ||
return dynamic_cast<S3BackupAccountControl &>(*mapControl); | ||
} | ||
BackupStoreAccountControl& store_control() const | ||
{ | ||
return dynamic_cast<BackupStoreAccountControl &>(*mapControl); | ||
} | ||
}; | ||
|
||
private: | ||
std::list<Specialisation> mSpecialisations; | ||
Specialisation* mpS3; | ||
Specialisation* mpStore; | ||
|
||
public: | ||
std::list<Specialisation>& specs() { return mSpecialisations; } | ||
Specialisation& s3() const { return *mpS3; } | ||
Specialisation& store() const { return *mpStore; } | ||
|
||
RaidAndS3TestSpecs(); // constructor | ||
}; | ||
|
||
#endif // CLIENTTESTUTILS__H | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.