From fa7eb3876206571041b78301cd143994d1a04f08 Mon Sep 17 00:00:00 2001 From: Herve_eruam Date: Mon, 6 May 2024 11:27:39 +0200 Subject: [PATCH] Complete tests. --- src/libcommonserver/log/log.cpp | 5 ++- test/libcommonserver/log/testlog.cpp | 66 +++++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 4 deletions(-) diff --git a/src/libcommonserver/log/log.cpp b/src/libcommonserver/log/log.cpp index e341d3699..46e40fc3a 100644 --- a/src/libcommonserver/log/log.cpp +++ b/src/libcommonserver/log/log.cpp @@ -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; } @@ -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)) { diff --git a/test/libcommonserver/log/testlog.cpp b/test/libcommonserver/log/testlog.cpp index 28d1363e0..ab982a02a 100644 --- a/test/libcommonserver/log/testlog.cpp +++ b/test/libcommonserver/log/testlog.cpp @@ -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)"); @@ -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); @@ -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) { @@ -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 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) {