/* * Copyright (C) 2020-present ScyllaDB */ /* * SPDX-License-Identifier: LicenseRef-ScyllaDB-Source-Available-1.0 */ #include "test/lib/test_utils.hh" #include #include #include #include "test/lib/log.hh" #include "test/lib/simple_schema.hh" #include "utils/to_string.hh" #include "replica/database.hh" #include "seastarx.hh" #include namespace tests { namespace { std::string format_msg(std::string_view test_function_name, bool ok, seastar::compat::source_location sl, std::string_view msg) { return fmt::format("{}(): {} @ {}() {}:{:d}{}{}", test_function_name, ok ? "OK" : "FAIL", sl.function_name(), sl.file_name(), sl.line(), msg.empty() ? "" : ": ", msg); } } bool do_check(bool condition, seastar::compat::source_location sl, std::string_view msg) { if (condition) { testlog.trace("{}", format_msg(__FUNCTION__, condition, sl, msg)); } else { testlog.error("{}", format_msg(__FUNCTION__, condition, sl, msg)); } return condition; } void do_require(bool condition, seastar::compat::source_location sl, std::string_view msg) { if (condition) { testlog.trace("{}", format_msg(__FUNCTION__, condition, sl, msg)); } else { auto formatted_msg = format_msg(__FUNCTION__, condition, sl, msg); testlog.error("{}", formatted_msg); throw_with_backtrace(std::move(formatted_msg)); } } void fail(std::string_view msg, seastar::compat::source_location sl) { throw_with_backtrace(format_msg(__FUNCTION__, false, sl, msg)); } extern boost::test_tools::assertion_result has_scylla_test_env(boost::unit_test::test_unit_id) { if (::getenv("SCYLLA_TEST_ENV")) { return true; } testlog.info("Test environment is not configured. " "Check test/pylib/minio_server.py for an example of how to configure the environment for it to run."); return false; } } sstring make_random_string(size_t size) { static thread_local std::default_random_engine rng; std::uniform_int_distribution dist; sstring str = uninitialized_string(size); for (auto&& b : str) { b = dist(rng); } return str; } sstring make_random_numeric_string(size_t size) { static thread_local std::default_random_engine rng; std::uniform_int_distribution dist('0', '9'); sstring str = uninitialized_string(size); for (auto&& b : str) { b = dist(rng); } return str; } namespace tests { future compare_files(std::string fa, std::string fb) { auto cont_a = co_await util::read_entire_file_contiguous(fa); auto cont_b = co_await util::read_entire_file_contiguous(fb); co_return cont_a == cont_b; } future<> touch_file(std::string name) { auto f = co_await open_file_dma(name, open_flags::create); co_await f.close(); } std::mutex boost_logger_mutex; // Helper to get directory a table keeps its data in. // Only suitable for tests, that work with local storage type. fs::path table_dir(const replica::table& cf) { return std::get(cf.get_storage_options().value).dir; } void copy_directory(fs::path src_dir, fs::path dst_dir, fs::copy_options opts) { if (!fs::exists(dst_dir)) { fs::create_directory(dst_dir); } auto src_dir_components = std::distance(src_dir.begin(), src_dir.end()); using rdi = fs::recursive_directory_iterator; // Boost 1.55.0 doesn't support range for on recursive_directory_iterator // (even though previous and later versions do support it) for (auto&& dirent = rdi{src_dir}; dirent != rdi(); ++dirent) { auto&& path = dirent->path(); auto new_path = dst_dir; for (auto i = std::next(path.begin(), src_dir_components); i != path.end(); ++i) { new_path /= *i; } fs::copy(path, new_path, opts); } } }