collector/test.cpp
2022-10-06 11:22:08 +02:00

66 lines
2.2 KiB
C++

/**
* @file test.cpp
* The unit test executable
*
* @author Tobias Schmidl
* @copyright Copyright © 2022, Tobias Schmidl
* SPDX-License-Identifier: MIT
*/
#include "collector.hpp"
#include "monitor.hpp"
#include "version.hpp"
#define BOOST_TEST_MODULE Collector test
#include <boost/test/unit_test.hpp>
#include <chrono>
#include <fstream>
#include <future>
#include <thread>
using namespace collector::monitor;
using namespace collector::collector;
using namespace std::chrono_literals;
boost::asio::io_service service;
BOOST_AUTO_TEST_CASE(monitor_test)
{
BOOST_TEST_MESSAGE("Test version " << VERSION);
const std::filesystem::path test_file{ "test_file" };
std::filesystem::remove(test_file);
const auto create_file_result = std::async([&test_file]() {
std::this_thread::sleep_for(2s);
std::ofstream{ test_file };
});
Monitor monitor(service, std::filesystem::current_path());
const auto& watch_result =
monitor.watch(std::regex(test_file.generic_string()),
boost::asio::dir_monitor_event::event_type::added);
BOOST_TEST(std::filesystem::equivalent(test_file,
watch_result.path.generic_string()));
std::filesystem::remove(test_file);
}
BOOST_AUTO_TEST_CASE(collector_test)
{
BOOST_TEST_MESSAGE("Test version " << VERSION);
const std::filesystem::path target_file{ "test.tar" },
test_input_dir{ "test_input" };
std::filesystem::remove(target_file);
std::filesystem::remove_all(test_input_dir);
std::filesystem::create_directory(test_input_dir);
const auto create_file_result = std::async([&test_input_dir]() {
std::this_thread::sleep_for(5s);
std::ofstream test_file{ test_input_dir / "should_be_in_it" };
test_file << "Hello World." << std::endl;
test_file.flush();
std::ofstream{ test_input_dir / "Core.TEST.00.lz4" };
std::ofstream{ test_input_dir / "might_not_be_in_it" };
});
Collector collector(service, test_input_dir, target_file);
BOOST_TEST(collector());
BOOST_TEST(std::filesystem::is_regular_file(target_file));
std::filesystem::remove(target_file);
std::filesystem::remove_all(test_input_dir);
}