Skip to content

Commit

Permalink
Refactor test specialisations into RaidAndS3TestSpecs
Browse files Browse the repository at this point in the history
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
qris committed May 30, 2018
1 parent 0b6afea commit 2400f6c
Show file tree
Hide file tree
Showing 5 changed files with 298 additions and 236 deletions.
84 changes: 84 additions & 0 deletions lib/backupclient/ClientTestUtils.cpp
@@ -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());
}
91 changes: 91 additions & 0 deletions lib/backupclient/ClientTestUtils.h
@@ -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

44 changes: 0 additions & 44 deletions lib/backupstore/StoreTestUtils.cpp
Expand Up @@ -96,23 +96,6 @@ bool setup_test_unified()
return true;
}


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_unified()
{
Expand All @@ -132,33 +115,6 @@ bool teardown_test_unified()
return status;
}

//! 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;
}

std::vector<uint32_t> ExpectedRefCounts;
int bbstored_pid = 0, bbackupd_pid = 0, s3simulator_pid = 0;

Expand Down
26 changes: 0 additions & 26 deletions lib/backupstore/StoreTestUtils.h
Expand Up @@ -54,32 +54,6 @@ bool teardown_test_specialised(const std::string& spec_name,
TEST_THAT(teardown_test_unified()); \
TEARDOWN();

#define SETUP_TEST_SPECIALISED(name, control) \
SETUP_SPECIALISED(name); \
TEST_THAT_OR(setup_test_specialised(name, control), FAIL); \
try \
{ // left open for TEARDOWN_TEST_SPECIALISED()

#define _TEARDOWN_TEST_SPECIALISED(name, control, check_for_errors) \
TEST_THAT_OR(teardown_test_specialised(name, control, \
check_for_errors), FAIL); \
} \
catch (BoxException &e) \
{ \
BOX_WARNING("Specialised test failed with exception, cleaning up: " << \
name << ": " << e.what()); \
TEST_THAT_OR(teardown_test_specialised(name, control, \
check_for_errors), FAIL); \
throw; \
} \
TEARDOWN();

#define TEARDOWN_TEST_SPECIALISED(name, control) \
_TEARDOWN_TEST_SPECIALISED(name, control, true)

#define TEARDOWN_TEST_SPECIALISED_NO_CHECK(name, control) \
_TEARDOWN_TEST_SPECIALISED(name, control, false)

//! Holds the expected reference counts of each object.
extern std::vector<uint32_t> ExpectedRefCounts;

Expand Down

0 comments on commit 2400f6c

Please sign in to comment.