Merge in merge of libtest
[m6w6/libmemcached] / libtest / unittest.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * uTest self unit test.
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
22 * written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 */
37
38 #include <config.h>
39
40 #include <libtest/test.hpp>
41
42 #include <cstdlib>
43
44 using namespace libtest;
45
46 static test_return_t test_success_test(void *)
47 {
48 return TEST_SUCCESS;
49 }
50
51 static test_return_t test_failure_test(void *)
52 {
53 return TEST_SKIPPED; // Only run this when debugging
54
55 test_compare(1, 2);
56 return TEST_SUCCESS;
57 }
58
59 static test_return_t local_test(void *)
60 {
61 if (getenv("LIBTEST_LOCAL"))
62 {
63 test_true(test_is_local());
64 }
65 else
66 {
67 test_false(test_is_local());
68 }
69
70 return TEST_SUCCESS;
71 }
72
73 static test_return_t local_not_test(void *)
74 {
75 return TEST_SKIPPED;
76
77 std::string temp;
78
79 const char *ptr;
80 if ((ptr= getenv("LIBTEST_LOCAL")) == NULL)
81 {
82 temp.append(ptr);
83 }
84
85 // unsetenv() will cause issues with valgrind
86 _compare(__FILE__, __LINE__, __func__, 0, unsetenv("LIBTEST_LOCAL"));
87 test_compare(0, unsetenv("LIBTEST_LOCAL"));
88 test_false(test_is_local());
89
90 test_compare(0, setenv("LIBTEST_LOCAL", "1", 1));
91 test_true(test_is_local());
92
93 if (temp.empty())
94 {
95 test_compare(0, unsetenv("LIBTEST_LOCAL"));
96 }
97 else
98 {
99 char *old_string= strdup(temp.c_str());
100 test_compare(0, setenv("LIBTEST_LOCAL", old_string, 1));
101 }
102
103 return TEST_SUCCESS;
104 }
105
106 #if 0
107 static test_return_t pause_test(void *)
108 {
109 (void)getchar();
110 return TEST_SUCCESS;
111 }
112 #endif
113
114
115 static test_return_t gearmand_cycle_test(void *object)
116 {
117 server_startup_st *servers= (server_startup_st*)object;
118 test_true(servers);
119
120 #ifndef HAVE_LIBGEARMAN
121 return TEST_SKIPPED;
122 #endif
123
124 const char *argv[1]= { "cycle_gearmand" };
125 test_true(server_startup(*servers, "gearmand", 9999, 1, argv));
126
127 return TEST_SUCCESS;
128 }
129
130 static test_return_t memcached_cycle_test(void *object)
131 {
132 server_startup_st *servers= (server_startup_st*)object;
133 test_true(servers);
134
135 #if !defined(MEMCACHED_BINARY) || !defined(HAVE_LIBMEMCACHED)
136 return TEST_SKIPPED;
137 #endif
138
139 const char *argv[1]= { "cycle_memcached" };
140 test_true(server_startup(*servers, "memcached", 9998, 1, argv));
141
142 return TEST_SUCCESS;
143 }
144
145 static test_return_t memcached_socket_cycle_test(void *object)
146 {
147 server_startup_st *servers= (server_startup_st*)object;
148 test_true(servers);
149
150 #if !defined(MEMCACHED_BINARY) || !defined(HAVE_LIBMEMCACHED)
151 return TEST_SKIPPED;
152 #endif
153
154 const char *argv[1]= { "cycle_memcached" };
155 test_true(servers->start_socket_server("memcached", 9997, 1, argv));
156
157 return TEST_SUCCESS;
158 }
159
160 test_st gearmand_tests[] ={
161 #if 0
162 {"pause", 0, pause_test },
163 #endif
164 {"gearmand startup-shutdown", 0, gearmand_cycle_test },
165 {0, 0, 0}
166 };
167
168 test_st memcached_tests[] ={
169 {"memcached startup-shutdown", 0, memcached_cycle_test },
170 {"memcached(socket file) startup-shutdown", 0, memcached_socket_cycle_test },
171 {0, 0, 0}
172 };
173
174 test_st tests_log[] ={
175 {"TEST_SUCCESS", 0, test_success_test },
176 {"TEST_FAILURE", 0, test_failure_test },
177 {0, 0, 0}
178 };
179
180 test_st local_log[] ={
181 {"test_is_local()", 0, local_test },
182 {"test_is_local(NOT)", 0, local_not_test },
183 {0, 0, 0}
184 };
185
186 collection_st collection[] ={
187 {"return values", 0, 0, tests_log},
188 {"local", 0, 0, local_log},
189 {"gearmand", 0, 0, gearmand_tests},
190 {"memcached", 0, 0, memcached_tests},
191 {0, 0, 0, 0}
192 };
193
194 static void *world_create(server_startup_st& servers, test_return_t&)
195 {
196 return &servers;
197 }
198
199 void get_world(Framework *world)
200 {
201 world->collections= collection;
202 world->_create= world_create;
203 }