From 9b1ad7b99e7254345f83ce28a0f1db83a5533710 Mon Sep 17 00:00:00 2001 From: Colin Patrick McCabe Date: Wed, 20 Jul 2011 15:47:45 -0700 Subject: [PATCH] re-enable proflogger test Re-enable the proflogger test. This time, it doesn't test the basic socket functionality (that is what test/admin_socket.cc is for). Signed-off-by: Colin McCabe --- src/Makefile.am | 10 +- src/common/admin_socket_client.cc | 19 ++++ src/common/admin_socket_client.h | 2 + src/test/admin_socket.cc | 22 +---- src/test/proflogger.cc | 157 ++++-------------------------- 5 files changed, 48 insertions(+), 162 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index 3ddbc5a082416..e9c8b982221d0 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -475,11 +475,11 @@ unittest_crypto_LDADD = ${LIBGLOBAL_LDA} ${UNITTEST_LDADD} unittest_crypto_CXXFLAGS = ${CRYPTO_CXXFLAGS} ${AM_CXXFLAGS} ${UNITTEST_CXXFLAGS} check_PROGRAMS += unittest_crypto -#unittest_proflogger_SOURCES = test/proflogger.cc -#unittest_proflogger_LDFLAGS = -lrt ${AM_LDFLAGS} -#unittest_proflogger_LDADD = ${LIBGLOBAL_LDA} ${UNITTEST_LDADD} -#unittest_proflogger_CXXFLAGS = ${AM_CXXFLAGS} ${UNITTEST_CXXFLAGS} -#check_PROGRAMS += unittest_proflogger +unittest_proflogger_SOURCES = test/proflogger.cc +unittest_proflogger_LDFLAGS = -lrt ${AM_LDFLAGS} +unittest_proflogger_LDADD = ${LIBGLOBAL_LDA} ${UNITTEST_LDADD} +unittest_proflogger_CXXFLAGS = ${AM_CXXFLAGS} ${UNITTEST_CXXFLAGS} +check_PROGRAMS += unittest_proflogger unittest_admin_socket_SOURCES = test/admin_socket.cc unittest_admin_socket_LDFLAGS = -lrt ${AM_LDFLAGS} diff --git a/src/common/admin_socket_client.cc b/src/common/admin_socket_client.cc index 513b79f494fd1..dd745ea6ea5fa 100644 --- a/src/common/admin_socket_client.cc +++ b/src/common/admin_socket_client.cc @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -38,6 +39,24 @@ using std::ostringstream; +const char* get_rand_socket_path() +{ + static char *g_socket_path = NULL; + + if (g_socket_path == NULL) { + char buf[512]; + const char *tdir = getenv("TMPDIR"); + if (tdir == NULL) { + tdir = "/tmp"; + } + snprintf(buf, sizeof(sockaddr_un::sun_path), + "%s/proflogger_test_socket.%ld.%ld", + tdir, (long int)getpid(), time(NULL)); + g_socket_path = (char*)strdup(buf); + } + return g_socket_path; +} + static std::string asok_connect(const std::string &path, int *fd) { int socket_fd = socket(PF_UNIX, SOCK_STREAM, 0); diff --git a/src/common/admin_socket_client.h b/src/common/admin_socket_client.h index a40682f262b60..597f55601dc0c 100644 --- a/src/common/admin_socket_client.h +++ b/src/common/admin_socket_client.h @@ -30,4 +30,6 @@ private: std::string m_path; }; +const char* get_rand_socket_path(); + #endif diff --git a/src/test/admin_socket.cc b/src/test/admin_socket.cc index 8acf5941788aa..f1abccdd6b20d 100644 --- a/src/test/admin_socket.cc +++ b/src/test/admin_socket.cc @@ -23,22 +23,6 @@ #include #include -static char g_socket_path[sizeof(sockaddr_un::sun_path)] = { 0 }; - -static const char* get_socket_path() -{ - if (g_socket_path[0] == '\0') { - const char *tdir = getenv("TMPDIR"); - if (tdir == NULL) { - tdir = "/tmp"; - } - snprintf(g_socket_path, sizeof(sockaddr_un::sun_path), - "%s/proflogger_test_socket.%ld.%ld", - tdir, (long int)getpid(), time(NULL)); - } - return g_socket_path; -} - class AdminSocketTest { public: @@ -72,7 +56,7 @@ TEST(AdminSocket, TeardownSetup) { asokc(new AdminSocketConfigObs(g_ceph_context)); AdminSocketTest asoct(asokc.get()); ASSERT_EQ(true, asoct.shutdown()); - ASSERT_EQ(true, asoct.init(get_socket_path())); + ASSERT_EQ(true, asoct.init(get_rand_socket_path())); ASSERT_EQ(true, asoct.shutdown()); } @@ -81,8 +65,8 @@ TEST(AdminSocket, SendNoOp) { asokc(new AdminSocketConfigObs(g_ceph_context)); AdminSocketTest asoct(asokc.get()); ASSERT_EQ(true, asoct.shutdown()); - ASSERT_EQ(true, asoct.init(get_socket_path())); - AdminSocketClient client(get_socket_path()); + ASSERT_EQ(true, asoct.init(get_rand_socket_path())); + AdminSocketClient client(get_rand_socket_path()); ASSERT_EQ("", client.send_noop()); ASSERT_EQ(true, asoct.shutdown()); } diff --git a/src/test/proflogger.cc b/src/test/proflogger.cc index 631a333b0bfec..5ace3ce494d7d 100644 --- a/src/test/proflogger.cc +++ b/src/test/proflogger.cc @@ -13,7 +13,9 @@ */ #include "common/ProfLogger.h" +#include "common/admin_socket_client.h" #include "common/ceph_context.h" +#include "common/config.h" #include "common/errno.h" #include "common/safe_io.h" #include "test/unit.h" @@ -33,131 +35,12 @@ #include #include -static char g_socket_path[sizeof(sockaddr_un::sun_path)] = { 0 }; - -static const char* get_socket_path() -{ - if (g_socket_path[0] == '\0') { - const char *tdir = getenv("TMPDIR"); - if (tdir == NULL) { - tdir = "/tmp"; - } - snprintf(g_socket_path, sizeof(sockaddr_un::sun_path), - "%s/proflogger_test_socket.%ld.%ld", - tdir, (long int)getpid(), time(NULL)); - } - return g_socket_path; -} - -class Alarm -{ -public: - Alarm(int s) { - alarm(s); - } - ~Alarm() { - alarm(0); - } -}; - -class ProfLoggerTestClient -{ -public: - ProfLoggerTestClient(const std::string &uri) - : m_uri(uri) - { - } - - std::string get_message(std::string *message) - { - Alarm my_alarm(300); - - int socket_fd = socket(PF_UNIX, SOCK_STREAM, 0); - if(socket_fd < 0) { - int err = errno; - ostringstream oss; - oss << "socket(PF_UNIX, SOCK_STREAM, 0) failed: " << cpp_strerror(err); - return oss.str(); - } - - struct sockaddr_un address; - memset(&address, 0, sizeof(struct sockaddr_un)); - address.sun_family = AF_UNIX; - snprintf(address.sun_path, sizeof(address.sun_path), "%s", m_uri.c_str()); - - if (connect(socket_fd, (struct sockaddr *) &address, - sizeof(struct sockaddr_un)) != 0) { - int err = errno; - ostringstream oss; - oss << "connect(" << socket_fd << ") failed: " << cpp_strerror(err); - close(socket_fd); - return oss.str(); - } - - std::vector vec(65536, 0); - uint8_t *buffer = &vec[0]; - - uint32_t request = htonl(0x1); - ssize_t res = safe_write(socket_fd, &request, sizeof(request)); - if (res < 0) { - int err = res; - ostringstream oss; - oss << "safe_write(" << socket_fd << ") failed to write request code: " - << cpp_strerror(err); - close(socket_fd); - return oss.str(); - } - - uint32_t message_size_raw; - res = safe_read_exact(socket_fd, &message_size_raw, - sizeof(message_size_raw)); - if (res < 0) { - int err = res; - ostringstream oss; - oss << "safe_read(" << socket_fd << ") failed to read message size: " - << cpp_strerror(err); - close(socket_fd); - return oss.str(); - } - uint32_t message_size = ntohl(message_size_raw); - res = safe_read_exact(socket_fd, buffer, message_size); - if (res < 0) { - int err = res; - ostringstream oss; - oss << "safe_read(" << socket_fd << ") failed: " << cpp_strerror(err); - close(socket_fd); - return oss.str(); - } - - //printf("MESSAGE FROM SERVER: %s\n", buffer); - message->assign((const char*)buffer); - close(socket_fd); - return ""; - } - -private: - std::string m_uri; -}; - -TEST(ProfLogger, Teardown) { - AdminSocketTest plct(g_ceph_context->GetProfLoggerCollection()); - ASSERT_EQ(true, plct.shutdown()); -} - -TEST(ProfLogger, TeardownSetup) { - AdminSocketTest plct(g_ceph_context->GetProfLoggerCollection()); - ASSERT_EQ(true, plct.shutdown()); - ASSERT_EQ(true, plct.init(get_socket_path())); - ASSERT_EQ(true, plct.shutdown()); -} - TEST(ProfLogger, SimpleTest) { - AdminSocketTest plct(g_ceph_context->GetProfLoggerCollection()); - ASSERT_EQ(true, plct.shutdown()); - ASSERT_EQ(true, plct.init(get_socket_path())); - ProfLoggerTestClient test_client(get_socket_path()); + g_ceph_context->_conf->set_val_or_die("admin_socket", get_rand_socket_path()); + g_ceph_context->_conf->apply_changes(); + AdminSocketClient client(get_rand_socket_path()); std::string message; - ASSERT_EQ("", test_client.get_message(&message)); + ASSERT_EQ("", client.get_message(&message)); ASSERT_EQ("{}", message); } @@ -195,23 +78,22 @@ TEST(ProfLogger, SingleProfLogger) { ProfLoggerCollection *coll = g_ceph_context->GetProfLoggerCollection(); ProfLogger* fake_pf = setup_fake_proflogger1(g_ceph_context); coll->logger_add(fake_pf); - AdminSocketTest plct(coll); - ASSERT_EQ(true, plct.shutdown()); - ASSERT_EQ(true, plct.init(get_socket_path())); - ProfLoggerTestClient test_client(get_socket_path()); + g_ceph_context->_conf->set_val_or_die("admin_socket", get_rand_socket_path()); + g_ceph_context->_conf->apply_changes(); + AdminSocketClient client(get_rand_socket_path()); std::string msg; - ASSERT_EQ("", test_client.get_message(&msg)); + ASSERT_EQ("", client.get_message(&msg)); ASSERT_EQ(sd("{'fake_proflogger_1':{'element1':0," "'element2':0,'element3':{'count':0,'sum':0},},}"), msg); fake_pf->inc(FAKE_PROFLOGGER1_ELEMENT_1); fake_pf->fset(FAKE_PROFLOGGER1_ELEMENT_2, 0.5); fake_pf->finc(FAKE_PROFLOGGER1_ELEMENT_3, 100.0); - ASSERT_EQ("", test_client.get_message(&msg)); + ASSERT_EQ("", client.get_message(&msg)); ASSERT_EQ(sd("{'fake_proflogger_1':{'element1':1," "'element2':0.5,'element3':{'count':1,'sum':100},},}"), msg); fake_pf->finc(FAKE_PROFLOGGER1_ELEMENT_3, 0.0); fake_pf->finc(FAKE_PROFLOGGER1_ELEMENT_3, 25.0); - ASSERT_EQ("", test_client.get_message(&msg)); + ASSERT_EQ("", client.get_message(&msg)); ASSERT_EQ(sd("{'fake_proflogger_1':{'element1':1,'element2':0.5," "'element3':{'count':3,'sum':125},},}"), msg); } @@ -239,28 +121,27 @@ TEST(ProfLogger, MultipleProfloggers) { ProfLogger* fake_pf2 = setup_fake_proflogger2(g_ceph_context); coll->logger_add(fake_pf1); coll->logger_add(fake_pf2); - AdminSocketTest plct(coll); - ASSERT_EQ(true, plct.shutdown()); - ASSERT_EQ(true, plct.init(get_socket_path())); - ProfLoggerTestClient test_client(get_socket_path()); + g_ceph_context->_conf->set_val_or_die("admin_socket", get_rand_socket_path()); + g_ceph_context->_conf->apply_changes(); + AdminSocketClient client(get_rand_socket_path()); std::string msg; - ASSERT_EQ("", test_client.get_message(&msg)); + ASSERT_EQ("", client.get_message(&msg)); ASSERT_EQ(sd("{'fake_proflogger_1':{'element1':0,'element2':0,'element3':" "{'count':0,'sum':0},},'fake_proflogger_2':{'foo':0,'bar':0,},}"), msg); fake_pf1->inc(FAKE_PROFLOGGER1_ELEMENT_1); fake_pf1->inc(FAKE_PROFLOGGER1_ELEMENT_1, 5); - ASSERT_EQ("", test_client.get_message(&msg)); + ASSERT_EQ("", client.get_message(&msg)); ASSERT_EQ(sd("{'fake_proflogger_1':{'element1':6,'element2':0,'element3':" "{'count':0,'sum':0},},'fake_proflogger_2':{'foo':0,'bar':0,},}"), msg); coll->logger_remove(fake_pf2); - ASSERT_EQ("", test_client.get_message(&msg)); + ASSERT_EQ("", client.get_message(&msg)); ASSERT_EQ(sd("{'fake_proflogger_1':{'element1':6,'element2':0," "'element3':{'count':0,'sum':0},},}"), msg); coll->logger_clear(); - ASSERT_EQ("", test_client.get_message(&msg)); + ASSERT_EQ("", client.get_message(&msg)); ASSERT_EQ("{}", msg); } -- 2.39.5