From 064c78f8e28b441bd84afdd3412fbe4b3b75257b Mon Sep 17 00:00:00 2001 From: Eduardo Abreu Date: Wed, 6 Oct 2021 11:59:58 +0000 Subject: [PATCH] Fix Log Cleaner tests [VPNWIN-903] --- .../ProtonVPN.RestoreInternet.csproj | 2 + src/ProtonVPN.Common/Logging/LogCleaner.cs | 4 +- src/ProtonVPN.Crypto/ProtonVPN.Crypto.csproj | 2 + .../ProtonVPN.MarkupValidator.csproj | 2 + src/ProtonVPN.Native/ProtonVPN.Native.csproj | 2 + .../ProtonVPN.NetworkFilter.csproj | 2 + .../ProtonVPN.TlsVerify.csproj | 2 + .../ProtonVPN.Translations.csproj | 2 + .../ProtonVPN.UpdateService.csproj | 2 + .../ProtonVPN.UpdateServiceContract.csproj | 2 + .../ProtonVPN.WireguardService.csproj | 2 + .../Logging/LogCleanerTest.cs | 60 +++++++++++++------ .../ProtonVPN.Common.Test.csproj | 2 + 13 files changed, 66 insertions(+), 20 deletions(-) diff --git a/ProtonVPN.RestoreInternet/ProtonVPN.RestoreInternet.csproj b/ProtonVPN.RestoreInternet/ProtonVPN.RestoreInternet.csproj index bdb46d69d..c09a713a8 100644 --- a/ProtonVPN.RestoreInternet/ProtonVPN.RestoreInternet.csproj +++ b/ProtonVPN.RestoreInternet/ProtonVPN.RestoreInternet.csproj @@ -22,6 +22,7 @@ DEBUG;TRACE prompt 4 + latest AnyCPU @@ -31,6 +32,7 @@ TRACE prompt 4 + latest app.manifest diff --git a/src/ProtonVPN.Common/Logging/LogCleaner.cs b/src/ProtonVPN.Common/Logging/LogCleaner.cs index 79e2fc9b6..e05fc7669 100644 --- a/src/ProtonVPN.Common/Logging/LogCleaner.cs +++ b/src/ProtonVPN.Common/Logging/LogCleaner.cs @@ -27,6 +27,8 @@ namespace ProtonVPN.Common.Logging { public class LogCleaner { + public static readonly TimeSpan MAXIMUM_FILE_AGE = TimeSpan.FromDays(28); + private readonly ILogger _logger; public LogCleaner(ILogger logger) @@ -107,7 +109,7 @@ private string GetFileNames(IList files) private IList GetOldFiles(IList files) { - DateTime minimumDate = DateTime.UtcNow.AddMonths(-1); + DateTime minimumDate = DateTime.UtcNow.Subtract(MAXIMUM_FILE_AGE); IList oldFiles = files.Where(t => LastWriteTime(t) < minimumDate).ToList(); string oldFileNames = GetFileNames(oldFiles); _logger.Debug($"[LogCleaner] The folder has {oldFiles.Count} old files with a last write date before {minimumDate:O}.{oldFileNames}"); diff --git a/src/ProtonVPN.Crypto/ProtonVPN.Crypto.csproj b/src/ProtonVPN.Crypto/ProtonVPN.Crypto.csproj index 1ca518edd..e95d8459d 100644 --- a/src/ProtonVPN.Crypto/ProtonVPN.Crypto.csproj +++ b/src/ProtonVPN.Crypto/ProtonVPN.Crypto.csproj @@ -21,6 +21,7 @@ DEBUG;TRACE prompt 4 + latest pdbonly @@ -29,6 +30,7 @@ TRACE prompt 4 + latest diff --git a/src/ProtonVPN.MarkupValidator/ProtonVPN.MarkupValidator.csproj b/src/ProtonVPN.MarkupValidator/ProtonVPN.MarkupValidator.csproj index 27215f4ea..34b8ac9d0 100644 --- a/src/ProtonVPN.MarkupValidator/ProtonVPN.MarkupValidator.csproj +++ b/src/ProtonVPN.MarkupValidator/ProtonVPN.MarkupValidator.csproj @@ -22,6 +22,7 @@ DEBUG;TRACE prompt 4 + latest AnyCPU @@ -31,6 +32,7 @@ TRACE prompt 4 + latest diff --git a/src/ProtonVPN.Native/ProtonVPN.Native.csproj b/src/ProtonVPN.Native/ProtonVPN.Native.csproj index 9b864b59d..6d67d8def 100644 --- a/src/ProtonVPN.Native/ProtonVPN.Native.csproj +++ b/src/ProtonVPN.Native/ProtonVPN.Native.csproj @@ -21,6 +21,7 @@ DEBUG;TRACE prompt 4 + latest pdbonly @@ -29,6 +30,7 @@ TRACE prompt 4 + latest diff --git a/src/ProtonVPN.NetworkFilter/ProtonVPN.NetworkFilter.csproj b/src/ProtonVPN.NetworkFilter/ProtonVPN.NetworkFilter.csproj index ed44cb601..0b2ffb703 100644 --- a/src/ProtonVPN.NetworkFilter/ProtonVPN.NetworkFilter.csproj +++ b/src/ProtonVPN.NetworkFilter/ProtonVPN.NetworkFilter.csproj @@ -19,6 +19,7 @@ prompt 4 false + latest pdbonly @@ -28,6 +29,7 @@ prompt 4 false + latest ProtonVPN.NetworkFilter diff --git a/src/ProtonVPN.TlsVerify/ProtonVPN.TlsVerify.csproj b/src/ProtonVPN.TlsVerify/ProtonVPN.TlsVerify.csproj index 6eb758ea2..2e02b0cc6 100644 --- a/src/ProtonVPN.TlsVerify/ProtonVPN.TlsVerify.csproj +++ b/src/ProtonVPN.TlsVerify/ProtonVPN.TlsVerify.csproj @@ -23,6 +23,7 @@ prompt 4 false + latest AnyCPU @@ -33,6 +34,7 @@ prompt 4 false + latest diff --git a/src/ProtonVPN.Translations/ProtonVPN.Translations.csproj b/src/ProtonVPN.Translations/ProtonVPN.Translations.csproj index 822406c9f..cedf06087 100644 --- a/src/ProtonVPN.Translations/ProtonVPN.Translations.csproj +++ b/src/ProtonVPN.Translations/ProtonVPN.Translations.csproj @@ -21,6 +21,7 @@ DEBUG;TRACE prompt 4 + latest pdbonly @@ -29,6 +30,7 @@ TRACE prompt 4 + latest diff --git a/src/ProtonVPN.UpdateService/ProtonVPN.UpdateService.csproj b/src/ProtonVPN.UpdateService/ProtonVPN.UpdateService.csproj index 8b5a11772..737bfde19 100644 --- a/src/ProtonVPN.UpdateService/ProtonVPN.UpdateService.csproj +++ b/src/ProtonVPN.UpdateService/ProtonVPN.UpdateService.csproj @@ -23,6 +23,7 @@ prompt 4 false + latest AnyCPU @@ -33,6 +34,7 @@ prompt 4 false + latest protonvpn.ico diff --git a/src/ProtonVPN.UpdateServiceContract/ProtonVPN.UpdateServiceContract.csproj b/src/ProtonVPN.UpdateServiceContract/ProtonVPN.UpdateServiceContract.csproj index 32ac6c403..f34ad2898 100644 --- a/src/ProtonVPN.UpdateServiceContract/ProtonVPN.UpdateServiceContract.csproj +++ b/src/ProtonVPN.UpdateServiceContract/ProtonVPN.UpdateServiceContract.csproj @@ -21,6 +21,7 @@ DEBUG;TRACE prompt 4 + latest pdbonly @@ -29,6 +30,7 @@ TRACE prompt 4 + latest diff --git a/src/ProtonVPN.WireguardService/ProtonVPN.WireguardService.csproj b/src/ProtonVPN.WireguardService/ProtonVPN.WireguardService.csproj index aaa039576..e5b3330ed 100644 --- a/src/ProtonVPN.WireguardService/ProtonVPN.WireguardService.csproj +++ b/src/ProtonVPN.WireguardService/ProtonVPN.WireguardService.csproj @@ -23,6 +23,7 @@ prompt 4 false + latest AnyCPU @@ -33,6 +34,7 @@ prompt 4 false + latest protonvpn.ico diff --git a/test/ProtonVPN.Common.Test/Logging/LogCleanerTest.cs b/test/ProtonVPN.Common.Test/Logging/LogCleanerTest.cs index 4cdf4376b..4b903209f 100644 --- a/test/ProtonVPN.Common.Test/Logging/LogCleanerTest.cs +++ b/test/ProtonVPN.Common.Test/Logging/LogCleanerTest.cs @@ -17,13 +17,15 @@ * along with ProtonVPN. If not, see . */ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; using FluentAssertions; using Microsoft.VisualStudio.TestTools.UnitTesting; using NSubstitute; +using ProtonVPN.Common.Extensions; using ProtonVPN.Common.Logging; -using System; -using System.IO; -using System.Linq; namespace ProtonVPN.Common.Test.Logging { @@ -43,7 +45,7 @@ public void Clean_ShouldNotThrow_WhenFolderNotExists() { // Arrange const string logPath = "Folder\\Does\\Not\\Exist"; - var cleaner = new LogCleaner(_logger); + LogCleaner cleaner = new(_logger); // Act Action action = () => cleaner.Clean(logPath, 0); //Assert @@ -51,27 +53,45 @@ public void Clean_ShouldNotThrow_WhenFolderNotExists() } [TestMethod] - public void Clean_ShouldDeleteOldestFiles() + public void Clean_ShouldDeleteOldFiles() + { + DateTime lastWriteDate = DateTime.UtcNow.Subtract(LogCleaner.MAXIMUM_FILE_AGE).AddDays(-2); + + IEnumerable result = ArrangeAndActByLastWriteDate(lastWriteDate); + + // Assert + result.Should().HaveCount(0); + } + + private IEnumerable ArrangeAndActByLastWriteDate(DateTime lastWriteDate) { // Arrange - const string logPath = nameof(Clean_ShouldDeleteOldestFiles); + const string logPath = nameof(Clean_ShouldDeleteOldFiles); CreateEmptyDirectory(logPath); - var files = new[] {"file.log", "file1.log", "file2.log", "file3.log", "file4.log"}.Select(f => Path.Combine(logPath, f)).ToList(); + IList files = new[] { "file.log", "file1.log", "file2.log", "file3.log", "file4.log" }.Select(f => Path.Combine(logPath, f)).ToList(); files.ForEach(CreateEmptyFile); - File.SetCreationTimeUtc(files[0], new DateTime(2019, 03, 15, 10, 50, 0)); - File.SetCreationTimeUtc(files[1], new DateTime(2019, 03, 15, 10, 40, 0)); - File.SetCreationTimeUtc(files[2], new DateTime(2019, 03, 15, 10, 30, 0)); - File.SetCreationTimeUtc(files[3], new DateTime(2019, 03, 15, 10, 20, 0)); - File.SetCreationTimeUtc(files[4], new DateTime(2019, 03, 15, 10, 10, 0)); - var cleaner = new LogCleaner(_logger); + File.SetLastWriteTimeUtc(files[0], lastWriteDate.AddMinutes(50)); + File.SetLastWriteTimeUtc(files[1], lastWriteDate.AddMinutes(40)); + File.SetLastWriteTimeUtc(files[2], lastWriteDate.AddMinutes(30)); + File.SetLastWriteTimeUtc(files[3], lastWriteDate.AddMinutes(20)); + File.SetLastWriteTimeUtc(files[4], lastWriteDate.AddMinutes(10)); + LogCleaner cleaner = new(_logger); // Act cleaner.Clean(logPath, 2); - var result = Directory.GetFiles(logPath).Select(Path.GetFileName); - + return Directory.GetFiles(logPath).Select(Path.GetFileName); + } + + [TestMethod] + public void Clean_ShouldDeleteOldestFiles() + { + DateTime lastWriteDate = DateTime.UtcNow.Subtract(LogCleaner.MAXIMUM_FILE_AGE).AddDays(2); + + IEnumerable result = ArrangeAndActByLastWriteDate(lastWriteDate); + // Assert result.Should().HaveCount(2) - .And.Contain(new[] {"file.log", "file1.log"}); + .And.Contain(new[] { "file.log", "file1.log" }); } [TestMethod] @@ -80,19 +100,19 @@ public void Clean_ShouldSkipLockedFile() // Arrange const string logPath = nameof(Clean_ShouldSkipLockedFile); CreateEmptyDirectory(logPath); - var files = new[] { "file2.log", "file1.log", "file.log" }.Select(f => Path.Combine(logPath, f)).ToList(); + IList files = new[] { "file2.log", "file1.log", "file.log" }.Select(f => Path.Combine(logPath, f)).ToList(); files.ForEach(CreateEmptyFile); File.SetCreationTimeUtc(files[0], new DateTime(2000, 01, 01, 0, 10, 0)); File.SetCreationTimeUtc(files[1], new DateTime(2000, 01, 01, 0, 20, 0)); File.SetCreationTimeUtc(files[2], new DateTime(2000, 01, 01, 0, 30, 0)); - var cleaner = new LogCleaner(_logger); + LogCleaner cleaner = new(_logger); // Act using (File.OpenRead(files[1])) { cleaner.Clean(logPath, 0); } - var result = Directory.GetFiles(logPath).Select(Path.GetFileName); + IEnumerable result = Directory.GetFiles(logPath).Select(Path.GetFileName); // Assert result.Should().HaveCount(1) @@ -104,7 +124,9 @@ public void Clean_ShouldSkipLockedFile() private static void CreateEmptyDirectory(string path) { if (Directory.Exists(path)) + { Directory.Delete(path, true); + } Directory.CreateDirectory(path); } diff --git a/test/ProtonVPN.Common.Test/ProtonVPN.Common.Test.csproj b/test/ProtonVPN.Common.Test/ProtonVPN.Common.Test.csproj index 6ed93a48d..8be3fbc35 100644 --- a/test/ProtonVPN.Common.Test/ProtonVPN.Common.Test.csproj +++ b/test/ProtonVPN.Common.Test/ProtonVPN.Common.Test.csproj @@ -30,6 +30,7 @@ DEBUG;TRACE prompt 4 + latest pdbonly @@ -38,6 +39,7 @@ TRACE prompt 4 + latest