TEST_F(FileUtilsTest, copy_from_nonexisting) { // copy from a non-existing file const fs::path target = FileUtils::create_temp_file_in_temp_dir("target"); ALWAYS_CLEANUP_FILE(target); const fs::path source = FileUtils::create_temp_file_in_temp_dir("source"); ALWAYS_CLEANUP_FILE(source); ASSERT_TRUE(fs::exists(target)); fs::remove(source); ASSERT_FALSE(fs::exists(source)); EXPECT_THROW(FileUtils::safe_copy(source, target), FileUtils::CopyNoSourceException); }
void BackendConnectionInterface::read(const Namespace& nspace, const boost::filesystem::path& destination, const std::string& name, const InsistOnLatestVersion insist_on_latest) { Logger l(__FUNCTION__, nspace, name); //cfr SSOBF-9585 //The target file is synced for the following reason: //Suppose a scocachemiss reads SCO X from the backend, then there is //a powerloss, the volume is locally restarted and reuses the local scocache. //Although the scocache assumes it has SCO X cached, it could very well contain no //data at all as it got lost during the powerloss. This then results in a // read-error from that SCO, leading to offlining a mountpoint (for no good // reason) and (when the last mountpoint is gone) halting volumes. boost::filesystem::path temp_file; try { temp_file = youtils::FileUtils::create_temp_file(destination); } catch(...) { throw BackendOutputException(); } ALWAYS_CLEANUP_FILE(temp_file); read_(nspace, temp_file, name, insist_on_latest); youtils::FileUtils::syncAndRename(temp_file, destination); }
// This is not too interesting either; it's mainly a debugging / testing help TEST_F(BackendObjectTest, cache_invalidation) { const std::string oname("some-object"); const fs::path src(path_ / (oname + ".src")); const fs::path dst(path_ / (oname + ".dst")); const size_t size = 1ULL << 20; BackendInterfacePtr bi(bi_(nspace_->ns())); const std::string pattern1("some pattern"); const yt::CheckSum chksum1(createPutAndVerify(src, size, pattern1, cm_, oname, nspace_->ns(), OverwriteObject::F)); auto check([&](const std::string& pattern, const yt::CheckSum& chksum, InsistOnLatestVersion insist) { bi->read(dst, oname, insist); ALWAYS_CLEANUP_FILE(dst); EXPECT_TRUE(verifyTestFile(dst, size, pattern, &chksum)); }); check(pattern1, chksum1, InsistOnLatestVersion::F); const std::string pattern2("some other pattern"); const yt::CheckSum chksum2(createAndPut(src, size, pattern2, cm_, oname, nspace_->ns(), OverwriteObject::T)); check(pattern2, chksum2, InsistOnLatestVersion::T); bi->invalidate_cache(); check(pattern2, chksum2, InsistOnLatestVersion::F); }
scrubbing::ScrubberResult get_scrub_result(Volume& v, const scrubbing::ScrubReply& rep) { EXPECT_EQ(rep.ns_.str(), v.getNamespace().str()); fs::path p(FileUtils::temp_path() / rep.scrub_result_name_); ALWAYS_CLEANUP_FILE(p); v.getBackendInterface()->read(p, rep.scrub_result_name_, InsistOnLatestVersion::T); scrubbing::ScrubberResult scrub_result; fs::ifstream ifs(p); boost::archive::text_iarchive ia(ifs); ia >> scrub_result; return scrub_result; }
void truncate_sco_in_backend(SCO sco, uint64_t size) { BackendInterfacePtr bi = vol_->getBackendInterface()->clone(); ASSERT_TRUE(bi != 0); fs::path tmp_file; ASSERT_NO_THROW(tmp_file = yt::FileUtils::create_temp_file(yt::FileUtils::temp_path(), sco.str())); ALWAYS_CLEANUP_FILE(tmp_file); ASSERT_NO_THROW(bi->read(tmp_file, sco.str(), InsistOnLatestVersion::T)); ASSERT_TRUE(fs::exists(tmp_file)); FileUtils::truncate(tmp_file, size); ASSERT_NO_THROW(bi->write(tmp_file, sco.str(), OverwriteObject::T)); EXPECT_EQ(size, bi->getSize(sco.str())); }
void Restore::copy_(be::BackendInterfacePtr source_bi, be::BackendInterfacePtr target_bi) { LOG_INFO("Copying volume from " << source_bi->getNS() << " -> " << target_bi->getNS()); if (not source_bi->objectExists(vd::VolumeConfig::config_backend_name)) { LOG_N_THROW(RestoreException, ERROR, source_bi->getNS() << ": no volume config found in source namespace"); } std::list<std::string> objects; source_bi->listObjects(objects); // Y42 straight copy would save some disk IO's // -- Almost straight namespace - namespace copy for (const auto& object : objects) { if(object != vd::VolumeConfig::config_backend_name and object != "lock_name") { const fs::path p(vd::FileUtils::create_temp_file_in_temp_dir(object)); ALWAYS_CLEANUP_FILE(p); boost::this_thread::interruption_point(); source_bi->read(p, object, InsistOnLatestVersion::T); target_bi->write(p, object); } } }
TEST_F(UUIDTest, test6) { std::list<UUID> vec1(20); const fs::path tmpfile(FileUtils::create_temp_file(FileUtils::temp_path(), "serialization.txt")); ALWAYS_CLEANUP_FILE(tmpfile); { fs::ofstream ofs(tmpfile); boost::archive::xml_oarchive oa(ofs); oa << BOOST_SERIALIZATION_NVP(vec1); } std::list<UUID> vec2; fs::ifstream ifs(tmpfile); boost::archive::xml_iarchive ia(ifs); ia >> BOOST_SERIALIZATION_NVP(vec2); ASSERT_TRUE(vec1 == vec2); }
TEST_F(UUIDTest, test5) { UUID uuid; const fs::path tmpfile(FileUtils::create_temp_file(FileUtils::temp_path(), "serialization.txt")); ALWAYS_CLEANUP_FILE(tmpfile); { fs::ofstream ofs(tmpfile); boost::archive::xml_oarchive oa(ofs); oa << BOOST_SERIALIZATION_NVP(uuid); } UUID uuid2; fs::ifstream ifs(tmpfile); boost::archive::xml_iarchive ia(ifs); ia >>BOOST_SERIALIZATION_NVP(uuid2); ASSERT_TRUE(uuid == uuid2); }
TEST_F(BackendObjectTest, overwrite) { const std::string objname("test"); const fs::path src(path_ / std::string(objname + ".src")); const fs::path dst(path_ / std::string(objname + ".dst")); const size_t size1 = 4 << 20; const std::string pattern1("Stop me if you think you've heard this one before"); const yt::CheckSum chksum1(createPutAndVerify(src, size1, pattern1, cm_, objname, nspace_->ns(), OverwriteObject::F)); EXPECT_TRUE(retrieveAndVerify(dst, size1, pattern1, &chksum1, cm_, objname, nspace_->ns())); fs::remove_all(src); fs::remove_all(dst); { const size_t size2 = size1 / 2; const std::string pattern2("Shoplifters of the world unite"); const yt::CheckSum chksum2(createTestFile(src, size2, pattern2)); ALWAYS_CLEANUP_FILE(src); EXPECT_THROW(bi_(nspace_->ns())->write(src, objname, OverwriteObject::F, &chksum2), BackendAssertionFailedException); try { EXPECT_FALSE(chksum2 == bi_(nspace_->ns())->getCheckSum(objname)); EXPECT_TRUE(chksum1 == bi_(nspace_->ns())->getCheckSum(objname)); } catch(const BackendNotImplementedException& e) {} EXPECT_EQ(size1, bi_(nspace_->ns())->getSize(objname)); bi_(nspace_->ns())->read(dst, objname, InsistOnLatestVersion::T); ALWAYS_CLEANUP_FILE(dst); EXPECT_TRUE(retrieveAndVerify(dst, size1, pattern1, &chksum1, cm_, objname, nspace_->ns())); } size_t size2 = size1 / 2; const std::string pattern2("Bigmouth strikes again"); const yt::CheckSum chksum2(createPutAndVerify(src, size2, pattern2, cm_, objname, nspace_->ns(), OverwriteObject::T)); EXPECT_TRUE(retrieveAndVerify(dst, size2, pattern2, &chksum2, cm_, objname, nspace_->ns())); }