sonarr-repo-only/NzbDrone.Update.Test/UpdateProviderStartTest.cs

161 lines
4.9 KiB
C#
Raw Normal View History

2011-11-13 04:07:06 +00:00
using System.IO;
using AutoMoq;
2011-10-23 18:31:17 +00:00
using FizzWare.NBuilder;
using Moq;
using NUnit.Framework;
using NzbDrone.Common;
2011-10-23 18:31:17 +00:00
using NzbDrone.Common.Model;
2011-11-13 18:16:31 +00:00
using NzbDrone.Test.Common;
using NzbDrone.Update.Providers;
namespace NzbDrone.Update.Test
{
[TestFixture]
2011-11-13 18:16:31 +00:00
class UpdateProviderStartTest : TestBase
{
AutoMoqer mocker = new AutoMoqer();
private const string UPDATE_FOLDER = @"C:\Temp\nzbdrone_update\nzbdrone\";
private const string BACKUP_FOLDER = @"C:\Temp\nzbdrone_update\nzbdrone_backup\";
2011-11-13 04:07:06 +00:00
private const string TARGET_FOLDER = @"C:\NzbDrone\";
2011-11-13 07:27:16 +00:00
Mock<EnviromentProvider> _enviromentProvider;
2011-11-13 04:07:06 +00:00
[SetUp]
public void Setup()
{
mocker = new AutoMoqer();
2011-11-13 07:27:16 +00:00
_enviromentProvider = mocker.GetMock<EnviromentProvider>();
2011-11-13 04:07:06 +00:00
2011-11-13 07:27:16 +00:00
_enviromentProvider.SetupGet(c => c.SystemTemp).Returns(@"C:\Temp\");
2011-11-13 04:07:06 +00: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);
}
[Test]
2011-10-23 18:31:17 +00:00
public void should_stop_nzbdrone_service_if_installed()
{
2011-11-13 04:07:06 +00:00
WithInstalledService();
//Act
2011-11-13 04:07:06 +00:00
mocker.Resolve<UpdateProvider>().Start(TARGET_FOLDER);
//Assert
mocker.GetMock<ServiceProvider>().Verify(c => c.Stop(ServiceProvider.NZBDRONE_SERVICE_NAME), Times.Once());
mocker.VerifyAllMocks();
}
[Test]
2011-10-23 18:31:17 +00:00
public void should_kill_nzbdrone_process_if_running()
{
2011-10-23 18:31:17 +00:00
var proccesses = Builder<ProcessInfo>.CreateListOfSize(2).Build();
mocker.GetMock<ProcessProvider>()
.Setup(c => c.GetProcessByName(ProcessProvider.NzbDroneProccessName))
.Returns(proccesses);
//Act
2011-11-13 04:07:06 +00:00
mocker.Resolve<UpdateProvider>().Start(TARGET_FOLDER);
//Assert
2011-10-23 18:31:17 +00: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());
mocker.VerifyAllMocks();
}
2011-11-13 04:07:06 +00: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 18:16:31 +00:00
ExceptionVerification.ExcpectedFatals(1);
2011-11-13 04:07:06 +00: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();
}
}
}