Second pass on update of yatl.
[awesomized/libmemcached] / libtest / unittest.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Data Differential YATL (i.e. libtest) library
4 *
5 * Copyright (C) 2012 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 #include "libtest/yatlcon.h"
38
39 #include <libtest/yatl.h>
40
41 #if defined(HAVE_LIBMEMCACHED_1_0_TYPES_RETURN_H) && HAVE_LIBMEMCACHED_1_0_TYPES_RETURN_H
42 # include <libmemcached-1.0/types/return.h>
43 #endif
44
45 #if defined(HAVE_LIBGEARMAN_1_0_RETURN_H) && HAVE_LIBGEARMAN_1_0_RETURN_H
46 # include <libgearman-1.0/return.h>
47 #endif
48
49 #include <cstdlib>
50 #include <unistd.h>
51
52 using namespace libtest;
53
54 static std::string testing_service;
55
56 // Used to track setups where we see if failure is happening
57 static uint32_t fatal_calls= 0;
58
59 static test_return_t getenv_TEST(void *)
60 {
61 #if 0
62 for (char **ptr= environ; *ptr; ptr++)
63 {
64 Error << *ptr;
65 }
66 #endif
67
68 return TEST_SUCCESS;
69 }
70
71 static test_return_t LIBTOOL_COMMAND_test(void *)
72 {
73 test_true(getenv("LIBTOOL_COMMAND"));
74 return TEST_SUCCESS;
75 }
76
77 static test_return_t VALGRIND_COMMAND_test(void *)
78 {
79 test_true(getenv("VALGRIND_COMMAND"));
80 return TEST_SUCCESS;
81 }
82
83 static test_return_t HELGRIND_COMMAND_test(void *)
84 {
85 test_true(getenv("HELGRIND_COMMAND"));
86 return TEST_SUCCESS;
87 }
88
89 static test_return_t GDB_COMMAND_test(void *)
90 {
91 test_true(getenv("GDB_COMMAND"));
92 return TEST_SUCCESS;
93 }
94
95 static test_return_t test_success_equals_one_test(void *)
96 {
97 test_skip(HAVE_LIBMEMCACHED, 1);
98 #if defined(HAVE_LIBMEMCACHED_1_0_TYPES_RETURN_H) && HAVE_LIBMEMCACHED_1_0_TYPES_RETURN_H
99 test_zero(MEMCACHED_SUCCESS);
100 #endif
101 return TEST_SUCCESS;
102 }
103
104 static test_return_t test_success_test(void *)
105 {
106 return TEST_SUCCESS;
107 }
108
109 static test_return_t test_throw_success_TEST(void *)
110 {
111 try {
112 _SUCCESS;
113 }
114 catch (libtest::__success)
115 {
116 return TEST_SUCCESS;
117 }
118 catch (...)
119 {
120 return TEST_FAILURE;
121 }
122
123 return TEST_FAILURE;
124 }
125
126 static test_return_t test_throw_skip_TEST(void *)
127 {
128 try {
129 SKIP;
130 }
131 catch (libtest::__skipped)
132 {
133 return TEST_SUCCESS;
134 }
135 catch (...)
136 {
137 return TEST_FAILURE;
138 }
139
140 return TEST_FAILURE;
141 }
142
143 static test_return_t test_throw_fail_TEST(void *)
144 {
145 try {
146 FAIL("test message!");
147 }
148 catch (const libtest::__failure& e)
149 {
150 std::string compare_message("test message!");
151 test_zero(compare_message.compare(e.what()));
152 return TEST_SUCCESS;
153 }
154 catch (...)
155 {
156 return TEST_FAILURE;
157 }
158
159 return TEST_FAILURE;
160 }
161 #pragma GCC diagnostic ignored "-Wstack-protector"
162
163 static test_return_t ASSERT_FALSE__TEST(void *)
164 {
165 try {
166 ASSERT_FALSE_(true, __func__);
167 }
168 catch (const libtest::__failure& e)
169 {
170 ASSERT_STREQ(e.what(), "Assertion '!true' [ ASSERT_FALSE__TEST ]");
171 return TEST_SUCCESS;
172 }
173 catch (...)
174 {
175 return TEST_FAILURE;
176 }
177
178 return TEST_FAILURE;
179 }
180
181 static test_return_t ASSERT_FALSE_TEST(void *)
182 {
183 try {
184 FAIL(__func__);
185 }
186 catch (const libtest::__failure& e)
187 {
188 ASSERT_STREQ(e.what(), __func__);
189 return TEST_SUCCESS;
190 }
191 catch (...)
192 {
193 return TEST_FAILURE;
194 }
195
196 return TEST_FAILURE;
197 }
198
199 static test_return_t test_failure_test(void *)
200 {
201 return TEST_SKIPPED; // Only run this when debugging
202
203 test_compare(1, 2);
204 return TEST_SUCCESS;
205 }
206
207 static test_return_t local_test(void *)
208 {
209 if (getenv("LIBTEST_LOCAL"))
210 {
211 test_true(test_is_local());
212 }
213 else
214 {
215 test_false(test_is_local());
216 }
217
218 return TEST_SUCCESS;
219 }
220
221 static test_return_t local_not_test(void *)
222 {
223 return TEST_SKIPPED;
224
225 std::string temp;
226
227 const char *ptr;
228 if ((ptr= getenv("LIBTEST_LOCAL")) == NULL)
229 {
230 temp.append(ptr);
231 }
232
233 // unsetenv() will cause issues with valgrind
234 _compare(__FILE__, __LINE__, __func__, 0, unsetenv("LIBTEST_LOCAL"), true);
235 test_compare(0, unsetenv("LIBTEST_LOCAL"));
236 test_false(test_is_local());
237
238 test_compare(0, setenv("LIBTEST_LOCAL", "1", 1));
239 test_true(test_is_local());
240
241 if (temp.empty())
242 {
243 test_compare(0, unsetenv("LIBTEST_LOCAL"));
244 }
245 else
246 {
247 char *old_string= strdup(temp.c_str());
248 test_compare(0, setenv("LIBTEST_LOCAL", old_string, 1));
249 }
250
251 return TEST_SUCCESS;
252 }
253
254 static test_return_t var_exists_test(void *)
255 {
256 test_compare(0, access("var", R_OK | W_OK | X_OK));
257 return TEST_SUCCESS;
258 }
259
260 static test_return_t var_tmp_exists_test(void *)
261 {
262 test_compare(0, access("var/tmp", R_OK | W_OK | X_OK));
263 return TEST_SUCCESS;
264 }
265
266 static test_return_t var_run_exists_test(void *)
267 {
268 test_compare(0, access("var/run", R_OK | W_OK | X_OK));
269 return TEST_SUCCESS;
270 }
271
272 static test_return_t var_log_exists_test(void *)
273 {
274 test_compare(0, access("var/log", R_OK | W_OK | X_OK));
275 return TEST_SUCCESS;
276 }
277
278 static test_return_t var_drizzle_exists_test(void *)
279 {
280 test_compare(0, access("var/drizzle", R_OK | W_OK | X_OK));
281 return TEST_SUCCESS;
282 }
283
284 static test_return_t var_tmp_test(void *)
285 {
286 FILE *file= fopen("var/tmp/junk", "w+");
287 test_true(file);
288 fclose(file);
289 return TEST_SUCCESS;
290 }
291
292 static test_return_t var_run_test(void *)
293 {
294 FILE *file= fopen("var/run/junk", "w+");
295 test_true(file);
296 fclose(file);
297 return TEST_SUCCESS;
298 }
299
300 static test_return_t var_log_test(void *)
301 {
302 FILE *file= fopen("var/log/junk", "w+");
303 test_true(file);
304 fclose(file);
305 return TEST_SUCCESS;
306 }
307
308 static test_return_t var_drizzle_test(void *)
309 {
310 FILE *file= fopen("var/drizzle/junk", "w+");
311 test_true(file);
312 fclose(file);
313 return TEST_SUCCESS;
314 }
315
316 static test_return_t var_tmp_rm_test(void *)
317 {
318 test_true(unlink("var/tmp/junk") == 0);
319 return TEST_SUCCESS;
320 }
321
322 static test_return_t var_run_rm_test(void *)
323 {
324 test_true(unlink("var/run/junk") == 0);
325 return TEST_SUCCESS;
326 }
327
328 static test_return_t var_log_rm_test(void *)
329 {
330 test_true(unlink("var/log/junk") == 0);
331 return TEST_SUCCESS;
332 }
333
334 static test_return_t var_drizzle_rm_test(void *)
335 {
336 test_true(unlink("var/drizzle/junk") == 0);
337 return TEST_SUCCESS;
338 }
339
340 static test_return_t _compare_test_return_t_test(void *)
341 {
342 test_compare(TEST_SUCCESS, TEST_SUCCESS);
343
344 return TEST_SUCCESS;
345 }
346
347 static test_return_t _compare_memcached_return_t_test(void *)
348 {
349 test_skip(HAVE_LIBMEMCACHED, true);
350 #if defined(HAVE_LIBMEMCACHED_1_0_TYPES_RETURN_H) && HAVE_LIBMEMCACHED_1_0_TYPES_RETURN_H
351 test_compare(MEMCACHED_SUCCESS, MEMCACHED_SUCCESS);
352 #endif
353
354 return TEST_SUCCESS;
355 }
356
357 static test_return_t _compare_gearman_return_t_test(void *)
358 {
359 test_skip(HAVE_LIBGEARMAN, true);
360 #if defined(HAVE_LIBGEARMAN_1_0_RETURN_H) && HAVE_LIBGEARMAN_1_0_RETURN_H
361 test_compare(GEARMAN_SUCCESS, GEARMAN_SUCCESS);
362 #endif
363
364 return TEST_SUCCESS;
365 }
366
367 static test_return_t drizzled_cycle_test(void *object)
368 {
369 server_startup_st *servers= (server_startup_st*)object;
370 test_true(servers and servers->validate());
371
372 #if defined(HAVE_GEARMAND_BINARY) && HAVE_GEARMAND_BINARY
373 test_true(has_drizzled());
374 #endif
375
376 test_skip(true, has_drizzled());
377
378 test_skip(true, server_startup(*servers, "drizzled", get_free_port(), 0, NULL, false));
379
380 return TEST_SUCCESS;
381 }
382
383 static test_return_t gearmand_cycle_test(void *object)
384 {
385 server_startup_st *servers= (server_startup_st*)object;
386 test_true(servers and servers->validate());
387
388 test_skip(true, has_gearmand());
389 test_skip(true, server_startup(*servers, "gearmand", get_free_port(), 0, NULL, false));
390 servers->clear();
391
392 return TEST_SUCCESS;
393 }
394
395 static test_return_t skip_shim(bool a, bool b)
396 {
397 test_skip(a, b);
398 return TEST_SUCCESS;
399 }
400
401 static test_return_t test_skip_true_TEST(void*)
402 {
403 test_compare(true, true);
404 test_compare(false, false);
405 test_compare(TEST_SUCCESS, skip_shim(true, true));
406 test_compare(TEST_SUCCESS, skip_shim(false, false));
407
408 return TEST_SUCCESS;
409 }
410
411 static test_return_t test_skip_false_TEST(void*)
412 {
413 test_compare(TEST_SKIPPED, skip_shim(true, false));
414 test_compare(TEST_SKIPPED, skip_shim(false, true));
415 return TEST_SUCCESS;
416 }
417
418 static test_return_t server_startup_fail_TEST(void *object)
419 {
420 server_startup_st *servers= (server_startup_st*)object;
421 test_true(servers);
422
423 fatal::disable();
424 test_compare(servers->start_server(testing_service, LIBTEST_FAIL_PORT, 0, NULL, false), true);
425 fatal::enable();
426
427 return TEST_SUCCESS;
428 }
429
430 static test_return_t server_startup_TEST(void *object)
431 {
432 server_startup_st *servers= (server_startup_st*)object;
433 test_true(servers);
434
435 test_compare(servers->start_server(testing_service, get_free_port(), 0, NULL, false), true);
436
437 test_true(servers->last());
438 pid_t last_pid= servers->last()->pid();
439
440 test_compare(servers->last()->pid(), last_pid);
441 test_true(last_pid > 1);
442 test_compare(kill(last_pid, 0), 0);
443
444 test_true(servers->shutdown());
445 #if 0
446 test_compare(servers->last()->pid(), -1);
447 test_compare(kill(last_pid, 0), -1);
448 #endif
449
450 return TEST_SUCCESS;
451 }
452
453 static test_return_t socket_server_startup_TEST(void *object)
454 {
455 server_startup_st *servers= (server_startup_st*)object;
456 test_true(servers);
457
458 test_true(servers->start_socket_server(testing_service, get_free_port(), 0, NULL, false));
459
460 return TEST_SUCCESS;
461 }
462
463 #if 0
464 static test_return_t memcached_sasl_test(void *object)
465 {
466 server_startup_st *servers= (server_startup_st*)object;
467 test_true(servers);
468
469 test_skip(false, bool(getenv("TESTS_ENVIRONMENT")));
470
471 if (MEMCACHED_SASL_BINARY)
472 {
473 if (HAVE_LIBMEMCACHED)
474 {
475 test_true(has_memcached_sasl());
476 test_true(server_startup(*servers, "memcached-sasl", get_free_port(), 0, NULL, false));
477
478 return TEST_SUCCESS;
479 }
480 }
481
482 return TEST_SKIPPED;
483 }
484 #endif
485
486 static test_return_t application_true_BINARY(void *)
487 {
488 test_skip(0, access("/usr/bin/true", X_OK ));
489 Application true_app("/usr/bin/true");
490
491 test_compare(Application::SUCCESS, true_app.run());
492 test_compare(Application::SUCCESS, true_app.join());
493
494 return TEST_SUCCESS;
495 }
496
497 static test_return_t application_gdb_true_BINARY2(void *)
498 {
499 test_skip(0, access("/usr/bin/gdb", X_OK ));
500 test_skip(0, access("/usr/bin/true", X_OK ));
501
502 Application true_app("/usr/bin/true");
503 true_app.use_gdb(true);
504
505 test_compare(Application::SUCCESS, true_app.run());
506 test_compare(Application::SUCCESS, true_app.join());
507
508 return TEST_SUCCESS;
509 }
510
511 static test_return_t application_gdb_true_BINARY(void *)
512 {
513 test_skip(0, access("/usr/bin/gdb", X_OK ));
514 test_skip(0, access("/usr/bin/true", X_OK ));
515
516 Application true_app("/usr/bin/true");
517 true_app.use_gdb(true);
518
519 const char *args[]= { "--fubar", 0 };
520 test_compare(Application::SUCCESS, true_app.run(args));
521 test_compare(Application::SUCCESS, true_app.join());
522
523 return TEST_SUCCESS;
524 }
525
526 static test_return_t application_true_fubar_BINARY(void *)
527 {
528 test_skip(0, access("/usr/bin/true", X_OK ));
529 Application true_app("/usr/bin/true");
530
531 const char *args[]= { "--fubar", 0 };
532 test_compare(Application::SUCCESS, true_app.run(args));
533 test_compare(Application::SUCCESS, true_app.join());
534 test_zero(true_app.stdout_result().size());
535
536 return TEST_SUCCESS;
537 }
538
539 static test_return_t application_doesnotexist_BINARY(void *)
540 {
541
542 test_skip_valgrind();
543 Application true_app("doesnotexist");
544 true_app.will_fail();
545
546 const char *args[]= { "--fubar", 0 };
547 #if defined(TARGET_OS_OSX) && TARGET_OS_OSX
548 test_compare(Application::INVALID_POSIX_SPAWN, true_app.run(args));
549 #elif defined(TARGET_OS_FREEBSD) && TARGET_OS_FREEBSD
550 test_compare(Application::INVALID_POSIX_SPAWN, true_app.run(args));
551 #else
552 test_compare(Application::SUCCESS, true_app.run(args));
553 test_compare(Application::INVALID_POSIX_SPAWN, true_app.join());
554 #endif
555
556 test_zero(true_app.stdout_result().size());
557
558 return TEST_SUCCESS;
559 }
560
561 static test_return_t GET_TEST(void *)
562 {
563 libtest::http::GET get("http://foo.example.com/");
564
565 test_compare(false, get.execute());
566
567 return TEST_SUCCESS;
568 }
569
570 static test_return_t POST_TEST(void *)
571 {
572 libtest::vchar_t body;
573 libtest::http::POST post("http://foo.example.com/", body);
574
575 test_compare(false, post.execute());
576
577 return TEST_SUCCESS;
578 }
579
580 static test_return_t TRACE_TEST(void *)
581 {
582 libtest::vchar_t body;
583 libtest::http::TRACE trace("http://foo.example.com/", body);
584
585 test_compare(false, trace.execute());
586
587 return TEST_SUCCESS;
588 }
589
590
591 static test_return_t vchar_t_TEST(void *)
592 {
593 libtest::vchar_t response;
594 libtest::make_vector(response, test_literal_param("fubar\n"));
595 test_compare(response, response);
596
597 return TEST_SUCCESS;
598 }
599
600 static test_return_t vchar_t_compare_neg_TEST(void *)
601 {
602 libtest::vchar_t response;
603 libtest::vchar_t response2;
604 libtest::make_vector(response, test_literal_param("fubar\n"));
605 libtest::make_vector(response2, test_literal_param(__func__));
606 test_true(response != response2);
607
608 return TEST_SUCCESS;
609 }
610
611 static test_return_t application_echo_fubar_BINARY(void *)
612 {
613 if (0)
614 {
615 test_skip(0, access("/bin/echo", X_OK ));
616 Application true_app("/bin/echo");
617
618 const char *args[]= { "fubar", 0 };
619 test_compare(Application::SUCCESS, true_app.run(args));
620
621 while (true_app.slurp() == false) {} ;
622
623 libtest::vchar_t response;
624 make_vector(response, test_literal_param("fubar\n"));
625 test_compare(response, true_app.stdout_result());
626 }
627
628 return TEST_SUCCESS;
629 }
630
631 static test_return_t application_echo_fubar_BINARY2(void *)
632 {
633 if (0)
634 {
635 test_skip(0, access("/bin/echo", X_OK ));
636 Application true_app("/bin/echo");
637
638 true_app.add_option("fubar");
639
640 test_compare(Application::SUCCESS, true_app.run());
641 test_compare(Application::SUCCESS, true_app.join());
642
643 libtest::vchar_t response;
644 make_vector(response, test_literal_param("fubar\n"));
645 test_compare(response, true_app.stdout_result());
646 }
647
648 return TEST_SUCCESS;
649 }
650
651 static test_return_t echo_fubar_BINARY(void *)
652 {
653 const char *args[]= { "fubar", 0 };
654 test_compare(EXIT_SUCCESS, exec_cmdline("/bin/echo", args));
655
656 return TEST_SUCCESS;
657 }
658
659 static test_return_t core_count_BINARY(void *)
660 {
661 const char *args[]= { 0 };
662
663 test_compare(EXIT_SUCCESS, exec_cmdline("libtest/core-count", args, true));
664
665 return TEST_SUCCESS;
666 }
667
668 static test_return_t wait_BINARY(void *)
669 {
670 const char *args[]= { "--quiet", 0 };
671
672 test_compare(EXIT_FAILURE, exec_cmdline("libtest/wait", args, true));
673
674 return TEST_SUCCESS;
675 }
676
677 static test_return_t wait_help_BINARY(void *)
678 {
679 const char *args[]= { "--quiet", "--help", 0 };
680
681 test_compare(EXIT_SUCCESS, exec_cmdline("libtest/wait", args, true));
682
683 return TEST_SUCCESS;
684 }
685
686 static test_return_t wait_version_BINARY(void *)
687 {
688 const char *args[]= { "--quiet", "--version", 0 };
689
690 test_compare(EXIT_SUCCESS, exec_cmdline("libtest/wait", args, true));
691
692 return TEST_SUCCESS;
693 }
694
695 static test_return_t wait_services_BINARY(void *)
696 {
697 test_skip(0, access("/etc/services", R_OK ));
698
699 const char *args[]= { "--quiet", "/etc/services", 0 };
700
701 test_compare(EXIT_SUCCESS, exec_cmdline("libtest/wait", args, true));
702
703 return TEST_SUCCESS;
704 }
705
706 static test_return_t wait_services_BINARY2(void *)
707 {
708 test_skip(0, access("/etc/services", R_OK ));
709
710 const char *args[]= { "/etc/services", 0 };
711
712 test_compare(EXIT_SUCCESS, exec_cmdline("libtest/wait", args, true));
713
714 return TEST_SUCCESS;
715 }
716
717 static test_return_t wait_services_appliction_TEST(void *)
718 {
719 test_skip(0, access("/etc/services", R_OK ));
720 test_skip(0, access("/usr/bin/gdb", X_OK ));
721 test_skip(0, access("libtest/wait", X_OK ));
722
723 libtest::Application wait_app("libtest/wait", true);
724 wait_app.use_gdb(true);
725
726 const char *args[]= { "/etc/services", 0 };
727 test_compare(Application::SUCCESS, wait_app.run(args));
728 test_compare(Application::SUCCESS, wait_app.join());
729
730 return TEST_SUCCESS;
731 }
732
733 static test_return_t gdb_wait_services_appliction_TEST(void *)
734 {
735 test_skip(true, false);
736 #if defined(TARGET_OS_OSX) && TARGET_OS_OSX
737 test_skip(0, TARGET_OS_OSX);
738 #endif
739
740 test_skip(0, access("/etc/services", R_OK ));
741 test_skip(0, access("/usr/bin/gdb", X_OK ));
742 test_skip(0, access("libtest/wait", X_OK ));
743
744 libtest::Application wait_app("libtest/wait", true);
745 wait_app.use_gdb(true);
746
747 const char *args[]= { "/etc/services", 0 };
748 test_compare(Application::SUCCESS, wait_app.run(args));
749 test_compare(Application::SUCCESS, wait_app.join());
750
751 return TEST_SUCCESS;
752 }
753
754 static test_return_t gdb_abort_services_appliction_TEST(void *)
755 {
756 test_skip(0, access("/usr/bin/gdb", X_OK ));
757 test_skip(0, access("libtest/abort", X_OK ));
758 test_skip(true, false);
759
760 #if defined(TARGET_OS_OSX) && TARGET_OS_OSX
761 test_skip(0, TARGET_OS_OSX);
762 #endif
763
764 libtest::Application abort_app("libtest/abort", true);
765 abort_app.use_gdb(true);
766
767 test_compare(Application::SUCCESS, abort_app.run());
768 test_compare(Application::SUCCESS, abort_app.join());
769
770 std::string gdb_filename= abort_app.gdb_filename();
771 test_skip(0, access(gdb_filename.c_str(), R_OK ));
772 const char *args[]= { "SIGABRT", gdb_filename.c_str(), 0 };
773 test_compare(EXIT_SUCCESS, exec_cmdline("grep", args));
774
775 // Sanity test
776 args[0]= "THIS_WILL_NOT_BE_FOUND";
777 test_compare(EXIT_FAILURE, exec_cmdline("grep", args));
778
779 return TEST_SUCCESS;
780 }
781
782 static test_return_t get_free_port_TEST(void *)
783 {
784 in_port_t ret_port;
785 test_true((ret_port= get_free_port()));
786 test_true(get_free_port() != default_port());
787 test_true(get_free_port() != get_free_port());
788
789 return TEST_SUCCESS;
790 }
791
792 static test_return_t fatal_TEST(void *)
793 {
794 test_compare(fatal_calls++, fatal::disabled_counter());
795 throw libtest::fatal(LIBYATL_DEFAULT_PARAM, "Testing va_args based fatal(): %d", 10);
796
797 return TEST_SUCCESS;
798 }
799
800 static test_return_t number_of_cpus_TEST(void *)
801 {
802 test_true(number_of_cpus() >= 1);
803
804 return TEST_SUCCESS;
805 }
806
807 static test_return_t check_dns_TEST(void *)
808 {
809 test_warn(libtest::check_dns(), "Broken DNS server/no DNS server found");
810
811 return TEST_SUCCESS;
812 }
813
814 static test_return_t Timer_TEST(void *)
815 {
816 int64_t minutes= random() % 50;
817 minutes++;
818
819 Timer check;
820
821 check.reset();
822 check.offset(minutes, 2, 200);
823
824 test_compare(check.minutes(), minutes);
825
826 return TEST_SUCCESS;
827 }
828
829 static test_return_t lookup_true_TEST(void *)
830 {
831 test_warn(libtest::lookup("exist.gearman.info"), "dns is not currently working");
832 return TEST_SUCCESS;
833 }
834
835 static test_return_t lookup_false_TEST(void *)
836 {
837 if (libtest::lookup("does_not_exist.gearman.info"))
838 {
839 Error << "Broken DNS server detected";
840 return TEST_SKIPPED;
841 }
842
843 return TEST_SUCCESS;
844 }
845
846 static test_return_t create_tmpfile_TEST(void *)
847 {
848 test_skip(0, access("/usr/bin/touch", X_OK ));
849 std::string tmp= create_tmpfile(__func__);
850 test_compare(-1, access(tmp.c_str(), R_OK));
851 test_compare(-1, access(tmp.c_str(), F_OK));
852
853 Application touch_app("/usr/bin/touch");
854 const char *args[]= { tmp.c_str(), 0 };
855 test_compare(Application::SUCCESS, touch_app.run(args));
856 test_compare(Application::SUCCESS, touch_app.join());
857
858 test_compare(0, access(tmp.c_str(), R_OK));
859 test_compare(0, unlink(tmp.c_str()));
860
861 return TEST_SUCCESS;
862 }
863
864 static test_return_t fatal_message_TEST(void *)
865 {
866 test_compare(fatal_calls++, fatal::disabled_counter());
867 fatal_message("Fatal test");
868
869 return TEST_SUCCESS;
870 }
871
872 static test_return_t default_port_TEST(void *)
873 {
874 in_port_t ret_port= default_port();
875 test_compare(ret_port, libtest::default_port());
876 test_compare(ret_port, libtest::default_port());
877
878 return TEST_SUCCESS;
879 }
880
881 static test_return_t check_for_gearman(void *)
882 {
883 test_skip(true, HAVE_LIBGEARMAN);
884 test_skip(true, has_gearmand());
885 #if defined(HAVE_GEARMAND_BINARY) && HAVE_GEARMAND_BINARY
886 if (GEARMAND_BINARY)
887 {
888 if (strcmp(GEARMAND_BINARY, "./gearmand/gearmand"))
889 {
890 test_zero(access(GEARMAND_BINARY, X_OK ));
891 }
892 }
893 else
894 {
895 return TEST_SKIPPED;
896 }
897 #endif
898
899 testing_service= "gearmand";
900
901 return TEST_SUCCESS;
902 }
903
904 static test_return_t check_for_drizzle(void *)
905 {
906 test_skip(true, HAVE_LIBDRIZZLE);
907 test_skip(true, has_drizzled());
908
909 testing_service= "drizzled";
910
911 return TEST_SUCCESS;
912 }
913
914
915 test_st drizzled_tests[] ={
916 {"drizzled startup-shutdown", 0, drizzled_cycle_test },
917 {0, 0, 0}
918 };
919
920 test_st gearmand_tests[] ={
921 #if 0
922 {"pause", 0, pause_test },
923 #endif
924 {"gearmand startup-shutdown", 0, gearmand_cycle_test },
925 {"_compare(gearman_return_t)", 0, _compare_gearman_return_t_test },
926 {"server_startup(fail)", 0, server_startup_fail_TEST },
927 {0, 0, 0}
928 };
929
930 static test_return_t clear_servers(void* object)
931 {
932 server_startup_st *servers= (server_startup_st*)object;
933 test_true(servers);
934 servers->clear();
935
936 testing_service.clear();
937
938 return TEST_SUCCESS;
939 }
940
941 static test_return_t check_for_memcached(void* object)
942 {
943 test_skip(true, has_memcached());
944
945 server_startup_st *servers= (server_startup_st*)object;
946 test_true(servers);
947 servers->clear();
948
949 testing_service= "memcached";
950
951 return TEST_SUCCESS;
952 }
953
954 test_st memcached_TESTS[] ={
955 {"memcached startup-shutdown", 0, server_startup_TEST },
956 {"memcached(socket file) startup-shutdown", 0, socket_server_startup_TEST },
957 {"_compare(memcached_return_t)", 0, _compare_memcached_return_t_test },
958 {"server_startup(fail)", 0, server_startup_fail_TEST },
959 {0, 0, 0}
960 };
961
962 test_st test_skip_TESTS[] ={
963 {"true, true", 0, test_skip_true_TEST },
964 {"true, false", 0, test_skip_false_TEST },
965 {0, 0, 0}
966 };
967
968 test_st environment_tests[] ={
969 {"getenv()", 0, getenv_TEST },
970 {"LIBTOOL_COMMAND", 0, LIBTOOL_COMMAND_test },
971 {"VALGRIND_COMMAND", 0, VALGRIND_COMMAND_test },
972 {"HELGRIND_COMMAND", 0, HELGRIND_COMMAND_test },
973 {"GDB_COMMAND", 0, GDB_COMMAND_test },
974 {0, 0, 0}
975 };
976
977 test_st tests_log[] ={
978 {"TEST_SUCCESS", false, test_success_test },
979 {"TEST_FAILURE", false, test_failure_test },
980 {"TEST_SUCCESS == 0", false, test_success_equals_one_test },
981 {"SUCCESS", false, test_throw_success_TEST },
982 {"SKIP", false, test_throw_skip_TEST },
983 {"FAIL", false, test_throw_fail_TEST },
984 {"ASSERT_FALSE_", false, ASSERT_FALSE__TEST },
985 {"ASSERT_FALSE", false, ASSERT_FALSE_TEST },
986 {0, 0, 0}
987 };
988
989 test_st local_log[] ={
990 {"test_is_local()", 0, local_test },
991 {"test_is_local(NOT)", 0, local_not_test },
992 {0, 0, 0}
993 };
994
995 test_st directories_tests[] ={
996 {"var exists", 0, var_exists_test },
997 {"var/tmp exists", 0, var_tmp_exists_test },
998 {"var/run exists", 0, var_run_exists_test },
999 {"var/log exists", 0, var_log_exists_test },
1000 {"var/drizzle exists", 0, var_drizzle_exists_test },
1001 {"var/tmp", 0, var_tmp_test },
1002 {"var/run", 0, var_run_test },
1003 {"var/log", 0, var_log_test },
1004 {"var/drizzle", 0, var_drizzle_test },
1005 {"var/tmp rm", 0, var_tmp_rm_test },
1006 {"var/run rm", 0, var_run_rm_test },
1007 {"var/log rm", 0, var_log_rm_test },
1008 {"var/drizzle rm", 0, var_drizzle_rm_test },
1009 {0, 0, 0}
1010 };
1011
1012 test_st comparison_tests[] ={
1013 {"_compare(test_return_t)", 0, _compare_test_return_t_test },
1014 {0, 0, 0}
1015 };
1016
1017 test_st cmdline_tests[] ={
1018 {"echo fubar", 0, echo_fubar_BINARY },
1019 {"core-count", 0, core_count_BINARY },
1020 {"wait --quiet", 0, wait_BINARY },
1021 {"wait --quiet --help", 0, wait_help_BINARY },
1022 {"wait --quiet --version", 0, wait_version_BINARY },
1023 {"wait --quiet /etc/services", 0, wait_services_BINARY },
1024 {"wait /etc/services", 0, wait_services_BINARY2 },
1025 {"wait /etc/services", 0, wait_services_appliction_TEST },
1026 {"gdb wait /etc/services", 0, gdb_wait_services_appliction_TEST },
1027 {"gdb abort", 0, gdb_abort_services_appliction_TEST },
1028 {0, 0, 0}
1029 };
1030
1031 test_st get_free_port_TESTS[] ={
1032 {"get_free_port()", 0, get_free_port_TEST },
1033 {"default_port()", 0, default_port_TEST },
1034 {0, 0, 0}
1035 };
1036
1037 test_st fatal_message_TESTS[] ={
1038 {"libtest::fatal", 0, fatal_TEST },
1039 {"fatal_message()", 0, fatal_message_TEST },
1040 {0, 0, 0}
1041 };
1042
1043 test_st number_of_cpus_TESTS[] ={
1044 {"libtest::number_of_cpus()", 0, number_of_cpus_TEST },
1045 {0, 0, 0}
1046 };
1047
1048 test_st create_tmpfile_TESTS[] ={
1049 {"libtest::create_tmpfile()", 0, create_tmpfile_TEST },
1050 {0, 0, 0}
1051 };
1052
1053 test_st timer_TESTS[] ={
1054 {"libtest::Timer", 0, Timer_TEST },
1055 {0, 0, 0}
1056 };
1057
1058 test_st dns_TESTS[] ={
1059 {"libtest::lookup(true)", 0, lookup_true_TEST },
1060 {"libtest::lookup(false)", 0, lookup_false_TEST },
1061 {"libtest::check_dns()", 0, check_dns_TEST },
1062 {0, 0, 0}
1063 };
1064
1065 test_st application_tests[] ={
1066 {"vchar_t", 0, vchar_t_TEST },
1067 {"vchar_t compare()", 0, vchar_t_compare_neg_TEST },
1068 {"true", 0, application_true_BINARY },
1069 {"gbd true --fubar", 0, application_gdb_true_BINARY },
1070 {"gbd true", 0, application_gdb_true_BINARY2 },
1071 {"true --fubar", 0, application_true_fubar_BINARY },
1072 {"doesnotexist --fubar", 0, application_doesnotexist_BINARY },
1073 {"echo fubar", 0, application_echo_fubar_BINARY },
1074 {"echo fubar (as option)", 0, application_echo_fubar_BINARY2 },
1075 {0, 0, 0}
1076 };
1077
1078 static test_return_t check_for_curl(void *)
1079 {
1080 test_skip(true, HAVE_LIBCURL);
1081 return TEST_SUCCESS;
1082 }
1083
1084 static test_return_t disable_fatal_exception(void *)
1085 {
1086 fatal_calls= 0;
1087 fatal::disable();
1088 return TEST_SUCCESS;
1089 }
1090
1091 static test_return_t enable_fatal_exception(void *)
1092 {
1093 fatal::enable();
1094 return TEST_SUCCESS;
1095 }
1096
1097 test_st http_tests[] ={
1098 {"GET", 0, GET_TEST },
1099 {"POST", 0, POST_TEST },
1100 {"TRACE", 0, TRACE_TEST },
1101 {0, 0, 0}
1102 };
1103
1104 collection_st collection[] ={
1105 {"environment", 0, 0, environment_tests},
1106 {"return values", 0, 0, tests_log},
1107 {"test_skip()", 0, 0, test_skip_TESTS },
1108 {"local", 0, 0, local_log},
1109 {"directories", 0, 0, directories_tests},
1110 {"comparison", 0, 0, comparison_tests},
1111 {"gearmand", check_for_gearman, clear_servers, gearmand_tests},
1112 {"memcached", check_for_memcached, clear_servers, memcached_TESTS },
1113 {"drizzled", check_for_drizzle, clear_servers, drizzled_tests},
1114 {"cmdline", 0, 0, cmdline_tests},
1115 {"application", 0, 0, application_tests},
1116 {"http", check_for_curl, 0, http_tests},
1117 {"http", check_for_curl, 0, http_tests},
1118 {"get_free_port()", 0, 0, get_free_port_TESTS },
1119 {"fatal", disable_fatal_exception, enable_fatal_exception, fatal_message_TESTS },
1120 {"number_of_cpus()", 0, 0, number_of_cpus_TESTS },
1121 {"create_tmpfile()", 0, 0, create_tmpfile_TESTS },
1122 {"dns", 0, 0, dns_TESTS },
1123 {"libtest::Timer", 0, 0, timer_TESTS },
1124 {0, 0, 0, 0}
1125 };
1126
1127 static void *world_create(server_startup_st& servers, test_return_t&)
1128 {
1129 return &servers;
1130 }
1131
1132 void get_world(libtest::Framework *world)
1133 {
1134 world->collections(collection);
1135 world->create(world_create);
1136 }