1*11be35a1SLionel Sambuc // Copyright 2011 Google Inc.
2*11be35a1SLionel Sambuc // All rights reserved.
3*11be35a1SLionel Sambuc //
4*11be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
5*11be35a1SLionel Sambuc // modification, are permitted provided that the following conditions are
6*11be35a1SLionel Sambuc // met:
7*11be35a1SLionel Sambuc //
8*11be35a1SLionel Sambuc // * Redistributions of source code must retain the above copyright
9*11be35a1SLionel Sambuc // notice, this list of conditions and the following disclaimer.
10*11be35a1SLionel Sambuc // * Redistributions in binary form must reproduce the above copyright
11*11be35a1SLionel Sambuc // notice, this list of conditions and the following disclaimer in the
12*11be35a1SLionel Sambuc // documentation and/or other materials provided with the distribution.
13*11be35a1SLionel Sambuc // * Neither the name of Google Inc. nor the names of its contributors
14*11be35a1SLionel Sambuc // may be used to endorse or promote products derived from this software
15*11be35a1SLionel Sambuc // without specific prior written permission.
16*11be35a1SLionel Sambuc //
17*11be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*11be35a1SLionel Sambuc // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*11be35a1SLionel Sambuc // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*11be35a1SLionel Sambuc // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*11be35a1SLionel Sambuc // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*11be35a1SLionel Sambuc // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*11be35a1SLionel Sambuc // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*11be35a1SLionel Sambuc // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*11be35a1SLionel Sambuc // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*11be35a1SLionel Sambuc // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*11be35a1SLionel Sambuc // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc
29*11be35a1SLionel Sambuc #include "engine/drivers/scan_action.hpp"
30*11be35a1SLionel Sambuc
31*11be35a1SLionel Sambuc #include <set>
32*11be35a1SLionel Sambuc
33*11be35a1SLionel Sambuc #include <atf-c++.hpp>
34*11be35a1SLionel Sambuc
35*11be35a1SLionel Sambuc #include "engine/action.hpp"
36*11be35a1SLionel Sambuc #include "engine/context.hpp"
37*11be35a1SLionel Sambuc #include "engine/test_result.hpp"
38*11be35a1SLionel Sambuc #include "store/backend.hpp"
39*11be35a1SLionel Sambuc #include "store/exceptions.hpp"
40*11be35a1SLionel Sambuc #include "store/transaction.hpp"
41*11be35a1SLionel Sambuc #include "utils/datetime.hpp"
42*11be35a1SLionel Sambuc #include "utils/format/macros.hpp"
43*11be35a1SLionel Sambuc #include "utils/optional.ipp"
44*11be35a1SLionel Sambuc #include "utils/sanity.hpp"
45*11be35a1SLionel Sambuc
46*11be35a1SLionel Sambuc namespace datetime = utils::datetime;
47*11be35a1SLionel Sambuc namespace fs = utils::fs;
48*11be35a1SLionel Sambuc namespace scan_action = engine::drivers::scan_action;
49*11be35a1SLionel Sambuc
50*11be35a1SLionel Sambuc using utils::none;
51*11be35a1SLionel Sambuc using utils::optional;
52*11be35a1SLionel Sambuc
53*11be35a1SLionel Sambuc
54*11be35a1SLionel Sambuc namespace {
55*11be35a1SLionel Sambuc
56*11be35a1SLionel Sambuc
57*11be35a1SLionel Sambuc /// Records the callback values for futher investigation.
58*11be35a1SLionel Sambuc class capture_hooks : public scan_action::base_hooks {
59*11be35a1SLionel Sambuc public:
60*11be35a1SLionel Sambuc /// The captured action ID, if any.
61*11be35a1SLionel Sambuc optional< int64_t > _action_id;
62*11be35a1SLionel Sambuc
63*11be35a1SLionel Sambuc /// The captured action, if any.
64*11be35a1SLionel Sambuc optional< engine::action > _action;
65*11be35a1SLionel Sambuc
66*11be35a1SLionel Sambuc /// The captured results, flattened as "program:test_case:result".
67*11be35a1SLionel Sambuc std::set< std::string > _results;
68*11be35a1SLionel Sambuc
69*11be35a1SLionel Sambuc /// Callback executed when an action is found.
70*11be35a1SLionel Sambuc ///
71*11be35a1SLionel Sambuc /// \param action_id The identifier of the loaded action.
72*11be35a1SLionel Sambuc /// \param action The action loaded from the database.
got_action(const int64_t action_id,const engine::action & action)73*11be35a1SLionel Sambuc void got_action(const int64_t action_id,
74*11be35a1SLionel Sambuc const engine::action& action)
75*11be35a1SLionel Sambuc {
76*11be35a1SLionel Sambuc PRE(!_action_id);
77*11be35a1SLionel Sambuc _action_id = action_id;
78*11be35a1SLionel Sambuc PRE(!_action);
79*11be35a1SLionel Sambuc _action = action;
80*11be35a1SLionel Sambuc }
81*11be35a1SLionel Sambuc
82*11be35a1SLionel Sambuc /// Callback executed when a test results is found.
83*11be35a1SLionel Sambuc ///
84*11be35a1SLionel Sambuc /// \param iter Container for the test result's data.
got_result(store::results_iterator & iter)85*11be35a1SLionel Sambuc void got_result(store::results_iterator& iter)
86*11be35a1SLionel Sambuc {
87*11be35a1SLionel Sambuc const char* type;
88*11be35a1SLionel Sambuc switch (iter.result().type()) {
89*11be35a1SLionel Sambuc case engine::test_result::passed: type = "passed"; break;
90*11be35a1SLionel Sambuc case engine::test_result::skipped: type = "skipped"; break;
91*11be35a1SLionel Sambuc default:
92*11be35a1SLionel Sambuc UNREACHABLE_MSG("Formatting unimplemented");
93*11be35a1SLionel Sambuc }
94*11be35a1SLionel Sambuc _results.insert(F("%s:%s:%s:%s:%s:%s") %
95*11be35a1SLionel Sambuc iter.test_program()->absolute_path() %
96*11be35a1SLionel Sambuc iter.test_case_name() % type % iter.result().reason() %
97*11be35a1SLionel Sambuc iter.duration().seconds % iter.duration().useconds);
98*11be35a1SLionel Sambuc }
99*11be35a1SLionel Sambuc };
100*11be35a1SLionel Sambuc
101*11be35a1SLionel Sambuc
102*11be35a1SLionel Sambuc /// Populates a test database with a new action.
103*11be35a1SLionel Sambuc ///
104*11be35a1SLionel Sambuc /// It is OK to call this function multiple times on the same file. Doing this
105*11be35a1SLionel Sambuc /// will generate a new action every time on the test database.
106*11be35a1SLionel Sambuc ///
107*11be35a1SLionel Sambuc /// \param db_name The database to update.
108*11be35a1SLionel Sambuc /// \param count A number that indicates how many elements to insert in the
109*11be35a1SLionel Sambuc /// action. Can be used to determine from the caller which particular
110*11be35a1SLionel Sambuc /// action has been loaded.
111*11be35a1SLionel Sambuc ///
112*11be35a1SLionel Sambuc /// \return The identifier of the committed action.
113*11be35a1SLionel Sambuc static int64_t
populate_db(const char * db_name,const int count)114*11be35a1SLionel Sambuc populate_db(const char* db_name, const int count)
115*11be35a1SLionel Sambuc {
116*11be35a1SLionel Sambuc store::backend backend = store::backend::open_rw(fs::path(db_name));
117*11be35a1SLionel Sambuc
118*11be35a1SLionel Sambuc store::transaction tx = backend.start();
119*11be35a1SLionel Sambuc
120*11be35a1SLionel Sambuc std::map< std::string, std::string > env;
121*11be35a1SLionel Sambuc for (int i = 0; i < count; i++)
122*11be35a1SLionel Sambuc env[F("VAR%s") % i] = F("Value %s") % i;
123*11be35a1SLionel Sambuc const engine::context context(fs::path("/root"), env);
124*11be35a1SLionel Sambuc const engine::action action(context);
125*11be35a1SLionel Sambuc const int64_t context_id = tx.put_context(context);
126*11be35a1SLionel Sambuc const int64_t action_id = tx.put_action(action, context_id);
127*11be35a1SLionel Sambuc
128*11be35a1SLionel Sambuc for (int i = 0; i < count; i++) {
129*11be35a1SLionel Sambuc const engine::test_program test_program(
130*11be35a1SLionel Sambuc "plain", fs::path(F("dir/prog_%s") % i), fs::path("/root"),
131*11be35a1SLionel Sambuc F("suite_%s") % i, engine::metadata_builder().build());
132*11be35a1SLionel Sambuc const int64_t tp_id = tx.put_test_program(test_program, action_id);
133*11be35a1SLionel Sambuc
134*11be35a1SLionel Sambuc for (int j = 0; j < count; j++) {
135*11be35a1SLionel Sambuc const engine::test_case test_case(
136*11be35a1SLionel Sambuc "plain", test_program, "main",
137*11be35a1SLionel Sambuc engine::metadata_builder().build());
138*11be35a1SLionel Sambuc const engine::test_result result(engine::test_result::skipped,
139*11be35a1SLionel Sambuc F("Count %s") % j);
140*11be35a1SLionel Sambuc const int64_t tc_id = tx.put_test_case(test_case, tp_id);
141*11be35a1SLionel Sambuc const datetime::timestamp start =
142*11be35a1SLionel Sambuc datetime::timestamp::from_microseconds(1000010);
143*11be35a1SLionel Sambuc const datetime::timestamp end =
144*11be35a1SLionel Sambuc datetime::timestamp::from_microseconds(5000020 + i + j);
145*11be35a1SLionel Sambuc tx.put_result(result, tc_id, start, end);
146*11be35a1SLionel Sambuc }
147*11be35a1SLionel Sambuc }
148*11be35a1SLionel Sambuc
149*11be35a1SLionel Sambuc tx.commit();
150*11be35a1SLionel Sambuc
151*11be35a1SLionel Sambuc return action_id;
152*11be35a1SLionel Sambuc }
153*11be35a1SLionel Sambuc
154*11be35a1SLionel Sambuc
155*11be35a1SLionel Sambuc } // anonymous namespace
156*11be35a1SLionel Sambuc
157*11be35a1SLionel Sambuc
158*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(latest_action);
ATF_TEST_CASE_BODY(latest_action)159*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(latest_action)
160*11be35a1SLionel Sambuc {
161*11be35a1SLionel Sambuc (void)populate_db("test.db", 3);
162*11be35a1SLionel Sambuc const int64_t action_id = populate_db("test.db", 2);
163*11be35a1SLionel Sambuc
164*11be35a1SLionel Sambuc capture_hooks hooks;
165*11be35a1SLionel Sambuc scan_action::drive(fs::path("test.db"), none, hooks);
166*11be35a1SLionel Sambuc
167*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(action_id, hooks._action_id.get());
168*11be35a1SLionel Sambuc
169*11be35a1SLionel Sambuc std::map< std::string, std::string > env;
170*11be35a1SLionel Sambuc env["VAR0"] = "Value 0";
171*11be35a1SLionel Sambuc env["VAR1"] = "Value 1";
172*11be35a1SLionel Sambuc const engine::context context(fs::path("/root"), env);
173*11be35a1SLionel Sambuc const engine::action action(context);
174*11be35a1SLionel Sambuc ATF_REQUIRE(action == hooks._action.get());
175*11be35a1SLionel Sambuc
176*11be35a1SLionel Sambuc std::set< std::string > results;
177*11be35a1SLionel Sambuc results.insert("/root/dir/prog_0:main:skipped:Count 0:4:10");
178*11be35a1SLionel Sambuc results.insert("/root/dir/prog_0:main:skipped:Count 1:4:11");
179*11be35a1SLionel Sambuc results.insert("/root/dir/prog_1:main:skipped:Count 0:4:11");
180*11be35a1SLionel Sambuc results.insert("/root/dir/prog_1:main:skipped:Count 1:4:12");
181*11be35a1SLionel Sambuc ATF_REQUIRE(results == hooks._results);
182*11be35a1SLionel Sambuc }
183*11be35a1SLionel Sambuc
184*11be35a1SLionel Sambuc
185*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(explicit_action);
ATF_TEST_CASE_BODY(explicit_action)186*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(explicit_action)
187*11be35a1SLionel Sambuc {
188*11be35a1SLionel Sambuc (void)populate_db("test.db", 5);
189*11be35a1SLionel Sambuc const int64_t action_id = populate_db("test.db", 1);
190*11be35a1SLionel Sambuc (void)populate_db("test.db", 2);
191*11be35a1SLionel Sambuc
192*11be35a1SLionel Sambuc capture_hooks hooks;
193*11be35a1SLionel Sambuc scan_action::drive(fs::path("test.db"),
194*11be35a1SLionel Sambuc optional< int64_t >(action_id), hooks);
195*11be35a1SLionel Sambuc
196*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(action_id, hooks._action_id.get());
197*11be35a1SLionel Sambuc
198*11be35a1SLionel Sambuc std::map< std::string, std::string > env;
199*11be35a1SLionel Sambuc env["VAR0"] = "Value 0";
200*11be35a1SLionel Sambuc const engine::context context(fs::path("/root"), env);
201*11be35a1SLionel Sambuc const engine::action action(context);
202*11be35a1SLionel Sambuc ATF_REQUIRE(action == hooks._action.get());
203*11be35a1SLionel Sambuc
204*11be35a1SLionel Sambuc std::set< std::string > results;
205*11be35a1SLionel Sambuc results.insert("/root/dir/prog_0:main:skipped:Count 0:4:10");
206*11be35a1SLionel Sambuc ATF_REQUIRE(results == hooks._results);
207*11be35a1SLionel Sambuc }
208*11be35a1SLionel Sambuc
209*11be35a1SLionel Sambuc
210*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(missing_db);
ATF_TEST_CASE_BODY(missing_db)211*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(missing_db)
212*11be35a1SLionel Sambuc {
213*11be35a1SLionel Sambuc capture_hooks hooks;
214*11be35a1SLionel Sambuc ATF_REQUIRE_THROW(store::error,
215*11be35a1SLionel Sambuc scan_action::drive(fs::path("test.db"), none, hooks));
216*11be35a1SLionel Sambuc }
217*11be35a1SLionel Sambuc
218*11be35a1SLionel Sambuc
ATF_INIT_TEST_CASES(tcs)219*11be35a1SLionel Sambuc ATF_INIT_TEST_CASES(tcs)
220*11be35a1SLionel Sambuc {
221*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, latest_action);
222*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, explicit_action);
223*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, missing_db);
224*11be35a1SLionel Sambuc }
225