Fixed Environment Variable conflict in IISProvider

pull/2/head
kay.one 13 years ago
parent fbf7d20c5d
commit bc5307a4d3

@ -0,0 +1,64 @@
using System;
using System.Diagnostics;
using FluentAssertions;
using Moq;
using NUnit.Framework;
using Ninject;
using NzbDrone.Common;
using NzbDrone.Common.Model;
using NzbDrone.Providers;
using NzbDrone.Test.Common;
using NzbDrone.Test.Dummy;
namespace NzbDrone.App.Test
{
[TestFixture]
public class IISProviderFixture : TestBase
{
[Test]
public void should_not_set_env_varibles_twice()
{
WithTempAsAppPath();
var dummy = StartDummyProcess();
Environment.SetEnvironmentVariable(EnviromentProvider.NZBDRONE_PID, "Test");
Environment.SetEnvironmentVariable(EnviromentProvider.NZBDRONE_PATH, "Test");
Mocker.GetMock<ProcessProvider>()
.Setup(c => c.Start(It.IsAny<ProcessStartInfo>()))
.Returns(dummy);
Mocker.Resolve<IISProvider>().StartServer();
}
[Test]
public void should_set_iis_procces_id()
{
WithTempAsAppPath();
var dummy = StartDummyProcess();
Mocker.GetMock<ProcessProvider>()
.Setup(c => c.Start(It.IsAny<ProcessStartInfo>()))
.Returns(dummy);
//act
Mocker.Resolve<IISProvider>().StartServer();
//assert
Mocker.Resolve<IISProvider>().IISProcessId.Should().Be(dummy.Id);
}
public Process StartDummyProcess()
{
var startInfo = new ProcessStartInfo(DummyApp.DUMMY_PROCCESS_NAME + ".exe");
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.CreateNoWindow = true;
return new ProcessProvider().Start(startInfo);
}
}
}

@ -66,6 +66,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="CentralDispatchTests.cs" /> <Compile Include="CentralDispatchTests.cs" />
<Compile Include="IISProviderFixture.cs" />
<Compile Include="RouterTest.cs" /> <Compile Include="RouterTest.cs" />
<Compile Include="MonitoringProviderTest.cs" /> <Compile Include="MonitoringProviderTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />

@ -4,26 +4,27 @@ using System.Linq;
using FluentAssertions; using FluentAssertions;
using NUnit.Framework; using NUnit.Framework;
using NzbDrone.Test.Common; using NzbDrone.Test.Common;
using NzbDrone.Test.Dummy;
namespace NzbDrone.Common.Test namespace NzbDrone.Common.Test
{ {
[TestFixture] [TestFixture]
public class ProcessProviderTests : TestBase public class ProcessProviderTests : TestBase
{ {
private const string DummyProccessName = "NzbDrone.Test.Dummy";
ProcessProvider _processProvider; ProcessProvider _processProvider;
[SetUp] [SetUp]
public void Setup() public void Setup()
{ {
Process.GetProcessesByName(DummyProccessName).ToList().ForEach(c => c.Kill()); Process.GetProcessesByName(DummyApp.DUMMY_PROCCESS_NAME).ToList().ForEach(c => c.Kill());
_processProvider = new ProcessProvider(); _processProvider = new ProcessProvider();
} }
[TearDown] [TearDown]
public void TearDown() public void TearDown()
{ {
Process.GetProcessesByName(DummyProccessName).ToList().ForEach(c => c.Kill()); Process.GetProcessesByName(DummyApp.DUMMY_PROCCESS_NAME).ToList().ForEach(c => c.Kill());
} }
[TestCase(0)] [TestCase(0)]
@ -58,20 +59,20 @@ namespace NzbDrone.Common.Test
[Test] [Test]
public void Should_be_able_to_start_process() public void Should_be_able_to_start_process()
{ {
var startInfo = new ProcessStartInfo(DummyProccessName + ".exe"); var startInfo = new ProcessStartInfo(DummyApp.DUMMY_PROCCESS_NAME + ".exe");
//Act/Assert //Act/Assert
_processProvider.GetProcessByName(DummyProccessName).Should() _processProvider.GetProcessByName(DummyApp.DUMMY_PROCCESS_NAME).Should()
.BeEmpty("Dummy process is already running"); .BeEmpty("Dummy process is already running");
_processProvider.Start(startInfo).Should().NotBeNull(); _processProvider.Start(startInfo).Should().NotBeNull();
_processProvider.GetProcessByName(DummyProccessName).Should() _processProvider.GetProcessByName(DummyApp.DUMMY_PROCCESS_NAME).Should()
.HaveCount(1, "excepted one dummy process to be already running"); .HaveCount(1, "excepted one dummy process to be already running");
} }
public Process StartDummyProcess() public Process StartDummyProcess()
{ {
var startInfo = new ProcessStartInfo(DummyProccessName + ".exe"); var startInfo = new ProcessStartInfo(DummyApp.DUMMY_PROCCESS_NAME + ".exe");
return _processProvider.Start(startInfo); return _processProvider.Start(startInfo);
} }

@ -9,6 +9,9 @@ namespace NzbDrone.Common
{ {
public const string IIS_FOLDER_NAME = "iisexpress"; public const string IIS_FOLDER_NAME = "iisexpress";
public const string NZBDRONE_PATH = "NZBDRONE_PATH";
public const string NZBDRONE_PID = "NZBDRONE_PID";
#if DEBUG #if DEBUG
private static readonly bool isInDebug = true; private static readonly bool isInDebug = true;
#else #else
@ -98,7 +101,7 @@ namespace NzbDrone.Common
{ {
get get
{ {
var id = Convert.ToInt32(Environment.GetEnvironmentVariable("NZBDRONE_PID")); var id = Convert.ToInt32(Environment.GetEnvironmentVariable(NZBDRONE_PID));
if (id == 0) if (id == 0)
throw new InvalidOperationException("NZBDRONE_PID isn't a valid environment variable."); throw new InvalidOperationException("NZBDRONE_PID isn't a valid environment variable.");

@ -3,8 +3,10 @@ using System.Diagnostics;
namespace NzbDrone.Test.Dummy namespace NzbDrone.Test.Dummy
{ {
class Program public class DummyApp
{ {
public const string DUMMY_PROCCESS_NAME = "NzbDrone.Test.Dummy";
static void Main(string[] args) static void Main(string[] args)
{ {
Console.WriteLine("Dummy process. ID:{0} Path:{1}", Process.GetCurrentProcess().Id, Process.GetCurrentProcess().MainModule.FileName); Console.WriteLine("Dummy process. ID:{0} Path:{1}", Process.GetCurrentProcess().Id, Process.GetCurrentProcess().MainModule.FileName);

@ -43,7 +43,7 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Program.cs" /> <Compile Include="DummyApp.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

@ -53,9 +53,15 @@ namespace NzbDrone.Providers
startInfo.RedirectStandardError = true; startInfo.RedirectStandardError = true;
startInfo.CreateNoWindow = true; startInfo.CreateNoWindow = true;
//Set Variables for the config file. if (!startInfo.EnvironmentVariables.ContainsKey(EnviromentProvider.NZBDRONE_PATH))
startInfo.EnvironmentVariables.Add("NZBDRONE_PATH", _enviromentProvider.ApplicationPath); {
startInfo.EnvironmentVariables.Add("NZBDRONE_PID", Process.GetCurrentProcess().Id.ToString()); startInfo.EnvironmentVariables.Add(EnviromentProvider.NZBDRONE_PATH, _enviromentProvider.ApplicationPath);
}
if (!startInfo.EnvironmentVariables.ContainsKey(EnviromentProvider.NZBDRONE_PID))
{
startInfo.EnvironmentVariables.Add(EnviromentProvider.NZBDRONE_PID, Process.GetCurrentProcess().Id.ToString());
}
try try
{ {

@ -117,7 +117,7 @@ namespace NzbDrone.Providers
}.Submit(); }.Submit();
} }
Logger.FatalException("EPIC FAIL: {0}", excepion); Logger.FatalException("EPIC FAIL: " + excepion.Message, excepion);
} }
} }
} }
Loading…
Cancel
Save