Path: blob/main/contrib/kyua/store/migrate_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 "store/migrate.hpp"2930extern "C" {31#include <sys/stat.h>32}3334#include <atf-c++.hpp>3536#include "store/exceptions.hpp"37#include "utils/env.hpp"38#include "utils/fs/operations.hpp"39#include "utils/fs/path.hpp"4041namespace fs = utils::fs;424344ATF_TEST_CASE_WITHOUT_HEAD(detail__backup_database__ok);45ATF_TEST_CASE_BODY(detail__backup_database__ok)46{47atf::utils::create_file("test.db", "The DB\n");48store::detail::backup_database(fs::path("test.db"), 13);49ATF_REQUIRE(fs::exists(fs::path("test.db")));50ATF_REQUIRE(fs::exists(fs::path("test.db.v13.backup")));51ATF_REQUIRE(atf::utils::compare_file("test.db.v13.backup", "The DB\n"));52}535455ATF_TEST_CASE_WITHOUT_HEAD(detail__backup_database__ok_overwrite);56ATF_TEST_CASE_BODY(detail__backup_database__ok_overwrite)57{58atf::utils::create_file("test.db", "Original contents");59atf::utils::create_file("test.db.v1.backup", "Overwrite me");60store::detail::backup_database(fs::path("test.db"), 1);61ATF_REQUIRE(fs::exists(fs::path("test.db")));62ATF_REQUIRE(fs::exists(fs::path("test.db.v1.backup")));63ATF_REQUIRE(atf::utils::compare_file("test.db.v1.backup",64"Original contents"));65}666768ATF_TEST_CASE_WITHOUT_HEAD(detail__backup_database__fail_open);69ATF_TEST_CASE_BODY(detail__backup_database__fail_open)70{71ATF_REQUIRE_THROW_RE(store::error, "Cannot open.*foo.db",72store::detail::backup_database(fs::path("foo.db"), 5));73}747576ATF_TEST_CASE_WITH_CLEANUP(detail__backup_database__fail_create);77ATF_TEST_CASE_HEAD(detail__backup_database__fail_create)78{79set_md_var("require.user", "unprivileged");80}81ATF_TEST_CASE_BODY(detail__backup_database__fail_create)82{83ATF_REQUIRE(::mkdir("dir", 0755) != -1);84atf::utils::create_file("dir/test.db", "Does not need to be valid");85ATF_REQUIRE(::chmod("dir", 0111) != -1);86ATF_REQUIRE_THROW_RE(87store::error, "Cannot create.*dir/test.db.v13.backup",88store::detail::backup_database(fs::path("dir/test.db"), 13));89}90ATF_TEST_CASE_CLEANUP(detail__backup_database__fail_create)91{92if (::chmod("dir", 0755) == -1) {93// If we cannot restore the original permissions, we cannot do much94// more. However, leaving an unwritable directory behind will cause the95// runtime engine to report us as broken.96}97}9899100ATF_TEST_CASE_WITHOUT_HEAD(detail__migration_file__builtin);101ATF_TEST_CASE_BODY(detail__migration_file__builtin)102{103utils::unsetenv("KYUA_STOREDIR");104ATF_REQUIRE_EQ(fs::path(KYUA_STOREDIR) / "migrate_v5_v9.sql",105store::detail::migration_file(5, 9));106}107108109ATF_TEST_CASE_WITHOUT_HEAD(detail__migration_file__overriden);110ATF_TEST_CASE_BODY(detail__migration_file__overriden)111{112utils::setenv("KYUA_STOREDIR", "/tmp/test");113ATF_REQUIRE_EQ(fs::path("/tmp/test/migrate_v5_v9.sql"),114store::detail::migration_file(5, 9));115}116117118ATF_INIT_TEST_CASES(tcs)119{120ATF_ADD_TEST_CASE(tcs, detail__backup_database__ok);121ATF_ADD_TEST_CASE(tcs, detail__backup_database__ok_overwrite);122ATF_ADD_TEST_CASE(tcs, detail__backup_database__fail_open);123ATF_ADD_TEST_CASE(tcs, detail__backup_database__fail_create);124125ATF_ADD_TEST_CASE(tcs, detail__migration_file__builtin);126ATF_ADD_TEST_CASE(tcs, detail__migration_file__overriden);127128// Tests for migrate_schema are in schema_inttest. This is because, for129// such tests to be meaningful, they need to be integration tests and don't130// really fit the goal of this unit-test module.131}132133134