Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CommandLineOperationsTests.cs « UnitTest « Duplicati - github.com/duplicati/duplicati.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f66cf408182cd3311d06edda6ed79425435b3bc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//  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.Reflection;

namespace Duplicati.UnitTest
{
    [TestFixture]
    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;
            }
        }

        [TestFixtureSetUp()]
        public override void PrepareSourceData()
        {
            base.PrepareSourceData();
        }

        [Test]
        [Category("BulkData")]
        public void RunCommands()
        {
            if (Directory.Exists(DATAFOLDER))
                PrepareSourceData();
            DoRunCommands(TARGETFOLDER);
        }

        [Test]
        [Category("BulkData")]
        public void RunCommandsWithoutSize()
        {
            if (Directory.Exists(DATAFOLDER))
                PrepareSourceData();
            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);
                Console.WriteLine("Adding folder {0} to source", foldername);
                TestUtils.CopyDirectoryRecursive(n, targetfolder);

                var size = Directory.EnumerateFiles(targetfolder, "*", SearchOption.AllDirectories).Select(x => new FileInfo(x).Length).Sum();

                Console.WriteLine("Running backup with {0} data added ...", Duplicati.Library.Utility.Utility.FormatSizeString(size));
                using(new Library.Logging.Timer(string.Format("Backup with {0} data added", Duplicati.Library.Utility.Utility.FormatSizeString(size))))
                    Duplicati.CommandLine.Program.RealMain(backupargs);
            }

            Console.WriteLine("Running unchanged backup ...");
            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();

            Console.WriteLine("Renaming folder {0}", Path.GetFileName(f));
            Directory.Move(f, Path.Combine(Path.GetDirectoryName(f), Path.GetFileName(f) + "-renamed"));

            Console.WriteLine("Running backup with renamed folder...");
            using(new Library.Logging.Timer("Backup with renamed folder"))
                Duplicati.CommandLine.Program.RealMain(backupargs);

            datafolders = Directory.EnumerateDirectories(DATAFOLDER);

            Console.WriteLine("Deleting data");
            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);

            Console.WriteLine("Running backup with deleted data...");
            using(new Library.Logging.Timer("Backup with deleted data"))
                Duplicati.CommandLine.Program.RealMain(backupargs);

            Console.WriteLine("Testing the compare method ...");
            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++)
            {
                Console.WriteLine("Running backup with changed logfile {0} of {1} ...", i + 1, 5);
                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);                
            }

            Console.WriteLine("Compacting data ...");
            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);

            Console.WriteLine("Partial restore of {0} ...", Path.GetFileName(rf));
            using(new Library.Logging.Timer("Partial restore"))
                Duplicati.CommandLine.Program.RealMain((new string[] { "restore", target, rf + "*", "--restore-path=\"" + RESTOREFOLDER + "\"" }.Union(opts)).ToArray());

            Console.WriteLine("Verifying partial restore ...");
            using (new Library.Logging.Timer("Verification of partial restored files"))
                TestUtils.VerifyDir(rf, RESTOREFOLDER, true);

            if (Directory.Exists(RESTOREFOLDER))
                Directory.Delete(RESTOREFOLDER, true);

            Console.WriteLine("Partial restore of {0} without local db...", Path.GetFileName(rf));
            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());

            Console.WriteLine("Verifying partial restore ...");
            using (new Library.Logging.Timer("Verification of partial restored files"))
                TestUtils.VerifyDir(rf, RESTOREFOLDER, true);
            
            if (Directory.Exists(RESTOREFOLDER))
                Directory.Delete(RESTOREFOLDER, true);

            Console.WriteLine("Full restore ...");
            using(new Library.Logging.Timer("Full restore"))
                Duplicati.CommandLine.Program.RealMain((new string[] { "restore", target, "*", "--restore-path=\"" + RESTOREFOLDER + "\"" }.Union(opts)).ToArray());

            Console.WriteLine("Verifying full restore ...");
            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);

            Console.WriteLine("Full restore without local db...");
            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());

            Console.WriteLine("Verifying full restore ...");
            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);
            
            Console.WriteLine("Testing data ...");
            using(new Library.Logging.Timer("Test remote data"))
                Duplicati.CommandLine.Program.RealMain((new string[] { "test", target, "all" }.Union(opts)).ToArray());

        }

        protected void DeleteExistingData()
        {
        }
    }
}