-
Notifications
You must be signed in to change notification settings - Fork 240
/
LogCleanerTest.cs
141 lines (121 loc) · 4.78 KB
/
LogCleanerTest.cs
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
/*
* Copyright (c) 2020 Proton Technologies AG
*
* This file is part of ProtonVPN.
*
* ProtonVPN is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ProtonVPN is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* 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;
namespace ProtonVPN.Common.Test.Logging
{
[TestClass]
public class LogCleanerTest
{
private ILogger _logger;
[TestInitialize]
public void TestInitialize()
{
_logger = Substitute.For<ILogger>();
}
[TestMethod]
public void Clean_ShouldNotThrow_WhenFolderNotExists()
{
// Arrange
const string logPath = "Folder\\Does\\Not\\Exist";
LogCleaner cleaner = new(_logger);
// Act
Action action = () => cleaner.Clean(logPath, 0);
//Assert
action.Should().NotThrow<Exception>();
}
[TestMethod]
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_ShouldDeleteOldFiles);
CreateEmptyDirectory(logPath);
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.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);
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" });
}
[TestMethod]
public void Clean_ShouldSkipLockedFile()
{
// Arrange
const string logPath = nameof(Clean_ShouldSkipLockedFile);
CreateEmptyDirectory(logPath);
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));
LogCleaner cleaner = new(_logger);
// Act
using (File.OpenRead(files[1]))
{
cleaner.Clean(logPath, 0);
}
IEnumerable<string> result = Directory.GetFiles(logPath).Select(Path.GetFileName);
// Assert
result.Should().HaveCount(1)
.And.Contain(new[] { "file1.log" });
}
#region Helpers
private static void CreateEmptyDirectory(string path)
{
if (Directory.Exists(path))
{
Directory.Delete(path, true);
}
Directory.CreateDirectory(path);
}
private static void CreateEmptyFile(string filename)
{
File.Create(filename).Dispose();
}
#endregion
}
}