diff options
author | Kenneth Skovhede <kenneth@hexad.dk> | 2020-08-07 11:33:31 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-07 11:33:31 +0300 |
commit | 04dd510be0958908a8424b6883afeb24aed49f58 (patch) | |
tree | 844f0a4c95e8f65af55ecc7f9e7ba07351ecbfe6 /Duplicati/UnitTest | |
parent | e5b0ac2749eaeb55ca9ff07daf40d352e31c5ef5 (diff) | |
parent | 0314c388ef64cb892163820370ca24b82fb5a0b8 (diff) |
Merge pull request #4261 from warwickmm/avoid_deleting_unrelated_files
Avoid deleting unrelated files when removing backup configuration
Diffstat (limited to 'Duplicati/UnitTest')
-rw-r--r-- | Duplicati/UnitTest/ControllerTests.cs | 95 | ||||
-rw-r--r-- | Duplicati/UnitTest/Duplicati.UnitTest.csproj | 1 |
2 files changed, 96 insertions, 0 deletions
diff --git a/Duplicati/UnitTest/ControllerTests.cs b/Duplicati/UnitTest/ControllerTests.cs new file mode 100644 index 000000000..c50582928 --- /dev/null +++ b/Duplicati/UnitTest/ControllerTests.cs @@ -0,0 +1,95 @@ +using System.Collections.Generic; +using System.IO; +using System.Linq; +using Duplicati.Library.Interface; +using Duplicati.Library.Main; +using NUnit.Framework; + +namespace Duplicati.UnitTest +{ + public class ControllerTests : BasicSetupHelper + { + [Test] + [Category("Controller")] + public void DeleteAllRemoteFiles() + { + string filePath = Path.Combine(this.DATAFOLDER, "file"); + File.WriteAllBytes(filePath, new byte[] {0, 1, 2}); + + Dictionary<string, string> firstOptions = new Dictionary<string, string>(this.TestOptions); + using (Controller c = new Controller("file://" + this.TARGETFOLDER, firstOptions, null)) + { + IBackupResults backupResults = c.Backup(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. + string[] 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 (Controller 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(() => c.Backup(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 (Controller c = new Controller("file://" + this.TARGETFOLDER, secondOptions, null)) + { + IListRemoteResults listResults = c.DeleteAllRemoteFiles(); + 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 (string file in firstBackupFiles) + { + Assert.IsTrue(File.Exists(file)); + } + + // Configure and run a second backup with a different prefix. This should run without error. + secondOptions["prefix"] = new Options(firstOptions).Prefix + "2"; + using (Controller c = new Controller("file://" + this.TARGETFOLDER, secondOptions, null)) + { + IBackupResults backupResults = c.Backup(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 (Controller c = new Controller("file://" + this.TARGETFOLDER, secondOptions, null)) + { + IListRemoteResults listResults = c.DeleteAllRemoteFiles(); + 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 (string file in firstBackupFiles) + { + Assert.IsTrue(File.Exists(file)); + } + + // The first backup configuration should still run normally. + using (Controller c = new Controller("file://" + this.TARGETFOLDER, firstOptions, null)) + { + IBackupResults backupResults = c.Backup(new[] {this.DATAFOLDER}); + Assert.AreEqual(0, backupResults.Errors.Count()); + Assert.AreEqual(0, backupResults.Warnings.Count()); + } + } + } +}
\ No newline at end of file diff --git a/Duplicati/UnitTest/Duplicati.UnitTest.csproj b/Duplicati/UnitTest/Duplicati.UnitTest.csproj index 699294732..6c9a2a8b2 100644 --- a/Duplicati/UnitTest/Duplicati.UnitTest.csproj +++ b/Duplicati/UnitTest/Duplicati.UnitTest.csproj @@ -44,6 +44,7 @@ <Reference Include="System.IO.Compression.FileSystem" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="ControllerTests.cs" />
<Compile Include="DeleteHandlerTests.cs" />
<Compile Include="DisruptionTests.cs" />
<Compile Include="ProblematicPathTests.cs" />
|