From 8836e248aa149f3fb10d4e6a5ae1e487144db943 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 24 Sep 2019 20:51:31 +0100 Subject: Revise fuse_ls test to test cache hits --- netfs/fuse/fuseMisc.cpp | 3 +- netfs/unittests/Jamfile.jam | 2 +- netfs/unittests/mockDaemon.cpp | 4 +-- netfs/unittests/mockDaemon.h | 1 + netfs/unittests/testFuse.cpp | 65 +++++++++++++++++++++++++++++++----------- 5 files changed, 54 insertions(+), 21 deletions(-) diff --git a/netfs/fuse/fuseMisc.cpp b/netfs/fuse/fuseMisc.cpp index cbe48af..f46b2fe 100644 --- a/netfs/fuse/fuseMisc.cpp +++ b/netfs/fuse/fuseMisc.cpp @@ -12,8 +12,7 @@ int NetFS::FuseApp::getattr(const char * p, struct stat * s) { try { - auto cacehedStat = statCache.get(p); - if (cacehedStat) { + if (auto cacehedStat = statCache.get(p)) { *s = *cacehedStat; } else { diff --git a/netfs/unittests/Jamfile.jam b/netfs/unittests/Jamfile.jam index 2fbaf4a..3102e4c 100644 --- a/netfs/unittests/Jamfile.jam +++ b/netfs/unittests/Jamfile.jam @@ -68,7 +68,7 @@ run testEdgeCases.cpp : testEdgeCases ; run testFuse.cpp - : : + : -- : defaultDaemon.xml : BOOST_TEST_DYN_LINK diff --git a/netfs/unittests/mockDaemon.cpp b/netfs/unittests/mockDaemon.cpp index 4e8bd9a..c2bfca2 100644 --- a/netfs/unittests/mockDaemon.cpp +++ b/netfs/unittests/mockDaemon.cpp @@ -2,7 +2,7 @@ #include #include -const std::filesystem::path TestExportRoot(binDir / +const std::filesystem::path MockDaemonHost::TestExportRoot(binDir / UniqueExport::get(getpid())); MockDaemon::MockDaemon(std::string ep) : @@ -16,7 +16,7 @@ MockDaemon::ReadConfiguration(const std::filesystem::path & path) const { auto c = NetFSDaemon::ReadConfiguration(path); for(const auto & e : c->Exports) { - e.second->RootPath = TestExportRoot.string(); + e.second->RootPath = MockDaemonHost::TestExportRoot.string(); } c->Hosts.insert({ "unittest", std::make_shared(testEndpoint) }); return c; diff --git a/netfs/unittests/mockDaemon.h b/netfs/unittests/mockDaemon.h index 074d53f..d7374c5 100644 --- a/netfs/unittests/mockDaemon.h +++ b/netfs/unittests/mockDaemon.h @@ -25,6 +25,7 @@ class DLL_PUBLIC MockDaemonHost { void restart(); Ice::CommunicatorPtr ic; + static const std::filesystem::path TestExportRoot; private: void start(); diff --git a/netfs/unittests/testFuse.cpp b/netfs/unittests/testFuse.cpp index c5f9bbf..80a33a8 100644 --- a/netfs/unittests/testFuse.cpp +++ b/netfs/unittests/testFuse.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include "mockDaemon.h" static const std::filesystem::path mntpnt { binDir / "mnt" }; @@ -24,6 +25,9 @@ class FuseMountPoint : public MockDaemonHost, public NetFS::FuseApp { std::filesystem::remove(mntpnt); std::filesystem::create_directory(mntpnt); struct fuse_args fargs { }; + ::fuse_opt_add_arg(&fargs, "-onoauto_cache"); + ::fuse_opt_add_arg(&fargs, "-oattr_timeout=0"); + ::fuse_opt_add_arg(&fargs, "-oac_attr_timeout=0"); ch = ::fuse_mount(mntpnt.c_str(), &fargs); BOOST_REQUIRE(ch); fs = ::fuse_new(ch, &fargs, &operations, sizeof(fuse_operations), this); @@ -72,19 +76,32 @@ class FuseMountPoint : public MockDaemonHost, public NetFS::FuseApp { std::unique_ptr th; }; -struct pathrange { - const std::filesystem::path path; - - [[nodiscard]] std::filesystem::directory_iterator begin() const - { - return std::filesystem::directory_iterator(path); - } - - [[nodiscard]] std::filesystem::directory_iterator end() const - { - return {}; - } -}; +#define BOOST_CHECK_EQUAL_FIELD(left, right, field) \ + BOOST_CHECK_EQUAL(left.field, right.field); + +#define BOOST_CHECK_EQUAL_STAT(left, right) \ + BOOST_CHECK_NE(&left, &right); \ + BOOST_CHECK_EQUAL_FIELD(left, right, st_atim.tv_sec); \ + BOOST_CHECK_EQUAL_FIELD(left, right, st_mtim.tv_sec); \ + BOOST_CHECK_EQUAL_FIELD(left, right, st_ctim.tv_sec); \ + BOOST_CHECK_EQUAL_FIELD(left, right, st_mode); \ + BOOST_CHECK_EQUAL_FIELD(left, right, st_nlink); \ + BOOST_CHECK_EQUAL_FIELD(left, right, st_uid); \ + BOOST_CHECK_EQUAL_FIELD(left, right, st_gid); \ + BOOST_CHECK_EQUAL_FIELD(left, right, st_rdev); \ + BOOST_CHECK_EQUAL_FIELD(left, right, st_size); \ + BOOST_CHECK_EQUAL_FIELD(left, right, st_blksize); \ + BOOST_CHECK_EQUAL_FIELD(left, right, st_blocks); \ + +static +auto +get_lstat(const std::filesystem::path & p) +{ + struct stat st { }; + BOOST_TEST_INFO(p); + BOOST_REQUIRE_EQUAL(::lstat(p.c_str(), &st), 0); + return st; +} BOOST_FIXTURE_TEST_SUITE(fmp, FuseMountPoint); @@ -96,12 +113,28 @@ BOOST_AUTO_TEST_CASE(fuse, * boost::unit_test::timeout(5)) BOOST_AUTO_TEST_CASE(fuse_ls, * boost::unit_test::timeout(5)) { BOOST_REQUIRE(std::filesystem::is_directory(mntpnt)); - std::filesystem::create_symlink(selfExe, mntpnt / "me"); + BOOST_REQUIRE(std::filesystem::is_empty(mntpnt)); + const auto rpath = mntpnt / "me"; + const auto lpath = MockDaemonHost::TestExportRoot / "me"; + BOOST_REQUIRE_NE(lpath, rpath); + + std::filesystem::create_symlink(selfExe, rpath); + + const auto st_local = get_lstat(lpath); + + BOOST_CHECK(!statCache.get("/me")); + std::set paths(std::filesystem::directory_iterator(mntpnt), {}); BOOST_REQUIRE_EQUAL(paths.size(), 1); BOOST_CHECK_EQUAL(paths.begin()->filename(), "me"); - BOOST_CHECK(std::filesystem::is_symlink(mntpnt / "me")); - BOOST_CHECK(std::filesystem::is_symlink(mntpnt / "me")); + + auto cached = statCache.get("/me"); + BOOST_REQUIRE(cached); + const auto & st_cache = (*cached); + BOOST_CHECK_EQUAL_STAT(st_cache, st_local); + + const auto st_remote = get_lstat(rpath); + BOOST_CHECK_EQUAL_STAT(st_remote, st_local); } BOOST_AUTO_TEST_SUITE_END(); -- cgit v1.2.3