2018-11-23 07:03:32 +00:00
using System ;
2013-08-14 03:22:28 +00:00
using System.Diagnostics ;
using System.IO ;
using System.Threading ;
2013-09-21 02:07:42 +00:00
using System.Xml.Linq ;
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 ;
2019-09-03 02:22:25 +00:00
using NzbDrone.Core.Configuration ;
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-01-03 15:06:06 +00:00
public NzbDroneRunner ( Logger logger , int port = 7878 )
2013-08-14 03:22:28 +00:00
{
2014-12-17 07:12:26 +00:00
_processProvider = new ProcessProvider ( logger ) ;
2019-12-03 01:36:18 +00:00
_restClient = new RestClient ( "http://localhost:7878/api/v3" ) ;
2013-08-14 03:22:28 +00:00
}
2020-02-25 21:58:25 +00:00
private void CopyDirectory ( string source , string target )
{
foreach ( var dirPath in Directory . GetDirectories ( source , "*" , SearchOption . AllDirectories ) )
{
Directory . CreateDirectory ( dirPath . Replace ( source , target ) ) ;
}
foreach ( var newPath in Directory . GetFiles ( source , "*.*" , SearchOption . AllDirectories ) )
{
File . Copy ( newPath , newPath . Replace ( source , target ) , true ) ;
}
}
2013-08-14 03:22:28 +00:00
public void Start ( )
{
2019-09-03 02:22:25 +00:00
AppData = Path . Combine ( TestContext . CurrentContext . TestDirectory , "_intg_" + TestBase . GetUID ( ) ) ;
2020-02-25 21:58:25 +00:00
if ( ! Directory . Exists ( Path . Combine ( TestContext . CurrentContext . TestDirectory , "CachedAppData" ) ) )
{
Directory . CreateDirectory ( AppData ) ;
GenerateConfigFile ( ) ;
StartInternal ( ) ;
KillAll ( false ) ;
2013-08-14 03:22:28 +00:00
2020-02-25 21:58:25 +00:00
CopyDirectory ( AppData , Path . Combine ( TestContext . CurrentContext . TestDirectory , "CachedAppData" ) ) ;
}
else
{
CopyDirectory ( Path . Combine ( TestContext . CurrentContext . TestDirectory , "CachedAppData" ) , AppData ) ;
GenerateConfigFile ( ) ;
}
StartInternal ( ) ;
}
private void StartInternal ( )
{
2019-10-14 21:42:30 +00:00
string consoleExe ;
if ( OsInfo . IsWindows )
{
consoleExe = "Radarr.Console.exe" ;
}
else if ( PlatformInfo . IsMono )
{
consoleExe = "Radarr.exe" ;
}
else
{
consoleExe = "Radarr" ;
}
2013-08-26 19:35:53 +00:00
2013-08-14 03:22:28 +00:00
if ( BuildInfo . IsDebug )
{
2019-12-05 21:41:55 +00:00
var frameworkFolder = PlatformInfo . IsNetCore ? "netcoreapp3.1" : "net462" ;
2019-10-14 21:42:30 +00:00
Start ( Path . Combine ( TestContext . CurrentContext . TestDirectory , ".." , ".." , "_output" , frameworkFolder , consoleExe ) ) ;
2013-08-14 03:22:28 +00:00
}
else
{
2019-10-14 21:42:30 +00:00
Start ( Path . Combine ( TestContext . CurrentContext . TestDirectory , ".." , "bin" , consoleExe ) ) ;
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
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
{
2019-12-18 21:56:41 +00:00
TestContext . Progress . WriteLine ( "Radarr is started. Running Tests" ) ;
2013-08-14 03:22:28 +00:00
return ;
}
2019-12-18 21:56:41 +00:00
TestContext . Progress . WriteLine ( "Waiting for Radarr to start. Response Status : {0} [{1}] {2}" , statusCall . ResponseStatus , statusCall . StatusDescription , statusCall . ErrorException . Message ) ;
2013-08-28 23:14:31 +00:00
2013-08-14 03:22:28 +00:00
Thread . Sleep ( 500 ) ;
}
}
2020-02-25 21:58:25 +00:00
public void KillAll ( bool delete = true )
2013-08-14 03:22:28 +00:00
{
2019-09-03 02:22:25 +00:00
try
{
if ( _nzbDroneProcess ! = null )
{
_processProvider . Kill ( _nzbDroneProcess . Id ) ;
}
_processProvider . KillAll ( ProcessProvider . RADARR_CONSOLE_PROCESS_NAME ) ;
_processProvider . KillAll ( ProcessProvider . RADARR_PROCESS_NAME ) ;
}
catch ( InvalidOperationException )
2015-10-01 20:52:33 +00:00
{
2019-09-03 02:22:25 +00:00
// May happen if the process closes while being closed
2015-10-01 20:52:33 +00:00
}
2020-02-25 21:58:25 +00:00
if ( delete )
{
TestBase . DeleteTempFolder ( AppData ) ;
}
2013-08-14 03:22:28 +00:00
}
2018-11-23 07:03:32 +00:00
private void Start ( string outputRadarrConsoleExe )
2013-08-14 03:22:28 +00:00
{
2013-09-21 02:07:42 +00:00
var args = "-nobrowser -data=\"" + AppData + "\"" ;
2018-11-23 07:03:32 +00:00
_nzbDroneProcess = _processProvider . Start ( outputRadarrConsoleExe , args , null , OnOutputDataReceived , OnOutputDataReceived ) ;
2013-08-14 03:22:28 +00:00
}
private void OnOutputDataReceived ( string data )
{
2019-12-18 21:56:41 +00:00
TestContext . Progress . WriteLine ( data ) ;
2013-08-14 03:28:13 +00:00
2013-08-14 03:22:28 +00:00
if ( data . Contains ( "Press enter to exit" ) )
{
_nzbDroneProcess . StandardInput . WriteLine ( " " ) ;
}
}
2019-09-03 02:22:25 +00:00
private void GenerateConfigFile ( )
2013-09-21 02:07:42 +00:00
{
var configFile = Path . Combine ( AppData , "config.xml" ) ;
2019-09-03 02:22:25 +00:00
// Generate and set the api key so we don't have to poll the config file
var apiKey = Guid . NewGuid ( ) . ToString ( ) . Replace ( "-" , "" ) ;
2014-02-27 17:03:37 +00:00
2019-09-03 02:22:25 +00:00
var xDoc = new XDocument (
new XDeclaration ( "1.0" , "utf-8" , "yes" ) ,
new XElement ( ConfigFileProvider . CONFIG_ELEMENT_NAME ,
new XElement ( nameof ( ConfigFileProvider . ApiKey ) , apiKey ) ,
2019-12-22 22:08:53 +00:00
new XElement ( nameof ( ConfigFileProvider . AnalyticsEnabled ) , false ) ) ) ;
2019-09-03 02:22:25 +00:00
var data = xDoc . ToString ( ) ;
File . WriteAllText ( configFile , data ) ;
ApiKey = apiKey ;
2013-09-21 02:07:42 +00:00
}
2013-08-14 03:22:28 +00:00
}
2018-08-05 14:28:05 +00:00
}