return EXIT_FAILURE;
}
- if (opt_username and initialize_sasl(memc, opt_username, opt_passwd) == false)
+ if (opt_username)
{
- memcached_free(memc);
- return EXIT_FAILURE;
+ memcached_return_t ret;
+ if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd)))
+ {
+ std::cerr << memcached_last_error_message(memc) << std::endl;
+ memcached_free(memc);
+ return EXIT_FAILURE;
+ }
}
while (optind < argc)
if (opt_hash)
free(opt_hash);
- shutdown_sasl();
-
return return_code;
}
return EXIT_FAILURE;
}
- if (initialize_sasl(memc, opt_username, opt_passwd) == false)
+ if (opt_username)
{
- std::cerr << "Failed to initialize SASL support." << std::endl;
- memcached_free(memc);
- return EXIT_FAILURE;
+ memcached_return_t ret;
+ if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd)))
+ {
+ std::cerr << memcached_last_error_message(memc) << std::endl;
+ memcached_free(memc);
+ return EXIT_FAILURE;
+ }
}
while (optind < argc)
free(opt_servers);
if (opt_hash)
free(opt_hash);
- shutdown_sasl();
return return_code;
}
return EXIT_FAILURE;
}
- if (opt_username and initialize_sasl(memc, opt_username, opt_passwd) == false)
+ if (opt_username)
{
- std::cerr << "Failed to initialize SASL support." << std::endl;
-
- memcached_free(memc);
- return EXIT_FAILURE;
+ memcached_return_t ret;
+ if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd)))
+ {
+ std::cerr << memcached_last_error_message(memc) << std::endl;
+ memcached_free(memc);
+ return EXIT_FAILURE;
+ }
}
rc= memcached_dump(memc, callbacks, NULL, 1);
if (opt_hash)
free(opt_hash);
- shutdown_sasl();
-
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}
-
- if (!initialize_sasl(memc, opt_username, opt_passwd))
+ if (opt_username)
{
- memcached_free(memc);
- return EXIT_FAILURE;
+ memcached_return_t ret;
+ if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd)))
+ {
+ std::cerr << memcached_last_error_message(memc) << std::endl;
+ memcached_free(memc);
+ return EXIT_FAILURE;
+ }
}
rc = memcached_flush(memc, opt_expire);
free(opt_servers);
- shutdown_sasl();
-
return EXIT_SUCCESS;
}
free(opt_servers);
- shutdown_sasl();
-
return exit_code;
}
opt_servers= strdup(temp);
else
{
- fprintf(stderr, "No Servers provided\n");
- exit(1);
+ std::cerr << "No Servers provided" << std::endl;
+ return EXIT_FAILURE;
}
}
return EXIT_FAILURE;
}
-
- if (opt_username and initialize_sasl(memc, opt_username, opt_passwd) == false)
+ if (opt_username)
{
- std::cerr << "Failed to initialize SASL support." << std::endl;
-
- memcached_free(memc);
- return EXIT_FAILURE;
+ memcached_return_t ret;
+ if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd)))
+ {
+ std::cerr << memcached_last_error_message(memc) << std::endl;
+ memcached_free(memc);
+ return EXIT_FAILURE;
+ }
}
while (optind < argc)
{
if (opt_verbose)
- printf("key: %s\nexpires: %llu\n", argv[optind], (unsigned long long)opt_expire);
- rc = memcached_delete(memc, argv[optind], strlen(argv[optind]), opt_expire);
+ {
+ std::cout << "key: " << argv[optind] << std::endl;
+ std::cout << "expires: " << opt_expire << std::endl;
+ }
+ rc= memcached_delete(memc, argv[optind], strlen(argv[optind]), opt_expire);
- if (rc != MEMCACHED_SUCCESS)
+ if (memcached_failed(rc))
{
- fprintf(stderr, "memrm: %s: memcache error %s",
- argv[optind], memcached_strerror(memc, rc));
+ std::cerr << PROGRAM_NAME << ": " << argv[optind] << ": error " << memcached_strerror(memc, rc) << std::endl;
+
if (memcached_last_error_errno(memc))
- fprintf(stderr, " system error %s", strerror(memcached_last_error_errno(memc)));
- fprintf(stderr, "\n");
+ {
+ std::cerr << " system error " << strerror(memcached_last_error_errno(memc));
+ }
+ std::cerr << std::endl;
return_code= -1;
}
if (opt_hash)
free(opt_hash);
- shutdown_sasl();
-
return return_code;
}
}
}
-
-static char *username;
-static char *passwd;
-
-#if defined(LIBMEMCACHED_WITH_SASL_SUPPORT) && LIBMEMCACHED_WITH_SASL_SUPPORT
-
-static int get_username(void *context, int id, const char **result, unsigned int *len)
-{
- (void)context;
- if (!result || (id != SASL_CB_USER && id != SASL_CB_AUTHNAME))
- return SASL_BADPARAM;
-
- *result= username;
- if (len)
- {
- *len= (username == NULL) ? 0 : (unsigned int)strlen(username);
- }
-
- return SASL_OK;
-}
-
-static int get_password(sasl_conn_t *conn, void *context, int id,
- sasl_secret_t **psecret)
-{
- (void)context;
- static sasl_secret_t* ptr;
-
- if (!conn || ! psecret || id != SASL_CB_PASS)
- return SASL_BADPARAM;
-
- if (passwd == NULL)
- {
- *psecret= NULL;
- return SASL_OK;
- }
-
- size_t len= strlen(passwd);
- ptr= (sasl_secret_t *)malloc(sizeof(sasl_secret_t) + len +1);
- if (not ptr)
- return SASL_NOMEM;
-
- ptr->len= len;
- memcpy(ptr->data, passwd, len);
- ptr->data[len]= 0;
-
- *psecret= ptr;
- return SASL_OK;
-}
-
-typedef int (*local_sasl_fn)(void);
-
-/* callbacks we support */
-static sasl_callback_t sasl_callbacks[] = {
- { SASL_CB_USER, (local_sasl_fn)get_username, NULL },
- { SASL_CB_AUTHNAME, (local_sasl_fn)get_username, NULL },
- { SASL_CB_PASS, (local_sasl_fn)get_password, NULL },
- { SASL_CB_LIST_END, NULL, NULL }
-};
-
-#endif
-
-bool initialize_sasl(memcached_st *memc, char *user, char *password)
-{
- if (LIBMEMCACHED_WITH_SASL_SUPPORT == 0)
- {
- return false;
- }
-
- if (memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, (uint64_t)true) == 0)
- {
- return false;
- }
-
- if (user != NULL && password != NULL)
- {
- username= user;
- passwd= password;
-
-#if defined(LIBMEMCACHED_WITH_SASL_SUPPORT) && LIBMEMCACHED_WITH_SASL_SUPPORT
- if (sasl_client_init(NULL) != SASL_OK)
- {
- fprintf(stderr, "Failed to initialize sasl library!\n");
- return false;
- }
- memcached_set_sasl_callbacks(memc, sasl_callbacks);
-#else
- (void)memc;
-#endif
- }
-
- return true;
-}
-
-void shutdown_sasl(void)
-{
- if (username or passwd)
- {
-#if defined(LIBMEMCACHED_WITH_SASL_SUPPORT) && LIBMEMCACHED_WITH_SASL_SUPPORT
- sasl_done();
-#endif
- }
-}
-
void initialize_sockets(void)
{
/* Define the function for all platforms to avoid #ifdefs in each program */
AM_CONDITIONAL(BUILDING_LIBMEMCACHED, true)
AM_CONDITIONAL(HAVE_LIBMEMCACHED, false)
-AC_SUBST(_WITH_LIBMEMCACHED_SUPPORT, ["_WITH_LIBMEMCACHED_SUPPORT 1"])
+AC_DEFINE([HAVE_LIBMEMCACHED], [1], [Enables libmemcached Support])
AM_CONDITIONAL(BUILDING_GEARMAN, false)
AM_CONDITIONAL(HAVE_LIBGEARMAN, false)
-AC_SUBST(_WITH_LIBGEARMAN_SUPPORT, ["_WITH_LIBGEARMAN_SUPPORT 0"])
+AC_DEFINE([HAVE_LIBGEARMAN], [0], [Enables libgearman Support])
AC_SEARCH_LIBS(getopt_long, gnugetopt)
AC_SEARCH_LIBS(gethostbyname, nsl)
libmemcached_libmemcached_la_LIBADD+= $(LTLIBSASL) $(LTLIBSASL2)
libmemcached_libmemcached_la_SOURCES += libmemcached/sasl.cc
+if HAVE_SASL
+libmemcached_libmemcached_la_CXXFLAGS+= ${PTHREAD_CFLAGS}
+libmemcached_libmemcached_la_LIBADD+= ${PTHREAD_LIBS}
+endif
if HAVE_DTRACE
BUILT_SOURCES+= libmemcached/dtrace_probes.h
#if defined(LIBMEMCACHED_WITH_SASL_SUPPORT) && LIBMEMCACHED_WITH_SASL_SUPPORT
#include <sasl/sasl.h>
+#include <pthread.h>
void memcached_set_sasl_callbacks(memcached_st *ptr,
const sasl_callback_t *callbacks)
return MEMCACHED_SUCCESS;
}
+static void sasl_shutdown_function()
+{
+ sasl_done();
+}
+
+static int sasl_startup_state= SASL_OK;
+static pthread_once_t sasl_startup_once= PTHREAD_ONCE_INIT;
+static void sasl_startup_function(void)
+{
+ sasl_startup_state= sasl_client_init(NULL);
+
+ if (sasl_startup_state == SASL_OK)
+ {
+ (void)atexit(sasl_shutdown_function);
+ }
+}
+
memcached_return_t memcached_sasl_authenticate_connection(memcached_server_st *server)
{
if (LIBMEMCACHED_WITH_SASL_SUPPORT == 0)
return rc;
}
- int ret;
- if ((ret= sasl_client_init(NULL)) != SASL_OK)
+ int pthread_error;
+ if ((pthread_error= pthread_once(&sasl_startup_once, sasl_startup_function)) != 0)
{
- const char *sasl_error_msg= sasl_errstring(ret, NULL, NULL);
+ return memcached_set_errno(*server, pthread_error, MEMCACHED_AT);
+ }
+
+ if (sasl_startup_state != SASL_OK)
+ {
+ const char *sasl_error_msg= sasl_errstring(sasl_startup_state, NULL, NULL);
return memcached_set_error(*server, MEMCACHED_AUTH_PROBLEM, MEMCACHED_AT,
memcached_string_make_from_cstr(sasl_error_msg));
}
sasl_conn_t *conn;
+ int ret;
if ((ret= sasl_client_new("memcached", server->hostname, laddr, raddr, server->root->sasl.callbacks, 0, &conn) ) != SASL_OK)
{
const char *sasl_error_msg= sasl_errstring(ret, NULL, NULL);
+
+ sasl_dispose(&conn);
+
return memcached_set_error(*server, MEMCACHED_AUTH_PROBLEM, MEMCACHED_AT,
memcached_string_make_from_cstr(sasl_error_msg));
}
if (ret != SASL_OK and ret != SASL_CONTINUE)
{
const char *sasl_error_msg= sasl_errstring(ret, NULL, NULL);
+
+ sasl_dispose(&conn);
+
return memcached_set_error(*server, MEMCACHED_AUTH_PROBLEM, MEMCACHED_AT,
memcached_string_make_from_cstr(sasl_error_msg));
}
return MEMCACHED_INVALID_ARGUMENTS;
}
+ memcached_return_t ret;
+ if (memcached_failed(ret= memcached_behavior_set(ptr, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1)))
+ {
+ return memcached_set_error(*ptr, ret, MEMCACHED_AT, memcached_literal_param("Unable change to binary protocol which is required for SASL."));
+ }
+
memcached_destroy_sasl_auth_data(ptr);
sasl_callback_t *callbacks= (sasl_callback_t*)libmemcached_calloc(ptr, 4, sizeof(sasl_callback_t));
return -1;
}
- if (memcached_failed(*ret= memcached_behavior_set(memc_ptr, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1)))
- {
- memcached_free(memc_ptr);
- return false;
- }
-
if (memcached_failed(*ret= memcached_set_sasl_auth_data(memc_ptr, username, password)))
{
memcached_free(memc_ptr);
memcached_server_instance_st instance=
memcached_server_instance_by_position(memc_ptr, 0);
+#if 0
assert_msg(instance and instance->error_messages, " ");
+#endif
if (instance and instance->error_messages)
{
rc= memcached_server_error_return(instance);
return false;
}
- if (memcached_failed(*ret= memcached_behavior_set(memc_ptr, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1)))
- {
- memcached_free(memc_ptr);
- return false;
- }
-
if (memcached_failed(*ret= memcached_set_sasl_auth_data(memc_ptr, username, password)))
{
memcached_free(memc_ptr);
#include <libtest/memcached.h>
#include <libtest/libtool.hpp>
+#include <libtest/killpid.h>
#include <libtest/test.hpp>
libtest/memcached.h \
libtest/runner.h \
libtest/server.h \
+ libtest/server_container.h \
libtest/signal.h \
libtest/stats.h \
libtest/stream.h \
libtest/libtool.cc \
libtest/runner.cc \
libtest/server.cc \
+ libtest/server_container.cc \
libtest/signal.cc \
libtest/test.cc
libtest_unittest_LDADD+= libtest/libtest.la
libtest_unittest_SOURCES= libtest/unittest.cc
check_PROGRAMS+= libtest/unittest
+noinst_PROGRAMS+= libtest/unittest
test-unittest: libtest/unittest
@libtest/unittest
return true;
}
-
-pid_t kill_file(const std::string &filename)
+bool check_pid(const std::string &filename)
{
- pid_t ret= -1;
+ if (filename.empty())
+ {
+ return false;
+ }
+
FILE *fp;
+ if ((fp= fopen(filename.c_str(), "r")))
+ {
+ char pid_buffer[1024];
+
+ char *ptr= fgets(pid_buffer, sizeof(pid_buffer), fp);
+ fclose(fp);
+ if (ptr)
+ {
+ pid_t pid= (pid_t)atoi(pid_buffer);
+ if (pid > 0)
+ {
+ return (::kill(pid, 0) == 0);
+ }
+ }
+ }
+
+ return false;
+}
+
+
+bool kill_file(const std::string &filename)
+{
if (filename.empty())
- return ret;
+ {
+ return true;
+ }
+ FILE *fp;
if ((fp= fopen(filename.c_str(), "r")))
{
char pid_buffer[1024];
pid_t pid= (pid_t)atoi(pid_buffer);
if (pid != 0)
{
- kill_pid(pid);
+ bool ret= kill_pid(pid);
unlink(filename.c_str()); // If this happens we may be dealing with a dead server that left its pid file.
+
+ return ret;
}
}
}
- return ret;
+ return false;
}
#define STRINGIFY(x) #x
bool kill_pid(pid_t pid_arg);
-pid_t kill_file(const std::string &filename);
+bool kill_file(const std::string &filename);
+
+bool check_pid(const std::string &filename);
pid_t get_pid_from_file(const std::string &filename, std::stringstream& error_message);
+
+static inline bool check_pid(pid_t pid_arg)
+{
+ return (pid_arg > 1);
+}
memcached_return_t rc;
if (has_socket())
{
- if (username().empty())
- {
- local_pid= libmemcached_util_getpid(socket().c_str(), 0, &rc);
- }
- else
- {
- local_pid= libmemcached_util_getpid2(socket().c_str(), 0, username().c_str(), password().c_str(), &rc);
- }
+ local_pid= libmemcached_util_getpid(socket().c_str(), 0, &rc);
}
else
{
- if (username().empty())
- {
- local_pid= libmemcached_util_getpid(hostname().c_str(), port(), &rc);
- }
- else
- {
- local_pid= libmemcached_util_getpid2(hostname().c_str(), port(), username().c_str(), password().c_str(), &rc);
- }
+ local_pid= libmemcached_util_getpid(hostname().c_str(), port(), &rc);
}
if (error_is_ok and ((memcached_failed(rc) or not is_pid_valid(local_pid))))
if (has_socket())
{
- if (username().empty())
- {
ret= libmemcached_util_ping(socket().c_str(), 0, &rc);
- }
- else
- {
- ret= libmemcached_util_ping2(socket().c_str(), 0, username().c_str(), password().c_str(), &rc);
- }
}
else
{
- if (username().empty())
- {
- ret= libmemcached_util_ping(hostname().c_str(), port(), &rc);
- }
- else
- {
- ret= libmemcached_util_ping2(hostname().c_str(), port(), username().c_str(), password().c_str(), &rc);
- }
+ ret= libmemcached_util_ping(hostname().c_str(), port(), &rc);
}
if (memcached_failed(rc) or not ret)
{
- if (username().empty())
- {
- Error << "libmemcached_util_ping(" << hostname() << ", " << port() << ") error: " << memcached_strerror(NULL, rc);
- }
- else
- {
- Error << "libmemcached_util_ping2(" << hostname() << ", " << port() << ", " << username() << ", " << password() << ") error: " << memcached_strerror(NULL, rc);
- }
+ Error << "libmemcached_util_ping(" << hostname() << ", " << port() << ") error: " << memcached_strerror(NULL, rc);
}
return ret;
return MEMCACHED_SASL_BINARY;
}
+ pid_t get_pid(bool error_is_ok)
+ {
+ // Memcached is slow to start, so we need to do this
+ if (not pid_file().empty())
+ {
+ Wait wait(pid_file(), 0);
+
+ if (error_is_ok and not wait.successful())
+ {
+ Error << "Pidfile was not found:" << pid_file();
+ return -1;
+ }
+ }
+
+ pid_t local_pid;
+ memcached_return_t rc;
+ if (has_socket())
+ {
+ local_pid= libmemcached_util_getpid2(socket().c_str(), 0, username().c_str(), password().c_str(), &rc);
+ }
+ else
+ {
+ local_pid= libmemcached_util_getpid2(hostname().c_str(), port(), username().c_str(), password().c_str(), &rc);
+ }
+
+ if (error_is_ok and ((memcached_failed(rc) or not is_pid_valid(local_pid))))
+ {
+ Error << "libmemcached_util_getpid2(" << memcached_strerror(NULL, rc) << ") username: " << username() << " password: " << password() << " pid: " << local_pid << " for:" << *this;
+ }
+
+ return local_pid;
+ }
+
+ bool ping()
+ {
+ // Memcached is slow to start, so we need to do this
+ if (not pid_file().empty())
+ {
+ Wait wait(pid_file(), 0);
+
+ if (not wait.successful())
+ {
+ Error << "Pidfile was not found:" << pid_file();
+ return -1;
+ }
+ }
+
+ memcached_return_t rc;
+ bool ret;
+
+ if (has_socket())
+ {
+ ret= libmemcached_util_ping2(socket().c_str(), 0, username().c_str(), password().c_str(), &rc);
+ }
+ else
+ {
+ ret= libmemcached_util_ping2(hostname().c_str(), port(), username().c_str(), password().c_str(), &rc);
+ }
+
+ if (memcached_failed(rc) or not ret)
+ {
+ Error << "libmemcached_util_ping2(" << hostname() << ", " << port() << ", " << username() << ", " << password() << ") error: " << memcached_strerror(NULL, rc);
+ }
+
+ return ret;
+ }
+
};
libtest::Server *build_memcached_sasl(const std::string& hostname, const in_port_t try_port, const std::string& username, const std::string &password)
{
+ if (username.empty())
+ {
+ return new MemcachedSaSL(hostname, try_port, false, "memcached", "memcached");
+ }
+
return new MemcachedSaSL(hostname, try_port, false, username, password);
}
libtest::Server *build_memcached_sasl_socket(const std::string& socket_file, const in_port_t try_port, const std::string& username, const std::string &password)
{
+ if (username.empty())
+ {
+ return new MemcachedSaSL(socket_file, try_port, true, "memcached", "memcached");
+ }
+
return new MemcachedSaSL(socket_file, try_port, true, username, password);
}
}
}
-std::string server_startup_st::option_string() const
-{
- std::string temp= server_list;
- rtrim(temp);
- return temp;
-}
-
// If the server exists, kill it
bool Server::cycle()
{
return false;
}
-void server_startup_st::push_server(Server *arg)
-{
- servers.push_back(arg);
-
- char port_str[NI_MAXSERV];
- snprintf(port_str, sizeof(port_str), "%u", int(arg->port()));
-
- std::string server_config_string;
- if (arg->has_socket())
- {
- server_config_string+= "--socket=";
- server_config_string+= '"';
- server_config_string+= arg->socket();
- server_config_string+= '"';
- server_config_string+= " ";
- }
- else
- {
- server_config_string+= "--server=";
- server_config_string+= arg->hostname();
- server_config_string+= ":";
- server_config_string+= port_str;
- server_config_string+= " ";
- }
-
- server_list+= server_config_string;
-
-}
-
-Server* server_startup_st::pop_server()
-{
- Server *tmp= servers.back();
- servers.pop_back();
- return tmp;
-}
-
-void server_startup_st::shutdown(bool remove)
-{
- if (remove)
- {
- for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
- {
- delete *iter;
- }
- servers.clear();
- }
- else
- {
- for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
- {
- if ((*iter)->has_pid() and not (*iter)->kill((*iter)->pid()))
- {
- Error << "Unable to kill:" << *(*iter);
- }
- }
- }
-}
-
-server_startup_st::~server_startup_st()
-{
- shutdown(true);
-}
-
-bool server_startup_st::is_debug() const
-{
- return bool(getenv("LIBTEST_MANUAL_GDB"));
-}
-
-bool server_startup_st::is_valgrind() const
-{
- return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
-}
-
-bool server_startup_st::is_helgrind() const
-{
- return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
-}
-
-
-bool server_startup(server_startup_st& construct, const std::string& server_type, in_port_t try_port, int argc, const char *argv[])
-{
- Outn();
- (void)try_port;
-
- // Look to see if we are being provided ports to use
- {
- char variable_buffer[1024];
- snprintf(variable_buffer, sizeof(variable_buffer), "LIBTEST_PORT_%lu", (unsigned long)construct.count());
-
- char *var;
- if ((var= getenv(variable_buffer)))
- {
- in_port_t tmp= in_port_t(atoi(var));
-
- if (tmp > 0)
- try_port= tmp;
- }
- }
-
- libtest::Server *server= NULL;
- if (0)
- { }
- else if (server_type.compare("gearmand") == 0)
- {
- if (GEARMAND_BINARY)
- {
- if (HAVE_LIBGEARMAN)
- {
- server= build_gearmand("localhost", try_port);
- }
- else
- {
- Error << "Libgearman was not found";
- }
- }
- else
- {
- Error << "No gearmand binary is available";
- }
- }
- else if (server_type.compare("blobslap_worker") == 0)
- {
- if (GEARMAND_BINARY and GEARMAND_BLOBSLAP_WORKER)
- {
- if (HAVE_LIBGEARMAN)
- {
- server= build_blobslap_worker(try_port);
- }
- else
- {
- Error << "Libgearman was not found";
- }
- }
- else
- {
- Error << "No gearmand binary is available";
- }
- }
- else if (server_type.compare("memcached-sasl") == 0)
- {
- if (MEMCACHED_SASL_BINARY)
- {
- if (HAVE_LIBMEMCACHED)
- {
- server= build_memcached_sasl("localhost", try_port, construct.username(), construct.password());
- }
- else
- {
- Error << "Libmemcached was not found";
- }
- }
- else
- {
- Error << "No memcached binary that was compiled with sasl is available";
- }
- }
- else if (server_type.compare("memcached") == 0)
- {
- if (MEMCACHED_BINARY)
- {
- if (HAVE_LIBMEMCACHED)
- {
- server= build_memcached("localhost", try_port);
- }
- else
- {
- Error << "Libmemcached was not found";
- }
- }
- else
- {
- Error << "No memcached binary is available";
- }
- }
- else
- {
- Error << "Failed to start " << server_type << ", no support was found to be compiled in for it.";
- }
-
- if (server == NULL)
- {
- Error << "Failure occured while creating server: " << server_type;
- return false;
- }
-
- /*
- We will now cycle the server we have created.
- */
- if (not server->cycle())
- {
- Error << "Could not start up server " << *server;
- delete server;
- return false;
- }
-
- server->build(argc, argv);
-
- if (construct.is_debug())
- {
- Out << "Pausing for startup, hit return when ready.";
- std::string gdb_command= server->base_command();
- std::string options;
- Out << "run " << server->args(options);
- getchar();
- }
- else if (not server->start())
- {
- Error << "Failed to start " << *server;
- delete server;
- return false;
- }
- else
- {
- Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
- }
-
- construct.push_server(server);
-
- if (default_port() == 0)
- {
- assert(server->has_port());
- set_default_port(server->port());
- }
-
- Outn();
-
- return true;
-}
-
-bool server_startup_st::start_socket_server(const std::string& server_type, const in_port_t try_port, int argc, const char *argv[])
-{
- (void)try_port;
- Outn();
-
- Server *server= NULL;
- if (0)
- { }
- else if (server_type.compare("gearmand") == 0)
- {
- Error << "Socket files are not supported for gearmand yet";
- }
- else if (server_type.compare("memcached-sasl") == 0)
- {
- if (MEMCACHED_SASL_BINARY)
- {
- if (HAVE_LIBMEMCACHED)
- {
- server= build_memcached_sasl_socket("localhost", try_port, username(), password());
- }
- else
- {
- Error << "Libmemcached was not found";
- }
- }
- else
- {
- Error << "No memcached binary is available";
- }
- }
- else if (server_type.compare("memcached") == 0)
- {
- if (MEMCACHED_BINARY)
- {
- if (HAVE_LIBMEMCACHED)
- {
- server= build_memcached_socket("localhost", try_port);
- }
- else
- {
- Error << "Libmemcached was not found";
- }
- }
- else
- {
- Error << "No memcached binary is available";
- }
- }
- else
- {
- Error << "Failed to start " << server_type << ", no support was found to be compiled in for it.";
- }
-
- if (server == NULL)
- {
- Error << "Failure occured while creating server: " << server_type;
- return false;
- }
-
- /*
- We will now cycle the server we have created.
- */
- if (not server->cycle())
- {
- Error << "Could not start up server " << *server;
- delete server;
- return false;
- }
-
- server->build(argc, argv);
-
- if (is_debug())
- {
- Out << "Pausing for startup, hit return when ready.";
- std::string gdb_command= server->base_command();
- std::string options;
- Out << "run " << server->args(options);
- getchar();
- }
- else if (not server->start())
- {
- Error << "Failed to start " << *server;
- delete server;
- return false;
- }
- else
- {
- Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
- }
-
- push_server(server);
-
- set_default_socket(server->socket().c_str());
-
- Outn();
-
- return true;
-}
-
} // namespace libtest
std::ostream& operator<<(std::ostream& output, const libtest::Server &arg);
-class server_startup_st
-{
-private:
- std::string server_list;
- bool _socket;
- bool _sasl;
- std::string _username;
- std::string _password;
-
-public:
-
- uint8_t udp;
- std::vector<Server *> servers;
-
- server_startup_st() :
- _socket(false),
- _sasl(false),
- udp(0)
- { }
-
- bool start_socket_server(const std::string& server_type, const in_port_t try_port, int argc, const char *argv[]);
-
- std::string option_string() const;
-
- size_t count() const
- {
- return servers.size();
- }
-
- const std::string& password() const
- {
- return _password;
- }
-
- const std::string& username() const
- {
- return _username;
- }
-
-
- bool is_debug() const;
- bool is_helgrind() const;
- bool is_valgrind() const;
-
- bool socket()
- {
- return _socket;
- }
-
- bool sasl()
- {
- return _sasl;
- }
-
- void set_socket()
- {
- _socket= true;
- }
-
- void set_sasl(const std::string& username_arg, const std::string& password_arg)
- {
- _sasl= true;
- _username= username_arg;
- _password= password_arg;
- }
-
-
- void shutdown(bool remove= false);
- void push_server(Server *);
- Server *pop_server();
-
- ~server_startup_st();
-};
-
-bool server_startup(server_startup_st&, const std::string&, in_port_t try_port, int argc, const char *argv[]);
-
} // namespace libtest
--- /dev/null
+/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
+ *
+ * libtest
+ *
+ * Copyright (C) 2011 Data Differential, http://datadifferential.com/
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <libtest/common.h>
+
+#include <cassert>
+#include <cerrno>
+#include <cstdlib>
+#include <iostream>
+
+#include <algorithm>
+#include <functional>
+#include <locale>
+
+// trim from end
+static inline std::string &rtrim(std::string &s)
+{
+ s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
+ return s;
+}
+
+namespace libtest {
+
+void server_startup_st::push_server(Server *arg)
+{
+ servers.push_back(arg);
+
+ char port_str[NI_MAXSERV];
+ snprintf(port_str, sizeof(port_str), "%u", int(arg->port()));
+
+ std::string server_config_string;
+ if (arg->has_socket())
+ {
+ server_config_string+= "--socket=";
+ server_config_string+= '"';
+ server_config_string+= arg->socket();
+ server_config_string+= '"';
+ server_config_string+= " ";
+ }
+ else
+ {
+ server_config_string+= "--server=";
+ server_config_string+= arg->hostname();
+ server_config_string+= ":";
+ server_config_string+= port_str;
+ server_config_string+= " ";
+ }
+
+ server_list+= server_config_string;
+
+}
+
+Server* server_startup_st::pop_server()
+{
+ Server *tmp= servers.back();
+ servers.pop_back();
+ return tmp;
+}
+
+void server_startup_st::shutdown(bool remove)
+{
+ if (remove)
+ {
+ for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
+ {
+ delete *iter;
+ }
+ servers.clear();
+ }
+ else
+ {
+ for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
+ {
+ if ((*iter)->has_pid() and not (*iter)->kill((*iter)->pid()))
+ {
+ Error << "Unable to kill:" << *(*iter);
+ }
+ }
+ }
+}
+
+server_startup_st::~server_startup_st()
+{
+ shutdown(true);
+}
+
+bool server_startup_st::is_debug() const
+{
+ return bool(getenv("LIBTEST_MANUAL_GDB"));
+}
+
+bool server_startup_st::is_valgrind() const
+{
+ return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
+}
+
+bool server_startup_st::is_helgrind() const
+{
+ return bool(getenv("LIBTEST_MANUAL_HELGRIND"));
+}
+
+
+bool server_startup(server_startup_st& construct, const std::string& server_type, in_port_t try_port, int argc, const char *argv[])
+{
+ Outn();
+ (void)try_port;
+
+ // Look to see if we are being provided ports to use
+ {
+ char variable_buffer[1024];
+ snprintf(variable_buffer, sizeof(variable_buffer), "LIBTEST_PORT_%lu", (unsigned long)construct.count());
+
+ char *var;
+ if ((var= getenv(variable_buffer)))
+ {
+ in_port_t tmp= in_port_t(atoi(var));
+
+ if (tmp > 0)
+ try_port= tmp;
+ }
+ }
+
+ libtest::Server *server= NULL;
+ if (0)
+ { }
+ else if (server_type.compare("gearmand") == 0)
+ {
+ if (GEARMAND_BINARY)
+ {
+ if (HAVE_LIBGEARMAN)
+ {
+ server= build_gearmand("localhost", try_port);
+ }
+ else
+ {
+ Error << "Libgearman was not found";
+ }
+ }
+ else
+ {
+ Error << "No gearmand binary is available";
+ }
+ }
+ else if (server_type.compare("blobslap_worker") == 0)
+ {
+ if (GEARMAND_BINARY and GEARMAND_BLOBSLAP_WORKER)
+ {
+ if (HAVE_LIBGEARMAN)
+ {
+ server= build_blobslap_worker(try_port);
+ }
+ else
+ {
+ Error << "Libgearman was not found";
+ }
+ }
+ else
+ {
+ Error << "No gearmand binary is available";
+ }
+ }
+ else if (server_type.compare("memcached-sasl") == 0)
+ {
+ if (MEMCACHED_SASL_BINARY)
+ {
+ if (HAVE_LIBMEMCACHED)
+ {
+ server= build_memcached_sasl("localhost", try_port, construct.username(), construct.password());
+ }
+ else
+ {
+ Error << "Libmemcached was not found";
+ }
+ }
+ else
+ {
+ Error << "No memcached binary that was compiled with sasl is available";
+ }
+ }
+ else if (server_type.compare("memcached") == 0)
+ {
+ if (MEMCACHED_BINARY)
+ {
+ if (HAVE_LIBMEMCACHED)
+ {
+ server= build_memcached("localhost", try_port);
+ }
+ else
+ {
+ Error << "Libmemcached was not found";
+ }
+ }
+ else
+ {
+ Error << "No memcached binary is available";
+ }
+ }
+ else
+ {
+ Error << "Failed to start " << server_type << ", no support was found to be compiled in for it.";
+ }
+
+ if (server == NULL)
+ {
+ Error << "Failure occured while creating server: " << server_type;
+ return false;
+ }
+
+ /*
+ We will now cycle the server we have created.
+ */
+ if (not server->cycle())
+ {
+ Error << "Could not start up server " << *server;
+ delete server;
+ return false;
+ }
+
+ server->build(argc, argv);
+
+ if (construct.is_debug())
+ {
+ Out << "Pausing for startup, hit return when ready.";
+ std::string gdb_command= server->base_command();
+ std::string options;
+ Out << "run " << server->args(options);
+ getchar();
+ }
+ else if (not server->start())
+ {
+ Error << "Failed to start " << *server;
+ delete server;
+ return false;
+ }
+ else
+ {
+ Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
+ }
+
+ construct.push_server(server);
+
+ if (default_port() == 0)
+ {
+ assert(server->has_port());
+ set_default_port(server->port());
+ }
+
+ Outn();
+
+ return true;
+}
+
+bool server_startup_st::start_socket_server(const std::string& server_type, const in_port_t try_port, int argc, const char *argv[])
+{
+ (void)try_port;
+ Outn();
+
+ Server *server= NULL;
+ if (0)
+ { }
+ else if (server_type.compare("gearmand") == 0)
+ {
+ Error << "Socket files are not supported for gearmand yet";
+ }
+ else if (server_type.compare("memcached-sasl") == 0)
+ {
+ if (MEMCACHED_SASL_BINARY)
+ {
+ if (HAVE_LIBMEMCACHED)
+ {
+ server= build_memcached_sasl_socket("localhost", try_port, username(), password());
+ }
+ else
+ {
+ Error << "Libmemcached was not found";
+ }
+ }
+ else
+ {
+ Error << "No memcached binary is available";
+ }
+ }
+ else if (server_type.compare("memcached") == 0)
+ {
+ if (MEMCACHED_BINARY)
+ {
+ if (HAVE_LIBMEMCACHED)
+ {
+ server= build_memcached_socket("localhost", try_port);
+ }
+ else
+ {
+ Error << "Libmemcached was not found";
+ }
+ }
+ else
+ {
+ Error << "No memcached binary is available";
+ }
+ }
+ else
+ {
+ Error << "Failed to start " << server_type << ", no support was found to be compiled in for it.";
+ }
+
+ if (server == NULL)
+ {
+ Error << "Failure occured while creating server: " << server_type;
+ return false;
+ }
+
+ /*
+ We will now cycle the server we have created.
+ */
+ if (not server->cycle())
+ {
+ Error << "Could not start up server " << *server;
+ delete server;
+ return false;
+ }
+
+ server->build(argc, argv);
+
+ if (is_debug())
+ {
+ Out << "Pausing for startup, hit return when ready.";
+ std::string gdb_command= server->base_command();
+ std::string options;
+ Out << "run " << server->args(options);
+ getchar();
+ }
+ else if (not server->start())
+ {
+ Error << "Failed to start " << *server;
+ delete server;
+ return false;
+ }
+ else
+ {
+ Out << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
+ }
+
+ push_server(server);
+
+ set_default_socket(server->socket().c_str());
+
+ Outn();
+
+ return true;
+}
+
+std::string server_startup_st::option_string() const
+{
+ std::string temp= server_list;
+ rtrim(temp);
+ return temp;
+}
+
+
+} // namespace libtest
--- /dev/null
+/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
+ *
+ * libtest
+ *
+ * Copyright (C) 2011 Data Differential, http://datadifferential.com/
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#pragma once
+
+#include <cassert>
+#include <cstdio>
+#include <cstring>
+#include <netdb.h>
+#include <netinet/in.h>
+#include <string>
+#include <unistd.h>
+#include <vector>
+
+namespace libtest {
+
+class server_startup_st
+{
+private:
+ std::string server_list;
+ bool _socket;
+ bool _sasl;
+ std::string _username;
+ std::string _password;
+
+public:
+
+ uint8_t udp;
+ std::vector<Server *> servers;
+
+ server_startup_st() :
+ _socket(false),
+ _sasl(false),
+ udp(0)
+ { }
+
+ bool start_socket_server(const std::string& server_type, const in_port_t try_port, int argc, const char *argv[]);
+
+ std::string option_string() const;
+
+ size_t count() const
+ {
+ return servers.size();
+ }
+
+ const std::string& password() const
+ {
+ return _password;
+ }
+
+ const std::string& username() const
+ {
+ return _username;
+ }
+
+
+ bool is_debug() const;
+ bool is_helgrind() const;
+ bool is_valgrind() const;
+
+ bool socket()
+ {
+ return _socket;
+ }
+
+ bool sasl()
+ {
+ return _sasl;
+ }
+
+ void set_socket()
+ {
+ _socket= true;
+ }
+
+ void set_sasl(const std::string& username_arg, const std::string& password_arg)
+ {
+ _sasl= true;
+ _username= username_arg;
+ _password= password_arg;
+ }
+
+
+ void shutdown(bool remove= false);
+ void push_server(Server *);
+ Server *pop_server();
+
+ ~server_startup_st();
+};
+
+bool server_startup(server_startup_st&, const std::string&, in_port_t try_port, int argc, const char *argv[]);
+
+} // namespace libtest
#include <libtest/error.h>
#include <libtest/server.h>
+#include <libtest/server_container.h>
#include <libtest/wait.h>
#include <libtest/callbacks.h>
#include <libtest/test.h>
return TEST_SKIPPED;
}
+static test_return_t memcached_sasl_test(void *object)
+{
+ server_startup_st *servers= (server_startup_st*)object;
+ test_true(servers);
+
+ if (getenv("TESTS_ENVIRONMENT"))
+ {
+ return TEST_SKIPPED;
+ }
+
+ if (MEMCACHED_SASL_BINARY and HAVE_LIBMEMCACHED)
+ {
+ const char *argv[1]= { "cycle_memcached_sasl" };
+ test_true(server_startup(*servers, "memcached-sasl", 9996, 1, argv));
+
+ return TEST_SUCCESS;
+ }
+
+ return TEST_SKIPPED;
+}
+
test_st gearmand_tests[] ={
#if 0
{"pause", 0, pause_test },
test_st memcached_tests[] ={
{"memcached startup-shutdown", 0, memcached_cycle_test },
{"memcached(socket file) startup-shutdown", 0, memcached_socket_cycle_test },
+ {"memcached_sasl() startup-shutdown", 0, memcached_sasl_test },
{0, 0, 0}
};
extern "C" {
#endif
-#define LIBTEST@_WITH_LIBMEMCACHED_SUPPORT@
-#define LIBTEST@_WITH_LIBGEARMAN_SUPPORT@
-
#define LIBTEST_VERSION_STRING "@VERSION@"
#define LIBTEST_VERSION_HEX @HEX_VERSION@
valgrind-plus: tests/testplus
@$(VALGRIND_COMMAND) tests/testplus
+valgrind-sasl: tests/sasl
+ @$(VALGRIND_COMMAND) tests/sasl
+
valgrind-hash: tests/testhashkit
@$(VALGRIND_COMMAND) tests/testhashkit
valgrind-hashplus: tests/hash_plus
@$(VALGRIND_COMMAND) tests/hash_plus
-.PHONY += valgrind
-valgrind: valgrind-cycle valgrind-mem valgrind-udp valgrind-plus valgrind-hash valgrind-hashplus
-
helgrind-cycle: tests/cycle
@$(HELGRIND_COMMAND) tests/cycle
return NULL;
}
+ // Assume we are running under valgrind, and bail
+ if (servers.sasl() and getenv("TESTS_ENVIRONMENT"))
+ {
+ error= TEST_SKIPPED;
+ return NULL;
+ }
+
+
in_port_t max_port;
for (uint32_t x= 0; x < SERVERS_TO_CREATE; x++)
{