Skip to content

Commit

Permalink
Fix Log Cleaner tests [VPNWIN-903]
Browse files Browse the repository at this point in the history
  • Loading branch information
eaproton committed Oct 6, 2021
1 parent ace3186 commit 064c78f
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 20 deletions.
2 changes: 2 additions & 0 deletions ProtonVPN.RestoreInternet/ProtonVPN.RestoreInternet.csproj
Expand Up @@ -22,6 +22,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand All @@ -31,6 +32,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup>
<ApplicationManifest>app.manifest</ApplicationManifest>
Expand Down
4 changes: 3 additions & 1 deletion src/ProtonVPN.Common/Logging/LogCleaner.cs
Expand Up @@ -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)
Expand Down Expand Up @@ -107,7 +109,7 @@ private string GetFileNames(IList<FileInfo> files)

private IList<FileInfo> GetOldFiles(IList<FileInfo> files)
{
DateTime minimumDate = DateTime.UtcNow.AddMonths(-1);
DateTime minimumDate = DateTime.UtcNow.Subtract(MAXIMUM_FILE_AGE);
IList<FileInfo> 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}");
Expand Down
2 changes: 2 additions & 0 deletions src/ProtonVPN.Crypto/ProtonVPN.Crypto.csproj
Expand Up @@ -21,6 +21,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -29,6 +30,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<Reference Include="BouncyCastle.Crypto, Version=1.8.9.0, Culture=neutral, PublicKeyToken=0e99375e54769942">
Expand Down
Expand Up @@ -22,6 +22,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand All @@ -31,6 +32,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<Reference Include="PresentationFramework" />
Expand Down
2 changes: 2 additions & 0 deletions src/ProtonVPN.Native/ProtonVPN.Native.csproj
Expand Up @@ -21,6 +21,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -29,6 +30,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
Expand Down
2 changes: 2 additions & 0 deletions src/ProtonVPN.NetworkFilter/ProtonVPN.NetworkFilter.csproj
Expand Up @@ -19,6 +19,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -28,6 +29,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup>
<RootNamespace>ProtonVPN.NetworkFilter</RootNamespace>
Expand Down
2 changes: 2 additions & 0 deletions src/ProtonVPN.TlsVerify/ProtonVPN.TlsVerify.csproj
Expand Up @@ -23,6 +23,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand All @@ -33,6 +34,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
Expand Down
2 changes: 2 additions & 0 deletions src/ProtonVPN.Translations/ProtonVPN.Translations.csproj
Expand Up @@ -21,6 +21,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -29,6 +30,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<Reference Include="PluralNet, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
Expand Down
2 changes: 2 additions & 0 deletions src/ProtonVPN.UpdateService/ProtonVPN.UpdateService.csproj
Expand Up @@ -23,6 +23,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand All @@ -33,6 +34,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup>
<ApplicationIcon>protonvpn.ico</ApplicationIcon>
Expand Down
Expand Up @@ -21,6 +21,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -29,6 +30,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
Expand Down
Expand Up @@ -23,6 +23,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand All @@ -33,6 +34,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup>
<ApplicationIcon>protonvpn.ico</ApplicationIcon>
Expand Down
60 changes: 41 additions & 19 deletions test/ProtonVPN.Common.Test/Logging/LogCleanerTest.cs
Expand Up @@ -17,13 +17,15 @@
* along with ProtonVPN. If not, see <https://www.gnu.org/licenses/>.
*/

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
{
Expand All @@ -43,35 +45,53 @@ 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
action.Should().NotThrow<Exception>();
}

[TestMethod]
public void Clean_ShouldDeleteOldestFiles()
public void Clean_ShouldDeleteOldFiles()
{
DateTime lastWriteDate = DateTime.UtcNow.Subtract(LogCleaner.MAXIMUM_FILE_AGE).AddDays(-2);

IEnumerable<string> result = ArrangeAndActByLastWriteDate(lastWriteDate);

// Assert
result.Should().HaveCount(0);
}

private IEnumerable<string> 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<string> 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<string> result = ArrangeAndActByLastWriteDate(lastWriteDate);

// Assert
result.Should().HaveCount(2)
.And.Contain(new[] {"file.log", "file1.log"});
.And.Contain(new[] { "file.log", "file1.log" });
}

[TestMethod]
Expand All @@ -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<string> 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<string> result = Directory.GetFiles(logPath).Select(Path.GetFileName);

// Assert
result.Should().HaveCount(1)
Expand All @@ -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);
}
Expand Down
2 changes: 2 additions & 0 deletions test/ProtonVPN.Common.Test/ProtonVPN.Common.Test.csproj
Expand Up @@ -30,6 +30,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -38,6 +39,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<Reference Include="Castle.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
Expand Down

0 comments on commit 064c78f

Please sign in to comment.