2017-09-27 02:06:05 +00:00
using System ;
2013-08-14 03:22:28 +00:00
using System.Diagnostics ;
using System.IO ;
using System.Threading ;
2014-02-27 17:04:09 +00:00
using System.Xml ;
2013-09-21 02:07:42 +00:00
using System.Xml.Linq ;
2013-11-11 07:26:13 +00:00
using System.Xml.XPath ;
2014-12-17 07:12:26 +00:00
using NLog ;
2013-08-14 03:22:28 +00:00
using NUnit.Framework ;
using NzbDrone.Common.EnvironmentInfo ;
2013-09-20 23:56:17 +00:00
using NzbDrone.Common.Processes ;
2013-08-14 03:22:28 +00:00
using RestSharp ;
2013-11-12 03:25:54 +00:00
namespace NzbDrone.Test.Common
2013-08-14 03:22:28 +00:00
{
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 ; }
2017-03-31 18:55:07 +00:00
public NzbDroneRunner ( Logger logger , int port = 8686 )
2013-08-14 03:22:28 +00:00
{
2014-12-17 07:12:26 +00:00
_processProvider = new ProcessProvider ( logger ) ;
2017-03-31 18:55:07 +00:00
_restClient = new RestClient ( "http://localhost:8686/api" ) ;
2013-08-14 03:22:28 +00:00
}
public void Start ( )
{
2016-04-04 17:40:51 +00:00
AppData = Path . Combine ( TestContext . CurrentContext . TestDirectory , "_intg_" + DateTime . Now . Ticks ) ;
2013-08-14 03:22:28 +00:00
2017-09-27 02:06:05 +00:00
var lidarrConsoleExe = OsInfo . IsWindows ? "Lidarr.Console.exe" : "Lidarr.exe" ;
2013-08-26 19:35:53 +00:00
2013-08-14 03:22:28 +00:00
if ( BuildInfo . IsDebug )
{
2017-07-03 18:39:06 +00:00
Start ( Path . Combine ( TestContext . CurrentContext . TestDirectory , "..\\..\\..\\..\\..\\_output\\Lidarr.Console.exe" ) ) ;
2013-08-14 03:22:28 +00:00
}
else
{
2017-09-27 02:06:05 +00:00
Start ( Path . Combine ( "bin" , lidarrConsoleExe ) ) ;
2013-08-14 03:22:28 +00:00
}
while ( true )
{
_nzbDroneProcess . Refresh ( ) ;
if ( _nzbDroneProcess . HasExited )
{
Assert . Fail ( "Process has exited" ) ;
}
2013-11-11 07:26:13 +00:00
SetApiKey ( ) ;
2013-09-21 02:07:42 +00:00
var request = new RestRequest ( "system/status" ) ;
request . AddHeader ( "Authorization" , ApiKey ) ;
2015-10-01 20:53:55 +00:00
request . AddHeader ( "X-Api-Key" , 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 ( )
{
2015-10-01 20:52:33 +00:00
if ( _nzbDroneProcess ! = null )
{
_processProvider . Kill ( _nzbDroneProcess . Id ) ;
}
2017-09-27 02:06:05 +00:00
_processProvider . KillAll ( ProcessProvider . LIDARR_CONSOLE_PROCESS_NAME ) ;
_processProvider . KillAll ( ProcessProvider . LIDARR_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 + "\"" ;
2015-05-20 23:22:10 +00:00
_nzbDroneProcess = _processProvider . Start ( outputNzbdroneConsoleExe , args , null , 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-11-11 07:26:13 +00:00
private void SetApiKey ( )
2013-09-21 02:07:42 +00:00
{
var configFile = Path . Combine ( AppData , "config.xml" ) ;
2014-02-27 17:03:37 +00:00
var attempts = 0 ;
2013-09-21 02:07:42 +00:00
2014-02-27 17:03:37 +00:00
while ( ApiKey = = null & & attempts < 50 )
2013-11-11 07:26:13 +00:00
{
2014-02-27 17:03:37 +00:00
try
2013-11-11 07:26:13 +00:00
{
2014-02-27 17:03:37 +00:00
if ( File . Exists ( configFile ) )
2013-11-11 07:26:13 +00:00
{
2014-02-27 17:03:37 +00:00
var apiKeyElement = XDocument . Load ( configFile )
. XPathSelectElement ( "Config/ApiKey" ) ;
if ( apiKeyElement ! = null )
{
ApiKey = apiKeyElement . Value ;
}
2013-11-11 07:26:13 +00:00
}
}
2014-02-27 17:04:09 +00:00
catch ( XmlException ex )
2014-02-27 17:03:37 +00:00
{
Console . WriteLine ( "Error getting API Key from XML file: " + ex . Message , ex ) ;
}
attempts + + ;
2013-11-11 07:26:13 +00:00
Thread . Sleep ( 1000 ) ;
}
2013-09-21 02:07:42 +00:00
}
2013-08-14 03:22:28 +00:00
}
2017-09-27 02:06:05 +00:00
}