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