2011-11-13 05:07:06 +01:00
|
|
|
|
using System.IO;
|
|
|
|
|
using AutoMoq;
|
2011-10-23 20:31:17 +02:00
|
|
|
|
using FizzWare.NBuilder;
|
2011-10-23 07:26:43 +02:00
|
|
|
|
using Moq;
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using NzbDrone.Common;
|
2011-10-23 20:31:17 +02:00
|
|
|
|
using NzbDrone.Common.Model;
|
2011-11-13 19:16:31 +01:00
|
|
|
|
using NzbDrone.Test.Common;
|
2011-10-23 07:26:43 +02:00
|
|
|
|
using NzbDrone.Update.Providers;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Update.Test
|
|
|
|
|
{
|
|
|
|
|
[TestFixture]
|
2011-11-13 19:16:31 +01:00
|
|
|
|
class UpdateProviderStartTest : TestBase
|
2011-10-23 07:26:43 +02:00
|
|
|
|
{
|
|
|
|
|
AutoMoqer mocker = new AutoMoqer();
|
|
|
|
|
|
2011-11-13 06:19:19 +01:00
|
|
|
|
private const string UPDATE_FOLDER = @"C:\Temp\nzbdrone_update\nzbdrone\";
|
|
|
|
|
private const string BACKUP_FOLDER = @"C:\Temp\nzbdrone_update\nzbdrone_backup\";
|
2011-11-13 05:07:06 +01:00
|
|
|
|
private const string TARGET_FOLDER = @"C:\NzbDrone\";
|
|
|
|
|
|
2011-11-13 08:27:16 +01:00
|
|
|
|
Mock<EnviromentProvider> _enviromentProvider;
|
2011-11-13 05:07:06 +01:00
|
|
|
|
|
|
|
|
|
|
2011-10-23 07:26:43 +02:00
|
|
|
|
[SetUp]
|
|
|
|
|
public void Setup()
|
|
|
|
|
{
|
|
|
|
|
mocker = new AutoMoqer();
|
2011-11-13 06:19:19 +01:00
|
|
|
|
|
2011-11-13 08:27:16 +01:00
|
|
|
|
_enviromentProvider = mocker.GetMock<EnviromentProvider>();
|
2011-11-13 05:07:06 +01:00
|
|
|
|
|
2011-11-13 08:27:16 +01:00
|
|
|
|
_enviromentProvider.SetupGet(c => c.SystemTemp).Returns(@"C:\Temp\");
|
2011-11-13 05:07:06 +01:00
|
|
|
|
|
|
|
|
|
mocker.GetMock<DiskProvider>()
|
|
|
|
|
.Setup(c => c.FolderExists(UPDATE_FOLDER))
|
|
|
|
|
.Returns(true);
|
|
|
|
|
|
|
|
|
|
mocker.GetMock<DiskProvider>()
|
|
|
|
|
.Setup(c => c.FolderExists(TARGET_FOLDER))
|
|
|
|
|
.Returns(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void WithInstalledService()
|
|
|
|
|
{
|
|
|
|
|
mocker.GetMock<ServiceProvider>()
|
|
|
|
|
.Setup(c => c.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME))
|
|
|
|
|
.Returns(true);
|
2011-10-23 07:26:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2011-10-23 20:31:17 +02:00
|
|
|
|
public void should_stop_nzbdrone_service_if_installed()
|
2011-10-23 07:26:43 +02:00
|
|
|
|
{
|
2011-11-13 05:07:06 +01:00
|
|
|
|
WithInstalledService();
|
2011-10-23 07:26:43 +02:00
|
|
|
|
|
|
|
|
|
//Act
|
2011-11-13 05:07:06 +01:00
|
|
|
|
mocker.Resolve<UpdateProvider>().Start(TARGET_FOLDER);
|
2011-10-23 07:26:43 +02:00
|
|
|
|
|
|
|
|
|
//Assert
|
2011-10-26 19:15:47 +02:00
|
|
|
|
mocker.GetMock<ServiceProvider>().Verify(c => c.Stop(ServiceProvider.NZBDRONE_SERVICE_NAME), Times.Once());
|
2011-10-23 07:26:43 +02:00
|
|
|
|
mocker.VerifyAllMocks();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2011-10-23 20:31:17 +02:00
|
|
|
|
public void should_kill_nzbdrone_process_if_running()
|
2011-10-23 07:26:43 +02:00
|
|
|
|
{
|
2011-10-23 20:31:17 +02:00
|
|
|
|
var proccesses = Builder<ProcessInfo>.CreateListOfSize(2).Build();
|
|
|
|
|
|
|
|
|
|
mocker.GetMock<ProcessProvider>()
|
|
|
|
|
.Setup(c => c.GetProcessByName(ProcessProvider.NzbDroneProccessName))
|
|
|
|
|
.Returns(proccesses);
|
2011-10-23 07:26:43 +02:00
|
|
|
|
|
|
|
|
|
//Act
|
2011-11-13 05:07:06 +01:00
|
|
|
|
mocker.Resolve<UpdateProvider>().Start(TARGET_FOLDER);
|
2011-10-23 07:26:43 +02:00
|
|
|
|
|
|
|
|
|
//Assert
|
2011-10-23 20:31:17 +02:00
|
|
|
|
mocker.GetMock<ProcessProvider>().Verify(c => c.Kill(proccesses[0].Id), Times.Once());
|
|
|
|
|
mocker.GetMock<ProcessProvider>().Verify(c => c.Kill(proccesses[1].Id), Times.Once());
|
2011-10-23 07:26:43 +02:00
|
|
|
|
mocker.VerifyAllMocks();
|
|
|
|
|
}
|
2011-11-13 05:07:06 +01:00
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void should_create_backup_of_current_installation()
|
|
|
|
|
{
|
|
|
|
|
var diskprovider = mocker.GetMock<DiskProvider>()
|
|
|
|
|
.Setup(c => c.CopyDirectory(TARGET_FOLDER, BACKUP_FOLDER));
|
|
|
|
|
|
|
|
|
|
mocker.Resolve<UpdateProvider>().Start(TARGET_FOLDER);
|
|
|
|
|
|
|
|
|
|
mocker.VerifyAllMocks();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void should_copy_update_package_to_target()
|
|
|
|
|
{
|
|
|
|
|
var diskprovider = mocker.GetMock<DiskProvider>()
|
|
|
|
|
.Setup(c => c.CopyDirectory(UPDATE_FOLDER, TARGET_FOLDER));
|
|
|
|
|
|
|
|
|
|
mocker.Resolve<UpdateProvider>().Start(TARGET_FOLDER);
|
|
|
|
|
|
|
|
|
|
mocker.VerifyAllMocks();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void should_restore_if_update_fails()
|
|
|
|
|
{
|
|
|
|
|
var diskprovider = mocker.GetMock<DiskProvider>();
|
|
|
|
|
diskprovider.Setup(c => c.CopyDirectory(BACKUP_FOLDER, TARGET_FOLDER));
|
|
|
|
|
|
|
|
|
|
diskprovider.Setup(c => c.CopyDirectory(UPDATE_FOLDER, TARGET_FOLDER)).Throws(new IOException());
|
|
|
|
|
|
|
|
|
|
mocker.Resolve<UpdateProvider>().Start(TARGET_FOLDER);
|
|
|
|
|
|
2011-11-13 19:16:31 +01:00
|
|
|
|
ExceptionVerification.ExcpectedFatals(1);
|
|
|
|
|
|
2011-11-13 05:07:06 +01:00
|
|
|
|
mocker.VerifyAllMocks();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void should_restart_service_if_service_was_running()
|
|
|
|
|
{
|
|
|
|
|
WithInstalledService();
|
|
|
|
|
|
|
|
|
|
var serviceProvider = mocker.GetMock<ServiceProvider>();
|
|
|
|
|
|
|
|
|
|
serviceProvider.Setup(c => c.IsServiceRunning(ServiceProvider.NZBDRONE_SERVICE_NAME)).Returns(true);
|
|
|
|
|
|
|
|
|
|
//Act
|
|
|
|
|
mocker.Resolve<UpdateProvider>().Start(TARGET_FOLDER);
|
|
|
|
|
|
|
|
|
|
//Assert
|
|
|
|
|
serviceProvider
|
|
|
|
|
.Verify(c => c.Start(ServiceProvider.NZBDRONE_SERVICE_NAME), Times.Once());
|
|
|
|
|
|
|
|
|
|
mocker.VerifyAllMocks();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void should_not_restart_service_if_service_was_not_running()
|
|
|
|
|
{
|
|
|
|
|
WithInstalledService();
|
|
|
|
|
|
|
|
|
|
var serviceProvider = mocker.GetMock<ServiceProvider>();
|
|
|
|
|
|
|
|
|
|
serviceProvider.Setup(c => c.IsServiceRunning(ServiceProvider.NZBDRONE_SERVICE_NAME))
|
|
|
|
|
.Returns(false);
|
|
|
|
|
|
|
|
|
|
//Act
|
|
|
|
|
mocker.Resolve<UpdateProvider>().Start(TARGET_FOLDER);
|
|
|
|
|
|
|
|
|
|
//Assert
|
|
|
|
|
mocker.GetMock<ProcessProvider>()
|
|
|
|
|
.Verify(c => c.Start(TARGET_FOLDER + "nzbdrone.exe"), Times.Once());
|
|
|
|
|
|
|
|
|
|
serviceProvider
|
|
|
|
|
.Verify(c => c.Start(It.IsAny<string>()), Times.Never());
|
|
|
|
|
|
|
|
|
|
mocker.VerifyAllMocks();
|
|
|
|
|
}
|
2011-10-23 07:26:43 +02:00
|
|
|
|
}
|
|
|
|
|
}
|