xref: /freebsd/contrib/kyua/store/transaction_test.cpp (revision b0d29bc47dba79f6f38e67eabadfb4b32ffd9390)
1 // Copyright 2011 The Kyua Authors.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
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.
16 //
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.
28 
29 #include <map>
30 #include <string>
31 
32 #include <atf-c++.hpp>
33 
34 #include "model/context.hpp"
35 #include "model/metadata.hpp"
36 #include "model/test_program.hpp"
37 #include "store/read_backend.hpp"
38 #include "store/read_transaction.hpp"
39 #include "store/write_backend.hpp"
40 #include "store/write_transaction.hpp"
41 #include "utils/datetime.hpp"
42 #include "utils/fs/operations.hpp"
43 #include "utils/fs/path.hpp"
44 #include "utils/logging/operations.hpp"
45 #include "utils/sqlite/database.hpp"
46 #include "utils/units.hpp"
47 
48 namespace datetime = utils::datetime;
49 namespace fs = utils::fs;
50 namespace logging = utils::logging;
51 namespace units = utils::units;
52 
53 
54 namespace {
55 
56 
57 /// Puts and gets a context and validates the results.
58 ///
59 /// \param exp_context The context to save and restore.
60 static void
check_get_put_context(const model::context & exp_context)61 check_get_put_context(const model::context& exp_context)
62 {
63     const fs::path test_db("test.db");
64 
65     if (fs::exists(test_db))
66         fs::unlink(test_db);
67 
68     {
69         store::write_backend backend = store::write_backend::open_rw(test_db);
70         store::write_transaction tx = backend.start_write();
71         tx.put_context(exp_context);
72         tx.commit();
73     }
74     {
75         store::read_backend backend = store::read_backend::open_ro(test_db);
76         store::read_transaction tx = backend.start_read();
77         model::context context = tx.get_context();
78         tx.finish();
79 
80         ATF_REQUIRE(exp_context == context);
81     }
82 }
83 
84 
85 }  // anonymous namespace
86 
87 
88 ATF_TEST_CASE(get_put_context__ok);
ATF_TEST_CASE_HEAD(get_put_context__ok)89 ATF_TEST_CASE_HEAD(get_put_context__ok)
90 {
91     logging::set_inmemory();
92     set_md_var("require.files", store::detail::schema_file().c_str());
93 }
ATF_TEST_CASE_BODY(get_put_context__ok)94 ATF_TEST_CASE_BODY(get_put_context__ok)
95 {
96     std::map< std::string, std::string > env1;
97     env1["A1"] = "foo";
98     env1["A2"] = "bar";
99     std::map< std::string, std::string > env2;
100     check_get_put_context(model::context(fs::path("/foo/bar"), env1));
101     check_get_put_context(model::context(fs::path("/foo/bar"), env1));
102     check_get_put_context(model::context(fs::path("/foo/baz"), env2));
103 }
104 
105 
106 ATF_TEST_CASE(get_put_test_case__ok);
ATF_TEST_CASE_HEAD(get_put_test_case__ok)107 ATF_TEST_CASE_HEAD(get_put_test_case__ok)
108 {
109     logging::set_inmemory();
110     set_md_var("require.files", store::detail::schema_file().c_str());
111 }
ATF_TEST_CASE_BODY(get_put_test_case__ok)112 ATF_TEST_CASE_BODY(get_put_test_case__ok)
113 {
114     const model::metadata md2 = model::metadata_builder()
115         .add_allowed_architecture("powerpc")
116         .add_allowed_architecture("x86_64")
117         .add_allowed_platform("amd64")
118         .add_allowed_platform("macppc")
119         .add_custom("user1", "value1")
120         .add_custom("user2", "value2")
121         .add_required_config("var1")
122         .add_required_config("var2")
123         .add_required_config("var3")
124         .add_required_file(fs::path("/file1/yes"))
125         .add_required_file(fs::path("/file2/foo"))
126         .add_required_program(fs::path("/bin/ls"))
127         .add_required_program(fs::path("cp"))
128         .set_description("The description")
129         .set_has_cleanup(true)
130         .set_required_memory(units::bytes::parse("1k"))
131         .set_required_user("root")
132         .set_timeout(datetime::delta(520, 0))
133         .build();
134 
135     const model::test_program test_program = model::test_program_builder(
136         "atf", fs::path("the/binary"), fs::path("/some/root"), "the-suite")
137         .add_test_case("tc1")
138         .add_test_case("tc2", md2)
139         .build();
140 
141     int64_t test_program_id;
142     {
143         store::write_backend backend = store::write_backend::open_rw(
144             fs::path("test.db"));
145         backend.database().exec("PRAGMA foreign_keys = OFF");
146 
147         store::write_transaction tx = backend.start_write();
148         test_program_id = tx.put_test_program(test_program);
149         tx.put_test_case(test_program, "tc1", test_program_id);
150         tx.put_test_case(test_program, "tc2", test_program_id);
151         tx.commit();
152     }
153 
154     store::read_backend backend = store::read_backend::open_ro(
155         fs::path("test.db"));
156     backend.database().exec("PRAGMA foreign_keys = OFF");
157 
158     store::read_transaction tx = backend.start_read();
159     const model::test_program_ptr loaded_test_program =
160         store::detail::get_test_program(backend, test_program_id);
161     ATF_REQUIRE(test_program == *loaded_test_program);
162 }
163 
164 
ATF_INIT_TEST_CASES(tcs)165 ATF_INIT_TEST_CASES(tcs)
166 {
167     ATF_ADD_TEST_CASE(tcs, get_put_context__ok);
168 
169     ATF_ADD_TEST_CASE(tcs, get_put_test_case__ok);
170 }
171