~aleteoryx/muditaos

ref: sign_test muditaos/module-utils/rotator/tests/test_Rotator.cpp -rw-r--r-- 6.9 KiB
a217eeb3 — Dawid Wojtas [BH-2024] Fix lack of alarm directory after updating software 1 year, 5 months 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <catch2/catch.hpp>

#include <rotator/Rotator.hpp>

#include <fstream>
#include <iostream>
#include <filesystem>

namespace
{
    constexpr std::size_t maxRotationFilesCount = 5;

    class HexRotator : public utils::Rotator<maxRotationFilesCount>
    {
      public:
        HexRotator() : utils::Rotator<maxRotationFilesCount>{".hex"}
        {}

        std::filesystem::path rotatedFilePath(const std::filesystem::path &source, int rotationCount)
        {
            return this->getRotatedFilePath(source, rotationCount);
        }

        std::filesystem::path incrementFileIndex(const std::filesystem::path &source)
        {
            return this->incrementFileNumber(source);
        }
    };
} // namespace

TEST_CASE("Rotation of hex dump files")
{
    HexRotator rotator;
    constexpr auto fileName = "crashdump.hex";
    SECTION("Handle first dump")
    {
        REQUIRE(rotator.rotateFile(fileName));
    }
    SECTION("Handle first rotate")
    {
        constexpr auto firstRotationName = "crashdump.hex.1";
        std::ofstream file(fileName);
        REQUIRE(rotator.rotateFile(fileName));
        REQUIRE(std::filesystem::exists(firstRotationName));
        REQUIRE_FALSE(std::filesystem::exists(fileName));
        REQUIRE(std::filesystem::remove(firstRotationName));
    }
    SECTION("Handle max rotates")
    {
        for (std::size_t i = 1; i <= maxRotationFilesCount; i++) {
            const auto rotatedFileName = rotator.rotatedFilePath(fileName, i);
            std::ofstream file(fileName);
            REQUIRE(rotator.rotateFile(fileName));
            for (std::size_t j = i - 1; j > 0; j--) {
                REQUIRE(std::filesystem::exists(rotator.rotatedFilePath(fileName, j)));
            }
            if (i == maxRotationFilesCount) {
                REQUIRE_FALSE(std::filesystem::exists(rotator.rotatedFilePath(fileName, i)));
            }
            REQUIRE_FALSE(std::filesystem::exists(fileName));
        }
        for (std::size_t i = 1; i < maxRotationFilesCount; i++) {
            const auto rotatedFileName = rotator.rotatedFilePath(fileName, i);
            REQUIRE(std::filesystem::remove(rotatedFileName));
        }
    }

    SECTION("Handle more than max files")
    {
        for (std::size_t i = 1; i <= maxRotationFilesCount; i++) {
            const auto rotatedFileName = rotator.rotatedFilePath(fileName, i);
            std::ofstream file(fileName);
            REQUIRE(rotator.rotateFile(fileName));
            for (std::size_t j = i - 1; j > 0; j--) {
                REQUIRE(std::filesystem::exists(rotator.rotatedFilePath(fileName, j)));
            }
            if (i == maxRotationFilesCount) {
                REQUIRE_FALSE(std::filesystem::exists(rotator.rotatedFilePath(fileName, i)));
            }
            REQUIRE_FALSE(std::filesystem::exists(fileName));
        }
        std::ofstream file(fileName);
        REQUIRE(rotator.rotateFile(fileName));
        REQUIRE_FALSE(std::filesystem::exists(rotator.rotatedFilePath(fileName, maxRotationFilesCount + 1)));
        for (std::size_t i = 1; i < maxRotationFilesCount; i++) {
            const auto rotatedFileName = rotator.rotatedFilePath(fileName, i);
            REQUIRE(std::filesystem::exists(rotatedFileName));
        }
        REQUIRE_FALSE(std::filesystem::exists(rotator.rotatedFilePath(fileName, maxRotationFilesCount)));
        REQUIRE_FALSE(std::filesystem::exists(fileName));
        for (std::size_t i = 1; i < maxRotationFilesCount; i++) {
            const auto rotatedFileName = rotator.rotatedFilePath(fileName, i);
            REQUIRE(std::filesystem::remove(rotatedFileName));
        }
    }
}

TEST_CASE("Rotate files with serial_number and timestamp")
{
    HexRotator rotator;
    std::string path("./rotator_tests");

    SECTION("Increment filename index")
    {
        std::string filename          = "123_12345_crashdump.hex.1";
        std::string expected_filename = "123_12345_crashdump.hex.2";
        auto result                   = rotator.incrementFileIndex(std::filesystem::path(filename));
        REQUIRE(result == expected_filename);
    }

    SECTION("Rotate files")
    {

        std::vector<std::string> test_filenames = {
            "/123_12345_crashdump.hex.1", "/123_12346_crashdump.hex.2", "/123_12347_crashdump.hex.3"};
        std::vector<std::string> expected_test_filenames = {
            "/123_12345_crashdump.hex.2", "/123_12346_crashdump.hex.3", "/123_12347_crashdump.hex.4"};

        std::filesystem::create_directory(path);

        for (const auto &tf : test_filenames) {
            std::ofstream(std::string(path + tf).c_str());
        }
        REQUIRE(rotator.rotateFiles(std::filesystem::path(path)));
        for (const auto &etf : expected_test_filenames) {
            REQUIRE(std::filesystem::exists(std::string(path + etf)));
        }

        std::filesystem::remove_all(path);
    }

    SECTION("Rotate files - max number reach")
    {

        std::vector<std::string> test_filenames = {"/123_12345_crashdump.hex.1",
                                                   "/123_12346_crashdump.hex.2",
                                                   "/123_12347_crashdump.hex.3",
                                                   "/123_12349_crashdump.hex.4",
                                                   "/123_12351_crashdump.hex.5"};

        std::vector<std::string> expected_test_filenames = {"/123_12345_crashdump.hex.2",
                                                            "/123_12346_crashdump.hex.3",
                                                            "/123_12347_crashdump.hex.4",
                                                            "/123_12349_crashdump.hex.5"};

        std::string file_expected_to_not_exist = "/123_12351_crashdump.hex.6";

        std::filesystem::create_directory(path);

        for (const auto &tf : test_filenames) {
            std::ofstream(std::string(path + tf).c_str());
        }
        REQUIRE(rotator.rotateFiles(std::filesystem::path(path)));
        for (const auto &tf : test_filenames) {
            REQUIRE_FALSE(std::filesystem::exists(std::string(path + tf)));
        }
        for (const auto &etf : expected_test_filenames) {
            REQUIRE(std::filesystem::exists(std::string(path + etf)));
        }
        REQUIRE_FALSE(std::filesystem::exists(std::string(path + file_expected_to_not_exist)));

        std::filesystem::remove_all(path);
    }

    SECTION("Increment filename - no number at the end")
    {
        std::string filename          = "123_12345_crashdump.hex";
        std::string expected_filename = "123_12345_crashdump.hex.0";
        auto result                   = rotator.incrementFileIndex(std::filesystem::path(filename));
        REQUIRE(result == expected_filename);
    }

    // in case of fail in earlier tests
    std::filesystem::remove_all(path);
}