Merge in libtest updates.
[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 #include <unistd.h>
44
45 using namespace libtest;
46
47 static test_return_t LIBTOOL_COMMAND_test(void *)
48 {
49 test_true(getenv("LIBTOOL_COMMAND"));
50 return TEST_SUCCESS;
51 }
52
53 static test_return_t VALGRIND_COMMAND_test(void *)
54 {
55 test_true(getenv("VALGRIND_COMMAND"));
56 return TEST_SUCCESS;
57 }
58
59 static test_return_t HELGRIND_COMMAND_test(void *)
60 {
61 test_true(getenv("HELGRIND_COMMAND"));
62 return TEST_SUCCESS;
63 }
64
65 static test_return_t GDB_COMMAND_test(void *)
66 {
67 test_true(getenv("GDB_COMMAND"));
68 return TEST_SUCCESS;
69 }
70
71 static test_return_t test_success_test(void *)
72 {
73 return TEST_SUCCESS;
74 }
75
76 static test_return_t test_failure_test(void *)
77 {
78 return TEST_SKIPPED; // Only run this when debugging
79
80 test_compare(1, 2);
81 return TEST_SUCCESS;
82 }
83
84 static test_return_t local_test(void *)
85 {
86 if (getenv("LIBTEST_LOCAL"))
87 {
88 test_true(test_is_local());
89 }
90 else
91 {
92 test_false(test_is_local());
93 }
94
95 return TEST_SUCCESS;
96 }
97
98 static test_return_t local_not_test(void *)
99 {
100 return TEST_SKIPPED;
101
102 std::string temp;
103
104 const char *ptr;
105 if ((ptr= getenv("LIBTEST_LOCAL")) == NULL)
106 {
107 temp.append(ptr);
108 }
109
110 // unsetenv() will cause issues with valgrind
111 _compare(__FILE__, __LINE__, __func__, 0, unsetenv("LIBTEST_LOCAL"));
112 test_compare(0, unsetenv("LIBTEST_LOCAL"));
113 test_false(test_is_local());
114
115 test_compare(0, setenv("LIBTEST_LOCAL", "1", 1));
116 test_true(test_is_local());
117
118 if (temp.empty())
119 {
120 test_compare(0, unsetenv("LIBTEST_LOCAL"));
121 }
122 else
123 {
124 char *old_string= strdup(temp.c_str());
125 test_compare(0, setenv("LIBTEST_LOCAL", old_string, 1));
126 }
127
128 return TEST_SUCCESS;
129 }
130
131 #if 0
132 static test_return_t pause_test(void *)
133 {
134 (void)getchar();
135 return TEST_SUCCESS;
136 }
137 #endif
138
139
140 static test_return_t gearmand_cycle_test(void *object)
141 {
142 server_startup_st *servers= (server_startup_st*)object;
143 test_true(servers);
144
145 #ifndef HAVE_LIBGEARMAN
146 return TEST_SKIPPED;
147 #endif
148
149 const char *argv[1]= { "cycle_gearmand" };
150 test_true(server_startup(*servers, "gearmand", 9999, 1, argv));
151
152 return TEST_SUCCESS;
153 }
154
155 static test_return_t memcached_cycle_test(void *object)
156 {
157 server_startup_st *servers= (server_startup_st*)object;
158 test_true(servers);
159
160 #if !defined(MEMCACHED_BINARY) || !defined(HAVE_LIBMEMCACHED)
161 return TEST_SKIPPED;
162 #endif
163
164 const char *argv[1]= { "cycle_memcached" };
165 test_true(server_startup(*servers, "memcached", 9998, 1, argv));
166
167 return TEST_SUCCESS;
168 }
169
170 static test_return_t memcached_socket_cycle_test(void *object)
171 {
172 server_startup_st *servers= (server_startup_st*)object;
173 test_true(servers);
174
175 #if !defined(MEMCACHED_BINARY) || !defined(HAVE_LIBMEMCACHED)
176 return TEST_SKIPPED;
177 #endif
178
179 const char *argv[1]= { "cycle_memcached" };
180 test_true(servers->start_socket_server("memcached", 9997, 1, argv));
181
182 return TEST_SUCCESS;
183 }
184
185 test_st gearmand_tests[] ={
186 #if 0
187 {"pause", 0, pause_test },
188 #endif
189 {"gearmand startup-shutdown", 0, gearmand_cycle_test },
190 {0, 0, 0}
191 };
192
193 test_st memcached_tests[] ={
194 {"memcached startup-shutdown", 0, memcached_cycle_test },
195 {"memcached(socket file) startup-shutdown", 0, memcached_socket_cycle_test },
196 {0, 0, 0}
197 };
198
199 test_st environment_tests[] ={
200 {"LIBTOOL_COMMAND", 0, LIBTOOL_COMMAND_test },
201 {"VALGRIND_COMMAND", 0, VALGRIND_COMMAND_test },
202 {"HELGRIND_COMMAND", 0, HELGRIND_COMMAND_test },
203 {"GDB_COMMAND", 0, GDB_COMMAND_test },
204 {0, 0, 0}
205 };
206
207 test_st tests_log[] ={
208 {"TEST_SUCCESS", 0, test_success_test },
209 {"TEST_FAILURE", 0, test_failure_test },
210 {0, 0, 0}
211 };
212
213 test_st local_log[] ={
214 {"test_is_local()", 0, local_test },
215 {"test_is_local(NOT)", 0, local_not_test },
216 {0, 0, 0}
217 };
218
219 collection_st collection[] ={
220 {"environment", 0, 0, environment_tests},
221 {"return values", 0, 0, tests_log},
222 {"local", 0, 0, local_log},
223 {"gearmand", 0, 0, gearmand_tests},
224 {"memcached", 0, 0, memcached_tests},
225 {0, 0, 0, 0}
226 };
227
228 static void *world_create(server_startup_st& servers, test_return_t&)
229 {
230 return &servers;
231 }
232
233 void get_world(Framework *world)
234 {
235 world->collections= collection;
236 world->_create= world_create;
237 }