1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-14 11:23:42 +02:00
Sonarr/NzbDrone.Update.Test/UpdateProviderStartFixture.cs

251 lines
7.8 KiB
C#
Raw Normal View History

2011-11-13 22:15:40 +03:00
using System.Collections.Generic;
using System.IO;
2011-10-23 21:31:17 +03:00
using FizzWare.NBuilder;
using Moq;
using NUnit.Framework;
using NzbDrone.Common;
2011-10-23 21:31:17 +03:00
using NzbDrone.Common.Model;
2011-11-13 21:16:31 +03:00
using NzbDrone.Test.Common;
using NzbDrone.Update.Providers;
namespace NzbDrone.Update.Test
{
[TestFixture]
2011-11-13 23:31:02 +03:00
class UpdateProviderStartFixture : TestBase
{
private const string UPDATE_FOLDER = @"C:\Temp\nzbdrone_update\nzbdrone\";
private const string BACKUP_FOLDER = @"C:\Temp\nzbdrone_update\nzbdrone_backup\";
private const string SANDBOX_LOG_FOLDER = @"C:\Temp\nzbdrone_update\UpdateLogs\";
2011-11-13 07:07:06 +03:00
private const string TARGET_FOLDER = @"C:\NzbDrone\";
private const string UPDATE_LOG_FOLDER = @"C:\NzbDrone\UpdateLogs\";
2011-11-13 07:07:06 +03:00
2011-11-13 10:27:16 +03:00
Mock<EnviromentProvider> _enviromentProvider;
2011-11-13 07:07:06 +03:00
[SetUp]
public void Setup()
{
2011-11-13 22:15:40 +03:00
_enviromentProvider = Mocker.GetMock<EnviromentProvider>();
2011-11-13 07:07:06 +03:00
2011-11-13 10:27:16 +03:00
_enviromentProvider.SetupGet(c => c.SystemTemp).Returns(@"C:\Temp\");
2011-11-13 07:07:06 +03:00
2011-11-13 22:15:40 +03:00
Mocker.GetMock<DiskProvider>()
2011-11-13 07:07:06 +03:00
.Setup(c => c.FolderExists(UPDATE_FOLDER))
.Returns(true);
2011-11-13 22:15:40 +03:00
Mocker.GetMock<DiskProvider>()
2011-11-13 07:07:06 +03:00
.Setup(c => c.FolderExists(TARGET_FOLDER))
.Returns(true);
}
2011-11-13 22:15:40 +03:00
private void WithInstalledService()
2011-11-13 07:07:06 +03:00
{
2011-11-13 22:15:40 +03:00
Mocker.GetMock<ServiceProvider>()
2011-11-13 07:07:06 +03:00
.Setup(c => c.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME))
.Returns(true);
}
2011-11-13 22:15:40 +03:00
private void WithServiceRunning(bool state)
{
Mocker.GetMock<ServiceProvider>()
.Setup(c => c.IsServiceRunning(ServiceProvider.NZBDRONE_SERVICE_NAME)).Returns(state);
}
[Test]
public void should_stop_nzbdrone_service_if_installed_and_running()
{
2011-11-13 07:07:06 +03:00
WithInstalledService();
WithServiceRunning(true);
//Act
2011-11-13 22:15:40 +03:00
Mocker.Resolve<UpdateProvider>().Start(TARGET_FOLDER);
//Assert
2011-11-13 22:15:40 +03:00
Mocker.GetMock<ServiceProvider>().Verify(c => c.Stop(ServiceProvider.NZBDRONE_SERVICE_NAME), Times.Once());
}
[Test]
public void should_not_stop_nzbdrone_service_if_installed_but_not_running()
{
WithInstalledService();
WithServiceRunning(false);
//Act
Mocker.Resolve<UpdateProvider>().Start(TARGET_FOLDER);
//Assert
Mocker.GetMock<ServiceProvider>().Verify(c => c.Stop(ServiceProvider.NZBDRONE_SERVICE_NAME), Times.Never());
}
2011-11-13 22:15:40 +03:00
[Test]
public void should_not_stop_nzbdrone_service_if_service_isnt_installed()
{
//Act
Mocker.Resolve<UpdateProvider>().Start(TARGET_FOLDER);
//Assert
Mocker.GetMock<ServiceProvider>().Verify(c => c.Stop(It.IsAny<string>()), Times.Never());
}
[Test]
2011-10-23 21:31:17 +03:00
public void should_kill_nzbdrone_process_if_running()
{
2011-10-23 21:31:17 +03:00
var proccesses = Builder<ProcessInfo>.CreateListOfSize(2).Build();
2011-11-13 22:15:40 +03:00
Mocker.GetMock<ProcessProvider>()
2011-10-23 21:31:17 +03:00
.Setup(c => c.GetProcessByName(ProcessProvider.NzbDroneProccessName))
.Returns(proccesses);
//Act
2011-11-13 22:15:40 +03:00
Mocker.Resolve<UpdateProvider>().Start(TARGET_FOLDER);
//Assert
2011-11-13 22:15:40 +03: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());
}
[Test]
public void should_not_kill_nzbdrone_process_not_running()
{
Mocker.GetMock<ProcessProvider>()
.Setup(c => c.GetProcessByName(ProcessProvider.NzbDroneProccessName))
.Returns(new List<ProcessInfo>());
//Act
Mocker.Resolve<UpdateProvider>().Start(TARGET_FOLDER);
//Assert
Mocker.GetMock<ProcessProvider>().Verify(c => c.Kill(It.IsAny<int>()), Times.Never());
}
2011-11-13 07:07:06 +03:00
[Test]
public void should_stop_orphan_iisexpress_instances()
{
Mocker.Resolve<UpdateProvider>().Start(TARGET_FOLDER);
//Assert
Mocker.GetMock<IISProvider>().Verify(c => c.StopServer(), Times.Once());
}
2011-11-13 07:07:06 +03:00
[Test]
public void should_create_backup_of_current_installation()
{
2011-11-13 22:15:40 +03:00
Mocker.GetMock<DiskProvider>()
2011-11-13 07:07:06 +03:00
.Setup(c => c.CopyDirectory(TARGET_FOLDER, BACKUP_FOLDER));
2011-11-13 22:15:40 +03:00
Mocker.Resolve<UpdateProvider>().Start(TARGET_FOLDER);
2011-11-13 07:07:06 +03:00
}
[Test]
public void should_copy_update_package_to_target()
{
2011-11-13 22:15:40 +03:00
Mocker.GetMock<DiskProvider>()
.Setup(c => c.CopyDirectory(UPDATE_FOLDER, TARGET_FOLDER));
2011-11-13 07:07:06 +03:00
Mocker.GetMock<DiskProvider>()
.Setup(c => c.DeleteFolder(UPDATE_FOLDER, true));
2011-11-13 22:15:40 +03:00
Mocker.Resolve<UpdateProvider>().Start(TARGET_FOLDER);
2011-11-13 07:07:06 +03:00
}
[Test]
public void should_restore_if_update_fails()
{
2011-11-13 22:15:40 +03:00
Mocker.GetMock<DiskProvider>()
.Setup(c => c.CopyDirectory(UPDATE_FOLDER, TARGET_FOLDER))
2011-11-13 22:15:40 +03:00
.Throws(new IOException());
2011-11-13 07:07:06 +03:00
2011-11-13 22:15:40 +03:00
//Act
Mocker.Resolve<UpdateProvider>().Start(TARGET_FOLDER);
2011-11-13 07:07:06 +03:00
2011-11-13 22:15:40 +03:00
//Assert
Mocker.GetMock<DiskProvider>()
.Verify(c => c.CopyDirectory(BACKUP_FOLDER, TARGET_FOLDER), Times.Once());
ExceptionVerification.ExpectedFatals(1);
2011-11-13 07:07:06 +03:00
}
[Test]
public void should_restart_service_if_service_was_running()
{
WithInstalledService();
2011-11-13 22:15:40 +03:00
WithServiceRunning(true);
2011-11-13 07:07:06 +03:00
2011-11-13 22:15:40 +03:00
//Act
Mocker.Resolve<UpdateProvider>().Start(TARGET_FOLDER);
2011-11-13 07:07:06 +03:00
2011-11-13 22:15:40 +03:00
//Assert
VerifyServiceRestart();
}
[Test]
public void should_restart_process_if_service_was_not_running()
{
WithInstalledService();
WithServiceRunning(false);
2011-11-13 07:07:06 +03:00
//Act
2011-11-13 22:15:40 +03:00
Mocker.Resolve<UpdateProvider>().Start(TARGET_FOLDER);
2011-11-13 07:07:06 +03:00
//Assert
2011-11-13 22:15:40 +03:00
VerifyProcessRestart();
2011-11-13 07:07:06 +03:00
}
[Test]
2011-11-13 22:15:40 +03:00
public void should_restart_service_if_service_was_running_and_update_fails()
2011-11-13 07:07:06 +03:00
{
WithInstalledService();
2011-11-13 22:15:40 +03:00
WithServiceRunning(true);
Mocker.GetMock<DiskProvider>()
.Setup(c => c.CopyDirectory(UPDATE_FOLDER, TARGET_FOLDER))
2011-11-13 22:15:40 +03:00
.Throws(new IOException());
//Act
Mocker.Resolve<UpdateProvider>().Start(TARGET_FOLDER);
//Assert
VerifyServiceRestart();
ExceptionVerification.ExpectedFatals(1);
2011-11-13 22:15:40 +03:00
}
2011-11-13 07:07:06 +03:00
2011-11-13 22:15:40 +03:00
[Test]
public void should_restart_process_if_service_was_not_running_and_update_fails()
{
WithInstalledService();
WithServiceRunning(false);
2011-11-13 07:07:06 +03:00
2011-11-13 22:15:40 +03:00
Mocker.GetMock<DiskProvider>()
.Setup(c => c.CopyDirectory(UPDATE_FOLDER, TARGET_FOLDER))
2011-11-13 22:15:40 +03:00
.Throws(new IOException());
2011-11-13 07:07:06 +03:00
//Act
2011-11-13 22:15:40 +03:00
Mocker.Resolve<UpdateProvider>().Start(TARGET_FOLDER);
2011-11-13 07:07:06 +03:00
//Assert
2011-11-13 22:15:40 +03:00
VerifyProcessRestart();
ExceptionVerification.ExpectedFatals(1);
2011-11-13 22:15:40 +03:00
}
private void VerifyServiceRestart()
{
Mocker.GetMock<ServiceProvider>()
.Verify(c => c.Start(ServiceProvider.NZBDRONE_SERVICE_NAME), Times.Once());
2011-11-13 07:07:06 +03:00
2011-11-13 22:15:40 +03:00
Mocker.GetMock<ProcessProvider>()
2011-11-13 07:07:06 +03:00
.Verify(c => c.Start(It.IsAny<string>()), Times.Never());
2011-11-13 22:15:40 +03:00
}
2011-11-13 07:07:06 +03:00
2011-11-13 22:15:40 +03:00
private void VerifyProcessRestart()
{
Mocker.GetMock<ServiceProvider>()
.Verify(c => c.Start(It.IsAny<string>()), Times.Never());
Mocker.GetMock<ProcessProvider>()
.Verify(c => c.Start(TARGET_FOLDER + "nzbdrone.exe"), Times.Once());
2011-11-13 07:07:06 +03:00
}
}
}