Skip to content

Commit 536581f

Browse files
committed
gh-65 Fix unit test
Signed-off-by: Victor Chang <[email protected]>
1 parent 9f55f5f commit 536581f

File tree

3 files changed

+51
-57
lines changed

3 files changed

+51
-57
lines changed

.github/workflows/ci.yml

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -137,39 +137,19 @@ jobs:
137137
- name: Restore dependencies
138138
run: dotnet restore
139139
working-directory: ./src
140-
141-
- name: Begin SonarScanner
142-
env:
143-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
144-
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
145-
shell: powershell
146-
run: .\.sonar\scanner\dotnet-sonarscanner begin /k:"Project-MONAI_monai-deploy-informatics-gateway" /o:"project-monai" /d:sonar.login="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.cs.opencover.reportsPaths="src\${{ env.TEST_RESULTS }}\**\*.xml"
147140

148-
- name: Build
141+
- name: Build & Test
149142
env:
150143
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
151144
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
152145
shell: powershell
153-
run: dotnet build -c ${{ env.BUILD_CONFIG }} --nologo "${{ env.SOLUTION }}"
146+
run: |
147+
..\.sonar\scanner\dotnet-sonarscanner begin /k:"Project-MONAI_monai-deploy-informatics-gateway" /o:"project-monai" /d:sonar.login="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.cs.opencover.reportsPaths="src\${{ env.TEST_RESULTS }}\**\*.xml"
148+
dotnet build -c ${{ env.BUILD_CONFIG }} --nologo "${{ env.SOLUTION }}"
149+
Get-ChildItem -Recurse | Where-Object { $_.Name -like "*Test.csproj" } | ForEach-Object { dotnet test -c ${{ env.BUILD_CONFIG }} -v=minimal -r "${{ env.TEST_RESULTS }}" --collect:"XPlat Code Coverage" --settings coverlet.runsettings $_.FullName }
150+
..\.sonar\scanner\dotnet-sonarscanner end /d:sonar.login="${{ secrets.SONAR_TOKEN }}"
154151
working-directory: ./src
155152

156-
- name: Test
157-
env:
158-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
159-
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
160-
shell: powershell
161-
run: Get-ChildItem -Recurse | Where-Object { $_.Name -like "*Test.csproj" } | ForEach-Object { dotnet test -c ${{ env.BUILD_CONFIG }} -v=minimal -r "${{ env.TEST_RESULTS }}" --collect:"XPlat Code Coverage" --settings coverlet.runsettings $_.FullName }
162-
working-directory: ./src
163-
164-
165-
- name: End SonarScanner
166-
env:
167-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
168-
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
169-
shell: powershell
170-
run: .\.sonar\scanner\dotnet-sonarscanner end /d:sonar.login="${{ secrets.SONAR_TOKEN }}"
171-
172-
173153
- uses: codecov/codecov-action@v2
174154
with:
175155
token: ${{ secrets.CODECOV_TOKEN }}

src/InformaticsGateway/Test/Services/Scp/ScpServiceTest.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ public ScpServiceTest()
5656
_loggerFactory.Setup(p => p.CreateLogger(It.IsAny<string>())).Returns(_logger.Object);
5757
_associationDataProvider.Setup(p => p.GetLogger(It.IsAny<string>())).Returns(_loggerInternal.Object);
5858
_associationDataProvider.Setup(p => p.Configuration).Returns(_configuration);
59+
_logger.Setup(p => p.IsEnabled(It.IsAny<LogLevel>())).Returns(true);
60+
_loggerInternal.Setup(p => p.IsEnabled(It.IsAny<LogLevel>())).Returns(true);
5961
}
6062

6163
[RetryFact(5, 250, DisplayName = "StartAsync - shall stop application if failed to start SCP listner")]
@@ -107,7 +109,7 @@ public async Task CEcho_ShallRejectCEchoRequests()
107109
Assert.Equal(DicomRejectSource.ServiceUser, exception.RejectSource);
108110
Assert.Equal(DicomRejectResult.Permanent, exception.RejectResult);
109111

110-
_loggerInternal.VerifyLogging($"Verification service is disabled: rejecting association", LogLevel.Warning, Times.Once());
112+
_loggerInternal.VerifyLogging($"Verification service is disabled: rejecting association.", LogLevel.Warning, Times.Once());
111113

112114
Assert.True(countdownEvent.Wait(1000));
113115
}
@@ -367,7 +369,7 @@ public async Task CStore_OnClientAbort()
367369

368370
await client.SendAsync(_cancellationTokenSource.Token, DicomClientCancellationMode.ImmediatelyAbortAssociation);
369371
Assert.True(countdownEvent.Wait(2000));
370-
_loggerInternal.VerifyLogging($"Aborted {DicomAbortSource.ServiceUser} with reason {DicomAbortReason.NotSpecified}", LogLevel.Warning, Times.Once());
372+
_loggerInternal.VerifyLogging($"Aborted {DicomAbortSource.ServiceUser} with reason {DicomAbortReason.NotSpecified}.", LogLevel.Warning, Times.Once());
371373
}
372374

373375
private ScpService CreateService()

src/InformaticsGateway/Test/Services/Storage/SpaceReclaimerServiceTest.cs

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: Apache License 2.0
33

44
using System.Collections.Generic;
5+
using System.IO;
56
using System.IO.Abstractions;
67
using System.IO.Abstractions.TestingHelpers;
78
using System.Linq;
@@ -16,6 +17,7 @@
1617
using Moq;
1718
using xRetry;
1819
using Xunit;
20+
using Xunit.Abstractions;
1921

2022
namespace Monai.Deploy.InformaticsGateway.Test.Services.Storage
2123
{
@@ -27,16 +29,20 @@ public class SpaceReclaimerServiceTest
2729
private readonly IFileSystem _fileSystem;
2830
private readonly IOptions<InformaticsGatewayConfiguration> _configuration;
2931
private readonly SpaceReclaimerService _service;
32+
private readonly ITestOutputHelper _output;
33+
private readonly string _tempDirRoot = "/payloads";
3034

31-
public SpaceReclaimerServiceTest()
35+
public SpaceReclaimerServiceTest(ITestOutputHelper output)
3236
{
37+
_output = output ?? throw new System.ArgumentNullException(nameof(output));
38+
3339
_cancellationTokenSource = new CancellationTokenSource();
3440
_logger = new Mock<ILogger<SpaceReclaimerService>>();
3541
_queue = new Mock<IInstanceCleanupQueue>();
3642
_fileSystem = new MockFileSystem();
3743

3844
_configuration = Options.Create(new InformaticsGatewayConfiguration());
39-
_configuration.Value.Storage.Temporary = "/payloads";
45+
_configuration.Value.Storage.Temporary = Path.GetFullPath(_tempDirRoot);
4046
_service = new SpaceReclaimerService(_queue.Object, _logger.Object, _configuration, _fileSystem);
4147
_logger.Setup(p => p.IsEnabled(It.IsAny<LogLevel>())).Returns(true);
4248
}
@@ -60,19 +66,20 @@ public async Task ShallHonorCancellationRequest()
6066
_logger.VerifyLogging("Space Reclaimer Service is stopping.", LogLevel.Information, Times.Once());
6167
}
6268

63-
[RetryFact(5, 250, DisplayName = "Shall delete files")]
69+
[RetryFact(10, 250, DisplayName = "Shall delete files")]
6470
public async Task ShallDeleteFiles()
6571
{
6672
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"))},
7076
};
7177

7278
foreach (var file in files)
7379
{
7480
_fileSystem.Directory.CreateDirectory(_fileSystem.Path.GetDirectoryName(file.FilePath));
7581
_fileSystem.File.Create(file.FilePath);
82+
7683
}
7784

7885
var stack = new Stack<FileStorageInfo>(files);
@@ -96,7 +103,12 @@ public async Task ShallDeleteFiles()
96103
{
97104
Assert.False(_fileSystem.File.Exists(file.FilePath));
98105
}
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));
100112

101113
_logger.VerifyLogging("Space Reclaimer Service canceled.", LogLevel.Warning, Times.Once());
102114
}
@@ -105,14 +117,14 @@ public async Task ShallDeleteFiles()
105117
public async Task ShallDeleteDirectoriesIfEmpty()
106118
{
107119
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"))}
116128
};
117129
foreach (var file in files)
118130
{
@@ -137,20 +149,20 @@ public async Task ShallDeleteDirectoriesIfEmpty()
137149

138150
_queue.Verify(p => p.Dequeue(It.IsAny<CancellationToken>()), Times.AtLeast(7));
139151

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));
154166

155167
_logger.VerifyLogging("Space Reclaimer Service canceled.", LogLevel.Warning, Times.Once());
156168
}

0 commit comments

Comments
 (0)