Browse Source

Merge in test for gearadmin

Brian Aker 13 years ago
parent
commit
7f51cff283
10 changed files with 273 additions and 18 deletions
  1. 2 1
      .bzrignore
  2. 28 0
      libtest/cmdline.cc
  3. 1 0
      libtest/cmdline.h
  4. 3 6
      libtest/framework.cc
  5. 7 2
      libtest/gearmand.cc
  6. 8 7
      libtest/server.cc
  7. 9 1
      libtest/server.h
  8. 1 0
      libtest/test.hpp
  9. 200 0
      tests/gearadmin.cc
  10. 14 1
      tests/include.am

+ 2 - 1
.bzrignore

@@ -60,6 +60,7 @@ gearmand/gearmand_hostile
 gearmand/gearmand_test
 libgearman/version.h
 libhostile
+libtest/unittest
 libtest/wait
 libtool
 m4/libtool.m4
@@ -84,6 +85,7 @@ tests/client_test
 tests/client_test.res
 tests/cpp_test
 tests/drizzle_test
+tests/gearadmin
 tests/gearman.pi*
 tests/gearman.sql
 tests/gearmand.log*
@@ -106,4 +108,3 @@ tests/var/tmp/*
 tests/worker_test
 tests/worker_test.res
 unittests/unittests
-libtest/unittest

+ 28 - 0
libtest/cmdline.cc

@@ -34,3 +34,31 @@
  *
  */
 
+#include <libtest/common.h>
+
+#include <libtest/cmdline.h>
+
+#include <cstdlib>
+#include <string>
+#include <sstream>
+
+bool exec_cmdline(const std::string& executable, const char *args[])
+{
+  std::stringstream arg_buffer;
+
+  arg_buffer << "./libtool --mode=execute ";
+
+  arg_buffer << executable;
+  for (const char **ptr= args; *ptr; ++ptr)
+  {
+    arg_buffer << " " << *ptr;
+  }
+
+  arg_buffer << " > /dev/null 2>&1";
+  if (system(arg_buffer.str().c_str()) == -1)
+  {
+    return false;
+  }
+
+  return true;
+}

+ 1 - 0
libtest/cmdline.h

@@ -36,3 +36,4 @@
 
 #pragma once
 
+bool exec_cmdline(const std::string& executable, const char *args[]);

+ 3 - 6
libtest/framework.cc

@@ -77,14 +77,11 @@ Framework::Framework() :
 
 Framework::~Framework()
 {
-  server_shutdown(_servers);
+  _servers.shutdown();
 
-  if (_destroy)
+  if (_destroy and _destroy(_creators_ptr))
   {
-    if (_destroy(_creators_ptr))
-    {
-      Error << "Failure in _destroy(), some resources may not have been cleaned up.";
-    }
+    Error << "Failure in _destroy(), some resources may not have been cleaned up.";
   }
 }
 

+ 7 - 2
libtest/gearmand.cc

@@ -119,7 +119,6 @@ public:
         if (rc == GEARMAN_COULD_NOT_CONNECT)
         {
           sleep();
-          continue;
         }
       }
 
@@ -210,6 +209,11 @@ bool libtest::server_startup(server_startup_st& construct, in_port_t try_port, i
 
   Gearmand *server= new Gearmand("localhost", try_port);
   assert(server);
+  if (server == NULL)
+  {
+    Error << "Failure calling new for Gearmand";
+    return false;
+  }
 
   /*
     We will now cycle the server we have created.
@@ -217,6 +221,7 @@ bool libtest::server_startup(server_startup_st& construct, in_port_t try_port, i
   if (not server->cycle())
   {
     Error << "Could not start up server " << *server;
+    delete server;
     return false;
   }
 
@@ -238,7 +243,7 @@ bool libtest::server_startup(server_startup_st& construct, in_port_t try_port, i
   }
   else
   {
-    Log << "STARTING SERVER: " << server->running() << " pid:" << server->pid();
+    Log << "STARTING SERVER(pid:" << server->pid() << "): " << server->running();
   }
   construct.push_server(server);
 

+ 8 - 7
libtest/server.cc

@@ -116,7 +116,7 @@ bool Server::cycle()
   {
     if (kill())
     {
-      Error << "Killed existing server," << *this << " with pid:" << current_pid;
+      Log << "Killed existing server," << *this << " with pid:" << current_pid;
       sleep();
       continue;
     }
@@ -333,6 +333,13 @@ void server_startup_st::push_server(Server *arg)
   servers.push_back(arg);
 }
 
+Server* server_startup_st::pop_server()
+{
+  Server *tmp= servers.back();
+  servers.pop_back();
+  return tmp;
+}
+
 void server_startup_st::shutdown()
 {
   for (std::vector<Server *>::iterator iter= servers.begin(); iter != servers.end(); iter++)
@@ -363,10 +370,4 @@ bool server_startup_st::is_valgrind() const
   return bool(getenv("LIBTEST_MANUAL_VALGRIND"));
 }
 
-
-void server_shutdown(server_startup_st& construct)
-{
-  construct.shutdown();
-}
-
 } // namespace libtest

+ 9 - 1
libtest/server.h

@@ -86,6 +86,14 @@ public:
     return (_port != 0);
   }
 
+  // Reset a server if another process has killed the server
+  void reset()
+  {
+    _pid= -1;
+    _pid_file.clear();
+    _log_file.clear();
+  }
+
   void set_extra_args(const std::string &arg);
 
   bool args(std::string& options);
@@ -152,12 +160,12 @@ struct server_startup_st
 
   void shutdown();
   void push_server(Server *);
+  Server *pop_server();
 
   ~server_startup_st();
 };
 
 bool server_startup(server_startup_st&, in_port_t try_port, int argc, const char *argv[]);
-void server_shutdown(server_startup_st&);
 
 } // namespace libtest
 

+ 1 - 0
libtest/test.hpp

@@ -59,6 +59,7 @@
 #include <libtest/framework.h>
 #include <libtest/get.h>
 #include <libtest/stream.h>
+#include <libtest/cmdline.h>
 
 #pragma once
 

+ 200 - 0
tests/gearadmin.cc

@@ -0,0 +1,200 @@
+/*  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
+ * 
+ *  Cycle the Gearmand server
+ *
+ *  Copyright (C) 2011 Data Differential, http://datadifferential.com/
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions are
+ *  met:
+ *
+ *      * Redistributions of source code must retain the above copyright
+ *  notice, this list of conditions and the following disclaimer.
+ *
+ *      * Redistributions in binary form must reproduce the above
+ *  copyright notice, this list of conditions and the following disclaimer
+ *  in the documentation and/or other materials provided with the
+ *  distribution.
+ *
+ *      * The names of its contributors may not be used to endorse or
+ *  promote products derived from this software without specific prior
+ *  written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+
+/*
+  Test that we are cycling the servers we are creating during testing.
+*/
+
+#include <libtest/test.hpp>
+
+using namespace libtest;
+
+#ifndef __INTEL_COMPILER
+#pragma GCC diagnostic ignored "-Wstrict-aliasing"
+#endif
+
+#include "tests/ports.h"
+
+static std::string executable;
+
+static test_return_t help_test(void *)
+{
+  char buffer[1024];
+  snprintf(buffer, sizeof(buffer), "--port=%d", int(default_port()));
+  const char *args[]= { buffer, "--help", 0 };
+
+  test_success(exec_cmdline(executable, args));
+  return TEST_SUCCESS;
+}
+
+static test_return_t shutdown_test(void *object)
+{
+  server_startup_st *servers= (server_startup_st *)object;
+
+  char buffer[1024];
+  snprintf(buffer, sizeof(buffer), "--port=%d", int(default_port()));
+  const char *args[]= { buffer, "--shutdown", 0 };
+
+  test_success(exec_cmdline(executable, args));
+
+  // We have already shut the server down so it should fail
+  test_failed(exec_cmdline(executable, args));
+
+  Server *server= servers->pop_server();
+  test_true(server);
+  test_failed(server->ping());
+
+  // Since we killed the server above, we need to reset it
+  server->reset();
+  delete server;
+
+  return TEST_SUCCESS;
+}
+
+static test_return_t version_test(void *)
+{
+  char buffer[1024];
+  snprintf(buffer, sizeof(buffer), "--port=%d", int(default_port()));
+  const char *args[]= { buffer, "--server-version", 0 };
+
+  test_success(exec_cmdline(executable, args));
+  return TEST_SUCCESS;
+}
+
+static test_return_t verbose_test(void *)
+{
+  char buffer[1024];
+  snprintf(buffer, sizeof(buffer), "--port=%d", int(default_port()));
+  const char *args[]= { buffer, "--server-verbose", 0 };
+
+  test_success(exec_cmdline(executable, args));
+  return TEST_SUCCESS;
+}
+
+static test_return_t status_test(void *)
+{
+  char buffer[1024];
+  snprintf(buffer, sizeof(buffer), "--port=%d", int(default_port()));
+  const char *args[]= { buffer, "--status", 0 };
+
+  test_success(exec_cmdline(executable, args));
+  return TEST_SUCCESS;
+}
+
+static test_return_t workers_test(void *)
+{
+  char buffer[1024];
+  snprintf(buffer, sizeof(buffer), "--port=%d", int(default_port()));
+  const char *args[]= { buffer, "--workers", 0 };
+
+  test_success(exec_cmdline(executable, args));
+  return TEST_SUCCESS;
+}
+
+static test_return_t create_drop_test(void *)
+{
+  char buffer[1024];
+  snprintf(buffer, sizeof(buffer), "--port=%d", int(default_port()));
+
+  const char *create_args[]= { buffer, "--create-function=test_function", 0 };
+  test_success(exec_cmdline(executable, create_args));
+
+  const char *drop_args[]= { buffer, "--drop-function=test_function", 0 };
+  test_success(exec_cmdline(executable, drop_args));
+
+
+  return TEST_SUCCESS;
+}
+
+static test_return_t getpid_test(void *)
+{
+  char buffer[1024];
+  snprintf(buffer, sizeof(buffer), "--port=%d", int(default_port()));
+  const char *args[]= { buffer, "--getpid", 0 };
+
+  test_success(exec_cmdline(executable, args));
+  return TEST_SUCCESS;
+}
+
+static test_return_t unknown_test(void *)
+{
+  char buffer[1024];
+  snprintf(buffer, sizeof(buffer), "--port=%d", int(default_port()));
+  const char *args[]= { buffer, "--unknown", 0 };
+
+  // The argument doesn't exist, so we should see an error
+  test_failed(exec_cmdline(executable, args));
+  return TEST_SUCCESS;
+}
+
+test_st gearadmin_tests[] ={
+  {"--help", 0, help_test},
+  {"--server-version", 0, version_test},
+  {"--server-verbose", 0, verbose_test},
+  {"--status", 0, status_test},
+  {"--getpid", 0, getpid_test},
+  {"--workers", 0, workers_test},
+  {"--create-function and --drop-function", 0, create_drop_test},
+  {"--unknown", 0, unknown_test},
+  {"--shutdown", 0, shutdown_test}, // Must be run last since it shuts down the server
+  {0, 0, 0}
+};
+
+collection_st collection[] ={
+  {"gearadmin", 0, 0, gearadmin_tests},
+  {0, 0, 0, 0}
+};
+
+static void *world_create(server_startup_st& servers, test_return_t& error)
+{
+  const char *argv[1]= { "gearadmin_gearmand" };
+  if (not server_startup(servers, GEARADMIN_TEST_PORT, 1, argv))
+  {
+    error= TEST_FAILURE;
+  }
+
+  return &servers;
+}
+
+
+void get_world(Framework *world)
+{
+  executable= "./bin/gearadmin";
+  world->collections= collection;
+  world->_create= world_create;
+}
+

+ 14 - 1
tests/include.am

@@ -63,6 +63,10 @@ tests_burnin_test_LDADD= ${CLIENT_LDADD}
 tests_cycle_SOURCES= tests/cycle.cc
 tests_cycle_LDADD= ${CLIENT_LDADD}
 
+tests_gearadmin_SOURCES= tests/gearadmin.cc
+tests_gearadmin_LDADD= ${CLIENT_LDADD}
+check_PROGRAMS+= tests/gearadmin
+
 tests_internals_test_SOURCES= \
 			      tests/internals.cc \
 			      tests/regression.cc
@@ -92,6 +96,9 @@ test-burnin: tests/burnin_test
 test-cycle: tests/cycle
 	@tests/cycle $(ARG1) $(ARG2)
 
+test-gearadmin: tests/gearadmin
+	@tests/gearadmin $(ARG1) $(ARG2)
+
 test-client: tests/client_test
 	@tests/client_test $(ARG1) $(ARG2)
 
@@ -126,6 +133,9 @@ gdb-burnin: ${noinst_PROGRAMS}
 gdb-cycle: ${noinst_PROGRAMS}
 	@$(LIBTOOL) --mode=execute gdb tests/cycle
 
+gdb-gearadmin: ${noinst_PROGRAMS}
+	@$(LIBTOOL) --mode=execute gdb tests/gearadmin
+
 valgrind-client:
 	@$(VALGRIND_COMMAND) tests/client_test $(ARG1) $(ARG2)
 
@@ -141,10 +151,13 @@ valgrind-internals: tests/internals_test
 valgrind-burnin: tests/burnin_test
 	@$(VALGRIND_COMMAND) tests/burnin_test $(ARG1) $(ARG2)
 
+valgrind-gearadmin: tests/gearadmin
+	@$(VALGRIND_COMMAND) tests/gearadmin $(ARG1) $(ARG2)
+
 valgrind-cycle: tests/cycle
 	@$(VALGRIND_COMMAND) tests/cycle $(ARG1) $(ARG2)
 
-valgrind: ${noinst_PROGRAMS} valgrind-cycle valgrind-client valgrind-round-robin valgrind-worker valgrind-internals valgrind-sqlite valgrind-tokyocabinet valgrind-burnin
+valgrind: ${noinst_PROGRAMS} valgrind-gearadmin valgrind-cycle valgrind-client valgrind-round-robin valgrind-worker valgrind-internals valgrind-sqlite valgrind-tokyocabinet valgrind-burnin
 
 include tests/httpd.am
 include tests/libdrizzle.am

Some files were not shown because too many files changed in this diff