2015-08-05 13:09:01 +02:00
// Copyright (C) 2015, The Duplicati Team
2015-04-10 22:29:52 +02:00
// 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
2015-09-17 20:04:49 +02:00
using System ;
using NUnit.Framework ;
using System.Collections.Generic ;
using System.IO ;
using System.Linq ;
using System.Reflection ;
2015-04-10 22:29:52 +02:00
namespace Duplicati.UnitTest
{
2015-09-17 20:04:49 +02:00
public class CommandLineOperationsTests : BasicSetupHelper
{
/// <summary>
/// The folder that contains all the source data which the test is based on
/// </summary>
protected readonly string SOURCEFOLDER = Path . Combine ( BASEFOLDER , "data" );
protected virtual IEnumerable < string > SourceDataFolders
{
get
{
return
from x in Directory . EnumerateDirectories ( SOURCEFOLDER )
orderby x
select x ;
}
}
2017-09-23 14:52:53 +02:00
[OneTimeSetUp]
2015-09-17 20:04:49 +02:00
public override void PrepareSourceData ()
{
base . PrepareSourceData ();
}
[Test]
2016-09-15 11:39:27 +02:00
[Category("BulkData")]
2016-09-29 13:45:55 +02:00
[Category("BulkNormal")]
2016-09-15 11:39:27 +02:00
public void RunCommands ()
2015-09-17 20:04:49 +02:00
{
2016-01-26 23:30:03 +01:00
if ( Directory . Exists ( DATAFOLDER ))
PrepareSourceData ();
2015-09-17 20:04:49 +02:00
DoRunCommands ( TARGETFOLDER );
}
[Test]
2016-09-15 11:39:27 +02:00
[Category("BulkData")]
2016-09-29 13:45:55 +02:00
[Category("BulkNoSize")]
2016-09-15 11:39:27 +02:00
public void RunCommandsWithoutSize ()
2015-09-17 20:04:49 +02:00
{
2016-01-26 23:30:03 +01:00
if ( Directory . Exists ( DATAFOLDER ))
PrepareSourceData ();
2015-09-17 20:04:49 +02:00
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 ();
foreach ( var n in SourceDataFolders )
{
var foldername = Path . GetFileName ( n );
var targetfolder = Path . Combine ( DATAFOLDER , foldername );
2016-09-29 13:45:55 +02:00
ProgressWriteLine ( "Adding folder {0} to source" , foldername );
2015-09-17 20:04:49 +02:00
TestUtils . CopyDirectoryRecursive ( n , targetfolder );
var size = Directory . EnumerateFiles ( targetfolder , "*" , SearchOption . AllDirectories ). Select ( x => new FileInfo ( x ). Length ). Sum ();
2016-09-29 13:45:55 +02:00
ProgressWriteLine ( "Running backup with {0} data added ..." , Duplicati . Library . Utility . Utility . FormatSizeString ( size ));
2015-09-17 20:04:49 +02:00
using ( new Library . Logging . Timer ( string . Format ( "Backup with {0} data added" , Duplicati . Library . Utility . Utility . FormatSizeString ( size ))))
Duplicati . CommandLine . Program . RealMain ( backupargs );
}
2016-09-29 13:45:55 +02:00
ProgressWriteLine ( "Running unchanged backup ..." );
2015-09-17 20:04:49 +02:00
using ( new Library . Logging . Timer ( "Unchanged backup" ))
Duplicati . CommandLine . Program . RealMain ( backupargs );
var datafolders = Directory . EnumerateDirectories ( DATAFOLDER );
var f = datafolders . Skip ( datafolders . Count () / 2 ). First ();
2016-09-29 13:45:55 +02:00
ProgressWriteLine ( "Renaming folder {0}" , Path . GetFileName ( f ));
2015-09-17 20:04:49 +02:00
Directory . Move ( f , Path . Combine ( Path . GetDirectoryName ( f ), Path . GetFileName ( f ) + "-renamed" ));
2016-09-29 13:45:55 +02:00
ProgressWriteLine ( "Running backup with renamed folder..." );
2015-09-17 20:04:49 +02:00
using ( new Library . Logging . Timer ( "Backup with renamed folder" ))
Duplicati . CommandLine . Program . RealMain ( backupargs );
datafolders = Directory . EnumerateDirectories ( DATAFOLDER );
2016-09-29 13:45:55 +02:00
ProgressWriteLine ( "Deleting data" );
2015-09-17 20:04:49 +02:00
var rm1 = datafolders . First ();
var rm2 = datafolders . Skip ( 1 ). First ();
var rm3 = datafolders . Skip ( 2 ). First ();
Directory . Delete ( rm1 , true );
Directory . Delete ( rm2 , true );
var rmfiles = Directory . EnumerateFiles ( rm3 , "*" , SearchOption . AllDirectories );
foreach ( var n in rmfiles . Take ( rmfiles . Count () / 2 ))
File . Delete ( n );
2016-09-29 13:45:55 +02:00
ProgressWriteLine ( "Running backup with deleted data..." );
2015-09-17 20:04:49 +02:00
using ( new Library . Logging . Timer ( "Backup with deleted data" ))
Duplicati . CommandLine . Program . RealMain ( backupargs );
2016-09-29 13:45:55 +02:00
ProgressWriteLine ( "Testing the compare method ..." );
2015-09-17 20:04:49 +02:00
using ( new Library . Logging . Timer ( "Compare method" ))
Duplicati . CommandLine . Program . RealMain (( new string [] { "compare" , target , "0" , "1" }. Union ( opts )). ToArray ());
for ( var i = 0 ; i < 5 ; i ++)
{
2016-09-29 13:45:55 +02:00
ProgressWriteLine ( "Running backup with changed logfile {0} of {1} ..." , i + 1 , 5 );
2015-09-17 20:04:49 +02:00
File . Copy ( LOGFILE , Path . Combine ( SOURCEFOLDER , Path . GetFileName ( LOGFILE )), true );
using ( new Library . Logging . Timer ( string . Format ( "Backup with logfilechange {0}" , i + 1 )))
Duplicati . CommandLine . Program . RealMain ( backupargs );
}
2018-01-31 22:03:37 +01:00
ProgressWriteLine ( "Checking remote file sizes ..." );
string maxsizestr ;
TestOptions . TryGetValue ( "dblock-size" , out maxsizestr );
if ( string . IsNullOrWhiteSpace ( maxsizestr ))
maxsizestr = "50mb" ;
var maxsizeval = Library . Utility . Sizeparser . ParseSize ( maxsizestr , "mb" );
foreach ( var n in System . IO . Directory . EnumerateFiles ( TARGETFOLDER ))
if ( new FileInfo ( n ). Length > maxsizeval )
Library . Logging . Log . WriteMessage ( string . Format ( "File {0} is {1} larger than allowed volume size" , n , Library . Utility . Utility . FormatSizeString ( new FileInfo ( n ). Length - maxsizeval )), Library . Logging . LogMessageType . Warning );
2016-09-29 13:45:55 +02:00
ProgressWriteLine ( "Compacting data ..." );
2015-09-17 20:04:49 +02:00
using ( new Library . Logging . Timer ( "Compacting" ))
Duplicati . CommandLine . Program . RealMain (( new string [] { "compact" , target , "--small-file-max-count=2" }. Union ( opts )). ToArray ());
datafolders = Directory . EnumerateDirectories ( DATAFOLDER );
var rf = datafolders . Skip ( datafolders . Count () - 2 ). First ();
if ( Directory . Exists ( RESTOREFOLDER ))
Directory . Delete ( RESTOREFOLDER , true );
2016-09-29 13:45:55 +02:00
ProgressWriteLine ( "Partial restore of {0} ..." , Path . GetFileName ( rf ));
2015-09-17 20:04:49 +02:00
using ( new Library . Logging . Timer ( "Partial restore" ))
Duplicati . CommandLine . Program . RealMain (( new string [] { "restore" , target , rf + "*" , "--restore-path=\"" + RESTOREFOLDER + "\"" }. Union ( opts )). ToArray ());
2016-09-29 13:45:55 +02:00
ProgressWriteLine ( "Verifying partial restore ..." );
2015-09-17 20:04:49 +02:00
using ( new Library . Logging . Timer ( "Verification of partial restored files" ))
TestUtils . VerifyDir ( rf , RESTOREFOLDER , true );
if ( Directory . Exists ( RESTOREFOLDER ))
Directory . Delete ( RESTOREFOLDER , true );
2016-09-29 13:45:55 +02:00
ProgressWriteLine ( "Partial restore of {0} without local db..." , Path . GetFileName ( rf ));
2015-09-17 20:04:49 +02:00
using ( new Library . Logging . Timer ( "Partial restore without local db" ))
Duplicati . CommandLine . Program . RealMain (( new string [] { "restore" , target , rf + "*" , "--restore-path=\"" + RESTOREFOLDER + "\"" , "--no-local-db" }. Union ( opts )). ToArray ());
2016-09-29 13:45:55 +02:00
ProgressWriteLine ( "Verifying partial restore ..." );
2015-09-17 20:04:49 +02:00
using ( new Library . Logging . Timer ( "Verification of partial restored files" ))
TestUtils . VerifyDir ( rf , RESTOREFOLDER , true );
if ( Directory . Exists ( RESTOREFOLDER ))
Directory . Delete ( RESTOREFOLDER , true );
2016-09-29 13:45:55 +02:00
ProgressWriteLine ( "Full restore ..." );
2015-09-17 20:04:49 +02:00
using ( new Library . Logging . Timer ( "Full restore" ))
Duplicati . CommandLine . Program . RealMain (( new string [] { "restore" , target , "*" , "--restore-path=\"" + RESTOREFOLDER + "\"" }. Union ( opts )). ToArray ());
2016-09-29 13:45:55 +02:00
ProgressWriteLine ( "Verifying full restore ..." );
2015-09-17 20:04:49 +02:00
using ( new Library . Logging . Timer ( "Verification of restored files" ))
foreach ( var s in Directory . EnumerateDirectories ( DATAFOLDER ))
TestUtils . VerifyDir ( s , Path . Combine ( RESTOREFOLDER , Path . GetFileName ( s )), true );
if ( Directory . Exists ( RESTOREFOLDER ))
Directory . Delete ( RESTOREFOLDER , true );
2016-09-29 13:45:55 +02:00
ProgressWriteLine ( "Full restore without local db..." );
2015-09-17 20:04:49 +02:00
using ( new Library . Logging . Timer ( "Full restore without local db" ))
Duplicati . CommandLine . Program . RealMain (( new string [] { "restore" , target , "*" , "--restore-path=\"" + RESTOREFOLDER + "\"" , "--no-local-db" }. Union ( opts )). ToArray ());
2016-09-29 13:45:55 +02:00
ProgressWriteLine ( "Verifying full restore ..." );
2015-09-17 20:04:49 +02:00
using ( new Library . Logging . Timer ( "Verification of restored files" ))
foreach ( var s in Directory . EnumerateDirectories ( DATAFOLDER ))
TestUtils . VerifyDir ( s , Path . Combine ( RESTOREFOLDER , Path . GetFileName ( s )), true );
2016-09-29 13:45:55 +02:00
ProgressWriteLine ( "Testing data ..." );
2017-03-07 23:09:04 +01:00
using ( new Library . Logging . Timer ( "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" );
2015-09-17 20:04:49 +02:00
}
protected void DeleteExistingData ()
{
}
2015-04-10 22:29:52 +02:00
}
}