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.

This commit is contained in:
Sebastian Messmer 2014-11-18 00:14:33 +01:00
parent 0a04a5ad80
commit d29a8d67ca
7 changed files with 90 additions and 70 deletions

View File

@ -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");
}

View File

@ -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);
};

View File

@ -5,10 +5,12 @@
#include <thread>
#include <csignal>
#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<char**>(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<char**>(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);
}

View File

@ -1,32 +0,0 @@
#include "Daemon.h"
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
using std::function;
Daemon::Daemon(function<void()> 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");
}
}

View File

@ -1,18 +0,0 @@
#pragma once
#ifndef TEST_TESTUTILS_DAEMON_H_
#define TEST_TESTUTILS_DAEMON_H_
#include <functional>
class Daemon {
public:
Daemon(std::function<void()> runnable);
void start();
void stop();
private:
std::function<void()> _runnable;
pid_t _child_pid;
};
#endif

View File

@ -0,0 +1,29 @@
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "FuseThread.h"
#include <csignal>
#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();
}

View File

@ -0,0 +1,24 @@
#pragma once
#ifndef TEST_TESTUTILS_FUSETHREAD_H_
#define TEST_TESTUTILS_FUSETHREAD_H_
#include <thread>
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