1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <libtest/test.hpp>
27 #if defined(LIBTEST_WITH_LIBMEMCACHED_SUPPORT) && LIBTEST_WITH_LIBMEMCACHED_SUPPORT
28 #include <libmemcached/memcached.h>
31 #if defined(LIBTEST_WITH_LIBGEARMAN_SUPPORT) && LIBTEST_WITH_LIBGEARMAN_SUPPORT
32 #include <libgearman/gearman.h>
38 using namespace libtest
;
40 static test_return_t
LIBTOOL_COMMAND_test(void *)
42 test_true(getenv("LIBTOOL_COMMAND"));
46 static test_return_t
VALGRIND_COMMAND_test(void *)
48 test_true(getenv("VALGRIND_COMMAND"));
52 static test_return_t
HELGRIND_COMMAND_test(void *)
54 test_true(getenv("HELGRIND_COMMAND"));
58 static test_return_t
GDB_COMMAND_test(void *)
60 test_true(getenv("GDB_COMMAND"));
64 static test_return_t
test_success_equals_one_test(void *)
66 test_skip(HAVE_LIBMEMCACHED
, true);
67 #if defined(HAVE_LIBMEMCACHED) && HAVE_LIBMEMCACHED
68 test_zero(MEMCACHED_SUCCESS
);
73 static test_return_t
test_success_test(void *)
78 static test_return_t
test_failure_test(void *)
80 return TEST_SKIPPED
; // Only run this when debugging
86 static test_return_t
local_test(void *)
88 if (getenv("LIBTEST_LOCAL"))
90 test_true(test_is_local());
94 test_false(test_is_local());
100 static test_return_t
local_not_test(void *)
107 if ((ptr
= getenv("LIBTEST_LOCAL")) == NULL
)
112 // unsetenv() will cause issues with valgrind
113 _compare(__FILE__
, __LINE__
, __func__
, 0, unsetenv("LIBTEST_LOCAL"));
114 test_compare(0, unsetenv("LIBTEST_LOCAL"));
115 test_false(test_is_local());
117 test_compare(0, setenv("LIBTEST_LOCAL", "1", 1));
118 test_true(test_is_local());
122 test_compare(0, unsetenv("LIBTEST_LOCAL"));
126 char *old_string
= strdup(temp
.c_str());
127 test_compare(0, setenv("LIBTEST_LOCAL", old_string
, 1));
133 static test_return_t
var_exists_test(void *)
135 test_compare(0, access("var", R_OK
| W_OK
| X_OK
));
139 static test_return_t
var_tmp_exists_test(void *)
141 test_compare(0, access("var/tmp", R_OK
| W_OK
| X_OK
));
145 static test_return_t
var_run_exists_test(void *)
147 test_compare(0, access("var/run", R_OK
| W_OK
| X_OK
));
151 static test_return_t
var_log_exists_test(void *)
153 test_compare(0, access("var/log", R_OK
| W_OK
| X_OK
));
157 static test_return_t
var_tmp_test(void *)
159 FILE *file
= fopen("var/tmp/junk", "w+");
161 const char *dir
= getcwd(buffer
, sizeof(buffer
));
162 test_true_got(file
, dir
);
167 static test_return_t
var_run_test(void *)
169 FILE *file
= fopen("var/run/junk", "w+");
175 static test_return_t
var_log_test(void *)
177 FILE *file
= fopen("var/log/junk", "w+");
183 static test_return_t
var_tmp_rm_test(void *)
185 test_true(unlink("var/tmp/junk") == 0);
189 static test_return_t
var_run_rm_test(void *)
191 test_true(unlink("var/run/junk") == 0);
195 static test_return_t
var_log_rm_test(void *)
197 test_true(unlink("var/log/junk") == 0);
201 static test_return_t
_compare_test_return_t_test(void *)
203 test_compare(TEST_SUCCESS
, TEST_SUCCESS
);
208 static test_return_t
_compare_memcached_return_t_test(void *)
210 test_skip(HAVE_LIBMEMCACHED
, true);
211 #if defined(HAVE_LIBMEMCACHED) && HAVE_LIBMEMCACHED
212 test_compare(MEMCACHED_SUCCESS
, MEMCACHED_SUCCESS
);
218 static test_return_t
_compare_gearman_return_t_test(void *)
220 test_skip(HAVE_LIBGEARMAN
, true);
221 #if defined(HAVE_LIBGEARMAN) && HAVE_LIBGEARMAN
222 test_compare(GEARMAN_SUCCESS
, GEARMAN_SUCCESS
);
228 static test_return_t
gearmand_cycle_test(void *object
)
230 server_startup_st
*servers
= (server_startup_st
*)object
;
233 if (HAVE_LIBGEARMAN
and GEARMAND_BINARY
)
235 test_true(has_gearmand_binary());
236 const char *argv
[1]= { "cycle_gearmand" };
237 test_true(server_startup(*servers
, "gearmand", 9999, 1, argv
));
245 static test_return_t
memcached_cycle_test(void *object
)
247 server_startup_st
*servers
= (server_startup_st
*)object
;
250 if (MEMCACHED_BINARY
and HAVE_LIBMEMCACHED
)
252 test_true(has_memcached_binary());
253 const char *argv
[1]= { "cycle_memcached" };
254 test_true(server_startup(*servers
, "memcached", 9998, 1, argv
));
262 static test_return_t
memcached_socket_cycle_test(void *object
)
264 server_startup_st
*servers
= (server_startup_st
*)object
;
267 if (MEMCACHED_BINARY
and HAVE_LIBMEMCACHED
)
269 test_true(has_memcached_binary());
270 const char *argv
[1]= { "cycle_memcached" };
271 test_true(servers
->start_socket_server("memcached", 9997, 1, argv
));
279 static test_return_t
memcached_sasl_test(void *object
)
281 server_startup_st
*servers
= (server_startup_st
*)object
;
284 if (getenv("TESTS_ENVIRONMENT"))
289 if (MEMCACHED_SASL_BINARY
and HAVE_LIBMEMCACHED
)
291 test_true(has_memcached_sasl_binary());
292 const char *argv
[1]= { "cycle_memcached_sasl" };
293 test_true(server_startup(*servers
, "memcached-sasl", 9996, 1, argv
));
301 static test_return_t
wait_BINARY(void *)
303 const char *args
[]= { "--quiet", 0 };
305 test_true(exec_cmdline("libtest/wait", args
));
310 static test_return_t
wait_help_BINARY(void *)
312 const char *args
[]= { "--quiet", "--help", 0 };
314 test_true(exec_cmdline("libtest/wait", args
));
319 static test_return_t
wait_version_BINARY(void *)
321 const char *args
[]= { "--quiet", "--version", 0 };
323 test_true(exec_cmdline("libtest/wait", args
));
328 test_st gearmand_tests
[] ={
330 {"pause", 0, pause_test
},
332 {"gearmand startup-shutdown", 0, gearmand_cycle_test
},
336 test_st memcached_tests
[] ={
337 {"memcached startup-shutdown", 0, memcached_cycle_test
},
338 {"memcached(socket file) startup-shutdown", 0, memcached_socket_cycle_test
},
339 {"memcached_sasl() startup-shutdown", 0, memcached_sasl_test
},
343 test_st environment_tests
[] ={
344 {"LIBTOOL_COMMAND", 0, LIBTOOL_COMMAND_test
},
345 {"VALGRIND_COMMAND", 0, VALGRIND_COMMAND_test
},
346 {"HELGRIND_COMMAND", 0, HELGRIND_COMMAND_test
},
347 {"GDB_COMMAND", 0, GDB_COMMAND_test
},
351 test_st tests_log
[] ={
352 {"TEST_SUCCESS", false, test_success_test
},
353 {"TEST_FAILURE", false, test_failure_test
},
354 {"TEST_SUCCESS == 0", false, test_success_equals_one_test
},
358 test_st local_log
[] ={
359 {"test_is_local()", 0, local_test
},
360 {"test_is_local(NOT)", 0, local_not_test
},
364 test_st directories_tests
[] ={
365 {"var exists", 0, var_exists_test
},
366 {"var/tmp exists", 0, var_tmp_exists_test
},
367 {"var/run exists", 0, var_run_exists_test
},
368 {"var/log exists", 0, var_log_exists_test
},
369 {"var/tmp", 0, var_tmp_test
},
370 {"var/run", 0, var_run_test
},
371 {"var/log", 0, var_log_test
},
372 {"var/tmp rm", 0, var_tmp_rm_test
},
373 {"var/run rm", 0, var_run_rm_test
},
374 {"var/log rm", 0, var_log_rm_test
},
378 test_st comparison_tests
[] ={
379 {"_compare(test_return_t)", 0, _compare_test_return_t_test
},
380 {"_compare(memcached_return_t)", 0, _compare_memcached_return_t_test
},
381 {"_compare(gearman_return_t)", 0, _compare_gearman_return_t_test
},
385 test_st cmdline_tests
[] ={
386 {"wait --quiet", 0, wait_BINARY
},
387 {"wait --quiet --help", 0, wait_help_BINARY
},
388 {"wait --quiet --version", 0, wait_version_BINARY
},
392 collection_st collection
[] ={
393 {"environment", 0, 0, environment_tests
},
394 {"return values", 0, 0, tests_log
},
395 {"local", 0, 0, local_log
},
396 {"directories", 0, 0, directories_tests
},
397 {"comparison", 0, 0, comparison_tests
},
398 {"gearmand", 0, 0, gearmand_tests
},
399 {"memcached", 0, 0, memcached_tests
},
400 {"cmdline", 0, 0, cmdline_tests
},
404 static void *world_create(server_startup_st
& servers
, test_return_t
&)
409 void get_world(Framework
*world
)
411 world
->collections
= collection
;
412 world
->_create
= world_create
;