1 // Copyright 2011 The Kyua Authors.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "drivers/scan_results.hpp"
33 #include <atf-c++.hpp>
35 #include "engine/filters.hpp"
36 #include "model/context.hpp"
37 #include "model/metadata.hpp"
38 #include "model/test_case.hpp"
39 #include "model/test_program.hpp"
40 #include "model/test_result.hpp"
41 #include "store/exceptions.hpp"
42 #include "store/read_transaction.hpp"
43 #include "store/write_backend.hpp"
44 #include "store/write_transaction.hpp"
45 #include "utils/datetime.hpp"
46 #include "utils/format/containers.ipp"
47 #include "utils/format/macros.hpp"
48 #include "utils/optional.ipp"
49 #include "utils/sanity.hpp"
51 namespace datetime = utils::datetime;
52 namespace fs = utils::fs;
55 using utils::optional;
61 /// Records the callback values for futher investigation.
62 class capture_hooks : public drivers::scan_results::base_hooks {
64 /// Whether begin() was called or not.
67 /// The captured driver result, if any.
68 optional< drivers::scan_results::result > _end_result;
70 /// The captured context, if any.
71 optional< model::context > _context;
73 /// The captured results, flattened as "program:test_case:result".
74 std::set< std::string > _results;
82 /// Callback executed before any operation is performed.
89 /// Callback executed after all operations are performed.
91 /// \param r A structure with all results computed by this driver. Note
92 /// that this is also returned by the drive operation.
94 end(const drivers::scan_results::result& r)
100 /// Callback executed when the context is loaded.
102 /// \param context The context loaded from the database.
103 void got_context(const model::context& context)
109 /// Callback executed when a test results is found.
111 /// \param iter Container for the test result's data.
112 void got_result(store::results_iterator& iter)
115 switch (iter.result().type()) {
116 case model::test_result_passed: type = "passed"; break;
117 case model::test_result_skipped: type = "skipped"; break;
119 UNREACHABLE_MSG("Formatting unimplemented");
121 const datetime::delta duration = iter.end_time() - iter.start_time();
122 _results.insert(F("%s:%s:%s:%s:%s:%s") %
123 iter.test_program()->absolute_path() %
124 iter.test_case_name() % type % iter.result().reason() %
125 duration.seconds % duration.useconds);
130 /// Populates a results file.
132 /// It is not OK to call this function multiple times on the same file.
134 /// \param db_name The database to update.
135 /// \param count The number of "elements" to insert into the results file.
136 /// Determines the number of test programs and the number of test cases
137 /// each has. Can be used to determine from the caller which particular
138 /// results file has been loaded.
140 populate_results_file(const char* db_name, const int count)
142 store::write_backend backend = store::write_backend::open_rw(
145 store::write_transaction tx = backend.start_write();
147 std::map< std::string, std::string > env;
148 for (int i = 0; i < count; i++)
149 env[F("VAR%s") % i] = F("Value %s") % i;
150 const model::context context(fs::path("/root"), env);
151 tx.put_context(context);
153 for (int i = 0; i < count; i++) {
154 model::test_program_builder test_program_builder(
155 "fake", fs::path(F("dir/prog_%s") % i), fs::path("/root"),
157 for (int j = 0; j < count; j++) {
158 test_program_builder.add_test_case(F("case_%s") % j);
160 const model::test_program test_program = test_program_builder.build();
161 const int64_t tp_id = tx.put_test_program(test_program);
163 for (int j = 0; j < count; j++) {
164 const model::test_result result(model::test_result_skipped,
166 const int64_t tc_id = tx.put_test_case(test_program,
167 F("case_%s") % j, tp_id);
168 const datetime::timestamp start =
169 datetime::timestamp::from_microseconds(1000010);
170 const datetime::timestamp end =
171 datetime::timestamp::from_microseconds(5000020 + i + j);
172 tx.put_result(result, tc_id, start, end);
180 } // anonymous namespace
183 ATF_TEST_CASE_WITHOUT_HEAD(ok__all);
184 ATF_TEST_CASE_BODY(ok__all)
186 populate_results_file("test.db", 2);
189 const drivers::scan_results::result result = drivers::scan_results::drive(
190 fs::path("test.db"), std::set< engine::test_filter >(), hooks);
191 ATF_REQUIRE(result.unused_filters.empty());
192 ATF_REQUIRE(hooks._begin_called);
193 ATF_REQUIRE(hooks._end_result);
195 std::map< std::string, std::string > env;
196 env["VAR0"] = "Value 0";
197 env["VAR1"] = "Value 1";
198 const model::context context(fs::path("/root"), env);
199 ATF_REQUIRE(context == hooks._context.get());
201 std::set< std::string > results;
202 results.insert("/root/dir/prog_0:case_0:skipped:Count 0:4:10");
203 results.insert("/root/dir/prog_0:case_1:skipped:Count 1:4:11");
204 results.insert("/root/dir/prog_1:case_0:skipped:Count 0:4:11");
205 results.insert("/root/dir/prog_1:case_1:skipped:Count 1:4:12");
206 ATF_REQUIRE_EQ(results, hooks._results);
210 ATF_TEST_CASE_WITHOUT_HEAD(ok__filters);
211 ATF_TEST_CASE_BODY(ok__filters)
213 populate_results_file("test.db", 3);
215 std::set< engine::test_filter > filters;
216 filters.insert(engine::test_filter(fs::path("dir/prog_1"), ""));
217 filters.insert(engine::test_filter(fs::path("dir/prog_1"), "no"));
218 filters.insert(engine::test_filter(fs::path("dir/prog_2"), "case_1"));
219 filters.insert(engine::test_filter(fs::path("dir/prog_3"), ""));
222 const drivers::scan_results::result result = drivers::scan_results::drive(
223 fs::path("test.db"), filters, hooks);
224 ATF_REQUIRE(hooks._begin_called);
225 ATF_REQUIRE(hooks._end_result);
227 std::set< engine::test_filter > unused_filters;
228 unused_filters.insert(engine::test_filter(fs::path("dir/prog_1"), "no"));
229 unused_filters.insert(engine::test_filter(fs::path("dir/prog_3"), ""));
230 ATF_REQUIRE_EQ(unused_filters, result.unused_filters);
232 std::set< std::string > results;
233 results.insert("/root/dir/prog_1:case_0:skipped:Count 0:4:11");
234 results.insert("/root/dir/prog_1:case_1:skipped:Count 1:4:12");
235 results.insert("/root/dir/prog_1:case_2:skipped:Count 2:4:13");
236 results.insert("/root/dir/prog_2:case_1:skipped:Count 1:4:13");
237 ATF_REQUIRE_EQ(results, hooks._results);
241 ATF_TEST_CASE_WITHOUT_HEAD(missing_db);
242 ATF_TEST_CASE_BODY(missing_db)
247 drivers::scan_results::drive(fs::path("test.db"),
248 std::set< engine::test_filter >(),
253 ATF_INIT_TEST_CASES(tcs)
255 ATF_ADD_TEST_CASE(tcs, ok__all);
256 ATF_ADD_TEST_CASE(tcs, ok__filters);
257 ATF_ADD_TEST_CASE(tcs, missing_db);