b5ad738b2a88be1ff35ed91ab08e2c39d3f36376
[awesomized/libmemcached] / libtest / framework.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 <config.h>
38
39 #include <libtest/common.h>
40 #include <libtest/collection.h>
41 #include <libtest/signal.h>
42
43 #include <fnmatch.h>
44 #include <iostream>
45
46 namespace libtest {
47
48 Framework::Framework(libtest::SignalThread& signal,
49 const std::string& name_,
50 const std::string& only_run_arg,
51 const std::string& wildcard_arg) :
52 _total(0),
53 _success(0),
54 _skipped(0),
55 _failed(0),
56 _create(NULL),
57 _destroy(NULL),
58 _on_error(NULL),
59 _runner(NULL),
60 _socket(false),
61 _creators_ptr(NULL),
62 _signal(signal),
63 _only_run(only_run_arg),
64 _wildcard(wildcard_arg),
65 _name(name_)
66 {
67 get_world(this);
68 }
69
70 void Framework::collections(collection_st* collections_)
71 {
72 for (collection_st *next= collections_; next and next->name; next++)
73 {
74 _collection.push_back(new Collection(this, next));
75 }
76 }
77
78 Framework::~Framework()
79 {
80 if (_destroy and _destroy(_creators_ptr))
81 {
82 Error << "Failure in _destroy(), some resources may not have been cleaned up.";
83 }
84
85 _servers.shutdown();
86
87 delete _runner;
88
89 for (std::vector<Collection*>::iterator iter= _collection.begin();
90 iter != _collection.end();
91 ++iter)
92 {
93 delete *iter;
94 }
95 }
96
97 bool Framework::match(const char* arg)
98 {
99 if (_wildcard.empty() == false and fnmatch(_wildcard.c_str(), arg, 0))
100 {
101 return true;
102 }
103
104 return false;
105 }
106
107 void Framework::exec()
108 {
109 for (std::vector<Collection*>::iterator iter= _collection.begin();
110 iter != _collection.end() and (_signal.is_shutdown() == false);
111 ++iter)
112 {
113 if (_only_run.empty() == false and
114 fnmatch(_only_run.c_str(), (*iter)->name(), 0))
115 {
116 continue;
117 }
118
119 _total++;
120
121 try
122 {
123 switch ((*iter)->exec())
124 {
125 case TEST_FAILURE:
126 _failed++;
127 break;
128
129 case TEST_SKIPPED:
130 _skipped++;
131 break;
132
133 // exec() can return SUCCESS, but that doesn't mean that some tests did
134 // not fail or get skipped.
135 case TEST_SUCCESS:
136 _success++;
137 break;
138 }
139 }
140 catch (libtest::fatal& e)
141 {
142 _failed++;
143 stream::cerr(e.file(), e.line(), e.func()) << e.mesg();
144 }
145 catch (libtest::disconnected& e)
146 {
147 _failed++;
148 stream::cerr(e.file(), e.line(), e.func()) << "Unhandled disconnection occurred: " << e.mesg();
149 throw;
150 }
151 catch (...)
152 {
153 _failed++;
154 throw;
155 }
156 }
157
158 void xml(const std::string& testsuites_name, std::ostream& output);
159 }
160
161 uint32_t Framework::sum_total()
162 {
163 uint32_t count= 0;
164 for (std::vector<Collection*>::iterator iter= _collection.begin();
165 iter != _collection.end();
166 ++iter)
167 {
168 count+= (*iter)->total();
169 }
170
171 return count;
172 }
173
174 uint32_t Framework::sum_success()
175 {
176 uint32_t count= 0;
177 for (std::vector<Collection*>::iterator iter= _collection.begin();
178 iter != _collection.end();
179 ++iter)
180 {
181 count+= (*iter)->success();
182 }
183
184 return count;
185 }
186
187 uint32_t Framework::sum_skipped()
188 {
189 uint32_t count= 0;
190 for (std::vector<Collection*>::iterator iter= _collection.begin();
191 iter != _collection.end();
192 ++iter)
193 {
194 count+= (*iter)->skipped();
195 }
196
197 return count;
198 }
199
200 uint32_t Framework::sum_failed()
201 {
202 uint32_t count= 0;
203 for (std::vector<Collection*>::iterator iter= _collection.begin();
204 iter != _collection.end();
205 ++iter)
206 {
207 count+= (*iter)->failed();
208 }
209
210 return count;
211 }
212
213 libtest::Runner *Framework::runner()
214 {
215 if (_runner == NULL)
216 {
217 _runner= new Runner;
218 }
219 _runner->set_servers(_servers);
220
221 return _runner;
222 }
223
224 test_return_t Framework::create()
225 {
226 test_return_t rc= TEST_SUCCESS;
227 if (_create)
228 {
229 _creators_ptr= _create(_servers, rc);
230 }
231
232 return rc;
233 }
234
235 } // namespace libtest