Files
duplicati/Duplicati/UnitTest/ControllerTests.cs
T
Kenneth Skovhede 179d1ceb47 Make controller Async
This PR has a large blast radius because it takes the final step and bumps up the Controller to be fully async.

We have historically done a piece-by-piece update, so all operations were already async but the controller interface was kept synchronous.

With this update, the controller is now fully async and all tests are updated.

Most places where the new C# compiler warns about function names not ending in `Async` were also adressed, giving a massive refactor change.

Functionally, no changes are done.
2026-05-13 15:04:13 +02:00

119 lines
5.7 KiB
C#

// Copyright (C) 2026, The Duplicati Team
// https://duplicati.com, hello@duplicati.com
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Duplicati.Library.Interface;
using Duplicati.Library.Main;
using NUnit.Framework;
using Assert = NUnit.Framework.Legacy.ClassicAssert;
namespace Duplicati.UnitTest
{
public class ControllerTests : BasicSetupHelper
{
[Test]
[Category("Controller")]
public async Task DeleteAllRemoteFilesAsync()
{
var filePath = Path.Combine(this.DATAFOLDER, "file");
File.WriteAllBytes(filePath, new byte[] { 0, 1, 2 });
var firstOptions = new Dictionary<string, string>(this.TestOptions);
using (var c = new Controller("file://" + this.TARGETFOLDER, firstOptions, null))
{
var backupResults = await c.BackupAsync(new[] { this.DATAFOLDER });
Assert.AreEqual(0, backupResults.Errors.Count());
Assert.AreEqual(0, backupResults.Warnings.Count());
}
// Keep track of the backend files from the first backup configuration so that we can
// check that they remain after we remove the backend files from the second backup
// configuration.
var firstBackupFiles = Directory.GetFiles(this.TARGETFOLDER);
Assert.Greater(firstBackupFiles.Length, 0);
Dictionary<string, string> secondOptions = new Dictionary<string, string>(this.TestOptions)
{
["dbpath"] = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName())
};
using (var c = new Controller("file://" + this.TARGETFOLDER, secondOptions, null))
{
// An exception should be thrown due to unrecognized files in the target folder.
// ReSharper disable once AccessToDisposedClosure
Assert.That(async () => await c.BackupAsync(new[] { this.DATAFOLDER }), Throws.Exception);
}
// We should be able to safely remove backend files from the second backup by referring
// to the local database.
using (var c = new Controller("file://" + this.TARGETFOLDER, secondOptions, null))
{
var listResults = await c.DeleteAllRemoteFilesAsync();
Assert.AreEqual(0, listResults.Errors.Count());
Assert.AreEqual(0, listResults.Warnings.Count());
}
// After we delete backend files from the second backup configuration, those from the first
// configuration should remain (see issues #3845, and #4244).
foreach (var file in firstBackupFiles)
{
Assert.IsTrue(File.Exists(file));
}
// Configure and run a second backup with a different prefix. This should run without error.
File.Delete(secondOptions["dbpath"]);
secondOptions["prefix"] = new Options(firstOptions).Prefix + "2";
secondOptions["dbpath"] = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
using (var c = new Controller("file://" + this.TARGETFOLDER, secondOptions, null))
{
var backupResults = await c.BackupAsync(new[] { this.DATAFOLDER });
Assert.AreEqual(0, backupResults.Errors.Count());
Assert.AreEqual(0, backupResults.Warnings.Count());
}
// Even without a local database, we should be able to safely remove backend files from
// the second backup due to the prefix.
File.Delete(secondOptions["dbpath"]);
using (var c = new Controller("file://" + this.TARGETFOLDER, secondOptions, null))
{
var listResults = await c.DeleteAllRemoteFilesAsync();
Assert.AreEqual(0, listResults.Errors.Count());
Assert.AreEqual(0, listResults.Warnings.Count());
}
// After we delete backend files from the second backup configuration, those from the first
// configuration should remain (see issue #2678).
foreach (var file in firstBackupFiles)
{
Assert.IsTrue(File.Exists(file));
}
// The first backup configuration should still run normally.
using (var c = new Controller("file://" + this.TARGETFOLDER, firstOptions, null))
{
var backupResults = await c.BackupAsync(new[] { this.DATAFOLDER });
Assert.AreEqual(0, backupResults.Errors.Count());
Assert.AreEqual(0, backupResults.Warnings.Count());
}
}
}
}