Path: blob/main/contrib/kyua/store/transaction_test.cpp
39478 views
// Copyright 2011 The Kyua Authors.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above copyright10// notice, this list of conditions and the following disclaimer in the11// documentation and/or other materials provided with the distribution.12// * Neither the name of Google Inc. nor the names of its contributors13// may be used to endorse or promote products derived from this software14// without specific prior written permission.15//16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2728#include <map>29#include <string>3031#include <atf-c++.hpp>3233#include "model/context.hpp"34#include "model/metadata.hpp"35#include "model/test_program.hpp"36#include "store/read_backend.hpp"37#include "store/read_transaction.hpp"38#include "store/write_backend.hpp"39#include "store/write_transaction.hpp"40#include "utils/datetime.hpp"41#include "utils/fs/operations.hpp"42#include "utils/fs/path.hpp"43#include "utils/logging/operations.hpp"44#include "utils/sqlite/database.hpp"45#include "utils/units.hpp"4647namespace datetime = utils::datetime;48namespace fs = utils::fs;49namespace logging = utils::logging;50namespace units = utils::units;515253namespace {545556/// Puts and gets a context and validates the results.57///58/// \param exp_context The context to save and restore.59static void60check_get_put_context(const model::context& exp_context)61{62const fs::path test_db("test.db");6364if (fs::exists(test_db))65fs::unlink(test_db);6667{68store::write_backend backend = store::write_backend::open_rw(test_db);69store::write_transaction tx = backend.start_write();70tx.put_context(exp_context);71tx.commit();72}73{74store::read_backend backend = store::read_backend::open_ro(test_db);75store::read_transaction tx = backend.start_read();76model::context context = tx.get_context();77tx.finish();7879ATF_REQUIRE(exp_context == context);80}81}828384} // anonymous namespace858687ATF_TEST_CASE(get_put_context__ok);88ATF_TEST_CASE_HEAD(get_put_context__ok)89{90logging::set_inmemory();91set_md_var("require.files", store::detail::schema_file().c_str());92}93ATF_TEST_CASE_BODY(get_put_context__ok)94{95std::map< std::string, std::string > env1;96env1["A1"] = "foo";97env1["A2"] = "bar";98std::map< std::string, std::string > env2;99check_get_put_context(model::context(fs::path("/foo/bar"), env1));100check_get_put_context(model::context(fs::path("/foo/bar"), env1));101check_get_put_context(model::context(fs::path("/foo/baz"), env2));102}103104105ATF_TEST_CASE(get_put_test_case__ok);106ATF_TEST_CASE_HEAD(get_put_test_case__ok)107{108logging::set_inmemory();109set_md_var("require.files", store::detail::schema_file().c_str());110}111ATF_TEST_CASE_BODY(get_put_test_case__ok)112{113const model::metadata md2 = model::metadata_builder()114.add_allowed_architecture("powerpc")115.add_allowed_architecture("x86_64")116.add_allowed_platform("amd64")117.add_allowed_platform("macppc")118.add_custom("user1", "value1")119.add_custom("user2", "value2")120.add_required_config("var1")121.add_required_config("var2")122.add_required_config("var3")123.add_required_file(fs::path("/file1/yes"))124.add_required_file(fs::path("/file2/foo"))125.add_required_program(fs::path("/bin/ls"))126.add_required_program(fs::path("cp"))127.set_description("The description")128.set_has_cleanup(true)129.set_required_memory(units::bytes::parse("1k"))130.set_required_user("root")131.set_timeout(datetime::delta(520, 0))132.build();133134const model::test_program test_program = model::test_program_builder(135"atf", fs::path("the/binary"), fs::path("/some/root"), "the-suite")136.add_test_case("tc1")137.add_test_case("tc2", md2)138.build();139140int64_t test_program_id;141{142store::write_backend backend = store::write_backend::open_rw(143fs::path("test.db"));144backend.database().exec("PRAGMA foreign_keys = OFF");145146store::write_transaction tx = backend.start_write();147test_program_id = tx.put_test_program(test_program);148tx.put_test_case(test_program, "tc1", test_program_id);149tx.put_test_case(test_program, "tc2", test_program_id);150tx.commit();151}152153store::read_backend backend = store::read_backend::open_ro(154fs::path("test.db"));155backend.database().exec("PRAGMA foreign_keys = OFF");156157store::read_transaction tx = backend.start_read();158const model::test_program_ptr loaded_test_program =159store::detail::get_test_program(backend, test_program_id);160ATF_REQUIRE(test_program == *loaded_test_program);161}162163164ATF_INIT_TEST_CASES(tcs)165{166ATF_ADD_TEST_CASE(tcs, get_put_context__ok);167168ATF_ADD_TEST_CASE(tcs, get_put_test_case__ok);169}170171172