From 6d20e27d6b965b774e52ac1f3fbd58fc307c2465 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 16 Dec 2021 21:04:33 +0000 Subject: Factor FuseMountPoint mock into its own file --- netfs/unittests/Jamfile.jam | 2 +- netfs/unittests/mockMount.cpp | 72 ++++++++++++++++++++++++++++++++++++++ netfs/unittests/mockMount.h | 36 +++++++++++++++++++ netfs/unittests/testFuse.cpp | 80 +------------------------------------------ 4 files changed, 110 insertions(+), 80 deletions(-) create mode 100644 netfs/unittests/mockMount.cpp create mode 100644 netfs/unittests/mockMount.h diff --git a/netfs/unittests/Jamfile.jam b/netfs/unittests/Jamfile.jam index fc9d00b..e811c57 100644 --- a/netfs/unittests/Jamfile.jam +++ b/netfs/unittests/Jamfile.jam @@ -24,7 +24,7 @@ lib testMocks : ../fuse//netfs-client ../ice//netfs-api ..//adhocutil - BOOST_TEST_DYN_LINK + BOOST_TEST_NO_MAIN boost_utf ROOT=\"$(me)\" : : diff --git a/netfs/unittests/mockMount.cpp b/netfs/unittests/mockMount.cpp new file mode 100644 index 0000000..7655296 --- /dev/null +++ b/netfs/unittests/mockMount.cpp @@ -0,0 +1,72 @@ +#include "mockMount.h" +#include +#include + +const std::string testEndpoint("tcp -h localhost -p 12015"); +const std::string testUri("tcp://localhost:12015/testvol"); + +FuseMountPoint::FuseMountPoint() : + MockDaemonHost(::testEndpoint, {"--NetFSD.ConfigPath=" + (rootDir / "defaultDaemon.xml").string()}), + NetFS::FuseApp({::testUri}), fargs {} +{ + std::filesystem::remove_all(mntpnt); + std::filesystem::create_directory(mntpnt); + ::fuse_opt_add_arg(&fargs, selfExe.c_str()); + ::fuse_opt_add_arg(&fargs, "-onoauto_cache"); + ::fuse_opt_add_arg(&fargs, "-oattr_timeout=0"); + ::fuse_opt_add_arg(&fargs, "-oac_attr_timeout=0"); + fs = ::fuse_new(&fargs, &operations, sizeof(fuse_operations), this); + BOOST_REQUIRE(fs); +} + +void +FuseMountPoint::start() +{ + BOOST_REQUIRE(!th); + BOOST_REQUIRE_EQUAL(0, ::fuse_mount(fs, mntpnt.c_str())); + th = std::make_unique(::fuse_loop, fs); + BOOST_REQUIRE(th); +} + +FuseMountPoint::~FuseMountPoint() +{ + stop(); + std::filesystem::remove(mntpnt); + ::fuse_destroy(fs); + ::fuse_opt_free_args(&fargs); +} + +void +FuseMountPoint::stop() +{ + if (th) { + ::fuse_unmount(fs); + th->join(); + th.reset(); + } +} + +struct fuse_context * +FuseMountPoint::fuse_get_context() +{ + return ::fuse_get_context(); +} + +char * +FuseMountPoint::vstrdupf(const char * fmt, va_list args) +{ + char * out {}; + BOOST_REQUIRE_GE(vasprintf(&out, fmt, args), 0); + BOOST_REQUIRE(out); + return out; +} + +void +FuseMountPoint::vlogf(int, const char * fmt, va_list args) const noexcept +{ + std::unique_ptr msg(vstrdupf(fmt, args), std::free); + static std::mutex btm; + ScopeLock(btm) { + BOOST_TEST_MESSAGE(msg.get()); + } +} diff --git a/netfs/unittests/mockMount.h b/netfs/unittests/mockMount.h new file mode 100644 index 0000000..38fb2ee --- /dev/null +++ b/netfs/unittests/mockMount.h @@ -0,0 +1,36 @@ +#ifndef MOCKMOUNT_H +#define MOCKMOUNT_H + +#include "mockDaemon.h" +#include +#include +#include +#include +#include + +inline const std::filesystem::path mntpnt {binDir / "mnt"}; + +class DLL_PUBLIC FuseMountPoint : public MockDaemonHost, public NetFS::FuseApp { +public: + FuseMountPoint(); + + void start(); + + ~FuseMountPoint() override; + + void stop(); + + SPECIAL_MEMBERS_DELETE(FuseMountPoint); + + struct fuse_context * fuse_get_context() override; + + static char * vstrdupf(const char * fmt, va_list args); + + void vlogf(int, const char * fmt, va_list args) const noexcept override; + + struct fuse * fs; + struct fuse_args fargs; + std::unique_ptr th; +}; + +#endif diff --git a/netfs/unittests/testFuse.cpp b/netfs/unittests/testFuse.cpp index b0603b0..dd773b2 100644 --- a/netfs/unittests/testFuse.cpp +++ b/netfs/unittests/testFuse.cpp @@ -1,5 +1,6 @@ #define BOOST_TEST_MODULE TestNetFSFuse #include "mockDaemon.h" +#include "mockMount.h" #include #include #include @@ -12,85 +13,6 @@ #include #include -static const std::filesystem::path mntpnt {binDir / "mnt"}; -const std::string testEndpoint("tcp -h localhost -p 12015"); -const std::string testUri("tcp://localhost:12015/testvol"); - -class FuseMountPoint : public MockDaemonHost, public NetFS::FuseApp { -public: - FuseMountPoint() : - MockDaemonHost(::testEndpoint, {"--NetFSD.ConfigPath=" + (rootDir / "defaultDaemon.xml").string()}), - NetFS::FuseApp({::testUri}), fargs {} - { - std::filesystem::remove_all(mntpnt); - std::filesystem::create_directory(mntpnt); - ::fuse_opt_add_arg(&fargs, selfExe.c_str()); - ::fuse_opt_add_arg(&fargs, "-onoauto_cache"); - ::fuse_opt_add_arg(&fargs, "-oattr_timeout=0"); - ::fuse_opt_add_arg(&fargs, "-oac_attr_timeout=0"); - fs = ::fuse_new(&fargs, &operations, sizeof(fuse_operations), this); - BOOST_REQUIRE(fs); - } - - void - start() - { - BOOST_REQUIRE(!th); - BOOST_REQUIRE_EQUAL(0, ::fuse_mount(fs, mntpnt.c_str())); - th = std::make_unique(::fuse_loop, fs); - BOOST_REQUIRE(th); - } - - ~FuseMountPoint() override - { - stop(); - std::filesystem::remove(mntpnt); - ::fuse_destroy(fs); - ::fuse_opt_free_args(&fargs); - } - - void - stop() - { - if (th) { - ::fuse_unmount(fs); - th->join(); - th.reset(); - } - } - - SPECIAL_MEMBERS_DELETE(FuseMountPoint); - - struct fuse_context * - fuse_get_context() override - { - return ::fuse_get_context(); - } - - static inline char * - vstrdupf(const char * fmt, va_list args) - { - char * out {}; - BOOST_REQUIRE_GE(vasprintf(&out, fmt, args), 0); - BOOST_REQUIRE(out); - return out; - } - - void - vlogf(int, const char * fmt, va_list args) const noexcept override - { - std::unique_ptr msg(vstrdupf(fmt, args), std::free); - static std::mutex btm; - ScopeLock(btm) { - BOOST_TEST_MESSAGE(msg.get()); - } - } - - struct fuse * fs; - struct fuse_args fargs; - std::unique_ptr th; -}; - #define BOOST_CHECK_EQUAL_FIELD(left, right, field) BOOST_CHECK_EQUAL((left).field, (right).field); #define BOOST_CHECK_EQUAL_STAT(left, right) \ -- cgit v1.2.3