2013-05-21 06:03:05 +02:00
|
|
|
|
/*
|
|
|
|
|
using System.Collections.Generic;
|
2011-11-13 20:15:40 +01:00
|
|
|
|
using System.IO;
|
2013-03-04 00:18:43 +01:00
|
|
|
|
using System.Linq;
|
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;
|
2013-05-21 06:03:05 +02:00
|
|
|
|
using NzbDrone.Update.UpdateEngine;
|
2011-10-23 07:26:43 +02:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Update.Test
|
|
|
|
|
{
|
|
|
|
|
[TestFixture]
|
2013-04-16 06:52:41 +02:00
|
|
|
|
public class UpdateProviderStartFixture : TestBase
|
2011-10-23 07:26:43 +02:00
|
|
|
|
{
|
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\";
|
|
|
|
|
|
2013-06-28 02:04:52 +02:00
|
|
|
|
Mock<IIAppDirectoryInfo> _IAppDirectoryInfo;
|
2011-11-13 05:07:06 +01:00
|
|
|
|
|
|
|
|
|
|
2011-10-23 07:26:43 +02:00
|
|
|
|
[SetUp]
|
|
|
|
|
public void Setup()
|
|
|
|
|
{
|
2011-11-13 20:15:40 +01:00
|
|
|
|
|
2013-06-28 02:04:52 +02:00
|
|
|
|
_IAppDirectoryInfo = Mocker.GetMock<IIAppDirectoryInfo>();
|
2011-11-13 05:07:06 +01:00
|
|
|
|
|
2013-06-28 02:04:52 +02:00
|
|
|
|
_IAppDirectoryInfo.SetupGet(c => c.SystemTemp).Returns(@"C:\Temp\");
|
2011-11-13 05:07:06 +01:00
|
|
|
|
|
2013-05-11 01:53:50 +02:00
|
|
|
|
Mocker.GetMock<IDiskProvider>()
|
2011-11-13 05:07:06 +01:00
|
|
|
|
.Setup(c => c.FolderExists(UPDATE_FOLDER))
|
|
|
|
|
.Returns(true);
|
|
|
|
|
|
2013-05-11 01:53:50 +02:00
|
|
|
|
Mocker.GetMock<IDiskProvider>()
|
2011-11-13 05:07:06 +01:00
|
|
|
|
.Setup(c => c.FolderExists(TARGET_FOLDER))
|
|
|
|
|
.Returns(true);
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-13 20:15:40 +01:00
|
|
|
|
private void WithInstalledService()
|
2011-11-13 05:07:06 +01:00
|
|
|
|
{
|
2013-05-11 01:53:50 +02:00
|
|
|
|
Mocker.GetMock<IServiceProvider>()
|
2011-11-13 05:07:06 +01:00
|
|
|
|
.Setup(c => c.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME))
|
|
|
|
|
.Returns(true);
|
2011-10-23 07:26:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-13 20:15:40 +01:00
|
|
|
|
private void WithServiceRunning(bool state)
|
|
|
|
|
{
|
2013-05-11 01:53:50 +02:00
|
|
|
|
Mocker.GetMock<IServiceProvider>()
|
2011-11-13 20:15:40 +01:00
|
|
|
|
.Setup(c => c.IsServiceRunning(ServiceProvider.NZBDRONE_SERVICE_NAME)).Returns(state);
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-23 07:26:43 +02:00
|
|
|
|
[Test]
|
2011-11-15 03:38:15 +01:00
|
|
|
|
public void should_stop_nzbdrone_service_if_installed_and_running()
|
2011-10-23 07:26:43 +02:00
|
|
|
|
{
|
2011-11-13 05:07:06 +01:00
|
|
|
|
WithInstalledService();
|
2011-11-15 03:38:15 +01:00
|
|
|
|
WithServiceRunning(true);
|
2011-10-23 07:26:43 +02:00
|
|
|
|
|
2013-03-28 23:07:09 +01:00
|
|
|
|
|
2013-05-21 06:03:05 +02:00
|
|
|
|
Mocker.Resolve<InstallUpdateService>().Start(TARGET_FOLDER);
|
2011-10-23 07:26:43 +02:00
|
|
|
|
|
2013-03-28 23:07:09 +01:00
|
|
|
|
|
2013-05-11 01:53:50 +02:00
|
|
|
|
Mocker.GetMock<IServiceProvider>().Verify(c => c.Stop(ServiceProvider.NZBDRONE_SERVICE_NAME), Times.Once());
|
2011-11-13 20:15:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-15 03:38:15 +01:00
|
|
|
|
[Test]
|
|
|
|
|
public void should_not_stop_nzbdrone_service_if_installed_but_not_running()
|
|
|
|
|
{
|
|
|
|
|
WithInstalledService();
|
|
|
|
|
WithServiceRunning(false);
|
|
|
|
|
|
2013-03-28 23:07:09 +01:00
|
|
|
|
|
2013-05-21 06:03:05 +02:00
|
|
|
|
Mocker.Resolve<InstallUpdateService>().Start(TARGET_FOLDER);
|
2011-11-15 03:38:15 +01:00
|
|
|
|
|
2013-03-28 23:07:09 +01:00
|
|
|
|
|
2013-05-11 01:53:50 +02:00
|
|
|
|
Mocker.GetMock<IServiceProvider>().Verify(c => c.Stop(ServiceProvider.NZBDRONE_SERVICE_NAME), Times.Never());
|
2011-11-15 03:38:15 +01:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-13 20:15:40 +01:00
|
|
|
|
[Test]
|
|
|
|
|
public void should_not_stop_nzbdrone_service_if_service_isnt_installed()
|
|
|
|
|
{
|
2013-03-28 23:07:09 +01:00
|
|
|
|
|
2013-05-21 06:03:05 +02:00
|
|
|
|
Mocker.Resolve<InstallUpdateService>().Start(TARGET_FOLDER);
|
2011-11-13 20:15:40 +01:00
|
|
|
|
|
2013-03-28 23:07:09 +01:00
|
|
|
|
|
2013-05-11 01:53:50 +02:00
|
|
|
|
Mocker.GetMock<IServiceProvider>().Verify(c => c.Stop(It.IsAny<string>()), Times.Never());
|
2011-10-23 07:26:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[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
|
|
|
|
{
|
2013-03-04 00:18:43 +01:00
|
|
|
|
var proccesses = Builder<ProcessInfo>.CreateListOfSize(2).Build().ToList();
|
2011-10-23 20:31:17 +02:00
|
|
|
|
|
2013-05-11 01:53:50 +02:00
|
|
|
|
Mocker.GetMock<IProcessProvider>()
|
2013-04-18 01:32:53 +02:00
|
|
|
|
.Setup(c => c.GetProcessByName(ProcessProvider.NzbDroneProcessName))
|
2011-10-23 20:31:17 +02:00
|
|
|
|
.Returns(proccesses);
|
2011-10-23 07:26:43 +02:00
|
|
|
|
|
2013-03-28 23:07:09 +01:00
|
|
|
|
|
2013-05-21 06:03:05 +02:00
|
|
|
|
Mocker.Resolve<InstallUpdateService>().Start(TARGET_FOLDER);
|
2011-10-23 07:26:43 +02:00
|
|
|
|
|
2013-05-07 07:38:40 +02:00
|
|
|
|
|
2013-05-11 01:53:50 +02:00
|
|
|
|
Mocker.GetMock<IProcessProvider>().Verify(c => c.KillAll(ProcessProvider.NzbDroneProcessName), Times.Once());
|
2011-11-13 20:15:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void should_not_kill_nzbdrone_process_not_running()
|
|
|
|
|
{
|
2013-05-11 01:53:50 +02:00
|
|
|
|
Mocker.GetMock<IProcessProvider>()
|
2013-04-18 01:32:53 +02:00
|
|
|
|
.Setup(c => c.GetProcessByName(ProcessProvider.NzbDroneProcessName))
|
2011-11-13 20:15:40 +01:00
|
|
|
|
.Returns(new List<ProcessInfo>());
|
|
|
|
|
|
2013-03-28 23:07:09 +01:00
|
|
|
|
|
2013-05-21 06:03:05 +02:00
|
|
|
|
Mocker.Resolve<InstallUpdateService>().Start(TARGET_FOLDER);
|
2011-11-13 20:15:40 +01:00
|
|
|
|
|
2013-03-28 23:07:09 +01:00
|
|
|
|
|
2013-05-11 01:53:50 +02:00
|
|
|
|
Mocker.GetMock<IProcessProvider>().Verify(c => c.Kill(It.IsAny<int>()), Times.Never());
|
2011-10-23 07:26:43 +02:00
|
|
|
|
}
|
2011-11-13 05:07:06 +01:00
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void should_create_backup_of_current_installation()
|
|
|
|
|
{
|
2013-05-11 01:53:50 +02:00
|
|
|
|
Mocker.GetMock<IDiskProvider>()
|
2011-11-13 05:07:06 +01:00
|
|
|
|
.Setup(c => c.CopyDirectory(TARGET_FOLDER, BACKUP_FOLDER));
|
|
|
|
|
|
2013-05-21 06:03:05 +02:00
|
|
|
|
Mocker.Resolve<InstallUpdateService>().Start(TARGET_FOLDER);
|
2011-11-13 05:07:06 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void should_copy_update_package_to_target()
|
|
|
|
|
{
|
2013-05-11 01:53:50 +02:00
|
|
|
|
Mocker.GetMock<IDiskProvider>()
|
2011-11-22 01:45:45 +01:00
|
|
|
|
.Setup(c => c.CopyDirectory(UPDATE_FOLDER, TARGET_FOLDER));
|
2011-11-13 05:07:06 +01:00
|
|
|
|
|
2013-05-11 01:53:50 +02:00
|
|
|
|
Mocker.GetMock<IDiskProvider>()
|
2011-11-22 03:53:22 +01:00
|
|
|
|
.Setup(c => c.DeleteFolder(UPDATE_FOLDER, true));
|
|
|
|
|
|
2013-05-21 06:03:05 +02:00
|
|
|
|
Mocker.Resolve<InstallUpdateService>().Start(TARGET_FOLDER);
|
2011-11-13 05:07:06 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void should_restore_if_update_fails()
|
|
|
|
|
{
|
2013-05-11 01:53:50 +02:00
|
|
|
|
Mocker.GetMock<IDiskProvider>()
|
2011-11-22 01:45:45 +01:00
|
|
|
|
.Setup(c => c.CopyDirectory(UPDATE_FOLDER, TARGET_FOLDER))
|
2011-11-13 20:15:40 +01:00
|
|
|
|
.Throws(new IOException());
|
2011-11-13 05:07:06 +01:00
|
|
|
|
|
2013-03-28 23:07:09 +01:00
|
|
|
|
|
2013-05-21 06:03:05 +02:00
|
|
|
|
Mocker.Resolve<InstallUpdateService>().Start(TARGET_FOLDER);
|
2011-11-13 05:07:06 +01:00
|
|
|
|
|
2013-03-28 23:07:09 +01:00
|
|
|
|
|
2013-05-11 01:53:50 +02:00
|
|
|
|
Mocker.GetMock<IDiskProvider>()
|
2011-11-13 20:15:40 +01:00
|
|
|
|
.Verify(c => c.CopyDirectory(BACKUP_FOLDER, TARGET_FOLDER), Times.Once());
|
2011-12-20 01:58:26 +01:00
|
|
|
|
ExceptionVerification.ExpectedFatals(1);
|
2011-11-13 05:07:06 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void should_restart_service_if_service_was_running()
|
|
|
|
|
{
|
|
|
|
|
WithInstalledService();
|
2011-11-13 20:15:40 +01:00
|
|
|
|
WithServiceRunning(true);
|
2011-11-13 05:07:06 +01:00
|
|
|
|
|
2013-03-28 23:07:09 +01:00
|
|
|
|
|
2013-05-21 06:03:05 +02:00
|
|
|
|
Mocker.Resolve<InstallUpdateService>().Start(TARGET_FOLDER);
|
2011-11-13 05:07:06 +01:00
|
|
|
|
|
2013-03-28 23:07:09 +01:00
|
|
|
|
|
2011-11-13 20:15:40 +01:00
|
|
|
|
VerifyServiceRestart();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void should_restart_process_if_service_was_not_running()
|
|
|
|
|
{
|
|
|
|
|
WithInstalledService();
|
|
|
|
|
WithServiceRunning(false);
|
2011-11-13 05:07:06 +01:00
|
|
|
|
|
2013-03-28 23:07:09 +01:00
|
|
|
|
|
2013-05-21 06:03:05 +02:00
|
|
|
|
Mocker.Resolve<InstallUpdateService>().Start(TARGET_FOLDER);
|
2011-11-13 05:07:06 +01:00
|
|
|
|
|
2013-03-28 23:07:09 +01:00
|
|
|
|
|
2011-11-13 20:15:40 +01:00
|
|
|
|
VerifyProcessRestart();
|
2011-11-13 05:07:06 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2011-11-13 20:15:40 +01:00
|
|
|
|
public void should_restart_service_if_service_was_running_and_update_fails()
|
2011-11-13 05:07:06 +01:00
|
|
|
|
{
|
|
|
|
|
WithInstalledService();
|
2011-11-13 20:15:40 +01:00
|
|
|
|
WithServiceRunning(true);
|
|
|
|
|
|
2013-05-11 01:53:50 +02:00
|
|
|
|
Mocker.GetMock<IDiskProvider>()
|
2011-11-22 01:45:45 +01:00
|
|
|
|
.Setup(c => c.CopyDirectory(UPDATE_FOLDER, TARGET_FOLDER))
|
2011-11-13 20:15:40 +01:00
|
|
|
|
.Throws(new IOException());
|
|
|
|
|
|
2013-03-28 23:07:09 +01:00
|
|
|
|
|
2013-05-21 06:03:05 +02:00
|
|
|
|
Mocker.Resolve<InstallUpdateService>().Start(TARGET_FOLDER);
|
2011-11-13 20:15:40 +01:00
|
|
|
|
|
2013-03-28 23:07:09 +01:00
|
|
|
|
|
2011-11-13 20:15:40 +01:00
|
|
|
|
VerifyServiceRestart();
|
2011-12-20 01:58:26 +01:00
|
|
|
|
ExceptionVerification.ExpectedFatals(1);
|
2011-11-13 20:15:40 +01:00
|
|
|
|
}
|
2011-11-13 05:07:06 +01:00
|
|
|
|
|
2011-11-13 20:15:40 +01:00
|
|
|
|
[Test]
|
|
|
|
|
public void should_restart_process_if_service_was_not_running_and_update_fails()
|
|
|
|
|
{
|
|
|
|
|
WithInstalledService();
|
|
|
|
|
WithServiceRunning(false);
|
2011-11-13 05:07:06 +01:00
|
|
|
|
|
2013-05-11 01:53:50 +02:00
|
|
|
|
Mocker.GetMock<IDiskProvider>()
|
2011-11-22 01:45:45 +01:00
|
|
|
|
.Setup(c => c.CopyDirectory(UPDATE_FOLDER, TARGET_FOLDER))
|
2011-11-13 20:15:40 +01:00
|
|
|
|
.Throws(new IOException());
|
2011-11-13 05:07:06 +01:00
|
|
|
|
|
2013-03-28 23:07:09 +01:00
|
|
|
|
|
2013-05-21 06:03:05 +02:00
|
|
|
|
Mocker.Resolve<InstallUpdateService>().Start(TARGET_FOLDER);
|
2011-11-13 05:07:06 +01:00
|
|
|
|
|
2013-03-28 23:07:09 +01:00
|
|
|
|
|
2011-11-13 20:15:40 +01:00
|
|
|
|
VerifyProcessRestart();
|
2011-12-20 01:58:26 +01:00
|
|
|
|
ExceptionVerification.ExpectedFatals(1);
|
2011-11-13 20:15:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void VerifyServiceRestart()
|
|
|
|
|
{
|
2013-05-11 01:53:50 +02:00
|
|
|
|
Mocker.GetMock<IServiceProvider>()
|
2011-11-13 20:15:40 +01:00
|
|
|
|
.Verify(c => c.Start(ServiceProvider.NZBDRONE_SERVICE_NAME), Times.Once());
|
2011-11-13 05:07:06 +01:00
|
|
|
|
|
2013-05-11 01:53:50 +02:00
|
|
|
|
Mocker.GetMock<IProcessProvider>()
|
2011-11-13 05:07:06 +01:00
|
|
|
|
.Verify(c => c.Start(It.IsAny<string>()), Times.Never());
|
2011-11-13 20:15:40 +01:00
|
|
|
|
}
|
2011-11-13 05:07:06 +01:00
|
|
|
|
|
2011-11-13 20:15:40 +01:00
|
|
|
|
private void VerifyProcessRestart()
|
|
|
|
|
{
|
2013-05-11 01:53:50 +02:00
|
|
|
|
Mocker.GetMock<IServiceProvider>()
|
2011-11-13 20:15:40 +01:00
|
|
|
|
.Verify(c => c.Start(It.IsAny<string>()), Times.Never());
|
|
|
|
|
|
2013-05-11 01:53:50 +02:00
|
|
|
|
Mocker.GetMock<IProcessProvider>()
|
2013-01-06 09:11:14 +01:00
|
|
|
|
.Verify(c => c.Start(TARGET_FOLDER + "NzbDrone.exe"), Times.Once());
|
2011-11-13 05:07:06 +01:00
|
|
|
|
}
|
2011-11-21 03:13:10 +01:00
|
|
|
|
|
|
|
|
|
|
2011-10-23 07:26:43 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-05-21 06:03:05 +02:00
|
|
|
|
*/
|