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
|
2018-05-27 13:27:24 -07:00
|
|
|
using Duplicati.Library.Interface;
|
2019-02-22 21:58:40 -06:00
|
|
|
using System;
|
2018-05-27 13:27:24 -07:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
2019-02-22 21:58:40 -06:00
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2015-04-10 22:29:52 +02:00
|
|
|
|
|
|
|
|
namespace Duplicati.UnitTest
|
|
|
|
|
{
|
|
|
|
|
public class RandomErrorBackend : IBackend, IStreamingBackend
|
2018-05-27 13:27:24 -07:00
|
|
|
{
|
|
|
|
|
static RandomErrorBackend() { WrappedBackend = "file"; }
|
|
|
|
|
|
2019-10-19 10:56:21 -07:00
|
|
|
private static readonly Random random = new Random(42);
|
2018-05-27 13:27:24 -07:00
|
|
|
|
|
|
|
|
public static string WrappedBackend { get; set; }
|
|
|
|
|
|
|
|
|
|
private IStreamingBackend m_backend;
|
2015-04-10 22:29:52 +02:00
|
|
|
public RandomErrorBackend()
|
|
|
|
|
{
|
2018-05-27 13:27:24 -07:00
|
|
|
}
|
|
|
|
|
|
2020-02-17 12:38:58 -08:00
|
|
|
// ReSharper disable once UnusedMember.Global
|
2018-05-27 13:27:24 -07:00
|
|
|
public RandomErrorBackend(string url, Dictionary<string, string> options)
|
|
|
|
|
{
|
|
|
|
|
var u = new Library.Utility.Uri(url).SetScheme(WrappedBackend).ToString();
|
|
|
|
|
m_backend = (IStreamingBackend)Library.DynamicLoader.BackendLoader.GetBackend(u, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ThrowErrorRandom()
|
|
|
|
|
{
|
|
|
|
|
if (random.NextDouble() > 0.90)
|
|
|
|
|
throw new Exception("Random upload failure");
|
|
|
|
|
}
|
|
|
|
|
#region IStreamingBackend implementation
|
2019-03-17 18:20:14 -05:00
|
|
|
public async Task PutAsync(string remotename, Stream stream, CancellationToken cancelToken)
|
2018-05-27 13:27:24 -07:00
|
|
|
{
|
|
|
|
|
var uploadError = random.NextDouble() > 0.9;
|
|
|
|
|
|
2018-10-06 13:30:13 -07:00
|
|
|
using (var f = new Library.Utility.ProgressReportingStream(stream, x => { if (uploadError && stream.Position > stream.Length / 2) throw new Exception("Random upload failure"); }))
|
2019-03-17 18:20:14 -05:00
|
|
|
await m_backend.PutAsync(remotename, f, cancelToken);
|
2018-05-27 13:27:24 -07:00
|
|
|
ThrowErrorRandom();
|
|
|
|
|
}
|
|
|
|
|
public void Get(string remotename, Stream stream)
|
|
|
|
|
{
|
|
|
|
|
ThrowErrorRandom();
|
|
|
|
|
m_backend.Get(remotename, stream);
|
|
|
|
|
ThrowErrorRandom();
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region IBackend implementation
|
|
|
|
|
public IEnumerable<IFileEntry> List()
|
|
|
|
|
{
|
|
|
|
|
return m_backend.List();
|
|
|
|
|
}
|
2019-03-17 18:20:14 -05:00
|
|
|
public async Task PutAsync(string remotename, string filename, CancellationToken cancelToken)
|
2018-05-27 13:27:24 -07:00
|
|
|
{
|
|
|
|
|
ThrowErrorRandom();
|
2019-03-17 18:20:14 -05:00
|
|
|
await m_backend.PutAsync(remotename, filename, cancelToken);
|
2018-05-27 13:27:24 -07:00
|
|
|
ThrowErrorRandom();
|
|
|
|
|
}
|
|
|
|
|
public void Get(string remotename, string filename)
|
|
|
|
|
{
|
|
|
|
|
ThrowErrorRandom();
|
|
|
|
|
m_backend.Get(remotename, filename);
|
|
|
|
|
ThrowErrorRandom();
|
|
|
|
|
}
|
|
|
|
|
public void Delete(string remotename)
|
|
|
|
|
{
|
|
|
|
|
ThrowErrorRandom();
|
|
|
|
|
m_backend.Delete(remotename);
|
|
|
|
|
ThrowErrorRandom();
|
|
|
|
|
}
|
|
|
|
|
public void Test()
|
|
|
|
|
{
|
|
|
|
|
m_backend.Test();
|
|
|
|
|
}
|
|
|
|
|
public void CreateFolder()
|
|
|
|
|
{
|
|
|
|
|
m_backend.CreateFolder();
|
|
|
|
|
}
|
2018-02-26 11:37:10 +01:00
|
|
|
public string[] DNSName
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return m_backend.DNSName;
|
|
|
|
|
}
|
2018-05-27 13:27:24 -07:00
|
|
|
}
|
|
|
|
|
public string DisplayName
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return "Random Error Backend";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public string ProtocolKey
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return "randomerror";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public IList<ICommandLineArgument> SupportedCommands
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (m_backend == null)
|
|
|
|
|
try { return Duplicati.Library.DynamicLoader.BackendLoader.GetSupportedCommands(WrappedBackend + "://"); }
|
|
|
|
|
catch { }
|
|
|
|
|
|
|
|
|
|
return m_backend.SupportedCommands;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public string Description
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return "A testing backend that randomly fails";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
#region IDisposable implementation
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
if (m_backend != null)
|
|
|
|
|
try { m_backend.Dispose(); }
|
|
|
|
|
finally { m_backend = null; }
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
2015-04-10 22:29:52 +02:00
|
|
|
}
|
|
|
|
|
|