Skip to content

Commit

Permalink
tests: Move MockAppEngine implementation to a new file, allowing reuse
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Detsch <andre.detsch@foundries.io>
  • Loading branch information
detsch committed Jun 27, 2024
1 parent 43079a1 commit 7b89817
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 228 deletions.
42 changes: 3 additions & 39 deletions tests/apiclient_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,54 +24,18 @@
#include "fixtures/liteclienttest.cc"

using ::testing::NiceMock;
using ::testing::Return;

/**
* Class MockAppEngine
*
*/
class MockAppEngine : public AppEngine {
public:
MockAppEngine(bool default_behaviour = true) {
if (!default_behaviour) return;

ON_CALL(*this, fetch).WillByDefault(Return(true));
ON_CALL(*this, verify).WillByDefault(Return(true));
ON_CALL(*this, install).WillByDefault(Return(true));
ON_CALL(*this, run).WillByDefault(Return(true));
ON_CALL(*this, isFetched).WillByDefault(Return(true));
ON_CALL(*this, isRunning).WillByDefault(Return(true));
ON_CALL(*this, getRunningAppsInfo)
.WillByDefault(
Return(Utils::parseJSON("{\"app-07\": {\"services\": {\"nginx-07\": {\"hash\": "
"\"16e36b4ab48cb19c7100a22686f85ffcbdce5694c936bda03cb12a2cce88efcf\"}}}}")));
}

public:
MOCK_METHOD(AppEngine::Result, fetch, (const App& app), (override));
MOCK_METHOD(AppEngine::Result, verify, (const App& app), (override));
MOCK_METHOD(AppEngine::Result, install, (const App& app), (override));
MOCK_METHOD(AppEngine::Result, run, (const App& app), (override));
MOCK_METHOD(void, stop, (const App& app), (override));
MOCK_METHOD(void, remove, (const App& app), (override));
MOCK_METHOD(bool, isFetched, (const App& app), (const, override));
MOCK_METHOD(bool, isRunning, (const App& app), (const, override));
MOCK_METHOD(AppEngine::Apps, getInstalledApps, (), (const, override));
MOCK_METHOD(Json::Value, getRunningAppsInfo, (), (const, override));
MOCK_METHOD(void, prune, (const Apps& app), (override));
};

class ApiClientTest : public fixtures::ClientTest {
protected:
std::shared_ptr<LiteClient> createLiteClient(InitialVersion initial_version = InitialVersion::kOn,
boost::optional<std::vector<std::string>> apps = boost::none,
bool finalize = true) override {
app_engine_mock_ = std::make_shared<NiceMock<MockAppEngine>>();
app_engine_mock_ = std::make_shared<NiceMock<fixtures::MockAppEngine>>();
lite_client_ = ClientTest::createLiteClient(app_engine_mock_, initial_version, apps);
return lite_client_;
}

std::shared_ptr<NiceMock<MockAppEngine>>& getAppEngine() { return app_engine_mock_; }
std::shared_ptr<NiceMock<fixtures::MockAppEngine>>& getAppEngine() { return app_engine_mock_; }
bool resetEvents() { return getDeviceGateway().resetEvents(lite_client_->http_client); }
void tweakConf(Config& conf) override {
if (!pacman_type_.empty()) {
Expand All @@ -89,7 +53,7 @@ class ApiClientTest : public fixtures::ClientTest {
}

private:
std::shared_ptr<NiceMock<MockAppEngine>> app_engine_mock_;
std::shared_ptr<NiceMock<fixtures::MockAppEngine>> app_engine_mock_;
std::shared_ptr<LiteClient> lite_client_;
std::string pacman_type_;
};
Expand Down
42 changes: 3 additions & 39 deletions tests/boot_flag_mgmt_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,64 +14,28 @@
#include "fixtures/liteclienttest.cc"

using ::testing::NiceMock;
using ::testing::Return;

/**
* Class MockAppEngine
*
*/
class MockAppEngine : public AppEngine {
public:
MockAppEngine(bool default_behaviour = true) {
if (!default_behaviour) return;

ON_CALL(*this, fetch).WillByDefault(Return(true));
ON_CALL(*this, verify).WillByDefault(Return(true));
ON_CALL(*this, install).WillByDefault(Return(true));
ON_CALL(*this, run).WillByDefault(Return(true));
ON_CALL(*this, isFetched).WillByDefault(Return(true));
ON_CALL(*this, isRunning).WillByDefault(Return(true));
ON_CALL(*this, getRunningAppsInfo)
.WillByDefault(
Return(Utils::parseJSON("{\"app-07\": {\"services\": {\"nginx-07\": {\"hash\": "
"\"16e36b4ab48cb19c7100a22686f85ffcbdce5694c936bda03cb12a2cce88efcf\"}}}}")));
}

public:
MOCK_METHOD(AppEngine::Result, fetch, (const App& app), (override));
MOCK_METHOD(AppEngine::Result, verify, (const App& app), (override));
MOCK_METHOD(AppEngine::Result, install, (const App& app), (override));
MOCK_METHOD(AppEngine::Result, run, (const App& app), (override));
MOCK_METHOD(void, stop, (const App& app), (override));
MOCK_METHOD(void, remove, (const App& app), (override));
MOCK_METHOD(bool, isFetched, (const App& app), (const, override));
MOCK_METHOD(bool, isRunning, (const App& app), (const, override));
MOCK_METHOD(AppEngine::Apps, getInstalledApps, (), (const, override));
MOCK_METHOD(Json::Value, getRunningAppsInfo, (), (const, override));
MOCK_METHOD(void, prune, (const Apps& app), (override));
};

class BootFlagMgmtTest : public fixtures::ClientTest {
protected:
std::shared_ptr<LiteClient> createLiteClient(InitialVersion initial_version = InitialVersion::kOn,
boost::optional<std::vector<std::string>> apps = boost::none,
bool finalize = true) override {
app_engine_mock_ = std::make_shared<NiceMock<MockAppEngine>>();
app_engine_mock_ = std::make_shared<NiceMock<fixtures::MockAppEngine>>();

auto client =
ClientTest::createLiteClient(app_engine_mock_, initial_version, apps, "", boost::none, true, finalize);
boot_flag_mgr_->set("rollback_protection", rollback_protection_flag_);
return client;
}

std::shared_ptr<NiceMock<MockAppEngine>>& getAppEngine() { return app_engine_mock_; }
std::shared_ptr<NiceMock<fixtures::MockAppEngine>>& getAppEngine() { return app_engine_mock_; }
RollbackMode bootloader_type_{RollbackMode::kBootloaderNone};

protected:
std::string rollback_protection_flag_{"1"};

private:
std::shared_ptr<NiceMock<MockAppEngine>> app_engine_mock_;
std::shared_ptr<NiceMock<fixtures::MockAppEngine>> app_engine_mock_;
};

class BootFlagMgmtTestSuite : public BootFlagMgmtTest,
Expand Down
40 changes: 2 additions & 38 deletions tests/daemon_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,55 +19,19 @@
#include "fixtures/liteclienttest.cc"

using ::testing::NiceMock;
using ::testing::Return;

/**
* Class MockAppEngine
*
*/
class MockAppEngine : public AppEngine {
public:
MockAppEngine(bool default_behaviour = true) {
if (!default_behaviour) return;

ON_CALL(*this, fetch).WillByDefault(Return(true));
ON_CALL(*this, verify).WillByDefault(Return(true));
ON_CALL(*this, install).WillByDefault(Return(true));
ON_CALL(*this, run).WillByDefault(Return(true));
ON_CALL(*this, isFetched).WillByDefault(Return(true));
ON_CALL(*this, isRunning).WillByDefault(Return(true));
ON_CALL(*this, getRunningAppsInfo)
.WillByDefault(
Return(Utils::parseJSON("{\"app-07\": {\"services\": {\"nginx-07\": {\"hash\": "
"\"16e36b4ab48cb19c7100a22686f85ffcbdce5694c936bda03cb12a2cce88efcf\"}}}}")));
}

public:
MOCK_METHOD(AppEngine::Result, fetch, (const App& app), (override));
MOCK_METHOD(AppEngine::Result, verify, (const App& app), (override));
MOCK_METHOD(AppEngine::Result, install, (const App& app), (override));
MOCK_METHOD(AppEngine::Result, run, (const App& app), (override));
MOCK_METHOD(void, stop, (const App& app), (override));
MOCK_METHOD(void, remove, (const App& app), (override));
MOCK_METHOD(bool, isFetched, (const App& app), (const, override));
MOCK_METHOD(bool, isRunning, (const App& app), (const, override));
MOCK_METHOD(AppEngine::Apps, getInstalledApps, (), (const, override));
MOCK_METHOD(Json::Value, getRunningAppsInfo, (), (const, override));
MOCK_METHOD(void, prune, (const Apps& app), (override));
};

class DaemonTest : public fixtures::ClientTest {
protected:
std::shared_ptr<LiteClient> createLiteClient(InitialVersion initial_version = InitialVersion::kOn,
boost::optional<std::vector<std::string>> apps = boost::none,
bool finalize = true) override {
app_engine_mock_ = std::make_shared<NiceMock<MockAppEngine>>();
app_engine_mock_ = std::make_shared<NiceMock<fixtures::MockAppEngine>>();
lite_client_ = ClientTest::createLiteClient(app_engine_mock_, initial_version, apps);
return lite_client_;
}

private:
std::shared_ptr<NiceMock<MockAppEngine>> app_engine_mock_;
std::shared_ptr<NiceMock<fixtures::MockAppEngine>> app_engine_mock_;
std::shared_ptr<LiteClient> lite_client_;
std::string pacman_type_;
};
Expand Down
36 changes: 36 additions & 0 deletions tests/fixtures/liteclient/mockappengine.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Class MockAppEngine
*
*/
using ::testing::Return;

class MockAppEngine : public AppEngine {
public:
MockAppEngine(bool default_behaviour = true) {
if (!default_behaviour) return;

ON_CALL(*this, fetch).WillByDefault(Return(true));
ON_CALL(*this, verify).WillByDefault(Return(true));
ON_CALL(*this, install).WillByDefault(Return(true));
ON_CALL(*this, run).WillByDefault(Return(true));
ON_CALL(*this, isFetched).WillByDefault(Return(true));
ON_CALL(*this, isRunning).WillByDefault(Return(true));
ON_CALL(*this, getRunningAppsInfo)
.WillByDefault(
Return(Utils::parseJSON("{\"app-07\": {\"services\": {\"nginx-07\": {\"hash\": "
"\"16e36b4ab48cb19c7100a22686f85ffcbdce5694c936bda03cb12a2cce88efcf\"}}}}")));
}

public:
MOCK_METHOD(AppEngine::Result, fetch, (const App& app), (override));
MOCK_METHOD(AppEngine::Result, verify, (const App& app), (override));
MOCK_METHOD(AppEngine::Result, install, (const App& app), (override));
MOCK_METHOD(AppEngine::Result, run, (const App& app), (override));
MOCK_METHOD(void, stop, (const App& app), (override));
MOCK_METHOD(void, remove, (const App& app), (override));
MOCK_METHOD(bool, isFetched, (const App& app), (const, override));
MOCK_METHOD(bool, isRunning, (const App& app), (const, override));
MOCK_METHOD(AppEngine::Apps, getInstalledApps, (), (const, override));
MOCK_METHOD(Json::Value, getRunningAppsInfo, (), (const, override));
MOCK_METHOD(void, prune, (const Apps& app), (override));
};
1 change: 1 addition & 0 deletions tests/fixtures/liteclienttest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace fixtures {
#include "liteclient/tufrepomock.cc"
#include "liteclient/devicegatewaymock.cc"
#include "liteclient/boot_flag_mgr.cc"
#include "liteclient/mockappengine.cc"


class ClientTest :virtual public ::testing::Test {
Expand Down
42 changes: 3 additions & 39 deletions tests/liteclientHSM_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,56 +26,20 @@
#include "fixtures/liteclienthsmtest.cc"

using ::testing::NiceMock;
using ::testing::Return;

/**
* Class MockAppEngine
*
*/
class MockAppEngine : public AppEngine {
public:
MockAppEngine(bool default_behaviour = true) {
if (!default_behaviour) return;

ON_CALL(*this, fetch).WillByDefault(Return(true));
ON_CALL(*this, verify).WillByDefault(Return(true));
ON_CALL(*this, install).WillByDefault(Return(true));
ON_CALL(*this, run).WillByDefault(Return(true));
ON_CALL(*this, isFetched).WillByDefault(Return(true));
ON_CALL(*this, isRunning).WillByDefault(Return(true));
ON_CALL(*this, getRunningAppsInfo)
.WillByDefault(
Return(Utils::parseJSON("{\"app-07\": {\"services\": {\"test-factory\": {\"hash\": "
"\"7ca42b1567ca068dfd6a5392432a5a36700a4aa3e321922e91d974f832a2f243\"}}}}")));
}

public:
MOCK_METHOD(AppEngine::Result, fetch, (const App& app), (override));
MOCK_METHOD(AppEngine::Result, verify, (const App& app), (override));
MOCK_METHOD(AppEngine::Result, install, (const App& app), (override));
MOCK_METHOD(AppEngine::Result, run, (const App& app), (override));
MOCK_METHOD(void, stop, (const App& app), (override));
MOCK_METHOD(void, remove, (const App& app), (override));
MOCK_METHOD(bool, isFetched, (const App& app), (const, override));
MOCK_METHOD(bool, isRunning, (const App& app), (const, override));
MOCK_METHOD(AppEngine::Apps, getInstalledApps, (), (const, override));
MOCK_METHOD(Json::Value, getRunningAppsInfo, (), (const, override));
MOCK_METHOD(void, prune, (const Apps& app), (override));
};

class LiteClientHSMTest : public fixtures::ClientHSMTest {
protected:
std::shared_ptr<LiteClient> createLiteClient(InitialVersion initial_version = InitialVersion::kOn,
boost::optional<std::vector<std::string>> apps = boost::none,
bool finalize = true) override {
app_engine_mock_ = std::make_shared<NiceMock<MockAppEngine>>();
app_engine_mock_ = std::make_shared<NiceMock<fixtures::MockAppEngine>>();
return ClientHSMTest::createLiteClient(app_engine_mock_, initial_version, apps);
}

std::shared_ptr<NiceMock<MockAppEngine>>& getAppEngine() { return app_engine_mock_; }
std::shared_ptr<NiceMock<fixtures::MockAppEngine>>& getAppEngine() { return app_engine_mock_; }

private:
std::shared_ptr<NiceMock<MockAppEngine>> app_engine_mock_;
std::shared_ptr<NiceMock<fixtures::MockAppEngine>> app_engine_mock_;
};

/*----------------------------------------------------------------------------*/
Expand Down
41 changes: 3 additions & 38 deletions tests/liteclient_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,54 +29,19 @@ extern void UnsetFreeBlockNumb();
using ::testing::NiceMock;
using ::testing::Return;

/**
* Class MockAppEngine
*
*/
class MockAppEngine : public AppEngine {
public:
MockAppEngine(bool default_behaviour = true) {
if (!default_behaviour) return;

ON_CALL(*this, fetch).WillByDefault(Return(true));
ON_CALL(*this, verify).WillByDefault(Return(true));
ON_CALL(*this, install).WillByDefault(Return(true));
ON_CALL(*this, run).WillByDefault(Return(true));
ON_CALL(*this, isFetched).WillByDefault(Return(true));
ON_CALL(*this, isRunning).WillByDefault(Return(true));
ON_CALL(*this, getRunningAppsInfo)
.WillByDefault(
Return(Utils::parseJSON("{\"app-07\": {\"services\": {\"nginx-07\": {\"hash\": "
"\"16e36b4ab48cb19c7100a22686f85ffcbdce5694c936bda03cb12a2cce88efcf\"}}}}")));
}

public:
MOCK_METHOD(AppEngine::Result, fetch, (const App& app), (override));
MOCK_METHOD(AppEngine::Result, verify, (const App& app), (override));
MOCK_METHOD(AppEngine::Result, install, (const App& app), (override));
MOCK_METHOD(AppEngine::Result, run, (const App& app), (override));
MOCK_METHOD(void, stop, (const App& app), (override));
MOCK_METHOD(void, remove, (const App& app), (override));
MOCK_METHOD(bool, isFetched, (const App& app), (const, override));
MOCK_METHOD(bool, isRunning, (const App& app), (const, override));
MOCK_METHOD(AppEngine::Apps, getInstalledApps, (), (const, override));
MOCK_METHOD(Json::Value, getRunningAppsInfo, (), (const, override));
MOCK_METHOD(void, prune, (const Apps& app), (override));
};

class LiteClientTest : public fixtures::ClientTest {
protected:
std::shared_ptr<LiteClient> createLiteClient(InitialVersion initial_version = InitialVersion::kOn,
boost::optional<std::vector<std::string>> apps = boost::none,
bool finalize = true) override {
app_engine_mock_ = std::make_shared<NiceMock<MockAppEngine>>();
app_engine_mock_ = std::make_shared<NiceMock<fixtures::MockAppEngine>>();
return ClientTest::createLiteClient(app_engine_mock_, initial_version, apps, "", boost::none, true, finalize);
}

std::shared_ptr<NiceMock<MockAppEngine>>& getAppEngine() { return app_engine_mock_; }
std::shared_ptr<NiceMock<fixtures::MockAppEngine>>& getAppEngine() { return app_engine_mock_; }

private:
std::shared_ptr<NiceMock<MockAppEngine>> app_engine_mock_;
std::shared_ptr<NiceMock<fixtures::MockAppEngine>> app_engine_mock_;
};

class LiteClientTestMultiPacman : public LiteClientTest, public ::testing::WithParamInterface<std::string> {
Expand Down
Loading

0 comments on commit 7b89817

Please sign in to comment.