2013-08-14 05:22:28 +02:00
using System ;
using System.Diagnostics ;
using System.IO ;
using System.Threading ;
using NUnit.Framework ;
using NzbDrone.Common ;
using NzbDrone.Common.EnvironmentInfo ;
using RestSharp ;
namespace NzbDrone.Integration.Test
{
public class NzbDroneRunner
{
private readonly IProcessProvider _processProvider ;
private readonly IRestClient _restClient ;
private Process _nzbDroneProcess ;
public NzbDroneRunner ( int port = 8989 )
{
_processProvider = new ProcessProvider ( ) ;
_restClient = new RestClient ( "http://localhost:8989/api" ) ;
}
public void Start ( )
{
AppDate = Path . Combine ( Directory . GetCurrentDirectory ( ) , "_intg_" + DateTime . Now . Ticks ) ;
2013-08-26 21:35:53 +02:00
var nzbdroneConsoleExe = "NzbDrone.Console.exe" ;
if ( OsInfo . IsMono )
{
nzbdroneConsoleExe = "NzbDrone.exe" ;
}
2013-08-14 05:22:28 +02:00
if ( BuildInfo . IsDebug )
{
Start ( "..\\..\\..\\..\\_output\\NzbDrone.Console.exe" ) ;
}
else
{
2013-08-26 21:35:53 +02:00
Start ( Path . Combine ( "bin" , nzbdroneConsoleExe ) ) ;
2013-08-14 05:22:28 +02:00
}
while ( true )
{
_nzbDroneProcess . Refresh ( ) ;
if ( _nzbDroneProcess . HasExited )
{
Assert . Fail ( "Process has exited" ) ;
}
2013-08-29 01:14:31 +02:00
var statusCall = _restClient . Get ( new RestRequest ( "system/status" ) ) ;
if ( statusCall . ResponseStatus = = ResponseStatus . Completed )
2013-08-14 05:22:28 +02:00
{
2013-09-01 02:30:35 +02:00
Console . WriteLine ( "NzbDrone is started. Running Tests" ) ;
2013-08-14 05:22:28 +02:00
return ;
}
2013-08-29 01:14:31 +02:00
Console . WriteLine ( "Waiting for NzbDrone to start. Response Status : {0} [{1}] {2}" , statusCall . ResponseStatus , statusCall . StatusDescription , statusCall . ErrorException ) ;
2013-08-14 05:22:28 +02:00
Thread . Sleep ( 500 ) ;
}
}
public void KillAll ( )
{
2013-08-14 07:20:24 +02:00
_processProvider . KillAll ( ProcessProvider . NZB_DRONE_CONSOLE_PROCESS_NAME ) ;
_processProvider . KillAll ( ProcessProvider . NZB_DRONE_PROCESS_NAME ) ;
2013-08-14 05:22:28 +02:00
}
private void Start ( string outputNzbdroneConsoleExe )
{
var args = "-nobrowser -data=\"" + AppDate + "\"" ;
2013-08-14 07:20:24 +02:00
_nzbDroneProcess = _processProvider . Start ( outputNzbdroneConsoleExe , args , OnOutputDataReceived , OnOutputDataReceived ) ;
2013-08-14 05:22:28 +02:00
}
private void OnOutputDataReceived ( string data )
{
2013-08-14 05:28:13 +02:00
Console . WriteLine ( data ) ;
2013-08-14 05:22:28 +02:00
if ( data . Contains ( "Press enter to exit" ) )
{
_nzbDroneProcess . StandardInput . WriteLine ( " " ) ;
}
}
public string AppDate { get ; private set ; }
}
}