Skip to content

Commit

Permalink
Complete tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
herve-er committed May 6, 2024
1 parent ad694c9 commit fa7eb38
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/libcommonserver/log/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ ExitCode Log::copyLogsTo(const SyncPath &outputPath, bool includeOldLogs, ExitCa
continue;
}

if (!includeOldLogs && entry.path().filename().string().find(".gz") != std::string::npos) {
if (!includeOldLogs && entry.path().filename().extension() == L".gz") {
LOG_WARN(Log::instance()->getLogger(), "Ignoring old log file " << entry.path().filename().string().c_str());
continue;
}
Expand Down Expand Up @@ -387,8 +387,9 @@ ExitCode Log::compressLogFiles(const SyncPath &directoryToCompress, ExitCause &e
DirectoryEntry entry;

if (progressMonitoring) {
progressCallback(0);
bool endOfDirectory = false;
while (dir.next(entry, endOfDirectory, ioError)) {
while (dir.next(entry, endOfDirectory, ioError) && !endOfDirectory) {
nbFiles++;
}
if (!IoHelper::getDirectoryIterator(directoryToCompress, true, ioError, dir)) {
Expand Down
66 changes: 64 additions & 2 deletions test/libcommonserver/log/testlog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void TestLog::testGetLogEstimatedSize(void) {
}

void TestLog::testCopyLogsTo(void) {
{
{ // Test with old logs
TemporaryDirectory tempDir;
LOG_DEBUG(_logger, "Ensure that the log file is created (test)");

Expand All @@ -79,7 +79,7 @@ void TestLog::testCopyLogsTo(void) {
CPPUNIT_ASSERT(logDirsize >= 0);

ExitCause cause = ExitCauseUnknown;
ExitCode exitCode = Log::instance()->copyLogsTo(tempDir.path, false, cause);
ExitCode exitCode = Log::instance()->copyLogsTo(tempDir.path, true, cause);
CPPUNIT_ASSERT_EQUAL(ExitCauseUnknown, cause);
CPPUNIT_ASSERT_EQUAL(ExitCodeOk, exitCode);

Expand All @@ -88,6 +88,41 @@ void TestLog::testCopyLogsTo(void) {
CPPUNIT_ASSERT_EQUAL(IoErrorSuccess, err);
CPPUNIT_ASSERT_EQUAL(logDirsize, tempDirSize);
}

{ // Test without old logs
TemporaryDirectory tempDir;
SyncPath logDir = Log::instance()->getLogFilePath().parent_path();

// create a fake log file
std::ofstream logFile(tempDir.path / "test.log");
for (int i = 0; i < 10; i++) {
logFile << "Test log line " << i << std::endl;
}
logFile.close();

// compress the log file
ExitCause cause = ExitCauseUnknown;
ExitCode exitCode = Log::instance()->compressLogFiles(tempDir.path, cause);
CPPUNIT_ASSERT_EQUAL(ExitCauseUnknown, cause);
CPPUNIT_ASSERT_EQUAL(ExitCodeOk, exitCode);

// copy the compressed log file to the log directory
IoError err = IoErrorSuccess;
CPPUNIT_ASSERT_EQUAL(true, IoHelper::copyFileOrDirectory(tempDir.path / "test.log.gz", logDir / "test.log.gz", err));
CPPUNIT_ASSERT_EQUAL(IoErrorSuccess, err);

CPPUNIT_ASSERT_EQUAL(true, IoHelper::deleteDirectory(tempDir.path / "test.log.gz", err));

exitCode = Log::instance()->copyLogsTo(tempDir.path, false, cause);
CPPUNIT_ASSERT_EQUAL(ExitCauseUnknown, cause);
CPPUNIT_ASSERT_EQUAL(ExitCodeOk, exitCode);

// Check we do not have test.log.gz in the temp directory
bool exists = false;
CPPUNIT_ASSERT_EQUAL(true, IoHelper::checkIfPathExists(tempDir.path / "test.log.gz", exists, err));
CPPUNIT_ASSERT_EQUAL(IoErrorNoSuchFileOrDirectory, err);
CPPUNIT_ASSERT_EQUAL(false, exists);
}
}

void TestLog::testCopyParmsDbTo(void) {
Expand Down Expand Up @@ -158,6 +193,33 @@ void TestLog::testCompressLogs(void) {
CPPUNIT_ASSERT_EQUAL(IoErrorSuccess, err);
CPPUNIT_ASSERT_EQUAL(true, exists);
}

{ // test the progress bar
TemporaryDirectory tempDir;
for (int i = 0; i < 30; i++) {
std::ofstream logFile(tempDir.path / ("test" + std::to_string(i) + ".log"));
for (int j = 0; j < 10; j++) {
logFile << "Test log line " << j << std::endl;
}
logFile.close();
}

int percent = 0;
int oldPercent = 0;
std::function<void(int)> progress = [&percent, &oldPercent](int p) {
percent = p;
CPPUNIT_ASSERT(percent >= oldPercent);
oldPercent = percent;
};

ExitCause cause = ExitCauseUnknown;

ExitCode exitCode = Log::instance()->compressLogFiles(tempDir.path, cause, progress);

CPPUNIT_ASSERT_EQUAL(ExitCauseUnknown, cause);
CPPUNIT_ASSERT_EQUAL(ExitCodeOk, exitCode);
CPPUNIT_ASSERT_EQUAL(100, percent);
}
}

void TestLog::testGenerateUserDescriptionFile(void) {
Expand Down

0 comments on commit fa7eb38

Please sign in to comment.