From d29a8d67caf26db134e941ec389f8859258f1e75 Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Tue, 18 Nov 2014 00:14:33 +0100 Subject: [PATCH] Make test cases run fuse in a parallel thread. For now, test cases don't use mock fs implementation, but real cryfs. This is to be changed soon. --- src/fspp/fuse/Fuse.cpp | 8 +++++- src/fspp/fuse/Fuse.h | 2 ++ src/test/fspp/fuse/FuseTest.cpp | 47 ++++++++++++++++++------------- src/test/testutils/Daemon.cpp | 32 --------------------- src/test/testutils/Daemon.h | 18 ------------ src/test/testutils/FuseThread.cpp | 29 +++++++++++++++++++ src/test/testutils/FuseThread.h | 24 ++++++++++++++++ 7 files changed, 90 insertions(+), 70 deletions(-) delete mode 100644 src/test/testutils/Daemon.cpp delete mode 100644 src/test/testutils/Daemon.h create mode 100644 src/test/testutils/FuseThread.cpp create mode 100644 src/test/testutils/FuseThread.h diff --git a/src/fspp/fuse/Fuse.cpp b/src/fspp/fuse/Fuse.cpp index 3d29333c..006918bb 100644 --- a/src/fspp/fuse/Fuse.cpp +++ b/src/fspp/fuse/Fuse.cpp @@ -206,13 +206,17 @@ Fuse::~Fuse() { } Fuse::Fuse(FilesystemImpl *impl) - :_impl(impl) { + :_impl(impl), _running(false) { } void Fuse::run(int argc, char **argv) { fuse_main(argc, argv, operations(), (void*)this); } +bool Fuse::running() const { + return _running; +} + int Fuse::getattr(const bf::path &path, struct stat *stbuf) { //printf("getattr(%s, _, _)\n", path.c_str()); try { @@ -494,10 +498,12 @@ int Fuse::fsyncdir(const bf::path &path, int datasync, fuse_file_info *fileinfo) void Fuse::init(fuse_conn_info *conn) { UNUSED(conn); + _running = true; //printf("init()\n"); } void Fuse::destroy() { + _running = false; //printf("destroy()\n"); } diff --git a/src/fspp/fuse/Fuse.h b/src/fspp/fuse/Fuse.h index 6b537408..ec67b976 100644 --- a/src/fspp/fuse/Fuse.h +++ b/src/fspp/fuse/Fuse.h @@ -21,6 +21,7 @@ public: virtual ~Fuse(); void run(int argc, char **argv); + bool running() const; int getattr(const boost::filesystem::path &path, struct stat *stbuf); int fgetattr(const boost::filesystem::path &path, struct stat *stbuf, fuse_file_info *fileinfo); @@ -55,6 +56,7 @@ public: private: FilesystemImpl *_impl; + bool _running; DISALLOW_COPY_AND_ASSIGN(Fuse); }; diff --git a/src/test/fspp/fuse/FuseTest.cpp b/src/test/fspp/fuse/FuseTest.cpp index 7e7eb9b2..54bc3871 100644 --- a/src/test/fspp/fuse/FuseTest.cpp +++ b/src/test/fspp/fuse/FuseTest.cpp @@ -5,10 +5,12 @@ #include #include +#include "cryfs_lib/CryDevice.h" +#include "test/testutils/FuseThread.h" + #include "fspp/fuse/Fuse.h" #include "fspp/impl/FilesystemImpl.h" #include "test/testutils/TempDir.h" -#include "test/testutils/Daemon.h" using namespace fspp; using namespace fspp::fuse; @@ -46,23 +48,24 @@ public: }; struct FuseTest: public ::testing::Test { - FuseTest(): _fuse_process([](){}), fsimpl(), fuse(&fsimpl), mountDir() { - _fuse_process = Daemon([this] () { - string dirpath = mountDir.path().native(); - int argc = 3; - const char *argv[] = {"test", "-f", dirpath.c_str()}; - fuse.run(argc, const_cast(argv)); - }); - _fuse_process.start(); - } - ~FuseTest() { - _fuse_process.stop(); + FuseTest(): crydevice(bf::path("/home/heinzi/cryfstest/root")), fsimpl(&crydevice), mountDir(), fuse(&fsimpl), fuse_thread(&fuse) { + string dirpath = mountDir.path().native(); + int argc = 3; + const char *argv[] = {"test", "-f", dirpath.c_str()}; + + fuse_thread.start(argc, const_cast(argv)); } - Daemon _fuse_process; - MockFilesystemImpl fsimpl; - Fuse fuse; + ~FuseTest() { + fuse_thread.stop(); + } + + //MockFilesystemImpl fsimpl; + cryfs::CryDevice crydevice; + FilesystemImpl fsimpl; TempDir mountDir; + Fuse fuse; + FuseThread fuse_thread; }; TEST_F(FuseTest, setupAndTearDown) { @@ -72,8 +75,14 @@ TEST_F(FuseTest, setupAndTearDown) { TEST_F(FuseTest, openFile) { const bf::path filename("/myfile"); - EXPECT_CALL(fsimpl, openFile(filename, O_RDWR)) - .WillOnce(Return(1)); - - ::open((mountDir.path() / filename).c_str(), O_RDWR); + //EXPECT_CALL(fsimpl, openFile(filename, O_RDWR)) + // .WillOnce(Return(1)); + auto realpath = mountDir.path() / filename; + printf("Opening %s\n", realpath.c_str()); + fflush(stdout); + sleep(10); + int fd = ::open(realpath.c_str(), O_RDWR); + printf("Descriptor: %d, errno: %d\n", fd, errno); + fflush(stdout); + sleep(10); } diff --git a/src/test/testutils/Daemon.cpp b/src/test/testutils/Daemon.cpp deleted file mode 100644 index 675e1273..00000000 --- a/src/test/testutils/Daemon.cpp +++ /dev/null @@ -1,32 +0,0 @@ -#include "Daemon.h" - -#include -#include -#include -#include - -using std::function; - -Daemon::Daemon(function runnable) - : _runnable(runnable), _child_pid(0) { -} - -void Daemon::start() { - _child_pid = fork(); - if (_child_pid == 0) { - _runnable(); - exit(0); - } -} - -void Daemon::stop() { - int retval = kill(_child_pid, SIGINT); - if (retval != 0) { - throw std::runtime_error("Failed killing child process"); - } - int status; - pid_t pid = waitpid(_child_pid, &status, 0); - if (pid != _child_pid) { - throw std::runtime_error("Failed waiting for child process to die"); - } -} diff --git a/src/test/testutils/Daemon.h b/src/test/testutils/Daemon.h deleted file mode 100644 index 36b861f9..00000000 --- a/src/test/testutils/Daemon.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once -#ifndef TEST_TESTUTILS_DAEMON_H_ -#define TEST_TESTUTILS_DAEMON_H_ - -#include - -class Daemon { -public: - Daemon(std::function runnable); - void start(); - void stop(); - -private: - std::function _runnable; - pid_t _child_pid; -}; - -#endif diff --git a/src/test/testutils/FuseThread.cpp b/src/test/testutils/FuseThread.cpp new file mode 100644 index 00000000..98b3626b --- /dev/null +++ b/src/test/testutils/FuseThread.cpp @@ -0,0 +1,29 @@ +#include +#include +#include +#include "FuseThread.h" +#include + +#include "fspp/fuse/Fuse.h" + +using std::thread; +using std::string; + +using fspp::fuse::Fuse; + +FuseThread::FuseThread(Fuse *fuse) + :_fuse(fuse) { +} + +void FuseThread::start(int argc, char *argv[]) { + _child = thread([this, argc, argv] () { + _fuse->run(argc, argv); + }); + //Wait until it is running (busy waiting is simple and doesn't hurt much here) + while(!_fuse->running()) {} +} + +void FuseThread::stop() { + pthread_kill(_child.native_handle(), SIGINT); + _child.join(); +} diff --git a/src/test/testutils/FuseThread.h b/src/test/testutils/FuseThread.h new file mode 100644 index 00000000..0af3929d --- /dev/null +++ b/src/test/testutils/FuseThread.h @@ -0,0 +1,24 @@ +#pragma once +#ifndef TEST_TESTUTILS_FUSETHREAD_H_ +#define TEST_TESTUTILS_FUSETHREAD_H_ + +#include + +namespace fspp { +namespace fuse { + class Fuse; +} +} + +class FuseThread { +public: + FuseThread(fspp::fuse::Fuse *fuse); + void start(int argc, char *argv[]); + void stop(); + +private: + fspp::fuse::Fuse *_fuse; + std::thread _child; +}; + +#endif