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