diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2015-07-14 23:44:31 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2015-07-19 15:47:56 +0100 |
commit | 71ed9d0d33acea71ad4cb42c124d605c42e2be0f (patch) | |
tree | 7f58abf9e27c654bcb49a505f674f027cd7381a2 | |
parent | Basic WIP support for Glacier2 routing (diff) | |
download | netfs-71ed9d0d33acea71ad4cb42c124d605c42e2be0f.tar.bz2 netfs-71ed9d0d33acea71ad4cb42c124d605c42e2be0f.tar.xz netfs-71ed9d0d33acea71ad4cb42c124d605c42e2be0f.zip |
Refactor mocked daemon and sandbox export into it's own files
-rw-r--r-- | netfs/daemon/daemon.cpp | 1 | ||||
-rw-r--r-- | netfs/unittests/Jamfile.jam | 2 | ||||
-rw-r--r-- | netfs/unittests/mockDaemon.cpp | 74 | ||||
-rw-r--r-- | netfs/unittests/mockDaemon.h | 35 | ||||
-rw-r--r-- | netfs/unittests/testCore.cpp | 75 |
5 files changed, 118 insertions, 69 deletions
diff --git a/netfs/daemon/daemon.cpp b/netfs/daemon/daemon.cpp index f703386..9296b72 100644 --- a/netfs/daemon/daemon.cpp +++ b/netfs/daemon/daemon.cpp @@ -67,6 +67,7 @@ void NetFSDaemon::stop() { adapter->deactivate(); + adapter = nullptr; } extern "C" { diff --git a/netfs/unittests/Jamfile.jam b/netfs/unittests/Jamfile.jam index af02411..ac6ec38 100644 --- a/netfs/unittests/Jamfile.jam +++ b/netfs/unittests/Jamfile.jam @@ -9,7 +9,7 @@ lib Ice ; path-constant me : . ; run - testCore.cpp + testCore.cpp mockDaemon.cpp : : : <define>BOOST_TEST_DYN_LINK diff --git a/netfs/unittests/mockDaemon.cpp b/netfs/unittests/mockDaemon.cpp new file mode 100644 index 0000000..4a735e8 --- /dev/null +++ b/netfs/unittests/mockDaemon.cpp @@ -0,0 +1,74 @@ +#include "mockDaemon.h" +#include <boost/filesystem/operations.hpp> + +#ifndef ROOT +#error "ROOT needs to be defined at compilation time" +#endif + +#define XSTR(s) STR(s) +#define STR(s) #s +const boost::filesystem::path RootDir(XSTR(ROOT)); +const boost::filesystem::path TestExportRoot(RootDir / "testExport"); + +MockDaemon::MockDaemon(const std::string & ep) : + NetFSDaemon(), + testEndpoint(ep) +{ +} + +NetFS::Daemon::ConfigurationPtr +MockDaemon::ReadConfiguration(const boost::filesystem::path &) const +{ + return new NetFS::Daemon::Configuration( + { + { "testvol", new NetFS::Daemon::Export(TestExportRoot.string()) } + }, + { + { "unittest", new NetFS::Daemon::Host(testEndpoint) } + }); +} + +MockDaemonHost::MockDaemonHost(const std::string & ep, const Ice::StringSeq & ps) : + testEndpoint(ep), + params(ps), + daemon(nullptr) +{ + boost::filesystem::remove_all(TestExportRoot); + boost::filesystem::create_directories(TestExportRoot); + restart(); +} + +MockDaemonHost::~MockDaemonHost() +{ + stop(); + boost::filesystem::remove_all(TestExportRoot); +} + +void +MockDaemonHost::stop() +{ + if (daemon) { + daemon->stop(); + delete daemon; + } + if (ic) { + ic->destroy(); + } +} + +void +MockDaemonHost::start() +{ + ic = Ice::initialize(params); + ic->getProperties()->setProperty("NetFSD.HostNameOverride", "unittest"); + daemon = new MockDaemon(testEndpoint); + daemon->start("NetFSDaemonAdapter", ic, {}); +} + +void +MockDaemonHost::restart() +{ + stop(); + start(); +} + diff --git a/netfs/unittests/mockDaemon.h b/netfs/unittests/mockDaemon.h new file mode 100644 index 0000000..d2afe18 --- /dev/null +++ b/netfs/unittests/mockDaemon.h @@ -0,0 +1,35 @@ +#ifndef MOCKDAEMON_H +#define MOCKDAEMON_H + +#include <daemon.h> + +class MockDaemon : public NetFSDaemon { + public: + MockDaemon(const std::string & ep); + + const std::string testEndpoint; + + protected: + virtual NetFS::Daemon::ConfigurationPtr ReadConfiguration(const boost::filesystem::path &) const override; +}; + +class MockDaemonHost { + public: + MockDaemonHost(const std::string & ep, const Ice::StringSeq & ps = {}); + ~MockDaemonHost(); + + void restart(); + + Ice::CommunicatorPtr ic; + + private: + void start(); + void stop(); + + const std::string testEndpoint; + Ice::StringSeq params; + NetFSDaemon * daemon; +}; + +#endif + diff --git a/netfs/unittests/testCore.cpp b/netfs/unittests/testCore.cpp index b19c453..440fd29 100644 --- a/netfs/unittests/testCore.cpp +++ b/netfs/unittests/testCore.cpp @@ -5,7 +5,7 @@ #include <boost/scope_exit.hpp> #include <Ice/ObjectAdapter.h> #include <Ice/Service.h> -#include <daemon.h> +#include "mockDaemon.h" #include <fuseApp.h> #include <boost/filesystem/path.hpp> @@ -16,50 +16,7 @@ #define XSTR(s) STR(s) #define STR(s) #s const boost::filesystem::path RootDir(XSTR(ROOT)); -const boost::filesystem::path TestExportRoot(RootDir / "testExport"); - -std::string testEndpoint("tcp -h localhost -p 12012"); - -class MockDaemon : public NetFSDaemon { - public: - MockDaemon() : NetFSDaemon() { } - - protected: - virtual NetFS::Daemon::ConfigurationPtr ReadConfiguration(const boost::filesystem::path &) const override - { - return new NetFS::Daemon::Configuration( - { - { "testvol", new NetFS::Daemon::Export(TestExportRoot.string()) } - }, - { - { "unittest", new NetFS::Daemon::Host(testEndpoint) } - }); - } -}; - -class MockDaemonHost { - public: - MockDaemonHost(const Ice::StringSeq & ps = {}) : - params(ps), - ic(Ice::initialize(params)), - daemon(new MockDaemon()) - { - ic->getProperties()->setProperty("NetFSD.HostNameOverride", "unittest"); - daemon->start("NetFSDaemonAdapter", ic, {}); - - } - - ~MockDaemonHost() - { - delete daemon; - ic->destroy(); - } - - Ice::StringSeq params; - Ice::CommunicatorPtr ic; - NetFSDaemon * daemon; -}; -typedef boost::shared_ptr<MockDaemonHost> MockDaemonHostPtr; +const std::string testEndpoint("tcp -h localhost -p 12012"); char * argv[] = { strdup((RootDir / ":testvol").string().c_str()), @@ -144,8 +101,8 @@ class FuseMockHost { class Core { public: Core() : + daemonHost(testEndpoint), ic(daemonHost.ic), - daemon(daemonHost.daemon), fuse(fuseHost.fuse) { } @@ -155,25 +112,9 @@ class Core { FuseMockHost fuseHost; Ice::CommunicatorPtr ic; - NetFSDaemon * const daemon; fuse_operations * fuse;; }; -class GlobalSandBox { - public: - GlobalSandBox() - { - boost::filesystem::remove_all(TestExportRoot); - boost::filesystem::create_directories(TestExportRoot); - } - ~GlobalSandBox() - { - boost::filesystem::remove_all(TestExportRoot); - } -}; - -BOOST_GLOBAL_FIXTURE(GlobalSandBox); - BOOST_FIXTURE_TEST_SUITE( NetfsCore, Core ) BOOST_AUTO_TEST_CASE ( daemonInitialised ) @@ -193,7 +134,7 @@ BOOST_AUTO_TEST_SUITE_END(); BOOST_AUTO_TEST_CASE ( createAndDaemonRestart ) { - MockDaemonHostPtr daemon(new MockDaemonHost()); + MockDaemonHost daemon(testEndpoint); FuseMockHost fuse; struct statvfs s; @@ -221,10 +162,8 @@ BOOST_AUTO_TEST_CASE ( createAndDaemonRestart ) BOOST_REQUIRE_EQUAL(0, fuse.fuse->fgetattr(fileName, &st, &fh)); BOOST_REQUIRE_EQUAL(st.st_size, sizeof(someData)); - BOOST_CHECKPOINT("Delete old daemon object"); - daemon.reset(); - BOOST_CHECKPOINT("Create new daemon object"); - daemon = MockDaemonHostPtr(new MockDaemonHost()); + BOOST_CHECKPOINT("Restart daemon object"); + daemon.restart(); BOOST_CHECKPOINT("Fetch file attributes again"); BOOST_REQUIRE_EQUAL(0, fuse.fuse->fgetattr(fileName, &st, &fh)); @@ -250,7 +189,7 @@ BOOST_AUTO_TEST_CASE ( withRouter ) strdup("--Ice.Default.Router=Glacier2/router:tcp -h localhost -p 4063") }; - MockDaemonHostPtr daemon(new MockDaemonHost()); + MockDaemonHost daemon(testEndpoint); FuseMockHost fuse(3, argv); struct statvfs s; |