Written a test case setting up a file system and tearing it down again afterwards

This commit is contained in:
Sebastian Messmer 2014-11-17 21:13:58 +01:00
parent ff9a222e6a
commit 3371df2088
3 changed files with 120 additions and 0 deletions

View File

@ -0,0 +1,70 @@
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include <string>
#include <thread>
#include <csignal>
#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;
using std::string;
using std::unique_ptr;
using std::vector;
using std::thread;
class MockFilesystemImpl: public FilesystemImpl {
public:
MockFilesystemImpl(): FilesystemImpl(nullptr) {}
MOCK_METHOD2(openFile, int(const bf::path&, int));
MOCK_METHOD1(closeFile, void(int));
MOCK_METHOD2(lstat, void(const bf::path&, struct ::stat*));
MOCK_METHOD2(fstat, void(int, struct ::stat*));
MOCK_METHOD2(truncate, void(const bf::path&, off_t));
MOCK_METHOD2(ftruncate, void(int, off_t));
MOCK_METHOD4(read, int(int, void*, size_t, off_t));
MOCK_METHOD4(write, void(int, const void*, size_t, off_t));
MOCK_METHOD1(fsync, void(int));
MOCK_METHOD1(fdatasync, void(int));
MOCK_METHOD2(access, void(const bf::path&, int));
MOCK_METHOD2(createAndOpenFile, int(const bf::path&, mode_t));
MOCK_METHOD2(mkdir, void(const bf::path&, mode_t));
MOCK_METHOD1(rmdir, void(const bf::path&));
MOCK_METHOD1(unlink, void(const bf::path&));
MOCK_METHOD2(rename, void(const bf::path&, const bf::path&));
unique_ptr<vector<string>> readDir(const bf::path &path) {
return unique_ptr<vector<string>>(readDirMock(path));
}
MOCK_METHOD1(readDirMock, vector<string>*(const bf::path&));
MOCK_METHOD2(utimens, void(const bf::path&, const timespec[2]));
MOCK_METHOD2(statfs, void(const bf::path&, struct statvfs*));
};
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();
}
Daemon _fuse_process;
MockFilesystemImpl fsimpl;
Fuse fuse;
TempDir mountDir;
};
TEST_F(FuseTest, testDir) {
sleep(10);
}

View File

@ -0,0 +1,32 @@
#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

@ -0,0 +1,18 @@
#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