Update from trunk to launchpad
[m6w6/libmemcached] / libtest / unittest.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * libtest
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 *
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.
11 *
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.
16 *
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
20 */
21
22
23 #include <config.h>
24
25 #include <libtest/test.hpp>
26
27 #include <cstdlib>
28 #include <unistd.h>
29
30 using namespace libtest;
31
32 static test_return_t LIBTOOL_COMMAND_test(void *)
33 {
34 test_true(getenv("LIBTOOL_COMMAND"));
35 return TEST_SUCCESS;
36 }
37
38 static test_return_t VALGRIND_COMMAND_test(void *)
39 {
40 test_true(getenv("VALGRIND_COMMAND"));
41 return TEST_SUCCESS;
42 }
43
44 static test_return_t HELGRIND_COMMAND_test(void *)
45 {
46 test_true(getenv("HELGRIND_COMMAND"));
47 return TEST_SUCCESS;
48 }
49
50 static test_return_t GDB_COMMAND_test(void *)
51 {
52 test_true(getenv("GDB_COMMAND"));
53 return TEST_SUCCESS;
54 }
55
56 static test_return_t test_success_test(void *)
57 {
58 return TEST_SUCCESS;
59 }
60
61 static test_return_t test_failure_test(void *)
62 {
63 return TEST_SKIPPED; // Only run this when debugging
64
65 test_compare(1, 2);
66 return TEST_SUCCESS;
67 }
68
69 static test_return_t local_test(void *)
70 {
71 if (getenv("LIBTEST_LOCAL"))
72 {
73 test_true(test_is_local());
74 }
75 else
76 {
77 test_false(test_is_local());
78 }
79
80 return TEST_SUCCESS;
81 }
82
83 static test_return_t local_not_test(void *)
84 {
85 return TEST_SKIPPED;
86
87 std::string temp;
88
89 const char *ptr;
90 if ((ptr= getenv("LIBTEST_LOCAL")) == NULL)
91 {
92 temp.append(ptr);
93 }
94
95 // unsetenv() will cause issues with valgrind
96 _compare(__FILE__, __LINE__, __func__, 0, unsetenv("LIBTEST_LOCAL"));
97 test_compare(0, unsetenv("LIBTEST_LOCAL"));
98 test_false(test_is_local());
99
100 test_compare(0, setenv("LIBTEST_LOCAL", "1", 1));
101 test_true(test_is_local());
102
103 if (temp.empty())
104 {
105 test_compare(0, unsetenv("LIBTEST_LOCAL"));
106 }
107 else
108 {
109 char *old_string= strdup(temp.c_str());
110 test_compare(0, setenv("LIBTEST_LOCAL", old_string, 1));
111 }
112
113 return TEST_SUCCESS;
114 }
115
116 #if 0
117 static test_return_t pause_test(void *)
118 {
119 (void)getchar();
120 return TEST_SUCCESS;
121 }
122 #endif
123
124
125 static test_return_t gearmand_cycle_test(void *object)
126 {
127 server_startup_st *servers= (server_startup_st*)object;
128 test_true(servers);
129
130 #ifndef HAVE_LIBGEARMAN
131 return TEST_SKIPPED;
132 #endif
133
134 const char *argv[1]= { "cycle_gearmand" };
135 test_true(server_startup(*servers, "gearmand", 9999, 1, argv));
136
137 return TEST_SUCCESS;
138 }
139
140 static test_return_t memcached_cycle_test(void *object)
141 {
142 server_startup_st *servers= (server_startup_st*)object;
143 test_true(servers);
144
145 #if !defined(MEMCACHED_BINARY) || !defined(HAVE_LIBMEMCACHED)
146 return TEST_SKIPPED;
147 #endif
148
149 const char *argv[1]= { "cycle_memcached" };
150 test_true(server_startup(*servers, "memcached", 9998, 1, argv));
151
152 return TEST_SUCCESS;
153 }
154
155 static test_return_t memcached_socket_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(servers->start_socket_server("memcached", 9997, 1, argv));
166
167 return TEST_SUCCESS;
168 }
169
170 test_st gearmand_tests[] ={
171 #if 0
172 {"pause", 0, pause_test },
173 #endif
174 {"gearmand startup-shutdown", 0, gearmand_cycle_test },
175 {0, 0, 0}
176 };
177
178 test_st memcached_tests[] ={
179 {"memcached startup-shutdown", 0, memcached_cycle_test },
180 {"memcached(socket file) startup-shutdown", 0, memcached_socket_cycle_test },
181 {0, 0, 0}
182 };
183
184 test_st environment_tests[] ={
185 {"LIBTOOL_COMMAND", 0, LIBTOOL_COMMAND_test },
186 {"VALGRIND_COMMAND", 0, VALGRIND_COMMAND_test },
187 {"HELGRIND_COMMAND", 0, HELGRIND_COMMAND_test },
188 {"GDB_COMMAND", 0, GDB_COMMAND_test },
189 {0, 0, 0}
190 };
191
192 test_st tests_log[] ={
193 {"TEST_SUCCESS", 0, test_success_test },
194 {"TEST_FAILURE", 0, test_failure_test },
195 {0, 0, 0}
196 };
197
198 test_st local_log[] ={
199 {"test_is_local()", 0, local_test },
200 {"test_is_local(NOT)", 0, local_not_test },
201 {0, 0, 0}
202 };
203
204 collection_st collection[] ={
205 {"environment", 0, 0, environment_tests},
206 {"return values", 0, 0, tests_log},
207 {"local", 0, 0, local_log},
208 {"gearmand", 0, 0, gearmand_tests},
209 {"memcached", 0, 0, memcached_tests},
210 {0, 0, 0, 0}
211 };
212
213 static void *world_create(server_startup_st& servers, test_return_t&)
214 {
215 return &servers;
216 }
217
218 void get_world(Framework *world)
219 {
220 world->collections= collection;
221 world->_create= world_create;
222 }