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