diff --git a/Lombiq.Tests.UI/SecurityScanning/ZapManager.cs b/Lombiq.Tests.UI/SecurityScanning/ZapManager.cs index addea5713..8a1150a14 100644 --- a/Lombiq.Tests.UI/SecurityScanning/ZapManager.cs +++ b/Lombiq.Tests.UI/SecurityScanning/ZapManager.cs @@ -183,7 +183,7 @@ public async Task RunSecurityScanAsync( return new SecurityScanResult(reportsDirectoryPath, SarifLog.Load(jsonReports[0])); } - public ValueTask DisposeAsync() + public async ValueTask DisposeAsync() { if (_cancellationTokenSource != null && !_cancellationTokenSource.IsCancellationRequested) { @@ -191,7 +191,23 @@ public ValueTask DisposeAsync() _cancellationTokenSource.Dispose(); } - return ValueTask.CompletedTask; + try + { + using var cleanupCancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(30)); + var cleanupCancellationToken = cleanupCancellationTokenSource.Token; + var imagesOutput = await _docker.ExecuteAndGetOutputAsync( + cleanupCancellationToken, "images", _zapImage, "--format", "{{.ID}}"); + + if (!string.IsNullOrEmpty(imagesOutput)) + { + await _docker.ExecuteAsync(cleanupCancellationToken, "image", "rm", _zapImage); + } + } + catch (Exception ex) + { + _testOutputHelper.WriteLineTimestampedAndDebug( + "Removing the Docker image {0} for ZAP failed with the following exception: {1}", _zapImage, ex); + } } private async Task EnsureInitializedAsync()