|
| 1 | +#include <sys/wait.h> |
| 2 | + |
| 3 | +#include <cstdlib> |
| 4 | +#include <filesystem> |
| 5 | +#include <string> |
| 6 | +#include <string_view> |
| 7 | +#include <vector> |
| 8 | + |
| 9 | +#include <Catch2/single_include/catch2/catch.hpp> |
| 10 | +#include <fmt/format.h> |
| 11 | + |
| 12 | +#include "../src/clp_s/JsonConstructor.hpp" |
| 13 | +#include "../src/clp_s/JsonParser.hpp" |
| 14 | + |
| 15 | +constexpr std::string_view cTestEndToEndArchiveDirectory{"test-end-to-end-archive"}; |
| 16 | +constexpr std::string_view cTestEndToEndOutputDirectory{"test-end-to-end-out"}; |
| 17 | +constexpr std::string_view cTestEndToEndOutputSortedJson{"test-end-to-end_sorted.jsonl"}; |
| 18 | +constexpr std::string_view cTestEndToEndInputFileDirectory{"test_log_files"}; |
| 19 | +constexpr std::string_view cTestEndToEndInputFile{"test_no_floats_sorted.jsonl"}; |
| 20 | + |
| 21 | +namespace { |
| 22 | +/** |
| 23 | + * A class that deletes the directories and files created by test cases, both before and after each |
| 24 | + * test case where the class is instantiated. |
| 25 | + */ |
| 26 | +class TestOutputCleaner { |
| 27 | +public: |
| 28 | + TestOutputCleaner() { delete_files(); } |
| 29 | + |
| 30 | + ~TestOutputCleaner() { delete_files(); } |
| 31 | + |
| 32 | + // Delete copy & move constructors and assignment operators |
| 33 | + TestOutputCleaner(TestOutputCleaner const&) = delete; |
| 34 | + TestOutputCleaner(TestOutputCleaner&&) = delete; |
| 35 | + auto operator=(TestOutputCleaner const&) -> TestOutputCleaner& = delete; |
| 36 | + auto operator=(TestOutputCleaner&&) -> TestOutputCleaner& = delete; |
| 37 | + |
| 38 | +private: |
| 39 | + static void delete_files() { |
| 40 | + std::filesystem::remove_all(cTestEndToEndArchiveDirectory); |
| 41 | + std::filesystem::remove_all(cTestEndToEndOutputDirectory); |
| 42 | + std::filesystem::remove(cTestEndToEndOutputSortedJson); |
| 43 | + } |
| 44 | +}; |
| 45 | + |
| 46 | +auto get_test_input_path_relative_to_tests_dir() -> std::filesystem::path; |
| 47 | +auto get_test_input_local_path() -> std::string; |
| 48 | +void compress(bool structurize_arrays); |
| 49 | +auto extract() -> std::filesystem::path; |
| 50 | +void compare(std::filesystem::path const& extracted_json_path); |
| 51 | + |
| 52 | +auto get_test_input_path_relative_to_tests_dir() -> std::filesystem::path { |
| 53 | + return std::filesystem::path{cTestEndToEndInputFileDirectory} / cTestEndToEndInputFile; |
| 54 | +} |
| 55 | + |
| 56 | +auto get_test_input_local_path() -> std::string { |
| 57 | + std::filesystem::path const current_file_path{__FILE__}; |
| 58 | + auto const tests_dir{current_file_path.parent_path()}; |
| 59 | + return (tests_dir / get_test_input_path_relative_to_tests_dir()).string(); |
| 60 | +} |
| 61 | + |
| 62 | +void compress(bool structurize_arrays) { |
| 63 | + constexpr auto cDefaultTargetEncodedSize = 8ULL * 1024 * 1024 * 1024; // 8 GiB |
| 64 | + constexpr auto cDefaultMaxDocumentSize = 512ULL * 1024 * 1024; // 512 MiB |
| 65 | + constexpr auto cDefaultMinTableSize = 1ULL * 1024 * 1024; // 1 MiB |
| 66 | + constexpr auto cDefaultCompressionLevel = 3; |
| 67 | + constexpr auto cDefaultPrintArchiveStats = false; |
| 68 | + |
| 69 | + std::filesystem::create_directory(cTestEndToEndArchiveDirectory); |
| 70 | + REQUIRE((std::filesystem::is_directory(cTestEndToEndArchiveDirectory))); |
| 71 | + |
| 72 | + clp_s::JsonParserOption parser_option{}; |
| 73 | + parser_option.file_paths.push_back(get_test_input_local_path()); |
| 74 | + parser_option.archives_dir = cTestEndToEndArchiveDirectory; |
| 75 | + parser_option.target_encoded_size = cDefaultTargetEncodedSize; |
| 76 | + parser_option.max_document_size = cDefaultMaxDocumentSize; |
| 77 | + parser_option.min_table_size = cDefaultMinTableSize; |
| 78 | + parser_option.compression_level = cDefaultCompressionLevel; |
| 79 | + parser_option.print_archive_stats = cDefaultPrintArchiveStats; |
| 80 | + parser_option.structurize_arrays = structurize_arrays; |
| 81 | + |
| 82 | + clp_s::JsonParser parser{parser_option}; |
| 83 | + REQUIRE(parser.parse()); |
| 84 | + parser.store(); |
| 85 | + |
| 86 | + REQUIRE((false == std::filesystem::is_empty(cTestEndToEndArchiveDirectory))); |
| 87 | +} |
| 88 | + |
| 89 | +auto extract() -> std::filesystem::path { |
| 90 | + constexpr auto cDefaultOrdered = false; |
| 91 | + constexpr auto cDefaultTargetOrderedChunkSize = 0; |
| 92 | + |
| 93 | + std::filesystem::create_directory(cTestEndToEndOutputDirectory); |
| 94 | + REQUIRE(std::filesystem::is_directory(cTestEndToEndOutputDirectory)); |
| 95 | + |
| 96 | + clp_s::JsonConstructorOption constructor_option{}; |
| 97 | + constructor_option.archives_dir = cTestEndToEndArchiveDirectory; |
| 98 | + constructor_option.output_dir = cTestEndToEndOutputDirectory; |
| 99 | + constructor_option.ordered = cDefaultOrdered; |
| 100 | + constructor_option.target_ordered_chunk_size = cDefaultTargetOrderedChunkSize; |
| 101 | + for (auto const& entry : std::filesystem::directory_iterator(constructor_option.archives_dir)) { |
| 102 | + if (false == entry.is_directory()) { |
| 103 | + // Skip non-directories |
| 104 | + continue; |
| 105 | + } |
| 106 | + |
| 107 | + constructor_option.archive_id = entry.path().filename(); |
| 108 | + clp_s::JsonConstructor constructor{constructor_option}; |
| 109 | + constructor.store(); |
| 110 | + } |
| 111 | + std::filesystem::path extracted_json_path{cTestEndToEndOutputDirectory}; |
| 112 | + extracted_json_path /= "original"; |
| 113 | + REQUIRE(std::filesystem::exists(extracted_json_path)); |
| 114 | + |
| 115 | + return extracted_json_path; |
| 116 | +} |
| 117 | + |
| 118 | +// Silence the checks below since our use of `std::system` is safe in the context of testing. |
| 119 | +// NOLINTBEGIN(cert-env33-c,concurrency-mt-unsafe) |
| 120 | +void compare(std::filesystem::path const& extracted_json_path) { |
| 121 | + int result{std::system("command -v jq >/dev/null 2>&1")}; |
| 122 | + REQUIRE((0 == result)); |
| 123 | + auto command = fmt::format( |
| 124 | + "jq --sort-keys --compact-output '.' {} | sort > {}", |
| 125 | + extracted_json_path.string(), |
| 126 | + cTestEndToEndOutputSortedJson |
| 127 | + ); |
| 128 | + result = std::system(command.c_str()); |
| 129 | + REQUIRE((0 == result)); |
| 130 | + |
| 131 | + REQUIRE((false == std::filesystem::is_empty(cTestEndToEndOutputSortedJson))); |
| 132 | + |
| 133 | + result = std::system("command -v diff >/dev/null 2>&1"); |
| 134 | + REQUIRE((0 == result)); |
| 135 | + command = fmt::format( |
| 136 | + "diff --unified {} {} > /dev/null", |
| 137 | + cTestEndToEndOutputSortedJson, |
| 138 | + get_test_input_local_path() |
| 139 | + ); |
| 140 | + result = std::system(command.c_str()); |
| 141 | + REQUIRE((true == WIFEXITED(result))); |
| 142 | + REQUIRE((0 == WEXITSTATUS(result))); |
| 143 | +} |
| 144 | + |
| 145 | +// NOLINTEND(cert-env33-c,concurrency-mt-unsafe) |
| 146 | +} // namespace |
| 147 | + |
| 148 | +TEST_CASE("clp-s-compress-extract-no-floats", "[clp-s][end-to-end]") { |
| 149 | + auto structurize_arrays = GENERATE(true, false); |
| 150 | + |
| 151 | + TestOutputCleaner const test_cleanup; |
| 152 | + |
| 153 | + compress(structurize_arrays); |
| 154 | + |
| 155 | + auto extracted_json_path = extract(); |
| 156 | + |
| 157 | + compare(extracted_json_path); |
| 158 | +} |
0 commit comments