From fb57563f701c985ff6e9c3624cdfc2b6afc3e6f8 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 14 Apr 2022 10:44:42 +0100 Subject: Fix Glacier tests Creates a class which is usable as a global fixture for handling a Glacier instance. Fixes the issue in the previous version where flock was used, but in conjunction with --daemon and a pidfile, so it only prevented concurrent startup, not concurrent execution. --- netfs/unittests/mockGlacier.cpp | 29 +++++++++++++++++++++++++++++ netfs/unittests/mockGlacier.h | 19 +++++++++++++++++++ netfs/unittests/testGlacier.cpp | 35 +++++++++++------------------------ 3 files changed, 59 insertions(+), 24 deletions(-) create mode 100644 netfs/unittests/mockGlacier.cpp create mode 100644 netfs/unittests/mockGlacier.h diff --git a/netfs/unittests/mockGlacier.cpp b/netfs/unittests/mockGlacier.cpp new file mode 100644 index 0000000..f1e8e72 --- /dev/null +++ b/netfs/unittests/mockGlacier.cpp @@ -0,0 +1,29 @@ +#include "mockGlacier.h" +#include +#include +#include +#include +#include + +Glacier::Glacier() : lockdir(open("/tmp", O_DIRECTORY)) +{ + BOOST_REQUIRE_GE(lockdir, 0); + BOOST_REQUIRE_EQUAL(0, flock(lockdir, LOCK_EX)); + if (glacier = fork(); glacier == 0) { + prctl(PR_SET_PDEATHSIG, SIGINT); + execl("/usr/bin/glacier2router", "--Glacier2.Client.Endpoints=tcp -p 14063", + "--Glacier2.PermissionsVerifier=Glacier2/NullPermissionsVerifier", nullptr); + BOOST_FAIL("Should never get here"); + } + BOOST_REQUIRE_GT(glacier, 0); + sleep(1); +} + +Glacier::~Glacier() +{ + BOOST_CHECK_EQUAL(0, kill(glacier, SIGINT)); + int status {}; + BOOST_CHECK_EQUAL(glacier, waitpid(glacier, &status, 0)); + BOOST_CHECK_EQUAL(0, status); + BOOST_CHECK_EQUAL(0, close(lockdir)); +} diff --git a/netfs/unittests/mockGlacier.h b/netfs/unittests/mockGlacier.h new file mode 100644 index 0000000..007715d --- /dev/null +++ b/netfs/unittests/mockGlacier.h @@ -0,0 +1,19 @@ +#ifndef MOCKGLACIER_H +#define MOCKGLACIER_H + +#include +#include +#include + +class DLL_PUBLIC Glacier { +public: + Glacier(); + ~Glacier(); + SPECIAL_MEMBERS_DELETE(Glacier); + +private: + const int lockdir; + pid_t glacier; +}; + +#endif diff --git a/netfs/unittests/testGlacier.cpp b/netfs/unittests/testGlacier.cpp index 5467c41..afe3475 100644 --- a/netfs/unittests/testGlacier.cpp +++ b/netfs/unittests/testGlacier.cpp @@ -1,35 +1,22 @@ #define BOOST_TEST_MODULE TestNetFSGlacier +#include + #include "mockDaemon.h" #include "mockFuse.h" -#include -#include +#include "mockGlacier.h" #include const std::string testEndpoint("tcp -h localhost -p 12013"); -#define PID "/tmp/glacier.pid" + +BOOST_TEST_GLOBAL_FIXTURE(Glacier); BOOST_AUTO_TEST_CASE(withRouter) { - std::filesystem::remove(PID); - BOOST_REQUIRE_EQUAL(0, - system("flock /tmp/glacier.lock /usr/bin/glacier2router --Glacier2.Client.Endpoints='tcp -p 14063' " - "--Glacier2.PermissionsVerifier=Glacier2/NullPermissionsVerifier --daemon --pidfile " PID)); - sleep(1); - - BOOST_SCOPE_EXIT(void) - { - BOOST_REQUIRE_EQUAL(0, system("kill $(cat " PID ")")); - std::filesystem::remove(PID); - } - BOOST_SCOPE_EXIT_END; - - { - MockDaemonHost daemon(testEndpoint, {"--NetFSD.ConfigPath=" + (rootDir / "defaultDaemon.xml").string()}); - FuseMockHost fuse(testEndpoint, - {(rootDir / "defaultFuse.xml:testvol").string(), (rootDir / "test").string(), - "--Ice.Default.Router=Glacier2/router:tcp -h localhost -p 14063"}); + MockDaemonHost daemon(testEndpoint, {"--NetFSD.ConfigPath=" + (rootDir / "defaultDaemon.xml").string()}); + FuseMockHost fuse(testEndpoint, + {(rootDir / "defaultFuse.xml:testvol").string(), (rootDir / "test").string(), + "--Ice.Default.Router=Glacier2/router:tcp -h localhost -p 14063"}); - struct statvfs s { }; - BOOST_REQUIRE_EQUAL(0, fuse.fuse->statfs("/", &s)); - } + struct statvfs s { }; + BOOST_CHECK_EQUAL(0, fuse.fuse->statfs("/", &s)); } -- cgit v1.2.3