Files
duplicati/Duplicati/UnitTest/DisruptionTests.cs
T

856 lines
42 KiB
C#
Raw Normal View History

2025-01-07 09:40:39 +01:00
// Copyright (C) 2025, The Duplicati Team
2024-02-28 15:45:30 +01:00
// https://duplicati.com, hello@duplicati.com
2025-01-07 09:40:39 +01:00
//
// 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
2024-02-28 15:45:30 +01:00
// Software is furnished to do so, subject to the following conditions:
2025-01-07 09:40:39 +01:00
//
// The above copyright notice and this permission notice shall be included in
2024-02-28 15:45:30 +01:00
// all copies or substantial portions of the Software.
2025-01-07 09:40:39 +01:00
//
// 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
2024-02-28 15:45:30 +01:00
// DEALINGS IN THE SOFTWARE.
2019-09-15 10:33:35 -07:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Duplicati.Library.Interface;
using Duplicati.Library.Main;
2019-09-15 13:45:43 -07:00
using Duplicati.Library.Main.Database;
using Duplicati.Library.Main.Volumes;
2019-09-15 10:33:35 -07:00
using NUnit.Framework;
using IFileEntry = Duplicati.Library.Interface.IFileEntry;
2019-09-15 15:05:03 -07:00
using Utility = Duplicati.Library.Utility.Utility;
2019-09-15 10:33:35 -07:00
namespace Duplicati.UnitTest
{
public class DisruptionTests : BasicSetupHelper
{
2019-09-15 15:28:53 -07:00
// Files to create in MB.
2024-04-18 11:49:39 +02:00
private readonly int[] fileSizes = { 10, 20, 30 };
2019-09-15 15:28:53 -07:00
2019-09-15 10:33:35 -07:00
private void ModifySourceFiles()
{
2019-09-15 15:28:53 -07:00
foreach (int size in this.fileSizes)
2019-09-15 10:33:35 -07:00
{
byte[] data = new byte[size * 1024 * 1024];
Random rng = new Random();
rng.NextBytes(data);
File.WriteAllBytes(Path.Combine(this.DATAFOLDER, size + "MB"), data);
}
}
2019-09-15 12:50:31 -07:00
private async Task<IBackupResults> RunPartialBackup(Controller controller)
{
this.ModifySourceFiles();
2024-12-18 08:27:59 +01:00
var stopped = new TaskCompletionSource<bool>();
controller.OnOperationStarted = r =>
2024-04-18 11:49:39 +02:00
{
2024-12-18 08:27:59 +01:00
var pv = r as ITaskControlProvider;
if (pv == null)
throw new Exception("Task control provider not found");
#if DEBUG
pv.TaskControl.TestMethodCallback = (path) =>
{
if (path.EndsWith(this.fileSizes[2] + "MB"))
{
2025-01-28 08:55:28 +01:00
Thread.Sleep(500);
2024-12-18 08:27:59 +01:00
controller.Stop();
2025-01-28 08:55:28 +01:00
stopped.TrySetResult(true);
2024-12-18 08:27:59 +01:00
}
};
#endif
};
2024-04-18 11:49:39 +02:00
2024-12-18 08:27:59 +01:00
// ReSharper disable once AccessToDisposedClosure
Task<IBackupResults> backupTask = Task.Run(() => controller.Backup(new[] { this.DATAFOLDER }));
2024-04-18 11:49:39 +02:00
2024-12-18 08:27:59 +01:00
var t = await Task.WhenAny(backupTask, stopped.Task).ConfigureAwait(false);
if (t != stopped.Task)
2024-04-18 11:49:39 +02:00
throw new Exception("Backup task completed before we could stop it");
return await backupTask.ConfigureAwait(false);
}
[SetUp]
public void SetUp()
2019-09-15 10:33:35 -07:00
{
this.ModifySourceFiles();
}
2019-09-15 12:50:31 -07:00
[Test]
[Category("Disruption")]
public async Task FilesetFiles()
{
2024-12-18 08:27:59 +01:00
#if !DEBUG
Assert.Ignore("This test requires DEBUG to be defined");
#endif
// Choose a dblock size that is small enough so that more than one volume is needed.
Dictionary<string, string> options = new Dictionary<string, string>(this.TestOptions)
{
["dblock-size"] = "10mb",
// This allows us to inspect the dlist files without needing the BackendManager (which is inaccessible here) to decrypt them.
2024-12-18 08:27:59 +01:00
["no-encryption"] = "true",
2025-01-28 08:55:28 +01:00
["disable-file-scanner"] = "true",
["concurrency-fileprocessors"] = "1",
};
// Run a full backup.
using (Controller c = new Controller("file://" + this.TARGETFOLDER, options, null))
{
2024-04-18 11:49:39 +02:00
IBackupResults backupResults = c.Backup(new[] { this.DATAFOLDER });
Assert.AreEqual(0, backupResults.Errors.Count());
Assert.AreEqual(0, backupResults.Warnings.Count());
}
2024-04-18 11:49:39 +02:00
// Inject some spacing to allow for the purged fileset
Thread.Sleep(2000);
// Run a partial backup.
using (Controller c = new Controller("file://" + this.TARGETFOLDER, options, null))
{
IBackupResults backupResults = await this.RunPartialBackup(c).ConfigureAwait(false);
Assert.AreEqual(0, backupResults.Errors.Count());
Assert.AreEqual(1, backupResults.Warnings.Count());
}
Dictionary<DateTime, int> GetBackupTypesFromRemoteFiles(Controller c, out List<string> filelistFiles)
{
Dictionary<DateTime, int> map = new Dictionary<DateTime, int>();
filelistFiles = new List<string>();
IListRemoteResults remoteFiles = c.ListRemote();
foreach (IFileEntry file in remoteFiles.Files)
{
IParsedVolume volume = VolumeBase.ParseFilename(file);
if (volume != null && volume.FileType == RemoteVolumeType.Files)
{
string dlistFile = Path.Combine(this.TARGETFOLDER, volume.File.Name);
filelistFiles.Add(dlistFile);
VolumeBase.FilesetData filesetData = VolumeReaderBase.GetFilesetData(volume.CompressionModule, dlistFile, new Options(options));
map[volume.Time] = filesetData.IsFullBackup ? BackupType.FULL_BACKUP : BackupType.PARTIAL_BACKUP;
}
}
return map;
}
// Purge a file and verify that the fileset file exists in the new dlist files.
List<string> dlistFiles;
Dictionary<DateTime, int> backupTypeMap;
using (Controller c = new Controller("file://" + this.TARGETFOLDER, options, null))
{
IPurgeFilesResults purgeResults = c.PurgeFiles(new Library.Utility.FilterExpression($"{this.DATAFOLDER}/*{this.fileSizes[0]}*"));
Assert.AreEqual(0, purgeResults.Errors.Count());
Assert.AreEqual(0, purgeResults.Warnings.Count());
List<IListResultFileset> filesets = c.List().Filesets.ToList();
Assert.AreEqual(2, filesets.Count);
Assert.AreEqual(BackupType.FULL_BACKUP, filesets.Single(x => x.Version == 1).IsFullBackup);
Assert.AreEqual(BackupType.PARTIAL_BACKUP, filesets.Single(x => x.Version == 0).IsFullBackup);
backupTypeMap = GetBackupTypesFromRemoteFiles(c, out dlistFiles);
}
int[] backupTypes = backupTypeMap.OrderByDescending(x => x.Key).Select(x => x.Value).ToArray();
Assert.AreEqual(2, backupTypes.Length);
Assert.AreEqual(BackupType.FULL_BACKUP, backupTypes[1]);
Assert.AreEqual(BackupType.PARTIAL_BACKUP, backupTypes[0]);
// Remove the dlist files.
foreach (string dlistFile in dlistFiles)
{
File.Delete(dlistFile);
}
// Run a repair and verify that the fileset file exists in the new dlist files.
using (Controller c = new Controller("file://" + this.TARGETFOLDER, options, null))
{
IRepairResults repairResults = c.Repair();
Assert.AreEqual(0, repairResults.Errors.Count());
Assert.AreEqual(0, repairResults.Warnings.Count());
List<IListResultFileset> filesets = c.List().Filesets.ToList();
Assert.AreEqual(2, filesets.Count);
Assert.AreEqual(BackupType.FULL_BACKUP, filesets.Single(x => x.Version == 1).IsFullBackup);
Assert.AreEqual(BackupType.PARTIAL_BACKUP, filesets.Single(x => x.Version == 0).IsFullBackup);
backupTypeMap = GetBackupTypesFromRemoteFiles(c, out _);
}
backupTypes = backupTypeMap.OrderByDescending(x => x.Key).Select(x => x.Value).ToArray();
Assert.AreEqual(2, backupTypes.Length);
Assert.AreEqual(BackupType.FULL_BACKUP, backupTypes[1]);
Assert.AreEqual(BackupType.PARTIAL_BACKUP, backupTypes[0]);
}
2019-11-28 21:06:24 -08:00
[Test]
[Category("Disruption")]
public async Task KeepTimeRetention()
{
2024-12-18 08:27:59 +01:00
#if !DEBUG
Assert.Ignore("This test requires DEBUG to be defined");
#endif
2019-11-28 21:06:24 -08:00
// Choose a dblock size that is small enough so that more than one volume is needed.
2024-12-18 08:27:59 +01:00
Dictionary<string, string> options = new Dictionary<string, string>(this.TestOptions)
{
["dblock-size"] = "10mb",
2025-01-28 08:55:28 +01:00
["disable-file-scanner"] = "true",
["concurrency-fileprocessors"] = "1",
2024-12-18 08:27:59 +01:00
};
2019-11-28 23:11:33 -08:00
// First, run two complete backups followed by a partial backup. We will then set the keep-time
2019-11-28 23:11:33 -08:00
// option so that the threshold lies between the first and second backups.
DateTime firstBackupTime;
2019-11-28 21:06:24 -08:00
using (Controller c = new Controller("file://" + this.TARGETFOLDER, options, null))
{
2024-04-18 11:49:39 +02:00
IBackupResults backupResults = c.Backup(new[] { this.DATAFOLDER });
Assert.AreEqual(0, backupResults.Errors.Count());
Assert.AreEqual(0, backupResults.Warnings.Count());
firstBackupTime = c.List().Filesets.First().Time;
2019-11-28 23:11:33 -08:00
}
2019-11-28 21:06:24 -08:00
2019-11-28 23:11:33 -08:00
// Wait before the second backup so that we can more easily define the keep-time threshold
// to lie between the first and second backups.
Thread.Sleep(5000);
DateTime secondBackupTime;
2019-11-28 23:11:33 -08:00
using (Controller c = new Controller("file://" + this.TARGETFOLDER, options, null))
{
2019-11-28 21:06:24 -08:00
this.ModifySourceFiles();
2024-04-18 11:49:39 +02:00
IBackupResults backupResults = c.Backup(new[] { this.DATAFOLDER });
Assert.AreEqual(0, backupResults.Errors.Count());
Assert.AreEqual(0, backupResults.Warnings.Count());
secondBackupTime = c.List().Filesets.First().Time;
2019-11-28 23:11:33 -08:00
}
2019-11-28 21:06:24 -08:00
2019-11-28 23:11:33 -08:00
// Run a partial backup.
DateTime thirdBackupTime;
2019-11-28 23:11:33 -08:00
using (Controller c = new Controller("file://" + this.TARGETFOLDER, options, null))
{
IBackupResults backupResults = await this.RunPartialBackup(c).ConfigureAwait(false);
Assert.AreEqual(0, backupResults.Errors.Count());
Assert.AreEqual(1, backupResults.Warnings.Count());
thirdBackupTime = c.List().Filesets.First().Time;
2019-11-28 23:11:33 -08:00
}
2019-11-28 21:06:24 -08:00
2019-11-28 23:11:33 -08:00
// Set the keep-time option so that the threshold lies between the first and second backups
// and run the delete operation.
using (Controller c = new Controller("file://" + this.TARGETFOLDER, options, null))
{
2024-04-18 11:49:39 +02:00
options["keep-time"] = $"{(int)((DateTime.Now - firstBackupTime).TotalSeconds - (secondBackupTime - firstBackupTime).TotalSeconds / 2)}s";
IDeleteResults deleteResults = c.Delete();
Assert.AreEqual(0, deleteResults.Errors.Count());
Assert.AreEqual(0, deleteResults.Warnings.Count());
2019-11-28 21:06:24 -08:00
List<IListResultFileset> filesets = c.List().Filesets.ToList();
2019-11-28 21:06:24 -08:00
Assert.AreEqual(2, filesets.Count);
Assert.AreEqual(secondBackupTime, filesets[1].Time);
2019-11-28 21:06:24 -08:00
Assert.AreEqual(BackupType.FULL_BACKUP, filesets[1].IsFullBackup);
Assert.AreEqual(thirdBackupTime, filesets[0].Time);
2019-11-28 21:06:24 -08:00
Assert.AreEqual(BackupType.PARTIAL_BACKUP, filesets[0].IsFullBackup);
2019-11-28 23:11:33 -08:00
}
2019-11-28 21:06:24 -08:00
// Run another partial backup. We will then verify that a full backup is retained
2019-11-28 23:11:33 -08:00
// even when all the "recent" backups are partial.
using (Controller c = new Controller("file://" + this.TARGETFOLDER, options, null))
{
IBackupResults backupResults = await this.RunPartialBackup(c).ConfigureAwait(false);
Assert.AreEqual(0, backupResults.Errors.Count());
Assert.AreEqual(1, backupResults.Warnings.Count());
DateTime fourthBackupTime = c.List().Filesets.First().Time;
2019-11-28 21:06:24 -08:00
// Set the keep-time option so that the threshold lies after the most recent full backup
// and run the delete operation.
options["keep-time"] = "1s";
IDeleteResults deleteResults = c.Delete();
Assert.AreEqual(0, deleteResults.Errors.Count());
Assert.AreEqual(0, deleteResults.Warnings.Count());
2019-11-28 21:06:24 -08:00
2019-11-28 23:11:33 -08:00
List<IListResultFileset> filesets = c.List().Filesets.ToList();
2019-11-28 21:06:24 -08:00
Assert.AreEqual(3, filesets.Count);
Assert.AreEqual(secondBackupTime, filesets[2].Time);
2019-11-28 21:06:24 -08:00
Assert.AreEqual(BackupType.FULL_BACKUP, filesets[2].IsFullBackup);
Assert.AreEqual(thirdBackupTime, filesets[1].Time);
2019-11-28 21:06:24 -08:00
Assert.AreEqual(BackupType.PARTIAL_BACKUP, filesets[1].IsFullBackup);
Assert.AreEqual(fourthBackupTime, filesets[0].Time);
2019-11-28 21:06:24 -08:00
Assert.AreEqual(BackupType.PARTIAL_BACKUP, filesets[0].IsFullBackup);
}
}
2019-11-28 23:11:33 -08:00
[Test]
[Category("Disruption")]
public async Task KeepVersionsRetention()
{
2024-12-18 08:27:59 +01:00
#if !DEBUG
Assert.Ignore("This test requires DEBUG to be defined");
#endif
2019-11-28 23:11:33 -08:00
// Choose a dblock size that is small enough so that more than one volume is needed.
2024-12-18 08:27:59 +01:00
Dictionary<string, string> options = new Dictionary<string, string>(this.TestOptions)
{
["dblock-size"] = "10mb",
["disable-file-scanner"] = "true",
2025-01-28 08:55:28 +01:00
["concurrency-fileprocessors"] = "1",
2024-12-18 08:27:59 +01:00
};
2019-11-28 23:11:33 -08:00
// Run a full backup.
DateTime firstBackupTime;
2019-11-28 23:11:33 -08:00
using (Controller c = new Controller("file://" + this.TARGETFOLDER, options, null))
{
2024-04-18 11:49:39 +02:00
IBackupResults backupResults = c.Backup(new[] { this.DATAFOLDER });
Assert.AreEqual(0, backupResults.Errors.Count());
Assert.AreEqual(0, backupResults.Warnings.Count());
firstBackupTime = c.List().Filesets.First().Time;
2019-11-28 23:11:33 -08:00
}
// Run a partial backup.
using (Controller c = new Controller("file://" + this.TARGETFOLDER, options, null))
{
IBackupResults backupResults = await this.RunPartialBackup(c).ConfigureAwait(false);
Assert.AreEqual(0, backupResults.Errors.Count());
Assert.AreEqual(1, backupResults.Warnings.Count());
2019-11-28 23:11:33 -08:00
}
// Run a partial backup.
using (Controller c = new Controller("file://" + this.TARGETFOLDER, options, null))
{
IBackupResults backupResults = await this.RunPartialBackup(c).ConfigureAwait(false);
Assert.AreEqual(0, backupResults.Errors.Count());
Assert.AreEqual(1, backupResults.Warnings.Count());
2019-11-28 23:11:33 -08:00
}
// Run a full backup.
DateTime fourthBackupTime;
2019-11-28 23:11:33 -08:00
using (Controller c = new Controller("file://" + this.TARGETFOLDER, options, null))
{
this.ModifySourceFiles();
2024-04-18 11:49:39 +02:00
IBackupResults backupResults = c.Backup(new[] { this.DATAFOLDER });
Assert.AreEqual(0, backupResults.Errors.Count());
Assert.AreEqual(0, backupResults.Warnings.Count());
fourthBackupTime = c.List().Filesets.First().Time;
2019-11-28 23:11:33 -08:00
}
// Run a partial backup.
using (Controller c = new Controller("file://" + this.TARGETFOLDER, options, null))
{
options["keep-versions"] = "2";
IBackupResults backupResults = await this.RunPartialBackup(c).ConfigureAwait(false);
Assert.AreEqual(0, backupResults.Errors.Count());
Assert.AreEqual(1, backupResults.Warnings.Count());
DateTime fifthBackupTime = c.List().Filesets.First().Time;
2019-11-28 23:11:33 -08:00
2024-12-18 08:27:59 +01:00
// Since we stopped the operation, files were not deleted automatically
c.Delete();
2019-11-28 23:11:33 -08:00
// Partial backups that are followed by a full backup can be deleted.
List<IListResultFileset> filesets = c.List().Filesets.ToList();
Assert.AreEqual(3, filesets.Count);
Assert.AreEqual(firstBackupTime, filesets[2].Time);
2019-11-28 23:11:33 -08:00
Assert.AreEqual(BackupType.FULL_BACKUP, filesets[2].IsFullBackup);
Assert.AreEqual(fourthBackupTime, filesets[1].Time);
2019-11-28 23:11:33 -08:00
Assert.AreEqual(BackupType.FULL_BACKUP, filesets[1].IsFullBackup);
Assert.AreEqual(fifthBackupTime, filesets[0].Time);
2019-11-28 23:11:33 -08:00
Assert.AreEqual(BackupType.PARTIAL_BACKUP, filesets[0].IsFullBackup);
}
// Run a full backup.
using (Controller c = new Controller("file://" + this.TARGETFOLDER, options, null))
{
this.ModifySourceFiles();
2024-04-18 11:49:39 +02:00
IBackupResults backupResults = c.Backup(new[] { this.DATAFOLDER });
Assert.AreEqual(0, backupResults.Errors.Count());
Assert.AreEqual(0, backupResults.Warnings.Count());
DateTime sixthBackupTime = c.List().Filesets.First().Time;
// Since the last backup was full, we can now expect to have just the 2 most recent full backups.
List<IListResultFileset> filesets = c.List().Filesets.ToList();
Assert.AreEqual(2, filesets.Count);
Assert.AreEqual(fourthBackupTime, filesets[1].Time);
Assert.AreEqual(BackupType.FULL_BACKUP, filesets[1].IsFullBackup);
Assert.AreEqual(sixthBackupTime, filesets[0].Time);
Assert.AreEqual(BackupType.FULL_BACKUP, filesets[0].IsFullBackup);
}
2019-11-28 23:11:33 -08:00
}
[Test]
[Category("Disruption")]
public async Task ListWithoutLocalDb()
{
2024-12-18 08:27:59 +01:00
#if !DEBUG
Assert.Ignore("This test requires DEBUG to be defined");
#endif
// Choose a dblock size that is small enough so that more than one volume is needed.
Dictionary<string, string> options = new Dictionary<string, string>(this.TestOptions)
{
["dblock-size"] = "10mb",
2024-12-18 08:27:59 +01:00
["no-local-db"] = "true",
2025-01-28 08:55:28 +01:00
["disable-file-scanner"] = "true",
["concurrency-fileprocessors"] = "1",
};
// Run a full backup.
using (Controller c = new Controller("file://" + this.TARGETFOLDER, options, null))
{
2024-04-18 11:49:39 +02:00
IBackupResults backupResults = c.Backup(new[] { this.DATAFOLDER });
Assert.AreEqual(0, backupResults.Errors.Count());
Assert.AreEqual(0, backupResults.Warnings.Count());
}
// Run a partial backup.
using (Controller c = new Controller("file://" + this.TARGETFOLDER, options, null))
{
IBackupResults backupResults = await this.RunPartialBackup(c).ConfigureAwait(false);
Assert.AreEqual(0, backupResults.Errors.Count());
Assert.AreEqual(1, backupResults.Warnings.Count());
List<IListResultFileset> filesets = c.List().Filesets.ToList();
Assert.AreEqual(2, filesets.Count);
Assert.AreEqual(BackupType.FULL_BACKUP, filesets[1].IsFullBackup);
Assert.AreEqual(BackupType.PARTIAL_BACKUP, filesets[0].IsFullBackup);
}
}
2019-11-29 16:22:55 -08:00
[Test]
[Category("Disruption")]
public async Task RetentionPolicyRetention()
{
2024-12-18 08:27:59 +01:00
#if !DEBUG
Assert.Ignore("This test requires DEBUG to be defined");
#endif
2019-11-29 16:22:55 -08:00
Dictionary<string, string> options = new Dictionary<string, string>(this.TestOptions)
{
// Choose a dblock size that is small enough so that more than one volume is needed.
["dblock-size"] = "10mb",
// This test assumes that we can perform 3 backups within 1 minute.
["retention-policy"] = "1m:59s,U:1m",
2024-12-18 08:27:59 +01:00
["no-backend-verification"] = "true",
2025-01-28 08:55:28 +01:00
["disable-file-scanner"] = "true",
["concurrency-fileprocessors"] = "1"
2019-11-29 16:22:55 -08:00
};
DateTime firstBackupTime;
using (Controller c = new Controller("file://" + this.TARGETFOLDER, options, null))
{
2024-04-18 11:49:39 +02:00
IBackupResults backupResults = c.Backup(new[] { this.DATAFOLDER });
Assert.AreEqual(0, backupResults.Errors.Count());
Assert.AreEqual(0, backupResults.Warnings.Count());
2019-11-29 16:22:55 -08:00
firstBackupTime = c.List().Filesets.First().Time;
List<IListResultFileset> filesets = c.List().Filesets.ToList();
Assert.AreEqual(1, filesets.Count);
this.ModifySourceFiles();
2024-04-18 11:49:39 +02:00
backupResults = c.Backup(new[] { this.DATAFOLDER });
Assert.AreEqual(0, backupResults.Errors.Count());
Assert.AreEqual(0, backupResults.Warnings.Count());
2019-11-29 16:22:55 -08:00
DateTime secondBackupTime = c.List().Filesets.First().Time;
// Since the most recent backup is not considered in the retention logic, the only backup in the first time frame
// is the initial one. As a result, we should have 2 backups.
2019-11-29 16:22:55 -08:00
filesets = c.List().Filesets.ToList();
Assert.AreEqual(2, filesets.Count);
Assert.AreEqual(firstBackupTime, filesets[1].Time);
Assert.AreEqual(BackupType.FULL_BACKUP, filesets[1].IsFullBackup);
Assert.AreEqual(secondBackupTime, filesets[0].Time);
Assert.AreEqual(BackupType.FULL_BACKUP, filesets[0].IsFullBackup);
}
// Wait so that the next backups fall in the next retention interval.
Thread.Sleep(new TimeSpan(0, 0, 1, 0));
DateTime thirdBackupTime;
using (Controller c = new Controller("file://" + this.TARGETFOLDER, options, null))
{
IBackupResults backupResults = await this.RunPartialBackup(c).ConfigureAwait(false);
Assert.AreEqual(0, backupResults.Errors.Count());
Assert.AreEqual(1, backupResults.Warnings.Count());
2019-11-29 16:22:55 -08:00
thirdBackupTime = c.List().Filesets.First().Time;
2024-12-18 08:27:59 +01:00
// Since we stopped the backup, files were not deleted automatically
c.Delete();
2019-11-29 16:22:55 -08:00
// Since the most recent backup is not considered in the retention logic, there are no backups in the first time
// frame. The original 2 backups have now spilled over to the U:1m specification. Since we keep the first
2019-11-29 16:22:55 -08:00
// backup in the interval, we should be left with the first backup, as well as the third partial one.
List<IListResultFileset> filesets = c.List().Filesets.ToList();
Assert.AreEqual(2, filesets.Count);
Assert.AreEqual(firstBackupTime, filesets[1].Time);
Assert.AreEqual(BackupType.FULL_BACKUP, filesets[1].IsFullBackup);
Assert.AreEqual(thirdBackupTime, filesets[0].Time);
Assert.AreEqual(BackupType.PARTIAL_BACKUP, filesets[0].IsFullBackup);
}
DateTime fourthBackupTime;
using (Controller c = new Controller("file://" + this.TARGETFOLDER, options, null))
{
this.ModifySourceFiles();
2024-04-18 11:49:39 +02:00
IBackupResults backupResults = c.Backup(new[] { this.DATAFOLDER });
Assert.AreEqual(0, backupResults.Errors.Count());
Assert.AreEqual(0, backupResults.Warnings.Count());
2019-11-29 16:22:55 -08:00
fourthBackupTime = c.List().Filesets.First().Time;
// Since the most recent backup is not considered in the retention logic, the third backup is the only backup
// in the first time frame. There is no further spillover, so we simply add the fourth backup to the
2019-11-29 16:22:55 -08:00
// collection of retained backups.
List<IListResultFileset> filesets = c.List().Filesets.ToList();
Assert.AreEqual(3, filesets.Count);
Assert.AreEqual(firstBackupTime, filesets[2].Time);
Assert.AreEqual(BackupType.FULL_BACKUP, filesets[2].IsFullBackup);
Assert.AreEqual(thirdBackupTime, filesets[1].Time);
Assert.AreEqual(BackupType.PARTIAL_BACKUP, filesets[1].IsFullBackup);
Assert.AreEqual(fourthBackupTime, filesets[0].Time);
Assert.AreEqual(BackupType.FULL_BACKUP, filesets[0].IsFullBackup);
this.ModifySourceFiles();
2024-04-18 11:49:39 +02:00
backupResults = c.Backup(new[] { this.DATAFOLDER });
Assert.AreEqual(0, backupResults.Errors.Count());
Assert.AreEqual(0, backupResults.Warnings.Count());
2019-11-29 16:22:55 -08:00
DateTime fifthBackupTime = c.List().Filesets.First().Time;
// Since the most recent backup is not considered in the retention logic, we now have two backups in the
// first time frame: the third (partial) and fourth (full). Since the first backup in each interval is
// kept, we would typically keep just the third backup. However, since we should not discard a full
// backup in favor of a partial one, we keep the fourth as well. We also still have the initial backup.
2019-11-29 16:22:55 -08:00
filesets = c.List().Filesets.ToList();
Assert.AreEqual(4, filesets.Count);
Assert.AreEqual(firstBackupTime, filesets[3].Time);
Assert.AreEqual(BackupType.FULL_BACKUP, filesets[3].IsFullBackup);
Assert.AreEqual(thirdBackupTime, filesets[2].Time);
Assert.AreEqual(BackupType.PARTIAL_BACKUP, filesets[2].IsFullBackup);
Assert.AreEqual(fourthBackupTime, filesets[1].Time);
Assert.AreEqual(BackupType.FULL_BACKUP, filesets[1].IsFullBackup);
Assert.AreEqual(fifthBackupTime, filesets[0].Time);
Assert.AreEqual(BackupType.FULL_BACKUP, filesets[0].IsFullBackup);
}
// Wait so that the next backups fall in the next retention interval.
Thread.Sleep(new TimeSpan(0, 0, 1, 0));
using (Controller c = new Controller("file://" + this.TARGETFOLDER, options, null))
{
this.ModifySourceFiles();
2024-04-18 11:49:39 +02:00
IBackupResults backupResults = c.Backup(new[] { this.DATAFOLDER });
Assert.AreEqual(0, backupResults.Errors.Count());
Assert.AreEqual(0, backupResults.Warnings.Count());
2019-11-29 16:22:55 -08:00
DateTime sixthBackupTime = c.List().Filesets.First().Time;
// Since the most recent backup is not considered in the retention logic, we now have three backups in the
// second time frame: the third (partial), fourth (full), and fifth (full). Since we keep up to the first
2019-11-29 16:22:55 -08:00
// full backup in each time frame, we now drop the fifth backup.
List<IListResultFileset> filesets = c.List().Filesets.ToList();
Assert.AreEqual(4, filesets.Count);
Assert.AreEqual(firstBackupTime, filesets[3].Time);
Assert.AreEqual(BackupType.FULL_BACKUP, filesets[3].IsFullBackup);
Assert.AreEqual(thirdBackupTime, filesets[2].Time);
Assert.AreEqual(BackupType.PARTIAL_BACKUP, filesets[2].IsFullBackup);
Assert.AreEqual(fourthBackupTime, filesets[1].Time);
Assert.AreEqual(BackupType.FULL_BACKUP, filesets[1].IsFullBackup);
Assert.AreEqual(sixthBackupTime, filesets[0].Time);
Assert.AreEqual(BackupType.FULL_BACKUP, filesets[0].IsFullBackup);
}
}
2019-09-15 10:33:35 -07:00
[Test]
[Category("Disruption")]
public async Task StopAfterCurrentFile()
{
2024-12-18 08:27:59 +01:00
#if !DEBUG
Assert.Ignore("This test requires DEBUG to be defined");
#endif
Dictionary<string, string> options = new Dictionary<string, string>(this.TestOptions)
{
// Choose a dblock size that is small enough so that more than one volume is needed.
["dblock-size"] = "10mb",
["disable-file-scanner"] = "true",
2025-01-28 08:55:28 +01:00
["concurrency-fileprocessors"] = "1"
2024-12-18 08:27:59 +01:00
};
2019-09-15 12:50:31 -07:00
// Run a complete backup.
2019-09-15 10:33:35 -07:00
using (Controller c = new Controller("file://" + this.TARGETFOLDER, options, null))
{
2024-04-18 11:49:39 +02:00
IBackupResults backupResults = c.Backup(new[] { this.DATAFOLDER });
Assert.AreEqual(0, backupResults.Errors.Count());
Assert.AreEqual(0, backupResults.Warnings.Count());
2019-09-15 10:33:35 -07:00
Assert.AreEqual(1, c.List().Filesets.Count());
2019-09-15 13:45:43 -07:00
Assert.AreEqual(BackupType.FULL_BACKUP, c.List().Filesets.Single(x => x.Version == 0).IsFullBackup);
2019-09-15 12:50:31 -07:00
}
2019-09-15 10:33:35 -07:00
// Run a partial backup.
2019-09-15 12:50:31 -07:00
using (Controller c = new Controller("file://" + this.TARGETFOLDER, options, null))
{
IBackupResults backupResults = await this.RunPartialBackup(c).ConfigureAwait(false);
Assert.AreEqual(0, backupResults.Errors.Count());
Assert.AreEqual(1, backupResults.Warnings.Count());
2024-12-18 08:27:59 +01:00
Assert.That(backupResults.ModifiedFiles, Is.GreaterThan(0), "No files were added, likely the stop was issued too early");
Assert.That(backupResults.ModifiedFiles, Is.LessThan(fileSizes.Length), "All files were added, likely the stop was issued too late");
2019-09-15 12:50:31 -07:00
2019-09-15 10:33:35 -07:00
// If we interrupt the backup, the most recent Fileset should be marked as partial.
Assert.AreEqual(2, c.List().Filesets.Count());
2019-09-15 13:45:43 -07:00
Assert.AreEqual(BackupType.FULL_BACKUP, c.List().Filesets.Single(x => x.Version == 1).IsFullBackup);
Assert.AreEqual(BackupType.PARTIAL_BACKUP, c.List().Filesets.Single(x => x.Version == 0).IsFullBackup);
2019-09-15 10:33:35 -07:00
}
2019-09-15 12:50:31 -07:00
2019-09-15 15:05:03 -07:00
// Restore files from the partial backup set.
2024-04-18 11:49:39 +02:00
Dictionary<string, string> restoreOptions = new Dictionary<string, string>(options) { ["restore-path"] = this.RESTOREFOLDER };
2019-09-15 15:05:03 -07:00
using (Controller c = new Controller("file://" + this.TARGETFOLDER, restoreOptions, null))
{
IListResults lastResults = c.List("*");
2019-09-15 15:30:28 -07:00
string[] partialVersionFiles = lastResults.Files.Select(x => x.Path).Where(x => !Utility.IsFolder(x, File.GetAttributes)).ToArray();
Assert.GreaterOrEqual(partialVersionFiles.Length, 1);
2019-09-15 15:05:03 -07:00
c.Restore(partialVersionFiles);
foreach (string filepath in partialVersionFiles)
{
2019-09-15 15:30:28 -07:00
string filename = Path.GetFileName(filepath);
2020-09-10 18:41:55 -07:00
TestUtils.AssertFilesAreEqual(filepath, Path.Combine(this.RESTOREFOLDER, filename ?? String.Empty), false, filename);
2019-09-15 15:05:03 -07:00
}
}
// Recreating the database should preserve the backup types.
File.Delete(this.DBFILE);
using (Controller c = new Controller("file://" + this.TARGETFOLDER, options, null))
{
IRepairResults repairResults = c.Repair();
Assert.AreEqual(0, repairResults.Errors.Count());
Assert.AreEqual(0, repairResults.Warnings.Count());
Assert.AreEqual(2, c.List().Filesets.Count());
2019-09-15 13:45:43 -07:00
Assert.AreEqual(BackupType.FULL_BACKUP, c.List().Filesets.Single(x => x.Version == 1).IsFullBackup);
Assert.AreEqual(BackupType.PARTIAL_BACKUP, c.List().Filesets.Single(x => x.Version == 0).IsFullBackup);
}
// Run a complete backup. Listing the Filesets should include both full and partial backups.
2019-09-15 12:50:31 -07:00
using (Controller c = new Controller("file://" + this.TARGETFOLDER, options, null))
{
2024-04-18 11:49:39 +02:00
IBackupResults backupResults = c.Backup(new[] { this.DATAFOLDER });
Assert.AreEqual(0, backupResults.Errors.Count());
Assert.AreEqual(0, backupResults.Warnings.Count());
Assert.AreEqual(3, c.List().Filesets.Count());
Assert.AreEqual(BackupType.FULL_BACKUP, c.List().Filesets.Single(x => x.Version == 2).IsFullBackup);
Assert.AreEqual(BackupType.PARTIAL_BACKUP, c.List().Filesets.Single(x => x.Version == 1).IsFullBackup);
2019-09-15 13:45:43 -07:00
Assert.AreEqual(BackupType.FULL_BACKUP, c.List().Filesets.Single(x => x.Version == 0).IsFullBackup);
2019-09-15 12:50:31 -07:00
}
2019-09-15 15:31:02 -07:00
// Restore files from the full backup set.
restoreOptions["overwrite"] = "true";
using (Controller c = new Controller("file://" + this.TARGETFOLDER, restoreOptions, null))
{
IListResults lastResults = c.List("*");
string[] fullVersionFiles = lastResults.Files.Select(x => x.Path).Where(x => !Utility.IsFolder(x, File.GetAttributes)).ToArray();
Assert.AreEqual(this.fileSizes.Length, fullVersionFiles.Length);
IRestoreResults restoreResults = c.Restore(fullVersionFiles);
Assert.AreEqual(0, restoreResults.Errors.Count());
Assert.AreEqual(0, restoreResults.Warnings.Count());
2019-09-15 15:31:02 -07:00
foreach (string filepath in fullVersionFiles)
{
string filename = Path.GetFileName(filepath);
2020-09-10 18:41:55 -07:00
TestUtils.AssertFilesAreEqual(filepath, Path.Combine(this.RESTOREFOLDER, filename ?? String.Empty), false, filename);
2019-09-15 15:31:02 -07:00
}
}
2019-09-15 10:33:35 -07:00
}
[Test]
[Category("Disruption")]
2020-02-29 16:33:32 -06:00
public async Task StopNow()
{
2024-12-18 08:27:59 +01:00
#if !DEBUG
Assert.Ignore("This test requires DEBUG to be defined");
#endif
// Choose a dblock size that is small enough so that more than one volume is needed.
2024-12-18 08:27:59 +01:00
Dictionary<string, string> options = new Dictionary<string, string>(this.TestOptions)
{
["dblock-size"] = "10mb",
["disable-synthetic-filelist"] = "true",
2025-01-28 08:55:28 +01:00
["disable-file-scanner"] = "true",
["concurrency-fileprocessors"] = "1",
2024-12-18 08:27:59 +01:00
};
// Run a complete backup.
using (Controller c = new Controller("file://" + this.TARGETFOLDER, options, null))
{
2024-04-18 11:49:39 +02:00
IBackupResults backupResults = c.Backup(new[] { this.DATAFOLDER });
Assert.AreEqual(0, backupResults.Errors.Count());
Assert.AreEqual(0, backupResults.Warnings.Count());
List<IListResultFileset> filesets = c.List().Filesets.ToList();
Assert.AreEqual(1, filesets.Count);
Assert.AreEqual(BackupType.FULL_BACKUP, filesets[0].IsFullBackup);
}
2024-12-18 08:27:59 +01:00
// Interrupt a backup with "abort".
this.ModifySourceFiles();
using (Controller c = new Controller("file://" + this.TARGETFOLDER, options, null))
{
// ReSharper disable once AccessToDisposedClosure
2024-04-18 11:49:39 +02:00
Task backupTask = Task.Run(() => c.Backup(new[] { this.DATAFOLDER }));
// Block for a small amount of time to allow the ITaskControl to be associated
// with the Controller. Otherwise, the call to Stop will simply be a no-op.
Thread.Sleep(1000);
2024-12-18 08:27:59 +01:00
c.Abort();
try
{
await backupTask.ConfigureAwait(false);
}
catch (TaskCanceledException)
{
}
}
// The next backup should proceed without issues.
using (Controller c = new Controller("file://" + this.TARGETFOLDER, options, null))
{
2024-04-18 11:49:39 +02:00
IBackupResults backupResults = c.Backup(new[] { this.DATAFOLDER });
Assert.AreEqual(0, backupResults.Errors.Count());
Assert.AreEqual(0, backupResults.Warnings.Count());
List<IListResultFileset> filesets = c.List().Filesets.ToList();
Assert.AreEqual(2, filesets.Count);
Assert.AreEqual(BackupType.FULL_BACKUP, filesets[1].IsFullBackup);
Assert.AreEqual(BackupType.FULL_BACKUP, filesets[0].IsFullBackup);
}
// Restore from the backup that followed the interruption.
2024-04-18 11:49:39 +02:00
Dictionary<string, string> restoreOptions = new Dictionary<string, string>(options) { ["restore-path"] = this.RESTOREFOLDER };
using (Controller c = new Controller("file://" + this.TARGETFOLDER, restoreOptions, null))
{
IListResults lastResults = c.List("*");
string[] fullVersionFiles = lastResults.Files.Select(x => x.Path).Where(x => !Utility.IsFolder(x, File.GetAttributes)).ToArray();
Assert.AreEqual(this.fileSizes.Length, fullVersionFiles.Length);
IRestoreResults restoreResults = c.Restore(fullVersionFiles);
Assert.AreEqual(0, restoreResults.Errors.Count());
Assert.AreEqual(0, restoreResults.Warnings.Count());
foreach (string filepath in fullVersionFiles)
{
string filename = Path.GetFileName(filepath);
2020-09-10 18:41:55 -07:00
TestUtils.AssertFilesAreEqual(filepath, Path.Combine(this.RESTOREFOLDER, filename ?? String.Empty), false, filename);
}
}
}
2025-02-20 11:00:28 +01:00
[Test]
[Category("Disruption")]
public void TestFailedUploadWithNoRetries()
{
var testopts = TestOptions;
testopts["number-of-retries"] = "0";
testopts["dblock-size"] = "10mb";
// Make a base backup
using (var c = new Library.Main.Controller("file://" + TARGETFOLDER, testopts, null))
{
IBackupResults backupResults = c.Backup(new string[] { DATAFOLDER });
Assert.AreEqual(0, backupResults.Errors.Count());
Assert.AreEqual(0, backupResults.Warnings.Count());
}
// Make a new backup that fails uploading a dblock file
ModifySourceFiles();
// Deterministic error backend
Library.DynamicLoader.BackendLoader.AddBackend(new DeterministicErrorBackend());
var failtarget = new DeterministicErrorBackend().ProtocolKey + "://" + TARGETFOLDER;
var hasFailed = false;
var secondUploadStarted = false;
var secondUploadCompleted = false;
// Fail the compact after the first dblock put is completed
DeterministicErrorBackend.ErrorGenerator = (DeterministicErrorBackend.BackendAction action, string remotename) =>
{
if (action.IsGetOperation)
{
return true;
}
if (!hasFailed && action == DeterministicErrorBackend.BackendAction.PutBefore)
{
// We only fail one upload, but there are no retries
hasFailed = true;
// Make sure we can start a second upload
Thread.Sleep(1000);
return true;
}
if (action == DeterministicErrorBackend.BackendAction.PutBefore)
secondUploadStarted = true;
if (action == DeterministicErrorBackend.BackendAction.PutAfter)
secondUploadCompleted = true;
return false;
};
using (var c = new Library.Main.Controller(failtarget, testopts, null))
Assert.Throws<DeterministicErrorBackend.DeterministicErrorBackendException>(() => c.Backup(new string[] { DATAFOLDER }));
Assert.That(secondUploadStarted, Is.True, "Second upload was not started");
Assert.That(secondUploadCompleted, Is.True, "Second upload was not started");
// Create a regular backup
using (var c = new Library.Main.Controller("file://" + TARGETFOLDER, testopts, null))
{
IBackupResults backupResults = c.Backup(new string[] { DATAFOLDER });
Assert.AreEqual(0, backupResults.Errors.Count());
Assert.AreEqual(0, backupResults.Warnings.Count());
}
// Verify that all is in order
using (var c = new Library.Main.Controller("file://" + TARGETFOLDER, testopts.Expand(new { full_remote_verification = true }), null))
{
var r = c.Test(long.MaxValue);
Assert.AreEqual(0, r.Errors.Count());
Assert.AreEqual(0, r.Warnings.Count());
Assert.IsFalse(r.Verifications.Any(p => p.Value.Any()));
}
// Test that we can recreate
var recreatedDatabaseFile = Path.Combine(BASEFOLDER, "recreated-database.sqlite");
if (File.Exists(recreatedDatabaseFile))
File.Delete(recreatedDatabaseFile);
testopts["dbpath"] = recreatedDatabaseFile;
using (var c = new Library.Main.Controller("file://" + TARGETFOLDER, testopts, null))
{
IRepairResults repairResults = c.Repair();
Assert.AreEqual(0, repairResults.Errors.Count());
Assert.AreEqual(0, repairResults.Warnings.Count());
}
// Check that we have 3 versions
using (var c = new Library.Main.Controller("file://" + TARGETFOLDER, testopts, null))
{
IListResults listResults = c.List();
Assert.AreEqual(0, listResults.Errors.Count());
Assert.AreEqual(0, listResults.Warnings.Count());
Assert.AreEqual(3, listResults.Filesets.Count());
}
}
2019-09-15 10:33:35 -07:00
}
}