~aleteoryx/muditaos

ref: c1391090c67428aeaffcef5a7a71a6d2ed69ccc9 muditaos/module-utils/log/tests/test_logDumps.cpp -rw-r--r-- 3.6 KiB
c1391090 — Mateusz Piesta [BH-1389] Catch2 unit tests optimization 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <catch2/catch.hpp>

#include <string>

#include <log/Logger.hpp>

namespace
{
    int countFiles(const std::filesystem::path &dir)
    {
        int sum = 0;
        for ([[maybe_unused]] auto const &entry : std::filesystem::directory_iterator{dir}) {
            ++sum;
        }
        return sum;
    }

    bool checkIfLogFilesExist(const std::filesystem::path &path, int expectedFilesCount)
    {
        for (int i = 0; i < expectedFilesCount; ++i) {
            auto filePath = path;
            if (i > 0) {
                filePath.replace_extension(".log." + std::to_string(i));
            }
            if (!std::filesystem::exists(filePath)) {
                return false;
            }
        }
        return true;
    }
} // namespace

TEST_CASE("Test if logs are dumped to a file without rotation")
{
    auto app                            = Log::Application{"TestApp", "rev", "tag", "branch"};
    constexpr auto MaxFileSize          = 1024 * 1024; // 1 MB
    constexpr auto TestLog              = "12345678";
    const std::filesystem::path logsDir = "./ut_logs";
    const auto testLogFile              = logsDir / "TestApp.log";

    // Prepare the environment.
    if (std::filesystem::exists(logsDir)) {
        std::filesystem::remove_all(logsDir);
    }
    std::filesystem::create_directory(logsDir);

    // Initialize the logger with test parameters.
    Log::Logger::get().init(std::move(app), MaxFileSize);
    REQUIRE(countFiles(logsDir) == 0);

    // Dump logs.
    LOG_ERROR(TestLog);
    Log::Logger::get().dumpToFile(testLogFile);
    REQUIRE(countFiles(logsDir) == 1);
    REQUIRE(checkIfLogFilesExist(testLogFile, 1));

    LOG_ERROR(TestLog);
    Log::Logger::get().dumpToFile(testLogFile);
    REQUIRE(countFiles(logsDir) == 1);
    REQUIRE(checkIfLogFilesExist(testLogFile, 1));

    // Clean-up the environment
    std::filesystem::remove_all(logsDir);
}

TEST_CASE("Test if log files rotate")
{
    auto app                            = Log::Application{"TestApp", "rev", "tag", "branch"};
    constexpr auto MaxFileSize          = 10; // 10 bytes
    constexpr auto TestLog              = "12345678";
    const std::filesystem::path logsDir = "./ut_logs";
    const auto testLogFile              = logsDir / "TestApp.log";

    // Prepare the environment.
    if (std::filesystem::exists(logsDir)) {
        std::filesystem::remove_all(logsDir);
    }
    std::filesystem::create_directory(logsDir);

    // Initialize the logger with test parameters.
    Log::Logger::get().init(std::move(app), MaxFileSize);
    REQUIRE(countFiles(logsDir) == 0);

    // Dump logs.
    // Dumping logs to a file causes a log rotation.
    LOG_ERROR(TestLog);
    Log::Logger::get().dumpToFile(testLogFile);
    REQUIRE(countFiles(logsDir) == 1);
    REQUIRE(checkIfLogFilesExist(testLogFile, 1));

    LOG_ERROR(TestLog);
    Log::Logger::get().dumpToFile(testLogFile);
    REQUIRE(countFiles(logsDir) == 2);
    REQUIRE(checkIfLogFilesExist(testLogFile, 2));

    LOG_ERROR(TestLog);
    Log::Logger::get().dumpToFile(testLogFile);
    REQUIRE(countFiles(logsDir) == 3);
    REQUIRE(checkIfLogFilesExist(testLogFile, 3));

    LOG_ERROR(TestLog);
    Log::Logger::get().dumpToFile(testLogFile);
    REQUIRE(countFiles(logsDir) == 3);
    REQUIRE(checkIfLogFilesExist(testLogFile, 3));

    LOG_ERROR(TestLog);
    Log::Logger::get().dumpToFile(testLogFile);
    REQUIRE(countFiles(logsDir) == 3);
    REQUIRE(checkIfLogFilesExist(testLogFile, 3));

    // Clean-up the environment
    std::filesystem::remove_all(logsDir);
}