gearadmin.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Cycle the Gearmand server
  4. *
  5. * Copyright (C) 2011 Data Differential, http://datadifferential.com/
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are
  9. * met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * * Redistributions in binary form must reproduce the above
  15. * copyright notice, this list of conditions and the following disclaimer
  16. * in the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * * The names of its contributors may not be used to endorse or
  20. * promote products derived from this software without specific prior
  21. * written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  26. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  27. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  28. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  29. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  30. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  32. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. */
  36. /*
  37. Test that we are cycling the servers we are creating during testing.
  38. */
  39. #include <libtest/test.hpp>
  40. using namespace libtest;
  41. #ifndef __INTEL_COMPILER
  42. #pragma GCC diagnostic ignored "-Wstrict-aliasing"
  43. #endif
  44. #include "tests/ports.h"
  45. static std::string executable;
  46. static test_return_t help_test(void *)
  47. {
  48. char buffer[1024];
  49. snprintf(buffer, sizeof(buffer), "--port=%d", int(default_port()));
  50. const char *args[]= { buffer, "--help", 0 };
  51. test_success(exec_cmdline(executable, args));
  52. return TEST_SUCCESS;
  53. }
  54. static test_return_t shutdown_test(void *object)
  55. {
  56. server_startup_st *servers= (server_startup_st *)object;
  57. char buffer[1024];
  58. snprintf(buffer, sizeof(buffer), "--port=%d", int(default_port()));
  59. const char *args[]= { buffer, "--shutdown", 0 };
  60. test_success(exec_cmdline(executable, args));
  61. // We have already shut the server down so it should fail
  62. test_failed(exec_cmdline(executable, args));
  63. Server *server= servers->pop_server();
  64. test_true(server);
  65. test_failed(server->ping());
  66. // Since we killed the server above, we need to reset it
  67. server->reset();
  68. delete server;
  69. return TEST_SUCCESS;
  70. }
  71. static test_return_t version_test(void *)
  72. {
  73. char buffer[1024];
  74. snprintf(buffer, sizeof(buffer), "--port=%d", int(default_port()));
  75. const char *args[]= { buffer, "--server-version", 0 };
  76. test_success(exec_cmdline(executable, args));
  77. return TEST_SUCCESS;
  78. }
  79. static test_return_t verbose_test(void *)
  80. {
  81. char buffer[1024];
  82. snprintf(buffer, sizeof(buffer), "--port=%d", int(default_port()));
  83. const char *args[]= { buffer, "--server-verbose", 0 };
  84. test_success(exec_cmdline(executable, args));
  85. return TEST_SUCCESS;
  86. }
  87. static test_return_t status_test(void *)
  88. {
  89. char buffer[1024];
  90. snprintf(buffer, sizeof(buffer), "--port=%d", int(default_port()));
  91. const char *args[]= { buffer, "--status", 0 };
  92. test_success(exec_cmdline(executable, args));
  93. return TEST_SUCCESS;
  94. }
  95. static test_return_t workers_test(void *)
  96. {
  97. char buffer[1024];
  98. snprintf(buffer, sizeof(buffer), "--port=%d", int(default_port()));
  99. const char *args[]= { buffer, "--workers", 0 };
  100. test_success(exec_cmdline(executable, args));
  101. return TEST_SUCCESS;
  102. }
  103. static test_return_t create_drop_test(void *)
  104. {
  105. char buffer[1024];
  106. snprintf(buffer, sizeof(buffer), "--port=%d", int(default_port()));
  107. const char *create_args[]= { buffer, "--create-function=test_function", 0 };
  108. test_success(exec_cmdline(executable, create_args));
  109. const char *drop_args[]= { buffer, "--drop-function=test_function", 0 };
  110. test_success(exec_cmdline(executable, drop_args));
  111. return TEST_SUCCESS;
  112. }
  113. static test_return_t getpid_test(void *)
  114. {
  115. char buffer[1024];
  116. snprintf(buffer, sizeof(buffer), "--port=%d", int(default_port()));
  117. const char *args[]= { buffer, "--getpid", 0 };
  118. test_success(exec_cmdline(executable, args));
  119. return TEST_SUCCESS;
  120. }
  121. static test_return_t unknown_test(void *)
  122. {
  123. char buffer[1024];
  124. snprintf(buffer, sizeof(buffer), "--port=%d", int(default_port()));
  125. const char *args[]= { buffer, "--unknown", 0 };
  126. // The argument doesn't exist, so we should see an error
  127. test_failed(exec_cmdline(executable, args));
  128. return TEST_SUCCESS;
  129. }
  130. test_st gearadmin_tests[] ={
  131. {"--help", 0, help_test},
  132. {"--server-version", 0, version_test},
  133. {"--server-verbose", 0, verbose_test},
  134. {"--status", 0, status_test},
  135. {"--getpid", 0, getpid_test},
  136. {"--workers", 0, workers_test},
  137. {"--create-function and --drop-function", 0, create_drop_test},
  138. {"--unknown", 0, unknown_test},
  139. {"--shutdown", 0, shutdown_test}, // Must be run last since it shuts down the server
  140. {0, 0, 0}
  141. };
  142. collection_st collection[] ={
  143. {"gearadmin", 0, 0, gearadmin_tests},
  144. {0, 0, 0, 0}
  145. };
  146. static void *world_create(server_startup_st& servers, test_return_t& error)
  147. {
  148. const char *argv[1]= { "gearadmin_gearmand" };
  149. if (not server_startup(servers, GEARADMIN_TEST_PORT, 1, argv))
  150. {
  151. error= TEST_FAILURE;
  152. }
  153. return &servers;
  154. }
  155. void get_world(Framework *world)
  156. {
  157. executable= "./bin/gearadmin";
  158. world->collections= collection;
  159. world->_create= world_create;
  160. }