More update tests

This commit is contained in:
kay.one 2011-11-13 11:15:40 -08:00
parent 207550176c
commit 60d598f2c1
3 changed files with 123 additions and 56 deletions

3
.gitignore vendored
View File

@ -39,4 +39,5 @@ NzbDrone.Web/NzbDrone.Web.Publish.xml
[Bb]anners [Bb]anners
*.orig *.orig
_rawPackage/ _rawPackage/
NzbDrone.zip NzbDrone.zip
NzbDrone.sln.DotSettings.user*

View File

@ -24,7 +24,7 @@ namespace NzbDrone.Test.Common
} }
[SetUp] [SetUp]
public virtual void TestBaseSetup() public void TestBaseSetup()
{ {
if (Directory.Exists(TempFolder)) if (Directory.Exists(TempFolder))
{ {
@ -36,6 +36,12 @@ namespace NzbDrone.Test.Common
Mocker = new AutoMoqer(); Mocker = new AutoMoqer();
} }
[TearDown]
public void TestBaseTearDown()
{
Mocker.VerifyAllMocks();
}
protected virtual void WithStrictMocker() protected virtual void WithStrictMocker()
{ {
Mocker = new AutoMoqer(MockBehavior.Strict); Mocker = new AutoMoqer(MockBehavior.Strict);

View File

@ -1,5 +1,5 @@
using System.IO; using System.Collections.Generic;
using AutoMoq; using System.IO;
using FizzWare.NBuilder; using FizzWare.NBuilder;
using Moq; using Moq;
using NUnit.Framework; using NUnit.Framework;
@ -13,8 +13,6 @@ namespace NzbDrone.Update.Test
[TestFixture] [TestFixture]
class UpdateProviderStartTest : TestBase class UpdateProviderStartTest : TestBase
{ {
AutoMoqer mocker = new AutoMoqer();
private const string UPDATE_FOLDER = @"C:\Temp\nzbdrone_update\nzbdrone\"; private const string UPDATE_FOLDER = @"C:\Temp\nzbdrone_update\nzbdrone\";
private const string BACKUP_FOLDER = @"C:\Temp\nzbdrone_update\nzbdrone_backup\"; private const string BACKUP_FOLDER = @"C:\Temp\nzbdrone_update\nzbdrone_backup\";
private const string TARGET_FOLDER = @"C:\NzbDrone\"; private const string TARGET_FOLDER = @"C:\NzbDrone\";
@ -25,39 +23,53 @@ namespace NzbDrone.Update.Test
[SetUp] [SetUp]
public void Setup() public void Setup()
{ {
mocker = new AutoMoqer();
_enviromentProvider = Mocker.GetMock<EnviromentProvider>();
_enviromentProvider = mocker.GetMock<EnviromentProvider>();
_enviromentProvider.SetupGet(c => c.SystemTemp).Returns(@"C:\Temp\"); _enviromentProvider.SetupGet(c => c.SystemTemp).Returns(@"C:\Temp\");
mocker.GetMock<DiskProvider>() Mocker.GetMock<DiskProvider>()
.Setup(c => c.FolderExists(UPDATE_FOLDER)) .Setup(c => c.FolderExists(UPDATE_FOLDER))
.Returns(true); .Returns(true);
mocker.GetMock<DiskProvider>() Mocker.GetMock<DiskProvider>()
.Setup(c => c.FolderExists(TARGET_FOLDER)) .Setup(c => c.FolderExists(TARGET_FOLDER))
.Returns(true); .Returns(true);
} }
public void WithInstalledService() private void WithInstalledService()
{ {
mocker.GetMock<ServiceProvider>() Mocker.GetMock<ServiceProvider>()
.Setup(c => c.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME)) .Setup(c => c.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME))
.Returns(true); .Returns(true);
} }
private void WithServiceRunning(bool state)
{
Mocker.GetMock<ServiceProvider>()
.Setup(c => c.IsServiceRunning(ServiceProvider.NZBDRONE_SERVICE_NAME)).Returns(state);
}
[Test] [Test]
public void should_stop_nzbdrone_service_if_installed() public void should_stop_nzbdrone_service_if_installed()
{ {
WithInstalledService(); WithInstalledService();
//Act //Act
mocker.Resolve<UpdateProvider>().Start(TARGET_FOLDER); Mocker.Resolve<UpdateProvider>().Start(TARGET_FOLDER);
//Assert //Assert
mocker.GetMock<ServiceProvider>().Verify(c => c.Stop(ServiceProvider.NZBDRONE_SERVICE_NAME), Times.Once()); Mocker.GetMock<ServiceProvider>().Verify(c => c.Stop(ServiceProvider.NZBDRONE_SERVICE_NAME), Times.Once());
mocker.VerifyAllMocks(); }
[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] [Test]
@ -65,96 +77,144 @@ namespace NzbDrone.Update.Test
{ {
var proccesses = Builder<ProcessInfo>.CreateListOfSize(2).Build(); var proccesses = Builder<ProcessInfo>.CreateListOfSize(2).Build();
mocker.GetMock<ProcessProvider>() Mocker.GetMock<ProcessProvider>()
.Setup(c => c.GetProcessByName(ProcessProvider.NzbDroneProccessName)) .Setup(c => c.GetProcessByName(ProcessProvider.NzbDroneProccessName))
.Returns(proccesses); .Returns(proccesses);
//Act //Act
mocker.Resolve<UpdateProvider>().Start(TARGET_FOLDER); Mocker.Resolve<UpdateProvider>().Start(TARGET_FOLDER);
//Assert //Assert
mocker.GetMock<ProcessProvider>().Verify(c => c.Kill(proccesses[0].Id), Times.Once()); 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.GetMock<ProcessProvider>().Verify(c => c.Kill(proccesses[1].Id), Times.Once());
mocker.VerifyAllMocks(); }
[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());
} }
[Test] [Test]
public void should_create_backup_of_current_installation() public void should_create_backup_of_current_installation()
{ {
var diskprovider = mocker.GetMock<DiskProvider>() Mocker.GetMock<DiskProvider>()
.Setup(c => c.CopyDirectory(TARGET_FOLDER, BACKUP_FOLDER)); .Setup(c => c.CopyDirectory(TARGET_FOLDER, BACKUP_FOLDER));
mocker.Resolve<UpdateProvider>().Start(TARGET_FOLDER); Mocker.Resolve<UpdateProvider>().Start(TARGET_FOLDER);
mocker.VerifyAllMocks();
} }
[Test] [Test]
public void should_copy_update_package_to_target() public void should_copy_update_package_to_target()
{ {
var diskprovider = mocker.GetMock<DiskProvider>() Mocker.GetMock<DiskProvider>()
.Setup(c => c.CopyDirectory(UPDATE_FOLDER, TARGET_FOLDER)); .Setup(c => c.CopyDirectory(UPDATE_FOLDER, TARGET_FOLDER));
mocker.Resolve<UpdateProvider>().Start(TARGET_FOLDER); Mocker.Resolve<UpdateProvider>().Start(TARGET_FOLDER);
mocker.VerifyAllMocks();
} }
[Test] [Test]
public void should_restore_if_update_fails() public void should_restore_if_update_fails()
{ {
var diskprovider = mocker.GetMock<DiskProvider>(); Mocker.GetMock<DiskProvider>()
diskprovider.Setup(c => c.CopyDirectory(BACKUP_FOLDER, TARGET_FOLDER)); .Setup(c => c.CopyDirectory(UPDATE_FOLDER, TARGET_FOLDER))
.Throws(new IOException());
diskprovider.Setup(c => c.CopyDirectory(UPDATE_FOLDER, TARGET_FOLDER)).Throws(new IOException()); //Act
Mocker.Resolve<UpdateProvider>().Start(TARGET_FOLDER);
mocker.Resolve<UpdateProvider>().Start(TARGET_FOLDER);
//Assert
Mocker.GetMock<DiskProvider>()
.Verify(c => c.CopyDirectory(BACKUP_FOLDER, TARGET_FOLDER), Times.Once());
ExceptionVerification.ExcpectedFatals(1); ExceptionVerification.ExcpectedFatals(1);
mocker.VerifyAllMocks();
} }
[Test] [Test]
public void should_restart_service_if_service_was_running() public void should_restart_service_if_service_was_running()
{ {
WithInstalledService(); WithInstalledService();
WithServiceRunning(true);
var serviceProvider = mocker.GetMock<ServiceProvider>();
serviceProvider.Setup(c => c.IsServiceRunning(ServiceProvider.NZBDRONE_SERVICE_NAME)).Returns(true);
//Act //Act
mocker.Resolve<UpdateProvider>().Start(TARGET_FOLDER); Mocker.Resolve<UpdateProvider>().Start(TARGET_FOLDER);
//Assert //Assert
serviceProvider VerifyServiceRestart();
.Verify(c => c.Start(ServiceProvider.NZBDRONE_SERVICE_NAME), Times.Once());
mocker.VerifyAllMocks();
} }
[Test] [Test]
public void should_not_restart_service_if_service_was_not_running() public void should_restart_process_if_service_was_not_running()
{ {
WithInstalledService(); WithInstalledService();
WithServiceRunning(false);
var serviceProvider = mocker.GetMock<ServiceProvider>();
serviceProvider.Setup(c => c.IsServiceRunning(ServiceProvider.NZBDRONE_SERVICE_NAME))
.Returns(false);
//Act //Act
mocker.Resolve<UpdateProvider>().Start(TARGET_FOLDER); Mocker.Resolve<UpdateProvider>().Start(TARGET_FOLDER);
//Assert //Assert
mocker.GetMock<ProcessProvider>() VerifyProcessRestart();
.Verify(c => c.Start(TARGET_FOLDER + "nzbdrone.exe"), Times.Once()); }
serviceProvider [Test]
public void should_restart_service_if_service_was_running_and_update_fails()
{
WithInstalledService();
WithServiceRunning(true);
Mocker.GetMock<DiskProvider>()
.Setup(c => c.CopyDirectory(UPDATE_FOLDER, TARGET_FOLDER))
.Throws(new IOException());
//Act
Mocker.Resolve<UpdateProvider>().Start(TARGET_FOLDER);
//Assert
VerifyServiceRestart();
ExceptionVerification.ExcpectedFatals(1);
}
[Test]
public void should_restart_process_if_service_was_not_running_and_update_fails()
{
WithInstalledService();
WithServiceRunning(false);
Mocker.GetMock<DiskProvider>()
.Setup(c => c.CopyDirectory(UPDATE_FOLDER, TARGET_FOLDER))
.Throws(new IOException());
//Act
Mocker.Resolve<UpdateProvider>().Start(TARGET_FOLDER);
//Assert
VerifyProcessRestart();
ExceptionVerification.ExcpectedFatals(1);
}
private void VerifyServiceRestart()
{
Mocker.GetMock<ServiceProvider>()
.Verify(c => c.Start(ServiceProvider.NZBDRONE_SERVICE_NAME), Times.Once());
Mocker.GetMock<ProcessProvider>()
.Verify(c => c.Start(It.IsAny<string>()), Times.Never());
}
private void VerifyProcessRestart()
{
Mocker.GetMock<ServiceProvider>()
.Verify(c => c.Start(It.IsAny<string>()), Times.Never()); .Verify(c => c.Start(It.IsAny<string>()), Times.Never());
mocker.VerifyAllMocks(); Mocker.GetMock<ProcessProvider>()
.Verify(c => c.Start(TARGET_FOLDER + "nzbdrone.exe"), Times.Once());
} }
} }
} }