# Conflicts: # .appveyor.yml # .github/workflows/tests.yml # BuildTools/AutoUpdateBuilder/AutoUpdateBuilder.csproj # BuildTools/DependencyFinder/DependencyFinder.csproj # BuildTools/GnupgSigningTool/GnupgSigningTool.csproj # BuildTools/UpdateVersionStamp/UpdateVersionStamp.csproj # Duplicati CommandLine Only.sln # Duplicati.Library.RestAPI/RESTMethods/RequestInfo.cs # Duplicati.Library.RestAPI/Runner.cs # Duplicati.sln # Duplicati/CommandLine/BackendTester/Duplicati.CommandLine.BackendTester.csproj # Duplicati/CommandLine/BackendTester/app.config # Duplicati/CommandLine/BackendTool/Duplicati.CommandLine.BackendTool.csproj # Duplicati/CommandLine/BackendTool/app.config # Duplicati/CommandLine/ConfigurationImporter/Duplicati.CommandLine.ConfigurationImporter.csproj # Duplicati/CommandLine/Duplicati.CommandLine.csproj # Duplicati/CommandLine/RecoveryTool/Duplicati.CommandLine.RecoveryTool.csproj # Duplicati/CommandLine/app.config # Duplicati/GUI/Duplicati.GUI.TrayIcon/Assets/icons/normal-warning.png # Duplicati/GUI/Duplicati.GUI.TrayIcon/Duplicati.GUI.TrayIcon.csproj # Duplicati/GUI/Duplicati.GUI.TrayIcon/Program.cs # Duplicati/GUI/Duplicati.GUI.TrayIcon/app.config # Duplicati/Library/AutoUpdater/Duplicati.Library.AutoUpdater.csproj # Duplicati/Library/Backend/AlternativeFTP/Duplicati.Library.Backend.AlternativeFTP.csproj # Duplicati/Library/Backend/AzureBlob/Duplicati.Library.Backend.AzureBlob.csproj # Duplicati/Library/Backend/Backblaze/Duplicati.Library.Backend.Backblaze.csproj # Duplicati/Library/Backend/Box/Duplicati.Library.Backend.Box.csproj # Duplicati/Library/Backend/CloudFiles/Duplicati.Library.Backend.CloudFiles.csproj # Duplicati/Library/Backend/Dropbox/Duplicati.Library.Backend.Dropbox.csproj # Duplicati/Library/Backend/FTP/Duplicati.Library.Backend.FTP.csproj # Duplicati/Library/Backend/File/Duplicati.Library.Backend.File.csproj # Duplicati/Library/Backend/GoogleServices/Duplicati.Library.Backend.GoogleServices.csproj # Duplicati/Library/Backend/HubiC/Duplicati.Library.Backend.HubiC.csproj # Duplicati/Library/Backend/Idrivee2/Duplicati.Library.Backend.Idrivee2.csproj # Duplicati/Library/Backend/Jottacloud/Duplicati.Library.Backend.Jottacloud.csproj # Duplicati/Library/Backend/Mega/Duplicati.Library.Backend.Mega.csproj # Duplicati/Library/Backend/OAuthHelper/Duplicati.Library.OAuthHelper.csproj # Duplicati/Library/Backend/OneDrive/Duplicati.Library.Backend.OneDrive.csproj # Duplicati/Library/Backend/OpenStack/Duplicati.Library.Backend.OpenStack.csproj # Duplicati/Library/Backend/Rclone/Duplicati.Library.Backend.Rclone.csproj # Duplicati/Library/Backend/S3/Duplicati.Library.Backend.S3.csproj # Duplicati/Library/Backend/SSHv2/Duplicati.Library.Backend.SSHv2.csproj # Duplicati/Library/Backend/SharePoint/Duplicati.Library.Backend.SharePoint.csproj # Duplicati/Library/Backend/Sia/Duplicati.Library.Backend.Sia.csproj # Duplicati/Library/Backend/Storj/Duplicati.Library.Backend.Storj.csproj # Duplicati/Library/Backend/TahoeLAFS/Duplicati.Library.Backend.TahoeLAFS.csproj # Duplicati/Library/Backend/Tardigrade/Duplicati.Library.Backend.Tardigrade.csproj # Duplicati/Library/Backend/TencentCOS/Duplicati.Library.Backend.TencentCOS.csproj # Duplicati/Library/Backend/WEBDAV/Duplicati.Library.Backend.WEBDAV.csproj # Duplicati/Library/Common/Duplicati.Library.Common.csproj # Duplicati/Library/Compression/Duplicati.Library.Compression.csproj # Duplicati/Library/DynamicLoader/Duplicati.Library.DynamicLoader.csproj # Duplicati/Library/Encryption/Duplicati.Library.Encryption.csproj # Duplicati/Library/Interface/Duplicati.Library.Interface.csproj # Duplicati/Library/Localization/Duplicati.Library.Localization.csproj # Duplicati/Library/Logging/Duplicati.Library.Logging.csproj # Duplicati/Library/Main/Duplicati.Library.Main.csproj # Duplicati/Library/Modules/Builtin/CheckMonoSSL.cs # Duplicati/Library/Modules/Builtin/Duplicati.Library.Modules.Builtin.csproj # Duplicati/Library/Modules/Builtin/SendJabberMessage.cs # Duplicati/Library/SQLiteHelper/Duplicati.Library.SQLiteHelper.csproj # Duplicati/Library/Snapshots/Duplicati.Library.Snapshots.csproj # Duplicati/Library/UsageReporter/Duplicati.Library.UsageReporter.csproj # Duplicati/Library/Utility/Duplicati.Library.Utility.csproj # Duplicati/Library/Utility/HashAlgorithmHelper.cs # Duplicati/Library/Utility/Power/WindowsPowerSupplyState.cs # Duplicati/License/Duplicati.License.csproj # Duplicati/Server/Duplicati.Server.Serialization/Duplicati.Server.Serialization.csproj # Duplicati/Server/Duplicati.Server.csproj # Duplicati/Server/app.config # Duplicati/Service/Duplicati.Service.csproj # Duplicati/Service/app.config # Duplicati/Tools/Duplicati.Tools.csproj # Duplicati/UnitTest/Duplicati.UnitTest.csproj # Duplicati/UnitTest/GeneralBlackBoxTesting.cs # Duplicati/UnitTest/app.config # Duplicati/WindowsService/WindowsService.csproj # Installer/debian/docker/debian/control # Tools/SQLiteTool/SQLiteTool.csproj # Tools/ZipFileDebugger/ZipFileDebugger.csproj # guiTests/guiTest.py # thirdparty/UnixSupport/UnixSupport.csproj # thirdparty/alphavss/Homepage.txt # thirdparty/alphavss/License.txt # thirdparty/alphavss/licensedata.json
281 lines
14 KiB
C#
281 lines
14 KiB
C#
// Copyright (C) 2015, The Duplicati Team
|
|
// http://www.duplicati.com, info@duplicati.com
|
|
//
|
|
// This library is free software; you can redistribute it and/or modify
|
|
// it under the terms of the GNU Lesser General Public License as
|
|
// published by the Free Software Foundation; either version 2.1 of the
|
|
// License, or (at your option) any later version.
|
|
//
|
|
// This library is distributed in the hope that it will be useful, but
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
// Lesser General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
// License along with this library; if not, write to the Free Software
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
using System;
|
|
using NUnit.Framework;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net;
|
|
|
|
namespace Duplicati.UnitTest
|
|
{
|
|
public class CommandLineOperationsTests : BasicSetupHelper
|
|
{
|
|
private static readonly string S3_URL = $"https://testfiles.duplicati.com/";
|
|
|
|
/// <summary>
|
|
/// The log tag
|
|
/// </summary>
|
|
private static readonly string LOGTAG = Library.Logging.Log.LogTagFromType<CommandLineOperationsTests>();
|
|
|
|
/// <summary>
|
|
/// The folder that contains all the source data which the test is based on
|
|
/// </summary>
|
|
protected readonly string SOURCEFOLDER = Path.Combine(BASEFOLDER, "data");
|
|
|
|
private readonly string zipFilename = "data.zip";
|
|
private string zipFilepath => Path.Combine(BASEFOLDER, this.zipFilename);
|
|
|
|
private readonly string zipAlternativeFilename = "data-alternative.zip";
|
|
private string zipAlternativeFilepath => Path.Combine(BASEFOLDER, this.zipAlternativeFilename);
|
|
|
|
protected virtual IEnumerable<string> SourceDataFolders
|
|
{
|
|
get
|
|
{
|
|
return
|
|
from x in systemIO.EnumerateDirectories(SOURCEFOLDER)
|
|
orderby x
|
|
select x;
|
|
}
|
|
}
|
|
|
|
[SetUp]
|
|
public void SetUp()
|
|
{
|
|
if (!systemIO.FileExists(zipAlternativeFilepath))
|
|
{
|
|
var url = $"{S3_URL}{this.zipFilename}";
|
|
DownloadS3FileIfNewer(zipFilepath, url);
|
|
ZipFileExtractToDirectory(this.zipFilepath, BASEFOLDER);
|
|
}
|
|
else
|
|
{
|
|
ZipFileExtractToDirectory(this.zipAlternativeFilepath, BASEFOLDER);
|
|
}
|
|
}
|
|
|
|
private void DownloadS3FileIfNewer(string destinationFilePath, string url)
|
|
{
|
|
var webRequest = (HttpWebRequest)WebRequest.Create(url);
|
|
|
|
if (systemIO.FileExists(destinationFilePath))
|
|
{
|
|
webRequest.IfModifiedSince = systemIO.FileGetLastWriteTimeUtc(destinationFilePath);
|
|
}
|
|
|
|
try
|
|
{
|
|
// check if the file should be downloaded, exception if not
|
|
using (var wr = (HttpWebResponse)webRequest.GetResponse())
|
|
|
|
using (WebClient client = new WebClient())
|
|
{ // try to workaround a weird intermittent bug where downloaded size
|
|
// differs from real size but no error is thrown
|
|
Console.WriteLine("downloading test file to: {0}, length: {1}", destinationFilePath, wr.ContentLength);
|
|
var maxAttempts = 5;
|
|
while (maxAttempts-- > 0) {
|
|
DateTime beginTime = DateTime.Now;
|
|
client.DownloadFile(url, destinationFilePath);
|
|
long length = new System.IO.FileInfo(destinationFilePath).Length;
|
|
Console.WriteLine("downloaded test file: {0}: length {1}, duration {2}", destinationFilePath, length, (DateTime.Now - beginTime).TotalSeconds);
|
|
if (length == wr.ContentLength) {
|
|
maxAttempts = -1;
|
|
} else {
|
|
Console.WriteLine("invalid downloaded length {0}, should be {1}...", length, wr.ContentLength);
|
|
System.Threading.Thread.Sleep(120000);
|
|
}
|
|
}
|
|
if (maxAttempts == 0) {
|
|
throw new Exception(string.Format("Unable to download test file from {0}", url));
|
|
}
|
|
}
|
|
}
|
|
catch (WebException ex)
|
|
{
|
|
if (ex.Response == null){
|
|
Console.WriteLine("ex.Response is null !");
|
|
throw;
|
|
}
|
|
|
|
if (((HttpWebResponse) ex.Response).StatusCode != HttpStatusCode.NotModified)
|
|
{
|
|
Console.WriteLine("file to download {0} already exists and is up to date", destinationFilePath);
|
|
throw;
|
|
}
|
|
Console.WriteLine("download exception: {0}", ex.Response);
|
|
}
|
|
}
|
|
|
|
[OneTimeTearDown]
|
|
public void OneTimeTearDown()
|
|
{
|
|
if (systemIO.DirectoryExists(this.SOURCEFOLDER))
|
|
{
|
|
systemIO.DirectoryDelete(this.SOURCEFOLDER, true);
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
[Category("BulkData")]
|
|
[Category("BulkNormal")]
|
|
public void RunCommands()
|
|
{
|
|
DoRunCommands(TARGETFOLDER);
|
|
}
|
|
|
|
[Test]
|
|
[Category("BulkData")]
|
|
[Category("BulkNoSize")]
|
|
public void RunCommandsWithoutSize()
|
|
{
|
|
DoRunCommands(new SizeOmittingBackend().ProtocolKey + "://" + TARGETFOLDER);
|
|
}
|
|
|
|
private void DoRunCommands(string target)
|
|
{
|
|
var opts = from n in TestOptions select string.Format("--{0}=\"{1}\"", n.Key, n.Value);
|
|
var backupargs = (new string[] { "backup", target, DATAFOLDER }.Union(opts)).ToArray();
|
|
|
|
if (SourceDataFolders == null || SourceDataFolders.Count() < 3)
|
|
{
|
|
ProgressWriteLine($"ERROR: A minimum of 3 data folders are required in {SOURCEFOLDER}.");
|
|
throw new Exception("Failed during initial minimum data folder check");
|
|
}
|
|
|
|
foreach (var n in SourceDataFolders)
|
|
{
|
|
var foldername = Path.GetFileName(n);
|
|
var targetfolder = Path.Combine(DATAFOLDER, foldername);
|
|
ProgressWriteLine("Adding folder {0} to source", foldername);
|
|
|
|
systemIO.DirectoryMove(n, targetfolder);
|
|
|
|
var size = systemIO.EnumerateFiles(targetfolder, "*", SearchOption.AllDirectories).Select(systemIO.FileLength).Sum();
|
|
|
|
ProgressWriteLine("Running backup with {0} data added ...", Duplicati.Library.Utility.Utility.FormatSizeString(size));
|
|
using (new Library.Logging.Timer(LOGTAG, "BackupWithDataAdded", string.Format("Backup with {0} data added", Duplicati.Library.Utility.Utility.FormatSizeString(size))))
|
|
Duplicati.CommandLine.Program.RealMain(backupargs);
|
|
|
|
ProgressWriteLine("Testing data ...");
|
|
using (new Library.Logging.Timer(LOGTAG, "TestRemoteData", "Test remote data"))
|
|
if (Duplicati.CommandLine.Program.RealMain((new string[] { "test", target, "all" }.Union(opts)).ToArray()) != 0)
|
|
throw new Exception("Failed during remote verification");
|
|
}
|
|
|
|
ProgressWriteLine("Running unchanged backup ...");
|
|
using (new Library.Logging.Timer(LOGTAG, "UnchangedBackup", "Unchanged backup"))
|
|
Duplicati.CommandLine.Program.RealMain(backupargs);
|
|
|
|
var datafolders = systemIO.EnumerateDirectories(DATAFOLDER);
|
|
|
|
var f = datafolders.Skip(datafolders.Count() / 2).First();
|
|
|
|
ProgressWriteLine("Renaming folder {0}", Path.GetFileName(f));
|
|
systemIO.DirectoryMove(f, Path.Combine(Path.GetDirectoryName(f), Path.GetFileName(f) + "-renamed"));
|
|
|
|
ProgressWriteLine("Running backup with renamed folder...");
|
|
using (new Library.Logging.Timer(LOGTAG, "BackupWithRenamedFolder", "Backup with renamed folder"))
|
|
Duplicati.CommandLine.Program.RealMain(backupargs);
|
|
|
|
datafolders = systemIO.EnumerateDirectories(DATAFOLDER);
|
|
|
|
ProgressWriteLine("Deleting data");
|
|
var rm1 = datafolders.First();
|
|
var rm2 = datafolders.Skip(1).First();
|
|
var rm3 = datafolders.Skip(2).First();
|
|
|
|
systemIO.DirectoryDelete(rm1, true);
|
|
systemIO.DirectoryDelete(rm2, true);
|
|
var rmfiles = systemIO.EnumerateFiles(rm3, "*", SearchOption.AllDirectories);
|
|
foreach (var n in rmfiles.Take(rmfiles.Count() / 2))
|
|
systemIO.FileDelete(n);
|
|
|
|
ProgressWriteLine("Running backup with deleted data...");
|
|
using (new Library.Logging.Timer(LOGTAG, "BackupWithDeletedData", "Backup with deleted data"))
|
|
Duplicati.CommandLine.Program.RealMain(backupargs);
|
|
|
|
ProgressWriteLine("Testing the compare method ...");
|
|
using (new Library.Logging.Timer(LOGTAG, "CompareMethod", "Compare method"))
|
|
Duplicati.CommandLine.Program.RealMain((new string[] { "compare", target, "0", "1" }.Union(opts)).ToArray());
|
|
|
|
for (var i = 0; i < 5; i++)
|
|
{
|
|
ProgressWriteLine("Running backup with changed logfile {0} of {1} ...", i + 1, 5);
|
|
systemIO.FileCopy(LOGFILE, Path.Combine(SOURCEFOLDER, Path.GetFileName(LOGFILE)), true);
|
|
|
|
using (new Library.Logging.Timer(LOGTAG, "BackupWithLogfileChange", string.Format("Backup with logfilechange {0}", i + 1)))
|
|
Duplicati.CommandLine.Program.RealMain(backupargs);
|
|
}
|
|
|
|
ProgressWriteLine("Compacting data ...");
|
|
using (new Library.Logging.Timer(LOGTAG, "Compacting", "Compacting"))
|
|
Duplicati.CommandLine.Program.RealMain((new string[] { "compact", target, "--small-file-max-count=2" }.Union(opts)).ToArray());
|
|
|
|
|
|
datafolders = systemIO.EnumerateDirectories(DATAFOLDER);
|
|
var rf = datafolders.Skip(datafolders.Count() - 2).First();
|
|
|
|
ProgressWriteLine("Partial restore of {0} ...", Path.GetFileName(rf));
|
|
using (new Library.Logging.Timer(LOGTAG, "PartialRestore", "Partial restore"))
|
|
Duplicati.CommandLine.Program.RealMain((new string[] { "restore", target, rf + "*", "--restore-path=\"" + RESTOREFOLDER + "\"" }.Union(opts)).ToArray());
|
|
|
|
ProgressWriteLine("Verifying partial restore ...");
|
|
using (new Library.Logging.Timer(LOGTAG, "VerificationOfPartialRestore", "Verification of partial restored files"))
|
|
TestUtils.AssertDirectoryTreesAreEquivalent(rf, RESTOREFOLDER, true, "VerificationOfPartialRestore");
|
|
|
|
systemIO.DirectoryDelete(RESTOREFOLDER, true);
|
|
|
|
ProgressWriteLine("Partial restore of {0} without local db...", Path.GetFileName(rf));
|
|
using (new Library.Logging.Timer(LOGTAG, "PartialRestoreWithoutLocalDb", "Partial restore without local db"))
|
|
Duplicati.CommandLine.Program.RealMain((new string[] { "restore", target, rf + "*", "--restore-path=\"" + RESTOREFOLDER + "\"", "--no-local-db" }.Union(opts)).ToArray());
|
|
|
|
ProgressWriteLine("Verifying partial restore ...");
|
|
using (new Library.Logging.Timer(LOGTAG, "VerificationOfPartialRestore", "Verification of partial restored files"))
|
|
TestUtils.AssertDirectoryTreesAreEquivalent(rf, RESTOREFOLDER, true, "VerificationOfPartialRestore");
|
|
|
|
systemIO.DirectoryDelete(RESTOREFOLDER, true);
|
|
|
|
ProgressWriteLine("Full restore ...");
|
|
using (new Library.Logging.Timer(LOGTAG, "FullRestore", "Full restore"))
|
|
Duplicati.CommandLine.Program.RealMain((new string[] { "restore", target, "*", "--restore-path=\"" + RESTOREFOLDER + "\"" }.Union(opts)).ToArray());
|
|
|
|
ProgressWriteLine("Verifying full restore ...");
|
|
using (new Library.Logging.Timer(LOGTAG, "VerificationOfFullRestore", "Verification of restored files"))
|
|
foreach (var s in systemIO.EnumerateDirectories(DATAFOLDER))
|
|
TestUtils.AssertDirectoryTreesAreEquivalent(s, Path.Combine(RESTOREFOLDER, Path.GetFileName(s)), true, "VerificationOfFullRestore");
|
|
|
|
systemIO.DirectoryDelete(RESTOREFOLDER, true);
|
|
|
|
ProgressWriteLine("Full restore without local db...");
|
|
using (new Library.Logging.Timer(LOGTAG, "FullRestoreWithoutDb", "Full restore without local db"))
|
|
Duplicati.CommandLine.Program.RealMain((new string[] { "restore", target, "*", "--restore-path=\"" + RESTOREFOLDER + "\"", "--no-local-db" }.Union(opts)).ToArray());
|
|
|
|
ProgressWriteLine("Verifying full restore ...");
|
|
using (new Library.Logging.Timer(LOGTAG, "VerificationOfFullRestoreWithoutDb", "Verification of restored files"))
|
|
foreach (var s in systemIO.EnumerateDirectories(DATAFOLDER))
|
|
TestUtils.AssertDirectoryTreesAreEquivalent(s, Path.Combine(RESTOREFOLDER, Path.GetFileName(s)), true, "VerificationOfFullRestoreWithoutDb");
|
|
|
|
ProgressWriteLine("Testing data ...");
|
|
using (new Library.Logging.Timer(LOGTAG, "TestRemoteData", "Test remote data"))
|
|
if (Duplicati.CommandLine.Program.RealMain((new string[] { "test", target, "all" }.Union(opts)).ToArray()) != 0)
|
|
throw new Exception("Failed during final remote verification");
|
|
}
|
|
}
|
|
}
|
|
|