2
2
// SPDX-License-Identifier: Apache License 2.0
3
3
4
4
using System . Collections . Generic ;
5
+ using System . IO ;
5
6
using System . IO . Abstractions ;
6
7
using System . IO . Abstractions . TestingHelpers ;
7
8
using System . Linq ;
16
17
using Moq ;
17
18
using xRetry ;
18
19
using Xunit ;
20
+ using Xunit . Abstractions ;
19
21
20
22
namespace Monai . Deploy . InformaticsGateway . Test . Services . Storage
21
23
{
@@ -27,16 +29,20 @@ public class SpaceReclaimerServiceTest
27
29
private readonly IFileSystem _fileSystem ;
28
30
private readonly IOptions < InformaticsGatewayConfiguration > _configuration ;
29
31
private readonly SpaceReclaimerService _service ;
32
+ private readonly ITestOutputHelper _output ;
33
+ private readonly string _tempDirRoot = "/payloads" ;
30
34
31
- public SpaceReclaimerServiceTest ( )
35
+ public SpaceReclaimerServiceTest ( ITestOutputHelper output )
32
36
{
37
+ _output = output ?? throw new System . ArgumentNullException ( nameof ( output ) ) ;
38
+
33
39
_cancellationTokenSource = new CancellationTokenSource ( ) ;
34
40
_logger = new Mock < ILogger < SpaceReclaimerService > > ( ) ;
35
41
_queue = new Mock < IInstanceCleanupQueue > ( ) ;
36
42
_fileSystem = new MockFileSystem ( ) ;
37
43
38
44
_configuration = Options . Create ( new InformaticsGatewayConfiguration ( ) ) ;
39
- _configuration . Value . Storage . Temporary = "/payloads" ;
45
+ _configuration . Value . Storage . Temporary = Path . GetFullPath ( _tempDirRoot ) ;
40
46
_service = new SpaceReclaimerService ( _queue . Object , _logger . Object , _configuration , _fileSystem ) ;
41
47
_logger . Setup ( p => p . IsEnabled ( It . IsAny < LogLevel > ( ) ) ) . Returns ( true ) ;
42
48
}
@@ -60,19 +66,20 @@ public async Task ShallHonorCancellationRequest()
60
66
_logger . VerifyLogging ( "Space Reclaimer Service is stopping." , LogLevel . Information , Times . Once ( ) ) ;
61
67
}
62
68
63
- [ RetryFact ( 5 , 250 , DisplayName = "Shall delete files" ) ]
69
+ [ RetryFact ( 10 , 250 , DisplayName = "Shall delete files" ) ]
64
70
public async Task ShallDeleteFiles ( )
65
71
{
66
72
var files = new List < FileStorageInfo > ( ) {
67
- new FileStorageInfo { FilePath = "/payloads/ dir1/file1"} ,
68
- new FileStorageInfo { FilePath = "/payloads/dir1/ file2"} ,
69
- new FileStorageInfo { FilePath = "/payloads/dir1/ file3.exe"} ,
73
+ new FileStorageInfo { FilePath = Path . GetFullPath ( Path . Combine ( _tempDirRoot , " dir1/file1") ) } ,
74
+ new FileStorageInfo { FilePath = Path . GetFullPath ( Path . Combine ( _tempDirRoot , " file2") ) } ,
75
+ new FileStorageInfo { FilePath = Path . GetFullPath ( Path . Combine ( _tempDirRoot , " file3.exe") ) } ,
70
76
} ;
71
77
72
78
foreach ( var file in files )
73
79
{
74
80
_fileSystem . Directory . CreateDirectory ( _fileSystem . Path . GetDirectoryName ( file . FilePath ) ) ;
75
81
_fileSystem . File . Create ( file . FilePath ) ;
82
+
76
83
}
77
84
78
85
var stack = new Stack < FileStorageInfo > ( files ) ;
@@ -96,7 +103,12 @@ public async Task ShallDeleteFiles()
96
103
{
97
104
Assert . False ( _fileSystem . File . Exists ( file . FilePath ) ) ;
98
105
}
99
- Assert . True ( _fileSystem . Directory . Exists ( "/payloads" ) ) ;
106
+ foreach ( var dir in _fileSystem . Directory . GetDirectories ( _tempDirRoot ) )
107
+ {
108
+ _output . WriteLine ( dir ) ;
109
+ }
110
+ Assert . False ( _fileSystem . Directory . Exists ( Path . GetFullPath ( Path . Combine ( _tempDirRoot , "dir1" ) ) ) ) ;
111
+ Assert . True ( _fileSystem . Directory . Exists ( _tempDirRoot ) ) ;
100
112
101
113
_logger . VerifyLogging ( "Space Reclaimer Service canceled." , LogLevel . Warning , Times . Once ( ) ) ;
102
114
}
@@ -105,14 +117,14 @@ public async Task ShallDeleteFiles()
105
117
public async Task ShallDeleteDirectoriesIfEmpty ( )
106
118
{
107
119
var files = new List < FileStorageInfo > ( ) {
108
- new FileStorageInfo { FilePath = "/payloads/ dir1/dir1.1/file1" } ,
109
- new FileStorageInfo { FilePath = "/payloads/ dir1/dir1.2/file2" } ,
110
- new FileStorageInfo { FilePath = "/payloads/ dir1/dir1.2/dir1.2.1/file4" } ,
111
- new FileStorageInfo { FilePath = "/payloads/ dir1/dir1.2/dir1.2.1/file5" } ,
112
- new FileStorageInfo { FilePath = "/payloads/ dir1/dir1.2/dir1.2.2/file6" } ,
113
- new FileStorageInfo { FilePath = "/payloads/ dir1/dir1.2/file3" } ,
114
- new FileStorageInfo { FilePath = "/payloads/ dir1/dir1.3/file7" } ,
115
- new FileStorageInfo { FilePath = "/payloads/ dir2/dir2.1/dir2.1.1/file1.exe" }
120
+ new FileStorageInfo { FilePath = Path . GetFullPath ( Path . Combine ( _tempDirRoot , " dir1/dir1.1/file1") ) } ,
121
+ new FileStorageInfo { FilePath = Path . GetFullPath ( Path . Combine ( _tempDirRoot , " dir1/dir1.2/file2") ) } ,
122
+ new FileStorageInfo { FilePath = Path . GetFullPath ( Path . Combine ( _tempDirRoot , " dir1/dir1.2/dir1.2.1/file4") ) } ,
123
+ new FileStorageInfo { FilePath = Path . GetFullPath ( Path . Combine ( _tempDirRoot , " dir1/dir1.2/dir1.2.1/file5") ) } ,
124
+ new FileStorageInfo { FilePath = Path . GetFullPath ( Path . Combine ( _tempDirRoot , " dir1/dir1.2/dir1.2.2/file6") ) } ,
125
+ new FileStorageInfo { FilePath = Path . GetFullPath ( Path . Combine ( _tempDirRoot , " dir1/dir1.2/file3") ) } ,
126
+ new FileStorageInfo { FilePath = Path . GetFullPath ( Path . Combine ( _tempDirRoot , " dir1/dir1.3/file7") ) } ,
127
+ new FileStorageInfo { FilePath = Path . GetFullPath ( Path . Combine ( _tempDirRoot , " dir2/dir2.1/dir2.1.1/file1.exe") ) }
116
128
} ;
117
129
foreach ( var file in files )
118
130
{
@@ -137,20 +149,20 @@ public async Task ShallDeleteDirectoriesIfEmpty()
137
149
138
150
_queue . Verify ( p => p . Dequeue ( It . IsAny < CancellationToken > ( ) ) , Times . AtLeast ( 7 ) ) ;
139
151
140
- Assert . False ( _fileSystem . File . Exists ( "/payloads/ dir1/dir1.1/file1") ) ;
141
- Assert . False ( _fileSystem . File . Exists ( "/payloads/ dir1/dir1.2/file2") ) ;
142
- Assert . False ( _fileSystem . File . Exists ( "/payloads/ dir1/dir1.2/dir1.2.1/file4") ) ;
143
- Assert . False ( _fileSystem . File . Exists ( "/payloads/ dir1/dir1.2/dir1.2.1/file5") ) ;
144
- Assert . False ( _fileSystem . File . Exists ( "/payloads/ dir1/dir1.2/dir1.2.2/file6") ) ;
145
- Assert . False ( _fileSystem . File . Exists ( "/payloads/ dir2/dir2.1/dir2.1.1/file1.exe") ) ;
146
- Assert . False ( _fileSystem . File . Exists ( "/payloads/ dir1/dir1.3/file7") ) ;
147
- Assert . False ( _fileSystem . Directory . Exists ( "/payloads/ dir1/dir1.3") ) ;
148
- Assert . False ( _fileSystem . Directory . Exists ( "/payloads/ dir2") ) ;
149
-
150
- Assert . True ( _fileSystem . File . Exists ( "/payloads/ dir1/dir1.2/file3") ) ;
151
- Assert . True ( _fileSystem . Directory . Exists ( "/payloads/ dir1/dir1.2") ) ;
152
- Assert . True ( _fileSystem . Directory . Exists ( "/payloads/ dir1") ) ;
153
- Assert . True ( _fileSystem . Directory . Exists ( "/payloads" ) ) ;
152
+ Assert . False ( _fileSystem . File . Exists ( Path . GetFullPath ( Path . Combine ( _tempDirRoot , " dir1/dir1.1/file1") ) ) ) ;
153
+ Assert . False ( _fileSystem . File . Exists ( Path . GetFullPath ( Path . Combine ( _tempDirRoot , " dir1/dir1.2/file2") ) ) ) ;
154
+ Assert . False ( _fileSystem . File . Exists ( Path . GetFullPath ( Path . Combine ( _tempDirRoot , " dir1/dir1.2/dir1.2.1/file4") ) ) ) ;
155
+ Assert . False ( _fileSystem . File . Exists ( Path . GetFullPath ( Path . Combine ( _tempDirRoot , " dir1/dir1.2/dir1.2.1/file5") ) ) ) ;
156
+ Assert . False ( _fileSystem . File . Exists ( Path . GetFullPath ( Path . Combine ( _tempDirRoot , " dir1/dir1.2/dir1.2.2/file6") ) ) ) ;
157
+ Assert . False ( _fileSystem . File . Exists ( Path . GetFullPath ( Path . Combine ( _tempDirRoot , " dir2/dir2.1/dir2.1.1/file1.exe") ) ) ) ;
158
+ Assert . False ( _fileSystem . File . Exists ( Path . GetFullPath ( Path . Combine ( _tempDirRoot , " dir1/dir1.3/file7") ) ) ) ;
159
+ Assert . False ( _fileSystem . Directory . Exists ( Path . GetFullPath ( Path . Combine ( _tempDirRoot , " dir1/dir1.3") ) ) ) ;
160
+ Assert . False ( _fileSystem . Directory . Exists ( Path . GetFullPath ( Path . Combine ( _tempDirRoot , " dir2") ) ) ) ;
161
+
162
+ Assert . True ( _fileSystem . File . Exists ( Path . GetFullPath ( Path . Combine ( _tempDirRoot , " dir1/dir1.2/file3") ) ) ) ;
163
+ Assert . True ( _fileSystem . Directory . Exists ( Path . GetFullPath ( Path . Combine ( _tempDirRoot , " dir1/dir1.2") ) ) ) ;
164
+ Assert . True ( _fileSystem . Directory . Exists ( Path . GetFullPath ( Path . Combine ( _tempDirRoot , " dir1") ) ) ) ;
165
+ Assert . True ( _fileSystem . Directory . Exists ( _tempDirRoot ) ) ;
154
166
155
167
_logger . VerifyLogging ( "Space Reclaimer Service canceled." , LogLevel . Warning , Times . Once ( ) ) ;
156
168
}
0 commit comments