2013-05-03 04:37:08 +00:00
|
|
|
|
using System;
|
2012-12-21 01:49:57 +00:00
|
|
|
|
using System.ComponentModel;
|
2011-10-23 18:31:17 +00:00
|
|
|
|
using System.Diagnostics;
|
2013-08-06 02:42:22 +00:00
|
|
|
|
using System.IO;
|
2011-10-23 17:32:57 +00:00
|
|
|
|
using System.Linq;
|
2019-09-04 20:38:02 +00:00
|
|
|
|
using System.Threading;
|
2011-10-23 17:32:57 +00:00
|
|
|
|
using FluentAssertions;
|
|
|
|
|
using NUnit.Framework;
|
2019-10-14 21:42:30 +00:00
|
|
|
|
using NzbDrone.Common.EnvironmentInfo;
|
2011-12-12 06:52:58 +00:00
|
|
|
|
using NzbDrone.Common.Model;
|
2013-09-20 23:56:17 +00:00
|
|
|
|
using NzbDrone.Common.Processes;
|
2011-11-13 18:16:31 +00:00
|
|
|
|
using NzbDrone.Test.Common;
|
2011-11-14 03:37:36 +00:00
|
|
|
|
using NzbDrone.Test.Dummy;
|
2011-10-23 17:32:57 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Common.Test
|
|
|
|
|
{
|
|
|
|
|
[TestFixture]
|
2019-09-03 02:22:25 +00:00
|
|
|
|
public class ProcessProviderFixture : TestBase<ProcessProvider>
|
2011-10-23 17:32:57 +00:00
|
|
|
|
{
|
|
|
|
|
[SetUp]
|
|
|
|
|
public void Setup()
|
|
|
|
|
{
|
2013-08-13 14:39:00 +00:00
|
|
|
|
Process.GetProcessesByName(DummyApp.DUMMY_PROCCESS_NAME).ToList().ForEach(c =>
|
2019-09-03 02:22:25 +00:00
|
|
|
|
{
|
|
|
|
|
c.Kill();
|
|
|
|
|
c.WaitForExit();
|
|
|
|
|
});
|
2013-08-13 14:39:00 +00:00
|
|
|
|
|
|
|
|
|
Process.GetProcessesByName(DummyApp.DUMMY_PROCCESS_NAME).Should().BeEmpty();
|
2011-10-23 17:32:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TearDown]
|
|
|
|
|
public void TearDown()
|
|
|
|
|
{
|
2016-04-02 02:19:32 +00:00
|
|
|
|
Process.GetProcessesByName(DummyApp.DUMMY_PROCCESS_NAME).ToList().ForEach(c =>
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
c.Kill();
|
|
|
|
|
}
|
|
|
|
|
catch (Win32Exception ex)
|
|
|
|
|
{
|
|
|
|
|
TestLogger.Warn(ex, "{0} when killing process", ex.Message);
|
|
|
|
|
}
|
|
|
|
|
});
|
2011-10-23 17:32:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void GetById_should_return_null_if_process_doesnt_exist()
|
|
|
|
|
{
|
2013-05-07 05:38:40 +00:00
|
|
|
|
Subject.GetProcessById(1234567).Should().BeNull();
|
2011-12-12 06:52:58 +00:00
|
|
|
|
|
2011-12-20 00:58:26 +00:00
|
|
|
|
ExceptionVerification.ExpectedWarns(1);
|
2011-10-23 17:32:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestCase(0)]
|
|
|
|
|
[TestCase(-1)]
|
|
|
|
|
[TestCase(9999)]
|
|
|
|
|
public void GetProcessById_should_return_null_for_invalid_process(int processId)
|
|
|
|
|
{
|
2013-05-07 05:38:40 +00:00
|
|
|
|
Subject.GetProcessById(processId).Should().BeNull();
|
2011-12-12 06:52:58 +00:00
|
|
|
|
|
2011-12-20 00:58:26 +00:00
|
|
|
|
ExceptionVerification.ExpectedWarns(1);
|
2011-10-23 17:32:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2019-10-15 20:45:41 +00:00
|
|
|
|
[Retry(3)]
|
2019-09-04 20:38:02 +00:00
|
|
|
|
public void should_be_able_to_start_process()
|
|
|
|
|
{
|
|
|
|
|
var process = StartDummyProcess();
|
|
|
|
|
|
2019-10-15 20:45:41 +00:00
|
|
|
|
Thread.Sleep(500);
|
|
|
|
|
|
2019-09-04 20:38:02 +00:00
|
|
|
|
var check = Subject.GetProcessById(process.Id);
|
|
|
|
|
check.Should().NotBeNull();
|
|
|
|
|
|
|
|
|
|
process.Refresh();
|
|
|
|
|
process.HasExited.Should().BeFalse();
|
|
|
|
|
|
|
|
|
|
process.Kill();
|
|
|
|
|
process.WaitForExit();
|
|
|
|
|
process.HasExited.Should().BeTrue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2019-12-22 22:08:53 +00:00
|
|
|
|
[Platform(Exclude = "MacOsX")]
|
2019-09-04 20:38:02 +00:00
|
|
|
|
[Retry(3)]
|
|
|
|
|
public void exists_should_find_running_process()
|
2019-09-03 02:22:25 +00:00
|
|
|
|
{
|
|
|
|
|
var process = StartDummyProcess();
|
2013-05-07 05:38:40 +00:00
|
|
|
|
|
2019-10-15 20:45:41 +00:00
|
|
|
|
Thread.Sleep(500);
|
|
|
|
|
|
2013-07-30 20:19:09 +00:00
|
|
|
|
Subject.Exists(DummyApp.DUMMY_PROCCESS_NAME).Should()
|
2019-09-04 20:38:02 +00:00
|
|
|
|
.BeTrue("expected one dummy process to be already running");
|
2013-08-13 14:39:00 +00:00
|
|
|
|
|
|
|
|
|
process.Kill();
|
|
|
|
|
process.WaitForExit();
|
|
|
|
|
|
|
|
|
|
Subject.Exists(DummyApp.DUMMY_PROCCESS_NAME).Should().BeFalse();
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-02 18:50:32 +00:00
|
|
|
|
[Test]
|
2019-09-03 02:22:25 +00:00
|
|
|
|
[Explicit]
|
2019-07-02 18:50:32 +00:00
|
|
|
|
public void Should_be_able_to_start_powershell()
|
|
|
|
|
{
|
|
|
|
|
WindowsOnly();
|
|
|
|
|
|
|
|
|
|
var tempDir = GetTempFilePath();
|
|
|
|
|
var tempScript = Path.Combine(tempDir, "myscript.ps1");
|
|
|
|
|
|
|
|
|
|
Directory.CreateDirectory(tempDir);
|
|
|
|
|
|
|
|
|
|
File.WriteAllText(tempScript, "Write-Output 'Hello There'\r\n");
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var result = Subject.StartAndCapture(tempScript);
|
|
|
|
|
|
|
|
|
|
result.Standard.First().Content.Should().Be("Hello There");
|
|
|
|
|
}
|
|
|
|
|
catch (Win32Exception ex) when (ex.NativeErrorCode == 2)
|
|
|
|
|
{
|
|
|
|
|
Assert.Fail("No Powershell available?!?");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void Should_be_able_to_start_python()
|
|
|
|
|
{
|
|
|
|
|
WindowsOnly();
|
|
|
|
|
|
|
|
|
|
var tempDir = GetTempFilePath();
|
|
|
|
|
var tempScript = Path.Combine(tempDir, "myscript.py");
|
|
|
|
|
|
|
|
|
|
Directory.CreateDirectory(tempDir);
|
|
|
|
|
|
|
|
|
|
File.WriteAllText(tempScript, "print(\"Hello There\")\r\n");
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var result = Subject.StartAndCapture(tempScript);
|
|
|
|
|
|
|
|
|
|
result.Standard.First().Content.Should().Be("Hello There");
|
|
|
|
|
}
|
|
|
|
|
catch (Win32Exception ex) when (ex.NativeErrorCode == 2)
|
|
|
|
|
{
|
|
|
|
|
Assert.Inconclusive("No Python available");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-07 05:38:40 +00:00
|
|
|
|
[Test]
|
2019-10-15 20:45:41 +00:00
|
|
|
|
[Retry(3)]
|
2019-12-22 22:08:53 +00:00
|
|
|
|
[Platform(Exclude = "MacOsX")]
|
2013-05-07 05:38:40 +00:00
|
|
|
|
public void kill_all_should_kill_all_process_with_name()
|
|
|
|
|
{
|
|
|
|
|
var dummy1 = StartDummyProcess();
|
|
|
|
|
var dummy2 = StartDummyProcess();
|
|
|
|
|
|
2019-10-15 20:45:41 +00:00
|
|
|
|
Thread.Sleep(500);
|
|
|
|
|
|
2013-09-15 00:01:01 +00:00
|
|
|
|
Subject.KillAll(DummyApp.DUMMY_PROCCESS_NAME);
|
2013-05-07 05:38:40 +00:00
|
|
|
|
|
|
|
|
|
dummy1.HasExited.Should().BeTrue();
|
|
|
|
|
dummy2.HasExited.Should().BeTrue();
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-14 05:20:24 +00:00
|
|
|
|
private Process StartDummyProcess()
|
2011-10-23 17:32:57 +00:00
|
|
|
|
{
|
2019-09-04 20:38:02 +00:00
|
|
|
|
var processStarted = new ManualResetEventSlim();
|
|
|
|
|
|
2019-10-14 21:42:30 +00:00
|
|
|
|
string suffix;
|
2021-06-28 22:53:58 +00:00
|
|
|
|
if (OsInfo.IsWindows)
|
2019-10-14 21:42:30 +00:00
|
|
|
|
{
|
|
|
|
|
suffix = ".exe";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
suffix = "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var path = Path.Combine(TestContext.CurrentContext.TestDirectory, DummyApp.DUMMY_PROCCESS_NAME + suffix);
|
2019-12-22 22:08:53 +00:00
|
|
|
|
var process = Subject.Start(path, onOutputDataReceived: (string data) =>
|
|
|
|
|
{
|
|
|
|
|
if (data.StartsWith("Dummy process. ID:"))
|
|
|
|
|
{
|
|
|
|
|
processStarted.Set();
|
|
|
|
|
}
|
|
|
|
|
});
|
2019-09-04 20:38:02 +00:00
|
|
|
|
|
2019-10-15 20:45:41 +00:00
|
|
|
|
if (!processStarted.Wait(5000))
|
2019-09-04 20:38:02 +00:00
|
|
|
|
{
|
|
|
|
|
Assert.Fail("Failed to start process within 2 sec");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return process;
|
2011-10-23 17:32:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-12-12 06:52:58 +00:00
|
|
|
|
[Test]
|
2019-09-04 20:38:02 +00:00
|
|
|
|
[Retry(3)]
|
2011-12-12 06:52:58 +00:00
|
|
|
|
public void ToString_on_new_processInfo()
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(new ProcessInfo().ToString());
|
2012-12-21 01:49:57 +00:00
|
|
|
|
ExceptionVerification.MarkInconclusive(typeof(Win32Exception));
|
2011-12-12 06:52:58 +00:00
|
|
|
|
}
|
2011-10-23 17:32:57 +00:00
|
|
|
|
}
|
2019-09-04 20:38:02 +00:00
|
|
|
|
}
|